-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
61 lines (52 loc) · 1.72 KB
/
app.js
File metadata and controls
61 lines (52 loc) · 1.72 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
const video = document.getElementById("video");
const canvas = document.getElementById("canvas");
const context = canvas.getContext("2d");
const resultsDiv = document.getElementById("results");
async function setupCamera() {
console.log("Setting up camera...");
const stream = await navigator.mediaDevices.getUserMedia({ video: true });
video.srcObject = stream;
return new Promise((resolve) => {
video.onloadedmetadata = () => {
console.log("Camera ready!");
canvas.width = video.videoWidth;
canvas.height = video.videoHeight;
resolve(video);
};
});
}
async function detectObjects() {
console.log("Loading model...");
const model = await cocoSsd.load();
console.log("Model loaded!");
await setupCamera();
async function detectFrame() {
const predictions = await model.detect(video);
console.log("Predictions:", predictions);
context.clearRect(0, 0, canvas.width, canvas.height);
let resultsHTML = "<ul>";
predictions.forEach((prediction) => {
const { class: className, score, bbox } = prediction;
const confidence = Math.round(score * 100);
// Draw bounding box
context.beginPath();
context.rect(...bbox);
context.lineWidth = 2;
context.strokeStyle = "red";
context.stroke();
context.fillStyle = "white";
context.fillText(
`${className} (${confidence}%)`,
bbox[0],
bbox[1] > 10 ? bbox[1] - 5 : 10
);
// Add to results list
resultsHTML += `<li>${className} (${confidence}%)</li>`;
});
resultsHTML += "</ul>";
resultsDiv.innerHTML = resultsHTML;
requestAnimationFrame(detectFrame);
}
detectFrame();
}
detectObjects().catch((err) => console.error("Error:", err));