Skip to content

Commit 12cf29b

Browse files
committed
refactor for DeepAR Web v5.0.0
1 parent 8b01175 commit 12cf29b

23 files changed

+83
-228
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/node_modules
22
/dist
33
package-lock.json
4+
.DS_Store

package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"name": "quickstart-web-js-npm",
33
"version": "1.0.0",
4-
"description": "Quickstart project that shows how to use DeepAR Web SDK as an imported module.",
4+
"description": "Quickstart project for DeepAR Web.",
55
"main": "index.js",
66
"scripts": {
7-
"build": "webpack --config webpack.config.js --mode production && cp -r public/* dist",
7+
"build": "rm -rf dist && webpack --config webpack.config.js --mode production && cp -r public/* dist && cp -r node_modules/deepar dist/deepar-resources",
88
"dev": "webpack serve --mode development --open --port 8888"
99
},
1010
"author": "",
@@ -15,6 +15,6 @@
1515
"webpack-dev-server": "^4.11.0"
1616
},
1717
"dependencies": {
18-
"deepar": "^4.0.0"
18+
"deepar": "^5.0.0"
1919
}
2020
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

public/index.html

+4-12
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
bottom: 0px;
7878
z-index: 999999;
7979
background-color: rgba(255, 255, 255, 0.7);
80+
display: none;
8081
}
8182

8283
.thumb {
@@ -103,23 +104,12 @@
103104
.slick-slide {
104105
width: 130px;
105106
}
106-
107-
#recording-btn {
108-
position: fixed;
109-
top: 0px;
110-
left: 0px;
111-
width: 200px;
112-
height: 80px;
113-
z-index: 99999;
114-
}
115107
</style>
116108
<link rel="stylesheet" type="text/css" href="//cdn.jsdelivr.net/npm/[email protected]/slick/slick.css" />
117109
</head>
118110

119111
<body>
120-
<button id="recording-btn">Start/stop video recording</button>
121-
122-
<canvas class="deepar" id="deepar-canvas" oncontextmenu="event.preventDefault()"></canvas>
112+
<canvas class="deepar" id="deepar-canvas"></canvas>
123113
<div id="loader-wrapper">
124114
<span class="loader"></span></span>
125115
</div>
@@ -141,6 +131,8 @@
141131
<div><img class="thumb" src="thumbs/fire.png"></div>
142132
<div><img class="thumb" src="thumbs/elephant_trunk.png"></div>
143133
</div>
134+
135+
144136

145137
<script type="text/javascript" src="//code.jquery.com/jquery-1.11.0.min.js"></script>
146138
<script type="text/javascript" src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>

src/effects.js

-34
This file was deleted.

src/index.js

+62-179
Original file line numberDiff line numberDiff line change
@@ -1,190 +1,73 @@
1-
import { DeepAR } from 'deepar';
2-
import deeparWasmPath from 'deepar/wasm/deepar.wasm';
3-
import faceTrackingModelPath from 'deepar/models/face/models-68-extreme.bin';
4-
import segmentationModelPath from 'deepar/models/segmentation/segmentation-160x160-opt.bin';
5-
import poseEstimationWasmPath from 'deepar/wasm/libxzimgPoseEstimation.wasm';
6-
import footDetectorPath from 'deepar/models/foot/foot-detection-96x96x6.bin';
7-
import footTrackerPath from 'deepar/models/foot/foot-tracker-96x96x18-test.bin';
8-
import footObjPath from 'deepar/models/foot/foot-model.obj';
9-
import * as effects from './effects';
1+
import * as deepar from 'deepar';
102

3+
// Log the version. Just in case.
4+
console.log("Deepar version: " + deepar.version);
115

