Skip to content

Commit

Permalink
Create waitForResources function for hats and glasses
Browse files Browse the repository at this point in the history
Sometimes we got error that resource is not yet loaded but we are
already trying to process it.
Now we wait for all resources incrementing counter in onload callback
and checking it in a loop.
  • Loading branch information
aleksazh committed Nov 8, 2019
1 parent 58374d5 commit c6b391e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
33 changes: 32 additions & 1 deletion samples/funnyHats/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ let gray = null;
const imageWidth = 320;
const imageHeight = 240;

let nImagesLoaded = 0;
// NOTE! Update this value if you add or remove files
// from hatsData and glassesData in ui.js.
const N_IMAGES = 34;


function initOpencvObjects() {
src = new cv.Mat(video.height, video.width, cv.CV_8UC4);
Expand Down Expand Up @@ -72,6 +77,21 @@ function completeStyling() {
document.getElementById('takePhotoButton').disabled = false;
}

function waitForResources() {
if (nImagesLoaded == N_IMAGES) {
requestAnimationFrame(processVideo);
return;
}

// Show video stream while we are waiting for resources.
canvasInputCtx.drawImage(video, 0, 0, video.width, video.height);
let imageData = canvasInputCtx.getImageData(0, 0, video.width, video.height);
src.data.set(imageData.data);
cv.imshow(canvasOutput, src);

requestAnimationFrame(waitForResources);
}

function processVideo() {
try {
if (!streaming) {
Expand Down Expand Up @@ -145,13 +165,24 @@ function processVideo() {
function startCamera() {
if (!streaming) {
utils.clearError();
utils.startCamera(videoConstraint, 'videoInput', onVideoStarted);
utils.startCamera(videoConstraint, 'videoInput', onVideoStartedCustom);
} else {
utils.stopCamera();
onVideoStopped();
}
}

function onVideoStartedCustom() {
streaming = true;
setMainCanvasProperties(video);
videoTrack = video.srcObject.getVideoTracks()[0];
imageCapturer = new ImageCapture(videoTrack);
document.getElementById('mainContent').classList.remove('hidden');
completeStyling();
initOpencvObjects();
requestAnimationFrame(waitForResources);
}

function cleanupAndStop() {
deleteOpencvObjects();
deleteHats(); deleteGlasses();
Expand Down
2 changes: 2 additions & 0 deletions samples/funnyHats/js/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ function loadHats() {
});

hatImage.onload = function () {
++nImagesLoaded;
let rgbaVector = new cv.MatVector();
hatSrc = cv.imread(hatImage);
cv.split(hatSrc, rgbaVector); // Create mask from alpha channel.
Expand Down Expand Up @@ -236,6 +237,7 @@ function loadGlasses() {
});

glassesImage.onload = function () {
++nImagesLoaded;
let rgbaVector = new cv.MatVector();
glassesSrc = cv.imread(glassesImage);
cv.split(glassesSrc, rgbaVector); // Create mask from alpha channel.
Expand Down

0 comments on commit c6b391e

Please sign in to comment.