azizmeer40 commited on
Commit
7e35ef8
·
verified ·
1 Parent(s): 7a78cfa

Update static/script.js

Browse files
Files changed (1) hide show
  1. static/script.js +135 -167
static/script.js CHANGED
@@ -1,168 +1,136 @@
1
- /* ================================================================
2
- 1. WELCOME VOICE & INITIALIZATION
3
- ================================================================ */
4
- window.onload = () => {
5
- // FIX: Agar system shut down ho kar reload hua hai to welcome message na chalayen
6
- if (sessionStorage.getItem('isShuttingDown') === 'true') {
7
- sessionStorage.removeItem('isShuttingDown');
8
- return;
9
- }
10
-
11
- const introSound = new Audio('/static/sounds/hitech-logo-intro-164339.mp3');
12
- introSound.volume = 0.1;
13
- introSound.play().catch(() => console.log("Sound blocked by browser"));
14
-
15
- setTimeout(() => {
16
- const nameElement = document.getElementById('user-name-data');
17
- let actualName = "User";
18
- if (nameElement && nameElement.innerText.trim() !== "") {
19
- actualName = nameElement.innerText.trim();
20
- }
21
-
22
- const message = `Welcome ${actualName} to the Voice Vision world.`;
23
- const speech = new SpeechSynthesisUtterance(message);
24
-
25
- let voices = window.speechSynthesis.getVoices();
26
- let selectedVoice = voices.find(voice =>
27
- voice.name.includes('Google UK English Female') ||
28
- voice.name.includes('Microsoft Zira') ||
29
- voice.name.includes('Female')
30
- );
31
-
32
- if (selectedVoice) speech.voice = selectedVoice;
33
- speech.rate = 0.85;
34
- speech.pitch = 1.1;
35
-
36
- window.speechSynthesis.cancel();
37
- window.speechSynthesis.speak(speech);
38
- }, 1500);
39
- };
40
-
41
- window.speechSynthesis.onvoiceschanged = () => {
42
- window.speechSynthesis.getVoices();
43
- };
44
-
45
- /* ================================================================
46
- 2. DASHBOARD DYNAMIC CONTROLS (POWER ON / OFF)
47
- ================================================================ */
48
- let cameraOn = false;
49
- let detectionInterval;
50
-
51
- async function handlePowerOn() {
52
- const card = document.getElementById('mainCard');
53
- const welcome = document.getElementById('welcomeHeader');
54
- const cameraBox = document.getElementById('cameraBox');
55
- const powerBtn = document.getElementById('powerBtn');
56
- const startDetectBtn = document.getElementById('startDetectBtn');
57
- const powerOffBtn = document.getElementById('powerOffBtn');
58
- const video = document.getElementById('video');
59
-
60
- try {
61
- const stream = await navigator.mediaDevices.getUserMedia({ video: true });
62
- video.srcObject = stream;
63
- cameraOn = true;
64
-
65
- card.classList.add('card-expanded');
66
- welcome.style.display = 'none';
67
- cameraBox.style.display = 'block';
68
-
69
- powerBtn.style.display = 'none';
70
- startDetectBtn.style.display = 'block';
71
- powerOffBtn.style.display = 'block';
72
-
73
- const msg = new SpeechSynthesisUtterance("System online. Camera activated.");
74
- window.speechSynthesis.speak(msg);
75
-
76
- } catch (err) {
77
- console.error("Camera access error:", err);
78
- alert("Please allow camera access to use the dashboard.");
79
- }
80
- }
81
-
82
- function handlePowerOff() {
83
- // Flag set karein taake reload ke baad welcome na bole
84
- sessionStorage.setItem('isShuttingDown', 'true');
85
-
86
- const msg = new SpeechSynthesisUtterance("System shutting down.");
87
- window.speechSynthesis.cancel();
88
- window.speechSynthesis.speak(msg);
89
-
90
- // Reload se pehle thoda wait karein taake awaz puri ho
91
- setTimeout(() => {
92
- // location.reload() ki jagah hum simply home page par bhej dete hain
93
- // taake camera aur interval clean ho jayein
94
- window.location.href = "/dashboard";
95
- }, 1500);
96
- }
97
-
98
- /* ================================================================
99
- 3. DETECTION & MODES HANDLING
100
- ================================================================ */
101
- function handleStartDetection() {
102
- const modeGroup = document.getElementById('modeButtonsGroup');
103
- const startDetectBtn = document.getElementById('startDetectBtn');
104
-
105
- modeGroup.style.display = 'flex';
106
- startDetectBtn.style.display = 'none';
107
-
108
- const msg = new SpeechSynthesisUtterance("Detection started. Select a mode.");
109
- window.speechSynthesis.speak(msg);
110
-
111
- detectionInterval = setInterval(sendFrameToBackend, 1500);
112
- }
113
-
114
- function activateMode(mode) {
115
- const msg = new SpeechSynthesisUtterance(mode + " mode activated");
116
- window.speechSynthesis.cancel();
117
- window.speechSynthesis.speak(msg);
118
- }
119
- /* ================================================================
120
- 4. BACKEND COMMUNICATION (PROCESS FRAMES) Object_Detetion.py Script.js
121
- ================================================================ */
122
- async function sendFrameToBackend() {
123
- if (!cameraOn) return;
124
-
125
- const canvas = document.getElementById('canvas');
126
- const video = document.getElementById('video');
127
-
128
- if (!canvas || !video) return;
129
-
130
- canvas.width = 640;
131
- canvas.height = 480;
132
- const ctx = canvas.getContext('2d');
133
- ctx.drawImage(video, 0, 0, 640, 480);
134
-
135
- const dataUrl = canvas.toDataURL('image/jpeg', 0.5); // Quality kam ki taake transfer fast ho
136
-
137
- try {
138
- // URL ko relative rakha hai taake browser block na kare
139
- const response = await fetch('/process_frame', {
140
- method: 'POST',
141
- headers: {
142
- 'Content-Type': 'application/json',
143
- 'Accept': 'application/json'
144
- },
145
- body: JSON.stringify({ image: dataUrl })
146
- });
147
-
148
- if (!response.ok) throw new Error('Network response was not ok');
149
-
150
- const result = await response.json();
151
- if (result.objects && result.objects.length > 0) {
152
- speakResults(result.objects);
153
- }
154
- } catch (error) {
155
- // Agar console me error aaye to yahan dikhega
156
- console.log("Fetch Error: ", error.message);
157
- }
158
- }
159
-
160
- function speakResults(objects) {
161
- let message = "I can see " + objects.join(", ");
162
- let speech = new SpeechSynthesisUtterance(message);
163
- window.speechSynthesis.speak(speech);
164
- }
165
-
166
- function logout() {
167
- window.location.href = "/logout";
168
  }
 
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
  }