Spaces:
Sleeping
Sleeping
File size: 6,370 Bytes
41de4c7 c866c8d 41de4c7 c866c8d 95b7795 c866c8d 95b7795 41de4c7 c866c8d 95b7795 c866c8d 95b7795 c866c8d 41de4c7 95b7795 c866c8d 95b7795 41de4c7 c866c8d 95b7795 c866c8d 41de4c7 95b7795 c866c8d 95b7795 c866c8d 41de4c7 c866c8d 41de4c7 c866c8d 95b7795 c866c8d 41de4c7 c866c8d 41de4c7 c866c8d 95b7795 c866c8d 41de4c7 c866c8d 41de4c7 c866c8d 41de4c7 c866c8d 41de4c7 c866c8d 41de4c7 c866c8d 41de4c7 c866c8d 95b7795 c866c8d 95b7795 c866c8d 41de4c7 c866c8d | 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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 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 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 | const video = document.getElementById("webcam");
const canvas = document.getElementById("canvas");
const startOverlay = document.getElementById("startOverlay");
const ctx = canvas.getContext("2d");
let lastSpokenText = "";
let isSpeaking = false;
let silenceUntil = 0;
let isPageVisible = true;
let isRequestInFlight = false; // prevents double-taps from flooding the server
function speak(message) {
if (!message) return;
if (Date.now() < silenceUntil) return;
speechSynthesis.cancel();
isSpeaking = true;
const utterance = new SpeechSynthesisUtterance(message);
utterance.rate = 1.0;
utterance.pitch = 1.0;
utterance.onend = () => {
isSpeaking = false;
lastSpokenText = message;
};
utterance.onerror = (e) => {
console.warn("Speech error:", e.error);
isSpeaking = false;
};
speechSynthesis.speak(utterance);
}
function captureFrame() {
return new Promise((resolve) => {
ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
canvas.toBlob((blob) => resolve(blob), "image/jpeg", 0.8);
});
}
async function apiTell() {
if (isRequestInFlight) return;
isRequestInFlight = true;
try {
const blob = await captureFrame();
if (!blob) {
isRequestInFlight = false;
return;
}
const formData = new FormData();
formData.append("file", blob, "frame.jpg");
const res = await fetch("/api/tell", { method: "POST", body: formData });
const data = await res.json();
if (data.summary) {
speak(data.summary);
} else if (data.error) {
speak(data.error);
}
} catch (e) {
console.error("apiTell error:", e);
speak("Connection failed.");
} finally {
isRequestInFlight = false;
}
}
async function apiMore() {
if (isRequestInFlight) return;
isRequestInFlight = true;
speak("Looking closer...");
try {
const blob = await captureFrame();
if (!blob) {
isRequestInFlight = false;
return;
}
const formData = new FormData();
formData.append("file", blob, "frame.jpg");
const res = await fetch("/api/more", { method: "POST", body: formData });
const data = await res.json();
if (data.caption) {
speak(data.caption);
} else if (data.error) {
speak(data.error);
}
} catch (e) {
console.error("apiMore error:", e);
speak("Connection failed.");
} finally {
isRequestInFlight = false;
}
}
// gesture setup
let tapTimer = null;
let longPressTimer = null;
let isLongPress = false;
const DOUBLE_TAP_DELAY = 300;
function setupGestures(target) {
// prevent default context menus and text selection
target.addEventListener("contextmenu", (e) => e.preventDefault());
// mobile touch gestures
target.addEventListener("touchstart", (e) => {
if (e.touches.length >= 2) {
e.preventDefault();
clearTimeout(tapTimer);
clearTimeout(longPressTimer);
cmdShut();
return;
}
isLongPress = false;
longPressTimer = setTimeout(() => {
isLongPress = true;
cmdRepeat();
}, 600);
}, { passive: false });
target.addEventListener("touchend", (e) => {
clearTimeout(longPressTimer);
if (isLongPress || e.changedTouches.length > 1) return;
e.preventDefault();
if (tapTimer) {
clearTimeout(tapTimer);
tapTimer = null;
cmdMore();
} else {
tapTimer = setTimeout(() => {
tapTimer = null;
cmdTell();
}, DOUBLE_TAP_DELAY);
}
});
target.addEventListener("touchmove", () => {
clearTimeout(longPressTimer);
});
// desktop mouse gestures fallback
target.addEventListener("click", (e) => {
if (tapTimer) {
clearTimeout(tapTimer);
tapTimer = null;
cmdMore();
} else {
tapTimer = setTimeout(() => {
tapTimer = null;
cmdTell();
}, DOUBLE_TAP_DELAY);
}
});
target.addEventListener("contextmenu", (e) => {
e.preventDefault();
cmdShut();
});
target.addEventListener("mousedown", (e) => {
if (e.button === 1) {
e.preventDefault();
cmdRepeat();
}
});
}
function cmdTell() {
console.log("CMD: tell (single tap)");
apiTell();
}
function cmdMore() {
console.log("CMD: more (double tap)");
apiMore();
}
function cmdShut() {
console.log("CMD: shut (two-finger / right-click)");
speechSynthesis.cancel();
isSpeaking = false;
silenceUntil = Date.now() + 5000;
}
function cmdRepeat() {
console.log("CMD: repeat (long press / middle click)");
if (lastSpokenText) {
speak(lastSpokenText);
} else {
speak("Nothing to repeat yet.");
}
}
document.addEventListener("visibilitychange", () => {
isPageVisible = !document.hidden;
if (!isPageVisible) {
console.log("Page hidden — silencing speech");
speechSynthesis.cancel();
isSpeaking = false;
}
});
function initCamera() {
// try rear camera first (mobile), fall back to any camera (laptop)
navigator.mediaDevices.getUserMedia({
video: { facingMode: { ideal: "environment" } }
})
.then(onCameraReady)
.catch(() => {
console.log("Rear camera not available, trying any camera...");
return navigator.mediaDevices.getUserMedia({ video: true });
})
.then(onCameraReady)
.catch((err) => {
console.error("Camera error:", err);
speak("Camera access failed. Please allow camera permissions.");
});
}
function onCameraReady(stream) {
if (!stream || video.srcObject) return;
video.srcObject = stream;
video.addEventListener("loadedmetadata", () => {
canvas.width = 640;
canvas.height = 480;
speak("System online. Tap the screen to hear what I see.");
});
}
startOverlay.addEventListener("click", () => {
startOverlay.classList.add("hidden");
initCamera();
setupGestures(document.body);
}); |