Spaces:
Sleeping
Sleeping
Update static/script.js
Browse files- static/script.js +208 -56
static/script.js
CHANGED
|
@@ -1,52 +1,181 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
/* ================================================================
|
| 2 |
-
1.
|
| 3 |
================================================================ */
|
| 4 |
window.onload = () => {
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
return;
|
| 8 |
-
}
|
| 9 |
|
| 10 |
-
// Welcome sound & speech logic
|
| 11 |
setTimeout(() => {
|
| 12 |
-
|
| 13 |
-
let actualName = (nameElement && nameElement.innerText.trim() !== "") ? nameElement.innerText.trim() : "User";
|
| 14 |
-
|
| 15 |
-
const message = `Welcome ${actualName} to the Voice Vision world.`;
|
| 16 |
-
const speech = new SpeechSynthesisUtterance(message);
|
| 17 |
-
speech.rate = 0.9;
|
| 18 |
-
window.speechSynthesis.speak(speech);
|
| 19 |
}, 1500);
|
| 20 |
};
|
| 21 |
|
| 22 |
-
/* ================================================================
|
| 23 |
-
2. DASHBOARD DYNAMIC CONTROLS (POWER ON / OFF)
|
| 24 |
-
================================================================ */
|
| 25 |
let cameraOn = false;
|
| 26 |
let isDetecting = false;
|
|
|
|
|
|
|
| 27 |
|
|
|
|
|
|
|
|
|
|
| 28 |
async function handlePowerOn() {
|
| 29 |
const video = document.getElementById('video');
|
| 30 |
-
const cameraBox = document.getElementById('cameraBox');
|
| 31 |
const powerBtn = document.getElementById('powerBtn');
|
| 32 |
|
| 33 |
try {
|
| 34 |
-
// Muted hona zaroori hai browser policy ke liye
|
| 35 |
const stream = await navigator.mediaDevices.getUserMedia({
|
| 36 |
video: { width: 640, height: 480 }
|
| 37 |
});
|
| 38 |
video.srcObject = stream;
|
| 39 |
cameraOn = true;
|
| 40 |
|
| 41 |
-
|
| 42 |
powerBtn.style.display = 'none';
|
| 43 |
document.getElementById('startDetectBtn').style.display = 'inline-block';
|
| 44 |
document.getElementById('powerOffBtn').style.display = 'inline-block';
|
| 45 |
|
| 46 |
-
|
| 47 |
} catch (err) {
|
| 48 |
-
alert("Camera
|
| 49 |
-
console.error(err);
|
| 50 |
}
|
| 51 |
}
|
| 52 |
|
|
@@ -56,30 +185,29 @@ function handlePowerOff() {
|
|
| 56 |
}
|
| 57 |
|
| 58 |
/* ================================================================
|
| 59 |
-
3. DETECTION
|
| 60 |
================================================================ */
|
| 61 |
function handleStartDetection() {
|
| 62 |
isDetecting = true;
|
| 63 |
document.getElementById('modeButtonsGroup').style.display = 'flex';
|
| 64 |
document.getElementById('startDetectBtn').style.display = 'none';
|
| 65 |
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
// Detection loop shuru karein
|
| 69 |
captureAndSend();
|
| 70 |
}
|
| 71 |
|
| 72 |
async function captureAndSend() {
|
| 73 |
-
if (!cameraOn || !isDetecting) return;
|
| 74 |
|
| 75 |
const video = document.getElementById('video');
|
| 76 |
-
const canvas = document.createElement('canvas');
|
| 77 |
canvas.width = 640;
|
| 78 |
canvas.height = 480;
|
| 79 |
const ctx = canvas.getContext('2d');
|
| 80 |
-
|
| 81 |
ctx.drawImage(video, 0, 0, 640, 480);
|
| 82 |
-
|
|
|
|
|
|
|
| 83 |
|
| 84 |
try {
|
| 85 |
const response = await fetch('/process_frame', {
|
|
@@ -89,48 +217,72 @@ async function captureAndSend() {
|
|
| 89 |
});
|
| 90 |
|
| 91 |
const result = await response.json();
|
| 92 |
-
|
| 93 |
if (result.detections) {
|
| 94 |
-
// Screen par boxes draw karne wala function
|
| 95 |
drawDetections(result.detections);
|
| 96 |
-
|
| 97 |
-
// Agar koi object milay to bolein
|
| 98 |
-
if (result.detections.length > 0) {
|
| 99 |
-
const labels = result.detections.map(d => d.label).join(", ");
|
| 100 |
-
// Sirf tab bolein jab naya object aaye (optional)
|
| 101 |
-
// window.speechSynthesis.speak(new SpeechSynthesisUtterance("I see " + labels));
|
| 102 |
-
}
|
| 103 |
}
|
| 104 |
} catch (error) {
|
| 105 |
-
console.log("
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
}
|
| 107 |
-
|
| 108 |
-
// Har 1 second baad agla frame bhejein (Cloud ke liye 1s safe hai)
|
| 109 |
-
if (isDetecting) setTimeout(captureAndSend, 1000);
|
| 110 |
}
|
| 111 |
|
| 112 |
-
//
|
| 113 |
function drawDetections(detections) {
|
| 114 |
const canvas = document.getElementById('detectionCanvas');
|
| 115 |
-
if (!canvas) return;
|
| 116 |
const ctx = canvas.getContext('2d');
|
| 117 |
-
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
| 118 |
|
| 119 |
-
detections.forEach(det => {
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
// Box style
|
| 123 |
-
ctx.strokeStyle = "#00FF00";
|
| 124 |
ctx.lineWidth = 3;
|
| 125 |
-
ctx.strokeRect(
|
| 126 |
|
| 127 |
-
|
| 128 |
-
ctx.
|
| 129 |
-
ctx.
|
| 130 |
-
ctx.fillText(`${det.label} (${Math.round(det.confidence * 100)}%)`, x1, y1 > 20 ? y1 - 5 : y1 + 20);
|
| 131 |
});
|
| 132 |
}
|
| 133 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
function logout() {
|
| 135 |
window.location.href = "/logout";
|
| 136 |
}
|
|
|
|
| 1 |
+
// /* ================================================================
|
| 2 |
+
// 1. WELCOME VOICE & INITIALIZATION
|
| 3 |
+
// ================================================================ */
|
| 4 |
+
// window.onload = () => {
|
| 5 |
+
// if (sessionStorage.getItem('isShuttingDown') === 'true') {
|
| 6 |
+
// sessionStorage.removeItem('isShuttingDown');
|
| 7 |
+
// return;
|
| 8 |
+
// }
|
| 9 |
+
|
| 10 |
+
// // Welcome sound & speech logic
|
| 11 |
+
// setTimeout(() => {
|
| 12 |
+
// const nameElement = document.getElementById('user-name-data');
|
| 13 |
+
// let actualName = (nameElement && nameElement.innerText.trim() !== "") ? nameElement.innerText.trim() : "User";
|
| 14 |
+
|
| 15 |
+
// const message = `Welcome ${actualName} to the Voice Vision world.`;
|
| 16 |
+
// const speech = new SpeechSynthesisUtterance(message);
|
| 17 |
+
// speech.rate = 0.9;
|
| 18 |
+
// window.speechSynthesis.speak(speech);
|
| 19 |
+
// }, 1500);
|
| 20 |
+
// };
|
| 21 |
+
|
| 22 |
+
// /* ================================================================
|
| 23 |
+
// 2. DASHBOARD DYNAMIC CONTROLS (POWER ON / OFF)
|
| 24 |
+
// ================================================================ */
|
| 25 |
+
// let cameraOn = false;
|
| 26 |
+
// let isDetecting = false;
|
| 27 |
+
|
| 28 |
+
// async function handlePowerOn() {
|
| 29 |
+
// const video = document.getElementById('video');
|
| 30 |
+
// const cameraBox = document.getElementById('cameraBox');
|
| 31 |
+
// const powerBtn = document.getElementById('powerBtn');
|
| 32 |
+
|
| 33 |
+
// try {
|
| 34 |
+
// // Muted hona zaroori hai browser policy ke liye
|
| 35 |
+
// const stream = await navigator.mediaDevices.getUserMedia({
|
| 36 |
+
// video: { width: 640, height: 480 }
|
| 37 |
+
// });
|
| 38 |
+
// video.srcObject = stream;
|
| 39 |
+
// cameraOn = true;
|
| 40 |
+
|
| 41 |
+
// cameraBox.style.display = 'block';
|
| 42 |
+
// powerBtn.style.display = 'none';
|
| 43 |
+
// document.getElementById('startDetectBtn').style.display = 'inline-block';
|
| 44 |
+
// document.getElementById('powerOffBtn').style.display = 'inline-block';
|
| 45 |
+
|
| 46 |
+
// window.speechSynthesis.speak(new SpeechSynthesisUtterance("System online."));
|
| 47 |
+
// } catch (err) {
|
| 48 |
+
// alert("Camera permission denied!");
|
| 49 |
+
// console.error(err);
|
| 50 |
+
// }
|
| 51 |
+
// }
|
| 52 |
+
|
| 53 |
+
// function handlePowerOff() {
|
| 54 |
+
// sessionStorage.setItem('isShuttingDown', 'true');
|
| 55 |
+
// window.location.reload();
|
| 56 |
+
// }
|
| 57 |
+
|
| 58 |
+
// /* ================================================================
|
| 59 |
+
// 3. DETECTION LOGIC (Cloud Optimized)
|
| 60 |
+
// ================================================================ */
|
| 61 |
+
// function handleStartDetection() {
|
| 62 |
+
// isDetecting = true;
|
| 63 |
+
// document.getElementById('modeButtonsGroup').style.display = 'flex';
|
| 64 |
+
// document.getElementById('startDetectBtn').style.display = 'none';
|
| 65 |
+
|
| 66 |
+
// window.speechSynthesis.speak(new SpeechSynthesisUtterance("Detection started."));
|
| 67 |
+
|
| 68 |
+
// // Detection loop shuru karein
|
| 69 |
+
// captureAndSend();
|
| 70 |
+
// }
|
| 71 |
+
|
| 72 |
+
// async function captureAndSend() {
|
| 73 |
+
// if (!cameraOn || !isDetecting) return;
|
| 74 |
+
|
| 75 |
+
// const video = document.getElementById('video');
|
| 76 |
+
// const canvas = document.createElement('canvas'); // Arzi canvas frame lene ke liye
|
| 77 |
+
// canvas.width = 640;
|
| 78 |
+
// canvas.height = 480;
|
| 79 |
+
// const ctx = canvas.getContext('2d');
|
| 80 |
+
|
| 81 |
+
// ctx.drawImage(video, 0, 0, 640, 480);
|
| 82 |
+
// const dataUrl = canvas.toDataURL('image/jpeg', 0.6); // Quality kam taake fast transfer ho
|
| 83 |
+
|
| 84 |
+
// try {
|
| 85 |
+
// const response = await fetch('/process_frame', {
|
| 86 |
+
// method: 'POST',
|
| 87 |
+
// headers: { 'Content-Type': 'application/json' },
|
| 88 |
+
// body: JSON.stringify({ image: dataUrl })
|
| 89 |
+
// });
|
| 90 |
+
|
| 91 |
+
// const result = await response.json();
|
| 92 |
+
|
| 93 |
+
// if (result.detections) {
|
| 94 |
+
// // Screen par boxes draw karne wala function
|
| 95 |
+
// drawDetections(result.detections);
|
| 96 |
+
|
| 97 |
+
// // Agar koi object milay to bolein
|
| 98 |
+
// if (result.detections.length > 0) {
|
| 99 |
+
// const labels = result.detections.map(d => d.label).join(", ");
|
| 100 |
+
// // Sirf tab bolein jab naya object aaye (optional)
|
| 101 |
+
// // window.speechSynthesis.speak(new SpeechSynthesisUtterance("I see " + labels));
|
| 102 |
+
// }
|
| 103 |
+
// }
|
| 104 |
+
// } catch (error) {
|
| 105 |
+
// console.log("Error sending frame:", error);
|
| 106 |
+
// }
|
| 107 |
+
|
| 108 |
+
// // Har 1 second baad agla frame bhejein (Cloud ke liye 1s safe hai)
|
| 109 |
+
// if (isDetecting) setTimeout(captureAndSend, 1000);
|
| 110 |
+
// }
|
| 111 |
+
|
| 112 |
+
// // Canvas par Boxes draw karne ka function
|
| 113 |
+
// function drawDetections(detections) {
|
| 114 |
+
// const canvas = document.getElementById('detectionCanvas');
|
| 115 |
+
// if (!canvas) return;
|
| 116 |
+
// const ctx = canvas.getContext('2d');
|
| 117 |
+
// ctx.clearRect(0, 0, canvas.width, canvas.height); // Purane boxes saaf karein
|
| 118 |
+
|
| 119 |
+
// detections.forEach(det => {
|
| 120 |
+
// const [x1, y1, x2, y2] = det.box;
|
| 121 |
+
|
| 122 |
+
// // Box style
|
| 123 |
+
// ctx.strokeStyle = "#00FF00";
|
| 124 |
+
// ctx.lineWidth = 3;
|
| 125 |
+
// ctx.strokeRect(x1, y1, x2 - x1, y2 - y1);
|
| 126 |
+
|
| 127 |
+
// // Label style
|
| 128 |
+
// ctx.fillStyle = "#00FF00";
|
| 129 |
+
// ctx.font = "18px Arial";
|
| 130 |
+
// ctx.fillText(`${det.label} (${Math.round(det.confidence * 100)}%)`, x1, y1 > 20 ? y1 - 5 : y1 + 20);
|
| 131 |
+
// });
|
| 132 |
+
// }
|
| 133 |
+
|
| 134 |
+
// function logout() {
|
| 135 |
+
// window.location.href = "/logout";
|
| 136 |
+
// }
|
| 137 |
+
|
| 138 |
+
|
| 139 |
+
|
| 140 |
/* ================================================================
|
| 141 |
+
1. INITIALIZATION & WELCOME
|
| 142 |
================================================================ */
|
| 143 |
window.onload = () => {
|
| 144 |
+
const nameElement = document.getElementById('user-name-data');
|
| 145 |
+
let actualName = (nameElement && nameElement.innerText.trim() !== "") ? nameElement.innerText.trim() : "User";
|
|
|
|
|
|
|
| 146 |
|
|
|
|
| 147 |
setTimeout(() => {
|
| 148 |
+
speak(`Welcome ${actualName} to the Voice Vision world.`);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
}, 1500);
|
| 150 |
};
|
| 151 |
|
|
|
|
|
|
|
|
|
|
| 152 |
let cameraOn = false;
|
| 153 |
let isDetecting = false;
|
| 154 |
+
let isProcessing = false;
|
| 155 |
+
let lastSpoken = "";
|
| 156 |
|
| 157 |
+
/* ================================================================
|
| 158 |
+
2. POWER CONTROLS
|
| 159 |
+
================================================================ */
|
| 160 |
async function handlePowerOn() {
|
| 161 |
const video = document.getElementById('video');
|
|
|
|
| 162 |
const powerBtn = document.getElementById('powerBtn');
|
| 163 |
|
| 164 |
try {
|
|
|
|
| 165 |
const stream = await navigator.mediaDevices.getUserMedia({
|
| 166 |
video: { width: 640, height: 480 }
|
| 167 |
});
|
| 168 |
video.srcObject = stream;
|
| 169 |
cameraOn = true;
|
| 170 |
|
| 171 |
+
// UI Updates
|
| 172 |
powerBtn.style.display = 'none';
|
| 173 |
document.getElementById('startDetectBtn').style.display = 'inline-block';
|
| 174 |
document.getElementById('powerOffBtn').style.display = 'inline-block';
|
| 175 |
|
| 176 |
+
speak("System online. Camera activated.");
|
| 177 |
} catch (err) {
|
| 178 |
+
alert("Camera access denied! Please allow camera from browser settings.");
|
|
|
|
| 179 |
}
|
| 180 |
}
|
| 181 |
|
|
|
|
| 185 |
}
|
| 186 |
|
| 187 |
/* ================================================================
|
| 188 |
+
3. DETECTION & REALITY-BASED VI LOGIC
|
| 189 |
================================================================ */
|
| 190 |
function handleStartDetection() {
|
| 191 |
isDetecting = true;
|
| 192 |
document.getElementById('modeButtonsGroup').style.display = 'flex';
|
| 193 |
document.getElementById('startDetectBtn').style.display = 'none';
|
| 194 |
|
| 195 |
+
speak("Detection started. Focusing on your path.");
|
|
|
|
|
|
|
| 196 |
captureAndSend();
|
| 197 |
}
|
| 198 |
|
| 199 |
async function captureAndSend() {
|
| 200 |
+
if (!cameraOn || !isDetecting || isProcessing) return;
|
| 201 |
|
| 202 |
const video = document.getElementById('video');
|
| 203 |
+
const canvas = document.createElement('canvas');
|
| 204 |
canvas.width = 640;
|
| 205 |
canvas.height = 480;
|
| 206 |
const ctx = canvas.getContext('2d');
|
|
|
|
| 207 |
ctx.drawImage(video, 0, 0, 640, 480);
|
| 208 |
+
|
| 209 |
+
const dataUrl = canvas.toDataURL('image/jpeg', 0.4);
|
| 210 |
+
isProcessing = true;
|
| 211 |
|
| 212 |
try {
|
| 213 |
const response = await fetch('/process_frame', {
|
|
|
|
| 217 |
});
|
| 218 |
|
| 219 |
const result = await response.json();
|
|
|
|
| 220 |
if (result.detections) {
|
|
|
|
| 221 |
drawDetections(result.detections);
|
| 222 |
+
processVIAwareness(result.detections);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 223 |
}
|
| 224 |
} catch (error) {
|
| 225 |
+
console.log("Network lag...");
|
| 226 |
+
} finally {
|
| 227 |
+
isProcessing = false;
|
| 228 |
+
// 500ms delay for stability on Hugging Face
|
| 229 |
+
if (isDetecting) setTimeout(captureAndSend, 500);
|
| 230 |
}
|
|
|
|
|
|
|
|
|
|
| 231 |
}
|
| 232 |
|
| 233 |
+
// Draw Boxes on Canvas
|
| 234 |
function drawDetections(detections) {
|
| 235 |
const canvas = document.getElementById('detectionCanvas');
|
|
|
|
| 236 |
const ctx = canvas.getContext('2d');
|
| 237 |
+
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
| 238 |
|
| 239 |
+
detections.forEach((det, index) => {
|
| 240 |
+
// det object contains x, y, w, h from backend
|
| 241 |
+
ctx.strokeStyle = index === 0 ? "#00FF00" : "#ffcc00"; // Green for closest object
|
|
|
|
|
|
|
| 242 |
ctx.lineWidth = 3;
|
| 243 |
+
ctx.strokeRect(det.x, det.y, det.w, det.h);
|
| 244 |
|
| 245 |
+
ctx.fillStyle = index === 0 ? "#00FF00" : "#ffcc00";
|
| 246 |
+
ctx.font = "bold 18px Arial";
|
| 247 |
+
ctx.fillText(det.label.toUpperCase(), det.x, det.y - 10);
|
|
|
|
| 248 |
});
|
| 249 |
}
|
| 250 |
|
| 251 |
+
// REALITY-BASED LOGIC: Direction + Proximity
|
| 252 |
+
function processVIAwareness(detections) {
|
| 253 |
+
if (detections.length === 0) return;
|
| 254 |
+
|
| 255 |
+
// Sorting backend pe ho chuki hai, detections[0] sabse bari cheez hai
|
| 256 |
+
let obj = detections[0];
|
| 257 |
+
let centerX = obj.x + (obj.w / 2);
|
| 258 |
+
let area = (obj.w * obj.h) / (640 * 480);
|
| 259 |
+
|
| 260 |
+
// 1. Direction Logic (Clock System)
|
| 261 |
+
let direction = "in front of you";
|
| 262 |
+
if (centerX < 213) direction = "on your left";
|
| 263 |
+
else if (centerX > 426) direction = "on your right";
|
| 264 |
+
|
| 265 |
+
// 2. Proximity Logic (Distance)
|
| 266 |
+
let warning = "";
|
| 267 |
+
if (area > 0.45) warning = "STOP! OBSTACLE VERY CLOSE. ";
|
| 268 |
+
else if (area > 0.2) warning = "Careful, ";
|
| 269 |
+
|
| 270 |
+
let finalMessage = `${warning}${obj.label} ${direction}`;
|
| 271 |
+
|
| 272 |
+
// Sirf tab bolein jab object ya direction badle
|
| 273 |
+
if (finalMessage !== lastSpoken) {
|
| 274 |
+
speak(finalMessage);
|
| 275 |
+
lastSpoken = finalMessage;
|
| 276 |
+
}
|
| 277 |
+
}
|
| 278 |
+
|
| 279 |
+
function speak(text) {
|
| 280 |
+
window.speechSynthesis.cancel(); // Backlog saaf karein
|
| 281 |
+
let msg = new SpeechSynthesisUtterance(text);
|
| 282 |
+
msg.rate = 1.2;
|
| 283 |
+
window.speechSynthesis.speak(msg);
|
| 284 |
+
}
|
| 285 |
+
|
| 286 |
function logout() {
|
| 287 |
window.location.href = "/logout";
|
| 288 |
}
|