Skip to content

Commit cd6ffa5

Browse files
committed
Fix AI Safety mode switching: sample autoplay, camera toggle
- Fix switchMode(): stop camera stream when leaving camera mode, clear video.src when leaving sample mode, call startCamera() when entering camera mode - Fix loadSampleVideo(): clear srcObject before setting src - Default to Sample Video mode on page load (autoplay) - Update HTML: Sample Video button active by default, camera group hidden by default
1 parent 281a41d commit cd6ffa5

File tree

2 files changed

+22
-23
lines changed

2 files changed

+22
-23
lines changed

16-ai-safety/app.js

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,20 +1109,36 @@ If you see a hard hat AND a safety vest, safe=true. Otherwise safe=false.`;
11091109
uploadArea.classList.toggle('visible', mode === 'upload');
11101110
video.style.display = (mode === 'camera' || mode === 'sample') ? '' : 'none';
11111111

1112+
// Stop camera stream when leaving camera mode
1113+
if (mode !== 'camera' && currentStream) {
1114+
currentStream.getTracks().forEach(track => track.stop());
1115+
currentStream = null;
1116+
video.srcObject = null;
1117+
}
1118+
11121119
if (mode === 'upload') {
1120+
// Clear video src from sample mode
1121+
video.removeAttribute('src');
1122+
video.load();
11131123
window.reasoningConsole.logInfo('Switched to image upload mode');
11141124
} else if (mode === 'sample') {
11151125
window.reasoningConsole.logInfo('Switched to sample video mode');
11161126
loadSampleVideo();
11171127
} else {
1118-
window.reasoningConsole.logInfo('Switched to live camera mode');
1128+
// Camera mode — clear sample video src and start camera
1129+
video.removeAttribute('src');
1130+
video.load();
11191131
uploadedImages = [];
11201132
uploadIndex = 0;
1133+
window.reasoningConsole.logInfo('Switched to live camera mode');
1134+
startCamera();
11211135
}
11221136
}
11231137

11241138
// ── Sample Video ──
11251139
function loadSampleVideo() {
1140+
// Clear any srcObject from camera
1141+
video.srcObject = null;
11261142
video.src = 'Ai-saftey-sample-video.mp4';
11271143
video.loop = true;
11281144
video.muted = true;
@@ -1271,23 +1287,6 @@ If you see a hard hat AND a safety vest, safe=true. Otherwise safe=false.`;
12711287
updateThresholdDots();
12721288
updatePresetInfo();
12731289

1274-
// Initialize VideoSourceAdapter or start camera directly
1275-
if (window.VideoSourceAdapter) {
1276-
VideoSourceAdapter.init({
1277-
videoElement: video,
1278-
toolId: 'ai-safety',
1279-
insertInto: '.video-container',
1280-
onSourceChange: (source) => {
1281-
cameraSelect.disabled = source === 'sample';
1282-
refreshCamerasBtn.disabled = source === 'sample';
1283-
if (source === 'camera') enumerateCameras();
1284-
window.reasoningConsole.logInfo(`Switched to ${source === 'camera' ? 'live camera' : 'sample video'}`);
1285-
}
1286-
});
1287-
VideoSourceAdapter.switchToCamera().catch(() => {
1288-
VideoSourceAdapter.switchToSample();
1289-
});
1290-
} else {
1291-
await startCamera();
1292-
}
1290+
// Default to sample video mode on load
1291+
switchMode('sample');
12931292
});

16-ai-safety/index.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -453,12 +453,12 @@ <h2>Settings</h2>
453453

454454
<!-- Mode toggle -->
455455
<div class="mode-toggle">
456-
<button id="modeCameraBtn" class="active">Camera</button>
456+
<button id="modeCameraBtn">Camera</button>
457457
<button id="modeUploadBtn">Upload</button>
458-
<button id="modeSampleBtn">Sample Video</button>
458+
<button id="modeSampleBtn" class="active">Sample Video</button>
459459
</div>
460460

461-
<div class="control-group" id="cameraGroup">
461+
<div class="control-group" id="cameraGroup" style="display:none">
462462
<label for="cameraSelect">Camera</label>
463463
<div style="display: flex; gap: 8px;">
464464
<select id="cameraSelect" style="flex: 1;"></select>

0 commit comments

Comments
 (0)