Spaces:
Running
Running
Update assets/detection.js
Browse files- assets/detection.js +17 -7
assets/detection.js
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
const video = document.getElementById("video");
|
2 |
const canvas = document.getElementById("canvas");
|
3 |
const ctx = canvas.getContext("2d");
|
4 |
-
const
|
5 |
|
6 |
function speak(text) {
|
7 |
const synth = window.speechSynthesis;
|
@@ -20,8 +20,10 @@ async function setupCamera() {
|
|
20 |
}
|
21 |
|
22 |
async function detectObjects() {
|
23 |
-
console.log("Loading model...");
|
24 |
-
const model = await tf.loadGraphModel(
|
|
|
|
|
25 |
|
26 |
canvas.width = video.videoWidth;
|
27 |
canvas.height = video.videoHeight;
|
@@ -29,24 +31,32 @@ async function detectObjects() {
|
|
29 |
async function processFrame() {
|
30 |
ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
|
31 |
|
32 |
-
const inputTensor = tf.browser
|
|
|
|
|
|
|
|
|
33 |
const predictions = await model.executeAsync(inputTensor);
|
34 |
|
35 |
-
|
|
|
36 |
|
37 |
predictions.forEach((prediction) => {
|
38 |
const [x, y, width, height] = prediction.bbox;
|
39 |
const label = prediction.class;
|
40 |
const confidence = prediction.score;
|
41 |
|
|
|
42 |
ctx.strokeStyle = "red";
|
43 |
ctx.lineWidth = 2;
|
44 |
ctx.strokeRect(x, y, width, height);
|
45 |
|
|
|
46 |
ctx.fillStyle = "red";
|
47 |
ctx.font = "18px Arial";
|
48 |
ctx.fillText(`${label} (${(confidence * 100).toFixed(2)}%)`, x, y - 10);
|
49 |
|
|
|
50 |
speak(`${label} detected with ${(confidence * 100).toFixed(2)}% confidence.`);
|
51 |
});
|
52 |
|
@@ -56,8 +66,8 @@ async function detectObjects() {
|
|
56 |
processFrame();
|
57 |
}
|
58 |
|
59 |
-
|
60 |
-
|
61 |
await setupCamera();
|
62 |
video.play();
|
63 |
detectObjects();
|
|
|
1 |
const video = document.getElementById("video");
|
2 |
const canvas = document.getElementById("canvas");
|
3 |
const ctx = canvas.getContext("2d");
|
4 |
+
const startButton = document.getElementById("start");
|
5 |
|
6 |
function speak(text) {
|
7 |
const synth = window.speechSynthesis;
|
|
|
20 |
}
|
21 |
|
22 |
async function detectObjects() {
|
23 |
+
console.log("Loading YOLO model...");
|
24 |
+
const model = await tf.loadGraphModel(
|
25 |
+
"https://path-to-your-model/model.json"
|
26 |
+
); // Replace with your actual model path
|
27 |
|
28 |
canvas.width = video.videoWidth;
|
29 |
canvas.height = video.videoHeight;
|
|
|
31 |
async function processFrame() {
|
32 |
ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
|
33 |
|
34 |
+
const inputTensor = tf.browser
|
35 |
+
.fromPixels(video)
|
36 |
+
.resizeBilinear([640, 480])
|
37 |
+
.expandDims(0);
|
38 |
+
|
39 |
const predictions = await model.executeAsync(inputTensor);
|
40 |
|
41 |
+
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
42 |
+
ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
|
43 |
|
44 |
predictions.forEach((prediction) => {
|
45 |
const [x, y, width, height] = prediction.bbox;
|
46 |
const label = prediction.class;
|
47 |
const confidence = prediction.score;
|
48 |
|
49 |
+
// Draw bounding box
|
50 |
ctx.strokeStyle = "red";
|
51 |
ctx.lineWidth = 2;
|
52 |
ctx.strokeRect(x, y, width, height);
|
53 |
|
54 |
+
// Draw label
|
55 |
ctx.fillStyle = "red";
|
56 |
ctx.font = "18px Arial";
|
57 |
ctx.fillText(`${label} (${(confidence * 100).toFixed(2)}%)`, x, y - 10);
|
58 |
|
59 |
+
// Provide audio feedback
|
60 |
speak(`${label} detected with ${(confidence * 100).toFixed(2)}% confidence.`);
|
61 |
});
|
62 |
|
|
|
66 |
processFrame();
|
67 |
}
|
68 |
|
69 |
+
startButton.addEventListener("click", async () => {
|
70 |
+
startButton.disabled = true;
|
71 |
await setupCamera();
|
72 |
video.play();
|
73 |
detectObjects();
|