Update index.html
Browse files- index.html +125 -18
index.html
CHANGED
@@ -1,19 +1,126 @@
|
|
1 |
-
<!
|
2 |
-
<html>
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
</html>
|
|
|
1 |
+
<!DOCTYPE html>
|
2 |
+
<html lang="en">
|
3 |
+
<head>
|
4 |
+
<meta charset="UTF-8">
|
5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
6 |
+
<title>Real-Time Object Detection</title>
|
7 |
+
<style>
|
8 |
+
body {
|
9 |
+
margin: 0;
|
10 |
+
padding: 0;
|
11 |
+
display: flex;
|
12 |
+
justify-content: center;
|
13 |
+
align-items: center;
|
14 |
+
height: 100vh;
|
15 |
+
background-color: #000;
|
16 |
+
}
|
17 |
+
#video {
|
18 |
+
width: 640px; /* Adjusted video size */
|
19 |
+
height: 480px; /* Adjusted video size */
|
20 |
+
display: block;
|
21 |
+
background-color: #000;
|
22 |
+
}
|
23 |
+
#canvas {
|
24 |
+
width: 640px; /* Adjusted canvas size */
|
25 |
+
height: 480px; /* Adjusted canvas size */
|
26 |
+
position: absolute;
|
27 |
+
top: 0;
|
28 |
+
left: 0;
|
29 |
+
pointer-events: none;
|
30 |
+
}
|
31 |
+
.overlay {
|
32 |
+
position: absolute;
|
33 |
+
top: 10px;
|
34 |
+
left: 10px;
|
35 |
+
color: white;
|
36 |
+
font-size: 16px;
|
37 |
+
background-color: rgba(0, 0, 0, 0.5);
|
38 |
+
padding: 10px;
|
39 |
+
border-radius: 5px;
|
40 |
+
pointer-events: none;
|
41 |
+
}
|
42 |
+
</style>
|
43 |
+
</head>
|
44 |
+
<body>
|
45 |
+
|
46 |
+
<video id="video" width="640" height="480" autoplay muted></video>
|
47 |
+
<canvas id="canvas" width="640" height="480"></canvas>
|
48 |
+
<div id="overlay" class="overlay"></div>
|
49 |
+
|
50 |
+
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>
|
51 |
+
<script src="https://cdn.jsdelivr.net/npm/@tensorflow-models/coco-ssd"></script>
|
52 |
+
|
53 |
+
<script>
|
54 |
+
const video = document.getElementById('video');
|
55 |
+
const canvas = document.getElementById('canvas');
|
56 |
+
const ctx = canvas.getContext('2d');
|
57 |
+
const overlay = document.getElementById('overlay');
|
58 |
+
|
59 |
+
// Initialize video stream from webcam
|
60 |
+
navigator.mediaDevices.getUserMedia({ video: true })
|
61 |
+
.then((stream) => {
|
62 |
+
video.srcObject = stream;
|
63 |
+
})
|
64 |
+
.catch((err) => {
|
65 |
+
console.error('Error accessing webcam: ', err);
|
66 |
+
});
|
67 |
+
|
68 |
+
// Load the COCO-SSD model for object detection
|
69 |
+
let model;
|
70 |
+
async function loadModel() {
|
71 |
+
model = await cocoSsd.load();
|
72 |
+
console.log('COCO-SSD model loaded!');
|
73 |
+
}
|
74 |
+
|
75 |
+
loadModel();
|
76 |
+
|
77 |
+
// Function to run object detection on each frame
|
78 |
+
async function detectFrame() {
|
79 |
+
// Clear the canvas before drawing
|
80 |
+
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
81 |
+
|
82 |
+
// Draw the video frame to canvas
|
83 |
+
ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
|
84 |
+
|
85 |
+
if (model) {
|
86 |
+
const predictions = await model.detect(video);
|
87 |
+
|
88 |
+
// Clear previous overlay content
|
89 |
+
overlay.innerHTML = '';
|
90 |
+
|
91 |
+
predictions.forEach(prediction => {
|
92 |
+
// Draw bounding box
|
93 |
+
ctx.beginPath();
|
94 |
+
ctx.rect(
|
95 |
+
prediction.bbox[0],
|
96 |
+
prediction.bbox[1],
|
97 |
+
prediction.bbox[2],
|
98 |
+
prediction.bbox[3]
|
99 |
+
);
|
100 |
+
ctx.lineWidth = 2;
|
101 |
+
ctx.strokeStyle = 'red';
|
102 |
+
ctx.fillStyle = 'red';
|
103 |
+
ctx.stroke();
|
104 |
+
|
105 |
+
// Add label for the detected object
|
106 |
+
ctx.fillText(
|
107 |
+
`${prediction.class} (${Math.round(prediction.score * 100)}%)`,
|
108 |
+
prediction.bbox[0],
|
109 |
+
prediction.bbox[1] > 10 ? prediction.bbox[1] - 5 : 10
|
110 |
+
);
|
111 |
+
|
112 |
+
// Optionally display detected class names in the overlay
|
113 |
+
overlay.innerHTML += `<p>${prediction.class} - ${Math.round(prediction.score * 100)}%</p>`;
|
114 |
+
});
|
115 |
+
}
|
116 |
+
|
117 |
+
// Call detectFrame repeatedly for continuous object detection
|
118 |
+
requestAnimationFrame(detectFrame);
|
119 |
+
}
|
120 |
+
|
121 |
+
// Start the detection loop
|
122 |
+
detectFrame();
|
123 |
+
</script>
|
124 |
+
|
125 |
+
</body>
|
126 |
</html>
|