-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.js
More file actions
97 lines (82 loc) · 2.89 KB
/
worker.js
File metadata and controls
97 lines (82 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import tf from '@tensorflow/tfjs-node';
import cocoSsd from '@tensorflow-models/coco-ssd';
// import * as poseDetection from '@tensorflow-models/pose-detection';
import { parentPort } from 'worker_threads';
import config from './config.js';
let baseImageUrl = '';
// Listen for messages from the main thread
parentPort.on('message', (message) => {
try{
const data = JSON.parse(message);
if (data.topic === 'id') {
console.log(`Worker ${data.payload} received`);
baseImageUrl = config[`CAM${data.payload}`];
detectPeopleFromURL();
}
} catch (error) {
console.error(`Worker error`, error);
}
});
async function detectPeopleFromURL() {
try{
const timestamp = Date.now();
let start = timestamp;
const imageUrl = `${baseImageUrl}&date=${timestamp}`;
// Fetch the image from the URL
const response = await fetch(imageUrl);
const arrayBuffer = await response.arrayBuffer();
const imageBuffer = Buffer.from(arrayBuffer);
// Decode image into tensor using @tensorflow/tfjs-node
const tensor = tf.node.decodeImage(imageBuffer);
const fetchDecodeTime = Date.now() - start;
// Run object detection
start = Date.now();
const predictions = await model.detect(tensor);
const predictTime = Date.now() - start;
// Use the original tensor for pose detection
// start = Date.now();
// const poses = await poseDetector.estimatePoses(tensor);
// const poseTime = Date.now() - start;
// Get image dimensions
const [height, width, channels] = tensor.shape;
const results = {
image: {
height: height,
width: width,
channels: channels
},
predictions,
// poses,
timestamp,
fetchDecodeTime,
predictTime,
// poseTime,
}
tensor.dispose(); // Free up memory
parentPort.postMessage(JSON.stringify({topic: 'results', payload: results}));
} catch (e) {
console.log('error', e);
} finally {
setTimeout(detectPeopleFromURL, 1000);
}
}
let model;
let poseDetector;
async function main() {
console.log('Loading models...');
model = await cocoSsd.load();
// too much ram for heroku ----------------------
// model = await cocoSsd.load({base: 'mobilenet_v2'});
// Configure MoveNet Thunder to maximize accuracy at the expense of performance
// const moveNetModel = poseDetection.SupportedModels.MoveNet;
// poseDetector = await poseDetection.createDetector(moveNetModel, {
// modelType: 'SinglePose.Thunder', // The most accurate MoveNet model
// enableSmoothing: true,
// scoreThreshold: 0.05, // Extremely low threshold to maximize recall
// minPoseScore: 0.05 // Detect even low-confidence poses
// });
// too much ram for heroku ----------------------
console.log('All models loaded');
parentPort.postMessage(JSON.stringify({topic: 'event', payload: 'ready'}));
}
main();