Spaces:
Running
Running
Update index.html
Browse files- index.html +62 -45
index.html
CHANGED
@@ -101,8 +101,10 @@
|
|
101 |
|
102 |
// Initialize camera and AI model
|
103 |
async function init() {
|
|
|
104 |
// Load COCO-SSD model
|
105 |
model = await cocoSsd.load();
|
|
|
106 |
|
107 |
// Setup camera
|
108 |
const constraints = {
|
@@ -128,6 +130,15 @@
|
|
128 |
video.className = 'night-vision';
|
129 |
}
|
130 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
131 |
// Start detection loop
|
132 |
requestAnimationFrame(detect);
|
133 |
}
|
@@ -144,58 +155,64 @@
|
|
144 |
}
|
145 |
|
146 |
// Detect objects
|
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 |
requestAnimationFrame(detect);
|
194 |
}
|
195 |
|
196 |
// Start application
|
197 |
init().catch(err => {
|
198 |
-
console.error('Error initializing
|
199 |
});
|
200 |
|
201 |
// Add image processing for better night vision
|
|
|
101 |
|
102 |
// Initialize camera and AI model
|
103 |
async function init() {
|
104 |
+
console.log('Loading COCO-SSD model...');
|
105 |
// Load COCO-SSD model
|
106 |
model = await cocoSsd.load();
|
107 |
+
console.log('COCO-SSD model loaded.');
|
108 |
|
109 |
// Setup camera
|
110 |
const constraints = {
|
|
|
130 |
video.className = 'night-vision';
|
131 |
}
|
132 |
|
133 |
+
// Check if video is playing
|
134 |
+
video.onplaying = () => {
|
135 |
+
console.log('Video stream started successfully.');
|
136 |
+
};
|
137 |
+
|
138 |
+
video.onerror = (e) => {
|
139 |
+
console.error('Error starting video stream:', e);
|
140 |
+
};
|
141 |
+
|
142 |
// Start detection loop
|
143 |
requestAnimationFrame(detect);
|
144 |
}
|
|
|
155 |
}
|
156 |
|
157 |
// Detect objects
|
158 |
+
try {
|
159 |
+
const predictions = await model.detect(video);
|
160 |
+
|
161 |
+
// Clear previous detections
|
162 |
+
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
163 |
+
|
164 |
+
// Draw new detections
|
165 |
+
predictions.forEach(prediction => {
|
166 |
+
// Draw bounding box
|
167 |
+
ctx.strokeStyle = '#00ff00';
|
168 |
+
ctx.lineWidth = 2;
|
169 |
+
ctx.strokeRect(
|
170 |
+
prediction.bbox[0],
|
171 |
+
prediction.bbox[1],
|
172 |
+
prediction.bbox[2],
|
173 |
+
prediction.bbox[3]
|
174 |
+
);
|
175 |
+
|
176 |
+
// Draw label background
|
177 |
+
ctx.fillStyle = '#00ff00';
|
178 |
+
ctx.fillRect(
|
179 |
+
prediction.bbox[0],
|
180 |
+
prediction.bbox[1] - 20,
|
181 |
+
prediction.bbox[2],
|
182 |
+
20
|
183 |
+
);
|
184 |
+
|
185 |
+
// Draw label text
|
186 |
+
ctx.fillStyle = '#000000';
|
187 |
+
ctx.font = '16px monospace';
|
188 |
+
ctx.fillText(
|
189 |
+
`${prediction.class} ${Math.round(prediction.score * 100)}%`,
|
190 |
+
prediction.bbox[0] + 5,
|
191 |
+
prediction.bbox[1] - 5
|
192 |
+
);
|
193 |
+
});
|
194 |
+
|
195 |
+
// Update detection info
|
196 |
+
document.getElementById('objects').textContent =
|
197 |
+
`Objects detected: ${predictions.length}`;
|
198 |
+
|
199 |
+
document.getElementById('detections').innerHTML =
|
200 |
+
predictions.map(p =>
|
201 |
+
`Detected ${p.class} (${Math.round(p.score * 100)}% confidence)`
|
202 |
+
).join('<br>');
|
203 |
+
|
204 |
+
} catch (error) {
|
205 |
+
console.error('Error in detection:', error);
|
206 |
+
}
|
207 |
}
|
208 |
|
209 |
+
// Continue calling the detection loop
|
210 |
requestAnimationFrame(detect);
|
211 |
}
|
212 |
|
213 |
// Start application
|
214 |
init().catch(err => {
|
215 |
+
console.error('Error initializing application:', err);
|
216 |
});
|
217 |
|
218 |
// Add image processing for better night vision
|