12-
const canvas = document.getElementById('deepar-canvas');
13-
canvas.width = window.innerWidth > window.innerHeight ? Math.floor(window.innerHeight * 0.66) : window.innerWidth;
14-
canvas.height = window.innerHeight;
6+
// Top-level await is not supported.
7+
// So we wrap the whole code in an async function that is called immediatly.
8+
(async function() {
159

16-
const deepAR = new DeepAR({
17-
licenseKey: 'your_license_key_goes_here',
18-
deeparWasmPath,
19-
canvas,
20-
segmentationConfig: {
21-
modelPath: segmentationModelPath,
22-
},
23-
footTrackingConfig: {
24-
poseEstimationWasmPath,
25-
detectorPath: footDetectorPath,
26-
trackerPath: footTrackerPath,
27-
objPath: footObjPath,
28-
},
29-
callbacks: {
30-
onInitialize: () => {
31-
// start video immediately after the initalization, mirror = true
32-
deepAR.startVideo(true);
10+
// Resize the canvas according to screen size.
11+
const canvas = document.getElementById('deepar-canvas');
12+
canvas.width = window.innerWidth > window.innerHeight ? Math.floor(window.innerHeight * 0.66) : window.innerWidth;
13+
canvas.height = window.innerHeight;
3314

34-
// or we can setup the video element externally and call deepAR.setVideoElement (see startExternalVideo function below)
35-
36-
deepAR.switchEffect(0, 'slot', effects.viking, () => {
37-
// effect loaded
38-
});
39-
}
40-
},
41-
});
42-
43-
deepAR.callbacks.onCameraPermissionAsked = () => {
44-
console.log('camera permission asked');
45-
};
46-
47-
deepAR.callbacks.onCameraPermissionGranted = () => {
48-
console.log('camera permission granted');
49-
};
50-
51-
deepAR.callbacks.onCameraPermissionDenied = () => {
52-
console.log('camera permission denied');
53-
};
54-
55-
deepAR.callbacks.onScreenshotTaken = (photo) => {
56-
console.log('screenshot taken ' + photo);
57-
};
58-
59-
deepAR.callbacks.onImageVisibilityChanged = (visible) => {
60-
console.log('image visible ' + visible);
61-
};
62-
63-
deepAR.callbacks.onFaceVisibilityChanged = (visible) => {
64-
console.log('face visible ' + visible);
65-
};
66-
67-
deepAR.callbacks.onVideoStarted = () => {
68-
const loaderWrapper = document.getElementById('loader-wrapper');
69-
loaderWrapper.style.display = 'none';
70-
};
71-
72-
deepAR.downloadFaceTrackingModel(faceTrackingModelPath);
73-
74-
let isRecording = false;
75-
document.getElementById('recording-btn').onclick = (e) => {
76-
if (!isRecording) {
77-
isRecording = true;
78-
deepAR.startVideoRecording();
79-
console.log("Recording started!");
80-
} else {
81-
deepAR.finishVideoRecording((video) => {
82-
window.open(URL.createObjectURL(video), '_blank').focus();
83-
console.log("Recording finished!");
84-
isRecording = false;
85-
});
86-
}
87-
};
88-
89-
// Store video objects for cleanup
90-
let videoObjects = {};
91-
92-
function startExternalVideo() {
93-
cleanupVideoObjects();
94-
// create video element
95-
const video = document.createElement('video');
96-
video.muted = true;
97-
video.loop = true;
98-
video.controls = true;
99-
video.setAttribute('playsinline', 'playsinline');
100-
video.style.width = '100%';
101-
video.style.height = '100%';
102-
103-
// put it somewhere in the DOM
104-
const videoContainer = document.createElement('div');
105-
videoContainer.appendChild(video);
106-
videoContainer.style.width = '1px';
107-
videoContainer.style.height = '1px';
108-
videoContainer.style.position = 'absolute';
109-
videoContainer.style.top = '0px';
110-
videoContainer.style.left = '0px';
111-
videoContainer.style['z-index'] = '-1';
112-
document.body.appendChild(videoContainer);
113-
114-
videoObjects.videoContainer = videoContainer;
115-
videoObjects.video = video;
116-
117-
navigator.mediaDevices.getUserMedia({ video: true }).then((stream) => {
118-
try {
119-
video.srcObject = stream;
120-
} catch (error) {
121-
video.src = URL.createObjectURL(stream);
122-
}
123-
setTimeout(function () {
124-
video.play();
125-
}, 50);
126-
}).catch(function (error) {
127-
console.log('error in video play:', error);
15+
// All the effects are in the public/effects folder.
16+
// Here we define the order of effect files.
17+
const effectList = [
18+
'effects/viking_helmet.deepar',
19+
'effects/MakeupLook.deepar',
20+
'effects/Split_View_Look.deepar',
21+
'effects/flower_face.deepar',
22+
'effects/Stallone.deepar',
23+
'effects/galaxy_background_web.deepar',
24+
'effects/Humanoid.deepar',
25+
'effects/Neon_Devil_Horns.deepar',
26+
'effects/Ping_Pong.deepar',
27+
'effects/Pixel_Hearts.deepar',
28+
'effects/Snail.deepar',
29+
'effects/Hope.deepar',
30+
'effects/Vendetta_Mask.deepar',
31+
'effects/Fire_Effect.deepar',
32+
'effects/Elephant_Trunk.deepar'
33+
];
34+
35+
// Initialize DeepAR with an effect file.
36+
const deepAR = await deepar.initialize({
37+
licenseKey: 'your_license_key_goes_here',
38+
canvas: canvas,
39+
rootPath: "./deepar-resources", // See webpack.config.js and package.json build script.
40+
effect: effectList[0]
12841
});
12942

130-
// tell the DeepAR SDK about our new video element
131-
deepAR.setVideoElement(video, true);
132-
133-
const loaderWrapper = document.getElementById('loader-wrapper');
134-
loaderWrapper.style.display = 'none';
135-
}
136-
137-
function cleanupVideoObjects() {
138-
if (videoObjects.length > 0) {
139-
videoObjects.videoContainer.parentNode.removeChild(videoObjects.videoContainer);
140-
videoObjects.videoContainer = null;
141-
if (videoObjects.video.srcObject) {
142-
// getUserMedia starts a stream, all tracks on all streams need to be stopped before calling getUserMedia again
143-
videoObjects.video.srcObject.getTracks().forEach((track) => track.stop());
144-
}
145-
videoObjects.video.pause();
146-
videoObjects = {};
43+
// Hide the loading screen.
44+
document.getElementById("loader-wrapper").style.display = "none";
45+
46+
// Position the carousel to cover the canvas
47+
if (window.innerWidth > window.innerHeight) {
48+
const width = Math.floor(window.innerHeight * 0.66);
49+
const carousel = document.getElementsByClassName('effect-carousel')[0];
50+
carousel.style.width = width + 'px';
51+
carousel.style.marginLeft = (window.innerWidth - width) / 2 + "px";
14752
}
148-
}
149-
150-
// Position the carousel to cover the canvas
151-
if (window.innerWidth > window.innerHeight) {
152-
const width = Math.floor(window.innerHeight * 0.66);
153-
const carousel = document.getElementsByClassName('effect-carousel')[0];
154-
carousel.style.width = width + 'px';
155-
carousel.style.marginLeft = (window.innerWidth - width) / 2 + "px";
156-
}
53+
54+
// Configure carousel.
55+
$(document).ready(() => {
56+
$('.effect-carousel').slick({
57+
slidesToShow: 1,
58+
centerMode: true,
59+
focusOnSelect: true,
60+
arrows: false,
61+
accessibility: false,
62+
variableWidth: true,
63+
});
64+
65+
// Switch the effect when carusel moves.
66+
$('.effect-carousel').on('afterChange', async function (event, slick, currentSlide) {
67+
await deepAR.switchEffect(effectList[currentSlide]);
68+
});
15769

15870

159-
$(document).ready(function () {
160-
$('.effect-carousel').slick({
161-
slidesToShow: 1,
162-
centerMode: true,
163-
focusOnSelect: true,
164-
arrows: false,
165-
accessibility: false,
166-
variableWidth: true,
16771
});
16872

169-
const effectList = [
170-
effects.viking,
171-
effects.makeup,
172-
effects.makeup_split,
173-
effects.stallone,
174-
effects.flower_face,
175-
effects.galaxy_bacground,
176-
effects.humaniod,
177-
effects.devil_horns,
178-
effects.ping_pong,
179-
effects.hearts,
180-
effects.snail,
181-
effects.hope,
182-
effects.vendetta,
183-
effects.fire,
184-
effects.elephant_trunk
185-
];
186-
187-
$('.effect-carousel').on('afterChange', function (event, slick, currentSlide) {
188-
deepAR.switchEffect(0, 'slot', effectList[currentSlide]);
189-
});
190-
});
73+
})();

webpack.config.js

+13
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,17 @@ module.exports = {
3434
maxEntrypointSize: 1000000,
3535
maxAssetSize: 10000000,
3636
},
37+
devServer: {
38+
static: [
39+
{
40+
directory: path.join(__dirname, 'public')
41+
},
42+
{
43+
directory: path.join(__dirname, 'node_modules/deepar'),
44+
publicPath: "/deepar-resources"
45+
},
46+
],
47+
compress: true,
48+
port: 9000,
49+
},
3750
};

0 commit comments

Comments
 (0)