Spaces:
Running
Running
Update assets/detection.js
Browse files- assets/detection.js +48 -50
assets/detection.js
CHANGED
@@ -1,70 +1,68 @@
|
|
1 |
-
import * as tf from "https://cdn.jsdelivr.net/npm/@tensorflow/tfjs";
|
2 |
-
import * as cocoSsd from "https://cdn.jsdelivr.net/npm/@tensorflow-models/coco-ssd";
|
3 |
-
|
4 |
-
// Get references to the HTML elements
|
5 |
const video = document.getElementById("video");
|
6 |
const canvas = document.getElementById("canvas");
|
7 |
const ctx = canvas.getContext("2d");
|
8 |
-
const
|
9 |
-
|
10 |
-
let model;
|
11 |
|
12 |
-
//
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
}
|
18 |
|
19 |
-
|
20 |
-
|
21 |
-
|
|
|
22 |
video.srcObject = stream;
|
23 |
-
|
24 |
return new Promise((resolve) => {
|
25 |
-
video.onloadedmetadata = () =>
|
26 |
-
canvas.width = video.videoWidth;
|
27 |
-
canvas.height = video.videoHeight;
|
28 |
-
resolve(video);
|
29 |
-
};
|
30 |
});
|
31 |
}
|
32 |
|
33 |
-
// Perform object detection on the video feed
|
34 |
async function detectObjects() {
|
35 |
-
|
|
|
36 |
|
37 |
-
|
38 |
-
|
39 |
-
ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
|
40 |
|
41 |
-
|
42 |
-
|
43 |
-
const [x, y, width, height] = prediction.bbox;
|
44 |
|
45 |
-
//
|
46 |
-
|
47 |
-
|
48 |
-
ctx.strokeRect(x, y, width, height);
|
49 |
|
50 |
-
//
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
x,
|
56 |
-
y > 10 ? y - 5 : 10
|
57 |
-
);
|
58 |
-
});
|
59 |
|
60 |
-
|
61 |
-
|
|
|
|
|
62 |
|
63 |
-
//
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
}
|
69 |
|
70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
const video = document.getElementById("video");
|
2 |
const canvas = document.getElementById("canvas");
|
3 |
const ctx = canvas.getContext("2d");
|
4 |
+
const button = document.getElementById("start");
|
|
|
|
|
5 |
|
6 |
+
// Text-to-Speech for audio feedback
|
7 |
+
function speak(text) {
|
8 |
+
const synth = window.speechSynthesis;
|
9 |
+
const utterance = new SpeechSynthesisUtterance(text);
|
10 |
+
synth.speak(utterance);
|
11 |
}
|
12 |
|
13 |
+
async function setupCamera() {
|
14 |
+
const stream = await navigator.mediaDevices.getUserMedia({
|
15 |
+
video: { width: 640, height: 480 },
|
16 |
+
});
|
17 |
video.srcObject = stream;
|
|
|
18 |
return new Promise((resolve) => {
|
19 |
+
video.onloadedmetadata = () => resolve(video);
|
|
|
|
|
|
|
|
|
20 |
});
|
21 |
}
|
22 |
|
|
|
23 |
async function detectObjects() {
|
24 |
+
// Load YOLO model using TensorFlow.js or another library
|
25 |
+
const model = await tf.loadGraphModel("https://path-to-your-yolo-model/model.json");
|
26 |
|
27 |
+
canvas.width = video.videoWidth;
|
28 |
+
canvas.height = video.videoHeight;
|
|
|
29 |
|
30 |
+
async function processFrame() {
|
31 |
+
ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
|
|
|
32 |
|
33 |
+
// Preprocess the video frame for YOLO model
|
34 |
+
const inputTensor = tf.browser.fromPixels(video).resizeBilinear([640, 480]).expandDims(0);
|
35 |
+
const predictions = await model.executeAsync(inputTensor);
|
|
|
36 |
|
37 |
+
// Parse predictions and draw bounding boxes
|
38 |
+
predictions.forEach((prediction) => {
|
39 |
+
const [x, y, width, height] = prediction.bbox;
|
40 |
+
const label = prediction.class;
|
41 |
+
const confidence = prediction.score;
|
|
|
|
|
|
|
|
|
42 |
|
43 |
+
// Draw the bounding box
|
44 |
+
ctx.strokeStyle = "red";
|
45 |
+
ctx.lineWidth = 2;
|
46 |
+
ctx.strokeRect(x, y, width, height);
|
47 |
|
48 |
+
// Draw the label
|
49 |
+
ctx.fillStyle = "red";
|
50 |
+
ctx.font = "18px Arial";
|
51 |
+
ctx.fillText(`${label} (${(confidence * 100).toFixed(2)}%)`, x, y - 10);
|
52 |
+
|
53 |
+
// Provide audio feedback
|
54 |
+
speak(`${label} detected with ${(confidence * 100).toFixed(2)}% confidence.`);
|
55 |
+
});
|
56 |
+
|
57 |
+
requestAnimationFrame(processFrame);
|
58 |
+
}
|
59 |
+
|
60 |
+
processFrame();
|
61 |
}
|
62 |
|
63 |
+
button.addEventListener("click", async () => {
|
64 |
+
button.disabled = true; // Disable the button after starting
|
65 |
+
await setupCamera();
|
66 |
+
video.play();
|
67 |
+
detectObjects();
|
68 |
+
});
|