Skip to content

Commit

Permalink
More fussing - trying to get multiple faceapi worker. failed because …
Browse files Browse the repository at this point in the history
…of canvas module
  • Loading branch information
jackcannon committed Mar 20, 2020
1 parent a273590 commit 7658c42
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 38 deletions.
18 changes: 11 additions & 7 deletions src/camera.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ const createWorker = (): Promise<any> => {
return new Promise(resolve => {
worker = new Worker("./dist/worker-faceapi.js", {});
worker.on("message", data => {
console.log("camera worker started");
workerMsgs.next(data);
if (data && data.type && data.type === "init") {
// console.log(worker);
resolve();
}
});
Expand All @@ -42,9 +44,9 @@ const startCamera = (): Promise<any> => {
};

const runCamera = (): Promise<any> => {
const timer = createTimer("frame");
// const timer = createTimer("frame");
raspberryPiCamera.on("frame", (buffer: Buffer) => {
timer();
// timer();
framesSubject.next(buffer);
});
return framesSubject.pipe(first(frame => !!frame)).toPromise();
Expand All @@ -63,11 +65,13 @@ const startProcessing = () => {
.pipe(filter(({ type }) => type === "points"))
.subscribe(({ points }) => {
pointsSubject.next(points);
// timer(points.map((point) => ({
// x: toFixed(point.x, 2),
// y: toFixed(point.y, 2),
// score: point.score
// })));
timer(
points.map(point => ({
x: toFixed(point.x, 2),
y: toFixed(point.y, 2),
score: point.score
}))
);
runProcess();
});

Expand Down
3 changes: 2 additions & 1 deletion src/worker-faceapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
} from "./face-api/examples-nodejs/commons";
import { IFacePoint } from "./interfaces";
import { toFixed } from "./utils";
import { parentPort, isMainThread } from "worker_threads";
import { parentPort, isMainThread, threadId } from "worker_threads";
import {
cameraOptions,
savePhotoOnDetection,
Expand All @@ -33,6 +33,7 @@ const setup = async () => {
minFaceSize,
scaleFactor: 0.8
});
console.log("worker - all setup", threadId);
parentPort.postMessage({ type: "init" });
};

Expand Down
21 changes: 10 additions & 11 deletions testscripts/workers-child.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
const { workerData, parentPort, isMainThread } = require("worker_threads");

console.log('start child');
console.log("start child");

parentPort.on('message', ({type}) => {
parentPort.on("message", ({ type, num }) => {
switch (type) {
case 'start':
start();
case "start":
start(num);
break;
}
});


const start = () => {
parentPort.postMessage({ type: 'starting' });
for(var i = 0; i < 1000000; i++) {
const start = num => {
parentPort.postMessage({ type: "starting", num });
for (var i = 0; i < 1000000; i++) {
if (i % 100000 === 0) {
console.log('child', i);
console.log("child", num, i);
}
}
parentPort.postMessage({ type: 'finished' });
}
parentPort.postMessage({ type: "finished", num });
};

// parentPort.postMessage({ start: workerData, isMainThread });
38 changes: 19 additions & 19 deletions testscripts/workers-test.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
import {
Worker, isMainThread, parentPort, workerData
} from 'worker_threads';
import { Worker, isMainThread, parentPort, workerData } from "worker_threads";

console.log('start parent');
console.log("start parent");

let worker;

const handleMsg = ({type}) => {
const handleMsg = ({ type, num }) => {
switch (type) {
case 'starting':
start();
case "starting":
start(num);
break;
}
}
};

const setup = () => {
worker = new Worker('./workers-child.js', {});
worker.on('message', handleMsg);
const setup = num => {
worker = new Worker("./workers-child.js", {});
worker.on("message", handleMsg);

worker.postMessage({type: 'start'});
}
worker.postMessage({ type: "start", num });
};

const start = () => {
for(var i = 0; i < 1000000; i++) {
const start = num => {
for (var i = 0; i < 1000000; i++) {
if (i % 100000 === 0) {
console.log('parent', i);
console.log("parent", num, i);
}
}
}
};


setup();
setup(1);
setup(2);
setup(3);
setup(4);

0 comments on commit 7658c42

Please sign in to comment.