azizmeer40 commited on
Commit
b1f8699
·
verified ·
1 Parent(s): f2c1a7b

Update static/detection_handler.js

Browse files
Files changed (1) hide show
  1. static/detection_handler.js +58 -40
static/detection_handler.js CHANGED
@@ -3,7 +3,8 @@
3
  */
4
 
5
  let isProcessing = false;
6
- let lastSpoken = "";
 
7
 
8
  async function sendFrameToBackend() {
9
  if (isProcessing || !cameraOn) return;
@@ -15,7 +16,8 @@ async function sendFrameToBackend() {
15
  const ctx = canvas.getContext('2d');
16
  ctx.drawImage(video, 0, 0, 640, 480);
17
 
18
- const dataUrl = canvas.toDataURL('image/jpeg', 0.4);
 
19
  isProcessing = true;
20
 
21
  try {
@@ -28,41 +30,14 @@ async function sendFrameToBackend() {
28
  const result = await response.json();
29
  if (result.detections) {
30
  drawVisuals(result.detections);
31
- generateVIResponse(result.detections); // VI Awareness Logic
32
  }
33
  } catch (err) {
34
- console.error("Frame skip:", err);
35
  } finally {
36
  isProcessing = false;
37
- // Agla frame tabhi jayega jab purana process ho chuka ho
38
- setTimeout(sendFrameToBackend, 100);
39
- }
40
- }
41
-
42
- // Logic for Direction and Proximity
43
- function generateVIResponse(detections) {
44
- if (detections.length === 0) return;
45
-
46
- // Sabse kareeb wale object par focus karein
47
- let obj = detections[0];
48
- let center = obj.x + (obj.w / 2);
49
- let area = (obj.w * obj.h) / (640 * 480); // Screen coverage percentage
50
-
51
- let direction = "";
52
- if (center < 213) direction = "on your left";
53
- else if (center > 426) direction = "on your right";
54
- else direction = "in front of you";
55
-
56
- let proximity = "";
57
- if (area > 0.4) proximity = "Warning: Very close! ";
58
- else if (area > 0.15) proximity = "Nearby ";
59
-
60
- let message = `${proximity}${obj.label} ${direction}`;
61
-
62
- // Sirf tab bolein jab object ya direction badle (Noise reduction)
63
- if (message !== lastSpoken) {
64
- speak(message);
65
- lastSpoken = message;
66
  }
67
  }
68
 
@@ -71,20 +46,63 @@ function drawVisuals(detections) {
71
  const ctx = canvas.getContext('2d');
72
  ctx.clearRect(0, 0, canvas.width, canvas.height);
73
 
74
- detections.forEach(obj => {
75
- ctx.strokeStyle = "#00FF00";
76
- ctx.lineWidth = 3;
 
 
 
 
 
 
 
 
 
 
77
  ctx.strokeRect(obj.x, obj.y, obj.w, obj.h);
78
 
79
- ctx.fillStyle = "#00FF00";
80
- ctx.font = "16px Arial";
81
- ctx.fillText(obj.label.toUpperCase(), obj.x, obj.y - 10);
82
  });
83
  }
84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
  function speak(text) {
86
  window.speechSynthesis.cancel();
87
  let utterance = new SpeechSynthesisUtterance(text);
88
- utterance.rate = 1.2; // Thoda fast taake timely ho
 
89
  window.speechSynthesis.speak(utterance);
90
  }
 
3
  */
4
 
5
  let isProcessing = false;
6
+ let lastSpokenMessage = "";
7
+ let speechCounter = 0;
8
 
9
  async function sendFrameToBackend() {
10
  if (isProcessing || !cameraOn) return;
 
16
  const ctx = canvas.getContext('2d');
17
  ctx.drawImage(video, 0, 0, 640, 480);
18
 
19
+ // Quality 0.3 speed ke liye behtar hai
20
+ const dataUrl = canvas.toDataURL('image/jpeg', 0.3);
21
  isProcessing = true;
22
 
23
  try {
 
30
  const result = await response.json();
31
  if (result.detections) {
32
  drawVisuals(result.detections);
33
+ processVIOutput(result.detections);
34
  }
35
  } catch (err) {
36
+ console.error("Link Error:", err);
37
  } finally {
38
  isProcessing = false;
39
+ // 200ms ka gap Hugging Face CPU ke liye ideal hai
40
+ setTimeout(sendFrameToBackend, 200);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
  }
42
  }
43
 
 
46
  const ctx = canvas.getContext('2d');
47
  ctx.clearRect(0, 0, canvas.width, canvas.height);
48
 
49
+ // --- DEMO KE LIYE SECTORS DRAW KARNA ---
50
+ ctx.strokeStyle = "rgba(255, 255, 255, 0.2)";
51
+ ctx.setLineDash([5, 5]);
52
+ ctx.beginPath();
53
+ ctx.moveTo(213, 0); ctx.lineTo(213, 480); // Left line
54
+ ctx.moveTo(426, 0); ctx.lineTo(426, 480); // Right line
55
+ ctx.stroke();
56
+ ctx.setLineDash([]);
57
+
58
+ detections.forEach((obj, index) => {
59
+ // Sirf top object ko highlight karein (Reality-based)
60
+ ctx.strokeStyle = index === 0 ? "#00FF00" : "#ffcc00";
61
+ ctx.lineWidth = index === 0 ? 4 : 2;
62
  ctx.strokeRect(obj.x, obj.y, obj.w, obj.h);
63
 
64
+ ctx.fillStyle = index === 0 ? "#00FF00" : "#ffcc00";
65
+ ctx.font = "bold 18px Arial";
66
+ ctx.fillText(`${obj.label}`, obj.x, obj.y - 10);
67
  });
68
  }
69
 
70
+ function processVIOutput(detections) {
71
+ if (detections.length === 0) return;
72
+
73
+ // Sabse bada object (Sabat se kareeb)
74
+ let primary = detections[0];
75
+ let centerX = primary.x + (primary.w / 2);
76
+ let areaRatio = (primary.w * primary.h) / (640 * 480);
77
+
78
+ // 1. Direction Logic
79
+ let direction = "in front of you";
80
+ if (centerX < 213) direction = "on your left";
81
+ else if (centerX > 426) direction = "on your right";
82
+
83
+ // 2. Proximity Logic
84
+ let alert = "";
85
+ if (areaRatio > 0.45) alert = "STOP! ";
86
+ else if (areaRatio > 0.2) alert = "Careful, ";
87
+
88
+ let message = `${alert}${primary.label} ${direction}`;
89
+
90
+ // Status update on screen
91
+ document.getElementById('aiStatus').innerText = `Target: ${message}`;
92
+
93
+ // 3. Smart Voice Filter (Har waqt bolna band)
94
+ speechCounter++;
95
+ if (message !== lastSpokenMessage || speechCounter > 15) {
96
+ speak(message);
97
+ lastSpokenMessage = message;
98
+ speechCounter = 0;
99
+ }
100
+ }
101
+
102
  function speak(text) {
103
  window.speechSynthesis.cancel();
104
  let utterance = new SpeechSynthesisUtterance(text);
105
+ utterance.rate = 1.3;
106
+ utterance.pitch = 1.0;
107
  window.speechSynthesis.speak(utterance);
108
  }