Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/remove webworker #314

Merged
merged 2 commits into from
Dec 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/image-target/aframe.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ AFRAME.registerSystem('mindar-image-system', {
track.stop();
});
this.video.remove();
this.controller.dispose();
},

pause: function(keepVideo=false) {
Expand Down Expand Up @@ -229,6 +230,10 @@ AFRAME.registerComponent('mindar-image', {
arSystem.start();
});
}
},
remove: function () {
const arSystem = this.el.sceneEl.systems['mindar-image-system'];
arSystem.stop();
}
});

Expand Down
7 changes: 7 additions & 0 deletions src/image-target/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@ class Controller {
return {dimensions: dimensions, matchingDataList, trackingDataList};
}

dispose() {
this.stopProcessVideo();
this.worker.postMessage({
type: "dispose"
});
}

// warm up gpu - build kernels is slow
dummyRun(input) {
const inputT = this.inputLoader.loadInput(input);
Expand Down
97 changes: 54 additions & 43 deletions src/image-target/controller.worker.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Matcher} from './matching/matcher.js';
import {Estimator} from './estimation/estimator.js';
import { Matcher } from './matching/matcher.js';
import { Estimator } from './estimation/estimator.js';

let projectionTransform = null;
let matchingDataList = null;
Expand All @@ -8,53 +8,64 @@ let matcher = null;
let estimator = null;

onmessage = (msg) => {
const {data} = msg;

if (data.type === 'setup') {
projectionTransform = data.projectionTransform;
matchingDataList = data.matchingDataList;
debugMode = data.debugMode;
matcher = new Matcher(data.inputWidth, data.inputHeight, debugMode);
estimator = new Estimator(data.projectionTransform);
}
else if (data.type === 'match') {
const interestedTargetIndexes = data.targetIndexes;
const { data } = msg;

switch (data.type) {
case "setup":
projectionTransform = data.projectionTransform;
matchingDataList = data.matchingDataList;
debugMode = data.debugMode;
matcher = new Matcher(data.inputWidth, data.inputHeight, debugMode);
estimator = new Estimator(data.projectionTransform);
break;

let matchedTargetIndex = -1;
let matchedModelViewTransform = null;
let matchedDebugExtra = null;
case "match":
const interestedTargetIndexes = data.targetIndexes;

for (let i = 0; i < interestedTargetIndexes.length; i++) {
const matchingIndex = interestedTargetIndexes[i];
let matchedTargetIndex = -1;
let matchedModelViewTransform = null;
let matchedDebugExtra = null;

const {keyframeIndex, screenCoords, worldCoords, debugExtra} = matcher.matchDetection(matchingDataList[matchingIndex], data.featurePoints);
matchedDebugExtra = debugExtra;
for (let i = 0; i < interestedTargetIndexes.length; i++) {
const matchingIndex = interestedTargetIndexes[i];

if (keyframeIndex !== -1) {
const modelViewTransform = estimator.estimate({screenCoords, worldCoords});
const { keyframeIndex, screenCoords, worldCoords, debugExtra } = matcher.matchDetection(matchingDataList[matchingIndex], data.featurePoints);
matchedDebugExtra = debugExtra;

if (modelViewTransform) {
matchedTargetIndex = matchingIndex;
matchedModelViewTransform = modelViewTransform;
}
break;
if (keyframeIndex !== -1) {
const modelViewTransform = estimator.estimate({ screenCoords, worldCoords });

if (modelViewTransform) {
matchedTargetIndex = matchingIndex;
matchedModelViewTransform = modelViewTransform;
}
break;
}
}
}

postMessage({
type: 'matchDone',
targetIndex: matchedTargetIndex,
modelViewTransform: matchedModelViewTransform,
debugExtra: matchedDebugExtra
});
}
else if (data.type === 'trackUpdate') {
const {modelViewTransform, worldCoords, screenCoords} = data;
const finalModelViewTransform = estimator.refineEstimate({initialModelViewTransform: modelViewTransform, worldCoords, screenCoords});
postMessage({
type: 'trackUpdateDone',
modelViewTransform: finalModelViewTransform,
});

postMessage({
type: 'matchDone',
targetIndex: matchedTargetIndex,
modelViewTransform: matchedModelViewTransform,
debugExtra: matchedDebugExtra
});
break;

case 'trackUpdate':
const { modelViewTransform, worldCoords, screenCoords } = data;
const finalModelViewTransform = estimator.refineEstimate({ initialModelViewTransform: modelViewTransform, worldCoords, screenCoords });
postMessage({
type: 'trackUpdateDone',
modelViewTransform: finalModelViewTransform,
});
break;

case "dispose":
close();
break;

default:
throw new Error(`Invalid message type '${data.type}'`);
}
};