-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
314 lines (265 loc) · 9.07 KB
/
index.js
File metadata and controls
314 lines (265 loc) · 9.07 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import {
ObjectDetector,
FilesetResolver,
} from "https://cdn.jsdelivr.net/npm/@mediapipe/tasks-vision@0.10.2";
const demosSection = document.getElementById("demos");
let objectDetector;
let runningMode = "IMAGE";
// set this to the microcontroller's ip
// const ip = "192.168.42.1"; //default
const ip = "192.168.1.152"; //random cidr class c addr assigned by dhcp
// Initialize the object detector
const initializeObjectDetector = async () => {
const vision = await FilesetResolver.forVisionTasks(
"https://cdn.jsdelivr.net/npm/@mediapipe/tasks-vision@0.10.2/wasm"
);
objectDetector = await ObjectDetector.createFromOptions(vision, {
baseOptions: {
// modelAssetPath: `https://storage.googleapis.com/mediapipe-models/object_detector/efficientdet_lite0/float16/1/efficientdet_lite0.tflite`,
modelAssetPath: `https://storage.googleapis.com/mediapipe-models/object_detector/ssd_mobilenet_v2/float16/1/ssd_mobilenet_v2.tflite`,
delegate: "GPU",
},
scoreThreshold: confidenceThreshold,
runningMode: runningMode,
maxResults: maxObjects,
});
// demosSection.classList.remove("invisible")
};
initializeObjectDetector();
let video = document.getElementById("webcam");
let mouseControlBox = document.getElementById("mouseControlCheckBox");
const liveView = document.getElementById("liveView");
let enableWebcamButton;
let videoInput = document.getElementById("videoInput");
// Slider elements
let maxObjectsSlider = document.getElementById("maxObjects");
let confidenceThresholdSlider = document.getElementById("confidenceThreshold");
// Slider value display elements
let maxObjectsValue = document.getElementById("maxObjectsValue");
let confidenceThresholdValue = document.getElementById(
"confidenceThresholdValue"
);
let stopBtn = document.getElementById("stopBtn");
let pauseBtn = document.getElementById("pauseBtn");
// Default values
let maxObjects = parseInt(maxObjectsSlider.value);
let confidenceThreshold = parseFloat(confidenceThresholdSlider.value) / 100;
// Update slider values in real-time
maxObjectsSlider.addEventListener("input", () => {
maxObjects = parseInt(maxObjectsSlider.value);
maxObjectsValue.textContent = maxObjects;
});
confidenceThresholdSlider.addEventListener("input", () => {
confidenceThreshold = parseFloat(confidenceThresholdSlider.value) / 100;
confidenceThresholdValue.textContent = confidenceThresholdSlider.value;
});
stopBtn.addEventListener("click", () => {
stopVideo();
});
pauseBtn.addEventListener("click", () => {
if (!video) {
console.error("wait till video starts!");
return;
}
if (pauseBtn.value == "pause") {
pauseBtn.value = "unpause";
video.pause();
} else {
pauseBtn.value = "pause";
video.play();
}
});
function stopVideo() {
video.pause();
// isPlaying = false;
// // video = null;
// videoInput.src = "";
if (video.srcObject) {
video.srcObject.getTracks().forEach((track) => track.stop());
video.srcObject = null;
}
// video.width = 0; // Reset canvas dimensions
// video.height = 0;
}
// Check if webcam access is supported.
function hasGetUserMedia() {
return !!(navigator.mediaDevices && navigator.mediaDevices.getUserMedia);
}
// Keep a reference of all the child elements we create
// so we can remove them easily on each render.
var children = [];
// If webcam supported, add event listener to button for when user
// wants to activate it.
if (hasGetUserMedia()) {
enableWebcamButton = document.getElementById("webcamButton");
enableWebcamButton.addEventListener("click", enableCam);
} else {
console.warn("getUserMedia() is not supported by your browser");
}
// Add event listener for video file input
videoInput.addEventListener("change", function (event) {
const file = event.target.files[0];
if (file) {
const fileURL = URL.createObjectURL(file);
video.src = fileURL;
video.addEventListener("loadeddata", predictWebcam);
}
});
// Enable the live webcam view and start detection.
let camEnum = ["environment", "user"]; //TODO maybe allow user to flip cam.
async function enableCam(event) {
if (!objectDetector) {
console.log("Wait! objectDetector not loaded yet.");
return;
}
// Hide the button.
enableWebcamButton.classList.add("removed");
const constraints = {
video: {
facingMode: {
exact: "environment",
},
},
};
// Activate the webcam stream.
navigator.mediaDevices
.getUserMedia(constraints)
.then(function (stream) {
video.srcObject = stream;
video.addEventListener("loadeddata", predictWebcam);
})
.catch((err) => {
console.error(err);
/* handle the error */
});
camOrder += 1;
}
let lastVideoTime = -1;
async function predictWebcam() {
// if image mode is initialized, create a new classifier with video runningMode.
if (runningMode === "IMAGE") {
runningMode = "VIDEO";
await objectDetector.setOptions({
runningMode: "VIDEO",
maxResults: maxObjects,
scoreThreshold: confidenceThreshold,
});
}
let startTimeMs = performance.now();
// Detect objects using detectForVideo.
if (video.currentTime !== lastVideoTime) {
lastVideoTime = video.currentTime;
const detections = objectDetector.detectForVideo(video, startTimeMs);
// Filter detections further if neccessary (not really)
// const filteredDetections = detections.detections
// .filter((detection) => detection.categories[0].score >= confidenceThreshold)
// .slice(0, maxObjects);
displayVideoDetections({ detections: detections.detections }); //...wow
}
// Call this function again to keep predicting when the browser is ready.
window.requestAnimationFrame(predictWebcam);
}
const calibrationArea = {
x1: 0, // Top-left X
y1: 200, // Top-left Y
x2: 940*20, // Bottom-right X
y2: 500*40, // Bottom-right Y
};
// Function to map camera coordinates to mouse coordinates
function mapCoordinates(cameraX, cameraY) {
// Calculate scaling factors
const scaleX = (calibrationArea.x2 - calibrationArea.x1) / video.offsetWidth;
const scaleY = (calibrationArea.y2 - calibrationArea.y1) / video.offsetHeight;
// Map camera coordinates to mouse coordinates
const mouseX = calibrationArea.x1 + cameraX * scaleX;
const mouseY = calibrationArea.y1 + cameraY * scaleY;
return { mouseX, mouseY };
}
async function displayVideoDetections(result) {
// Remove any highlighting from previous frame.
for (let child of children) {
liveView.removeChild(child);
}
children.splice(0);
// Iterate through predictions and draw them to the live view
for (let detection of result.detections) {
const label = detection.categories[0].categoryName;
const { originX, originY, width, height } = detection.boundingBox;
// Map camera coordinates to mouse coordinates
const centerX = video.offsetWidth - width - originX + width / 2;
const centerY = originY + height / 2;
const { mouseX, mouseY } = mapCoordinates(centerX, centerY);
await updateMousePosition(mouseX, mouseY);
// Send the mapped coordinates to the server
// Check if the detected object is a "person" and height > width
// if (label === "person" && height > width) {
if (height >= width) {
// Calculate the center of the bounding box
// Draw crosshairs at the center (optional)
const crosshair = document.createElement("div");
crosshair.setAttribute("class", "crosshair");
crosshair.style =
"position: absolute; " +
"left: " +
(centerX - 10) +
"px; " + // Adjust for crosshair size
"top: " +
(centerY - 10) +
"px; " + // Adjust for crosshair size
"width: 20px; " +
"height: 20px; " +
"border: 2px solid red; " +
"pointer-events: none;";
liveView.appendChild(crosshair);
children.push(crosshair);
}
// Draw bounding box and label (optional)
const p = document.createElement("p");
p.innerText = `${label} - with ${Math.round(
parseFloat(detection.categories[0].score) * 100
)}% confidence.`;
p.style =
"left: " +
(video.offsetWidth - width - originX) +
"px;" +
"top: " +
originY +
"px; " +
"width: " +
(width - 10) +
"px;";
const highlighter = document.createElement("div");
highlighter.setAttribute("class", "highlighter");
highlighter.style =
"left: " +
(video.offsetWidth - width - originX) +
"px;" +
"top: " +
originY +
"px;" +
"width: " +
(width - 10) +
"px;" +
"height: " +
height +
"px;";
liveView.appendChild(highlighter);
liveView.appendChild(p);
// Store drawn objects in memory so they are queued to delete at next call.
children.push(highlighter);
children.push(p);
}
}
async function updateMousePosition(x, y) {
// if (!mouseControlBox.checked){return};
// const url = `http://${ip}`;
const url = `https://${ip}/update-mouse?x=${x}&y=${y}`;
try {
fetch(url, {
mode: "no-cors",
method: "GET",
})
} catch (e) {
// currently a bug where the requests never fully complete but it mouse will move regardless...
}
}