Skip to content

Commit 818216d

Browse files
committed
Fix helmet detection: try multiple terms, constrain video width
- Try 'helmet', 'hard hat', 'safety helmet' in sequence until one returns detections — Moondream may respond to different object descriptions differently - Log each attempt in reasoning console for visibility - Constrain video section to max-width 720px to prevent overly wide video on large screens
1 parent 5bebbf6 commit 818216d

File tree

2 files changed

+35
-14
lines changed

2 files changed

+35
-14
lines changed

16-ai-safety/app.js

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -381,24 +381,40 @@ If you see a hard hat AND a safety vest, safe=true. Otherwise safe=false.`;
381381
return null;
382382
}
383383

384-
// Step 1b: Detect hard hats in the frame
384+
// Step 1b: Detect hard hats / safety helmets in the frame
385385
let hardHatDetections = [];
386386
try {
387-
updateStatus('Detecting hard hats...');
388-
window.reasoningConsole.logApiCall('/detect (hard hat)', 0);
387+
updateStatus('Detecting safety helmets...');
389388
const hatStart = Date.now();
390-
const hatResult = await client.detect(imageDataUrl, 'hard hat');
391-
hardHatDetections = (hatResult.objects || []).map(h => ({
392-
x_min: h.x_min || 0,
393-
y_min: h.y_min || 0,
394-
x_max: h.x_max || 0,
395-
y_max: h.y_max || 0
396-
}));
397-
const hatLatency = Date.now() - hatStart;
398-
window.reasoningConsole.logApiCall('/detect (hard hat)', hatLatency);
399-
window.reasoningConsole.logInfo(`Detected ${hardHatDetections.length} hard hat(s) [${hatLatency}ms]`);
389+
390+
// Try multiple terms — Moondream may respond better to different phrasings
391+
const hatTerms = ['helmet', 'hard hat', 'safety helmet'];
392+
for (const term of hatTerms) {
393+
window.reasoningConsole.logApiCall('/detect (' + term + ')', 0);
394+
const hatResult = await client.detect(imageDataUrl, term);
395+
const found = (hatResult.objects || []);
396+
const hatLatency = Date.now() - hatStart;
397+
398+
if (found.length > 0) {
399+
hardHatDetections = found.map(h => ({
400+
x_min: h.x_min || 0,
401+
y_min: h.y_min || 0,
402+
x_max: h.x_max || 0,
403+
y_max: h.y_max || 0
404+
}));
405+
window.reasoningConsole.logApiCall('/detect (' + term + ')', hatLatency);
406+
window.reasoningConsole.logInfo(`Detected ${hardHatDetections.length} helmet(s) using term '${term}' [${hatLatency}ms]`);
407+
break; // Found helmets, stop trying other terms
408+
} else {
409+
window.reasoningConsole.logInfo(`No detections for '${term}', trying next...`);
410+
}
411+
}
412+
413+
if (hardHatDetections.length === 0) {
414+
window.reasoningConsole.logInfo('No helmets detected with any search term');
415+
}
400416
} catch (e) {
401-
window.reasoningConsole.logError('Hard hat detection failed: ' + e.message);
417+
window.reasoningConsole.logError('Helmet detection failed: ' + e.message);
402418
}
403419

404420
if (detections.length === 0) {

16-ai-safety/index.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
<title>AI Safety Monitor - Visual Reasoning Playground</title>
77
<link rel="stylesheet" href="../shared/styles.css">
88
<style>
9+
/* Constrain video area width */
10+
.video-section {
11+
max-width: 720px;
12+
}
13+
914
/* Rating card */
1015
.rating-card {
1116
background: var(--surface);

0 commit comments

Comments
 (0)