HuggingFace-SK commited on
Commit
717217e
·
1 Parent(s): bb63de4

unify android and web html

Browse files
Files changed (2) hide show
  1. main.py +1 -1
  2. templates/browser-detect.html +102 -27
main.py CHANGED
@@ -11,4 +11,4 @@ def send_report():
11
  return send_from_directory("better_exported", "model.tflite")
12
 
13
  if (__name__ == '__main__'):
14
- app.run( host='0.0.0.0', port=7860)
 
11
  return send_from_directory("better_exported", "model.tflite")
12
 
13
  if (__name__ == '__main__'):
14
+ app.run( host='0.0.0.0', port=7860)
templates/browser-detect.html CHANGED
@@ -3,7 +3,7 @@
3
 
4
  <head></head>
5
  <meta charset="UTF-8">
6
-
7
 
8
  <title>Sign Language Interpreter</title>
9
 
@@ -11,24 +11,39 @@
11
  <script>
12
  window.console = window.console || function (t) { };
13
  </script>
14
-
 
 
 
15
  <link rel="stylesheet" type="text/css" href="static/browser_detect.css" />
16
 
17
 
18
  </head>
19
 
20
  <body translate="no">
 
 
 
 
 
 
 
 
 
 
 
21
  <script src="https://cdn.jsdelivr.net/npm/@mediapipe/drawing_utils/drawing_utils.js"
22
  crossorigin="anonymous"></script>
23
  <script src="https://cdn.jsdelivr.net/npm/@mediapipe/hands/hands.js" crossorigin="anonymous"></script>
24
  <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs-core"></script>
25
  <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs-backend-cpu"></script>
26
  <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs-tflite/dist/tf-tflite.min.js"></script>
27
-
 
28
 
29
  <div class="container">
30
 
31
- <video id="webcam" style="display:none" autoplay="" playsinline=""></video>
32
  <div class="canvas_wrapper" id="canvas_wrapper">
33
  <button id="switch-camera" style="display:none; position: absolute; top:10px; left:10px; padding:5px; height:40px; width:40px; text-align: center; border-radius: 12.25px; font-size: 20px; font-weight: 900; border:none; background-color: #f2f2f2; color:black;
34
  box-shadow: 0px 4px 20px 4px rgba(0, 0, 0, 0.38); z-index:100">
@@ -53,13 +68,47 @@
53
  <span>Listen 🔊</span>
54
  </button>
55
 
 
 
 
 
56
  </div>
57
  <center>
58
  <script>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
  var synthesis = window.speechSynthesis;
60
 
61
  if ('speechSynthesis' in window) {
62
-
63
  var synthesis = window.speechSynthesis;
64
 
65
  // Get the first `en` language voice in the list
@@ -70,24 +119,42 @@
70
  // Create an utterance object
71
 
72
  } else {
 
73
  console.log('Text-to-speech not supported.');
74
  }
75
 
76
  function speak(text) {
 
 
 
 
 
 
 
 
 
 
 
77
 
78
- if ('speechSynthesis' in window) {
79
-
80
- // Speak the utterance
81
- var utterance = new SpeechSynthesisUtterance(text);
82
 
83
- // Set utterance properties
84
- utterance.voice = voice;
85
- utterance.pitch = 0.6;
86
- utterance.rate = 0.8;
87
- utterance.volume = 0.8;
 
 
 
 
 
 
 
 
88
  synthesis.speak(utterance);
89
  } else {
90
- console.log('Text-to-speech not supported.');
91
  }
92
  }
93
  var word_list = []
@@ -103,8 +170,9 @@
103
 
104
  <script type="module">
105
 
106
-
107
- import { HandLandmarker, FilesetResolver } from "https://cdn.jsdelivr.net/npm/@mediapipe/tasks-vision@0.10.0";
 
108
  let handLandmarker = undefined;
109
  let runningMode = "IMAGE";
110
  let enableWebcamButton;
@@ -116,10 +184,11 @@
116
  // loading. Machine Learning models can be large and take a moment to
117
  // get everything needed to run.
118
  const createHandLandmarker = async () => {
119
- const vision = await FilesetResolver.forVisionTasks("https://cdn.jsdelivr.net/npm/@mediapipe/tasks-vision@0.10.0/wasm");
120
  handLandmarker = await HandLandmarker.createFromOptions(vision, {
121
  baseOptions: {
122
- modelAssetPath: `https://storage.googleapis.com/mediapipe-models/hand_landmarker/hand_landmarker/float16/1/hand_landmarker.task`,
 
123
  delegate: "GPU"
124
  },
125
  runningMode: runningMode,
@@ -128,7 +197,8 @@
128
  };
129
  createHandLandmarker();
130
 
131
- const MODEL_PATH = "/exported"
 
132
  var objectDetector = tflite.loadTFLiteModel(MODEL_PATH);
133
 
134
  /********************************************************************
@@ -193,10 +263,15 @@
193
  }
194
  };
195
  // Activate the webcam stream.
196
- navigator.mediaDevices.getUserMedia(constraints).then((stream) => {
197
- video.srcObject = stream;
198
- video.addEventListener("loadeddata", predictWebcam);
199
- });
 
 
 
 
 
200
  }
201
  let lastVideoTime = -1;
202
  let results = undefined;
@@ -537,12 +612,12 @@
537
  }
538
  </script>
539
 
 
 
540
 
541
 
542
 
543
-
544
- <script src="https://cdn.jsdelivr.net/npm/@mediapipe/tasks-vision@0.10.0/wasm/vision_wasm_internal.js"
545
- crossorigin="anonymous"></script>
546
  </body>
547
 
548
  </html>
 
3
 
4
  <head></head>
5
  <meta charset="UTF-8">
6
+ <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" />
7
 
8
  <title>Sign Language Interpreter</title>
9
 
 
11
  <script>
12
  window.console = window.console || function (t) { };
13
  </script>
14
+ <!-- For Android
15
+ <link rel="stylesheet" type="text/css" href="http://127.0.0.1:8125/assets/static/browser_detect.css" />
16
+ -->
17
+ <!-- For Web -->
18
  <link rel="stylesheet" type="text/css" href="static/browser_detect.css" />
19
 
20
 
21
  </head>
22
 
23
  <body translate="no">
24
+ <!-- For Android
25
+ <script src="../assets/ipc/androidjs.js"></script>
26
+ <script src="http://127.0.0.1:8125/assets/static/drawing_utils.js" crossorigin="anonymous"></script>
27
+ <script src="http://127.0.0.1:8125/assets/static/hands.js" crossorigin="anonymous"></script>
28
+ <script src="http://127.0.0.1:8125/assets/static/tfjs-core"></script>
29
+ <script src="http://127.0.0.1:8125/assets/static/tfjs-backend-cpu"></script>
30
+ <script src="http://127.0.0.1:8125/assets/static/tf-tflite.min.js"></script>
31
+ <script src="http://127.0.0.1:8125/assets/static/vision_wasm_internal.js" crossorigin="anonymous"></script>
32
+ -->
33
+
34
+ <!-- For Web -->
35
  <script src="https://cdn.jsdelivr.net/npm/@mediapipe/drawing_utils/drawing_utils.js"
36
  crossorigin="anonymous"></script>
37
  <script src="https://cdn.jsdelivr.net/npm/@mediapipe/hands/hands.js" crossorigin="anonymous"></script>
38
  <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs-core"></script>
39
  <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs-backend-cpu"></script>
40
  <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs-tflite/dist/tf-tflite.min.js"></script>
41
+
42
+
43
 
44
  <div class="container">
45
 
46
+ <video id="webcam" style="display:none" autoplay playsinline muted></video>
47
  <div class="canvas_wrapper" id="canvas_wrapper">
48
  <button id="switch-camera" style="display:none; position: absolute; top:10px; left:10px; padding:5px; height:40px; width:40px; text-align: center; border-radius: 12.25px; font-size: 20px; font-weight: 900; border:none; background-color: #f2f2f2; color:black;
49
  box-shadow: 0px 4px 20px 4px rgba(0, 0, 0, 0.38); z-index:100">
 
68
  <span>Listen 🔊</span>
69
  </button>
70
 
71
+ <audio id="audioPlayer">-</audio>
72
+ </div>
73
+ <div id="logUI">
74
+
75
  </div>
76
  <center>
77
  <script>
78
+ var speechSupported = true
79
+ var prevSpeech = ""
80
+
81
+ logUI = document.getElementById("logUI")
82
+
83
+ function logMessage(msg) {
84
+ const span = document.createElement('span');
85
+ span.textContent = msg;
86
+ logUI.appendChild(span);
87
+ logUI.appendChild(document.createElement('br')); // Add a line break
88
+ }
89
+
90
+ const originalFetch = window.fetch;
91
+
92
+ // Override the fetch function
93
+ window.fetch = async function (input, init) {
94
+ // Convert input to URL if it's a Request object
95
+ const url = typeof input === 'string' ? input : input.url;
96
+ var newUrl = url
97
+ if (url == 'https://cdn.jsdelivr.net/npm/@mediapipe/tasks-vision@0.10.0/wasm/vision_wasm_internal.wasm') {
98
+ // newUrl = 'http://127.0.0.1:8125/assets/static/vision_wasm_internal.wasm' //For Android
99
+ newUrl = 'https://cdn.jsdelivr.net/npm/@mediapipe/tasks-vision@0.10.0/wasm/vision_wasm_internal.wasm' // For Web
100
+
101
+ }
102
+ console.log("This was FETCHED: ", newUrl)
103
+ // Call the original fetch function with the new URL
104
+ return originalFetch(newUrl, init);
105
+ };
106
+
107
+
108
  var synthesis = window.speechSynthesis;
109
 
110
  if ('speechSynthesis' in window) {
111
+
112
  var synthesis = window.speechSynthesis;
113
 
114
  // Get the first `en` language voice in the list
 
119
  // Create an utterance object
120
 
121
  } else {
122
+ speechSupported = false;
123
  console.log('Text-to-speech not supported.');
124
  }
125
 
126
  function speak(text) {
127
+ console.log("speech api support", speechSupported)
128
+ console.log("condition: ", !speechSupported)
129
+ console.log("condition2: ", speechSupported == false)
130
+ if (!speechSupported) {
131
+ console.log("speech api support", speechSupported)
132
+ const audioPlayer = document.getElementById('audioPlayer');
133
+ if (prevSpeech != text) {
134
+ prevSpeech = text
135
+ audioPlayer.src = 'http://127.0.0.1:8125/speech?t=' + text; // Set the audio source
136
+ console.log("Set src: ", audioPlayer.src)
137
+ }
138
 
139
+ audioPlayer.play() // Play the audio
140
+ .then(() => {
 
 
141
 
142
+ console.log('Audio is playing');
143
+ })
144
+ .catch(error => {
145
+ console.error('Error playing audio:', error);
146
+ prevSpeech = ''
147
+ });
148
+ } else
149
+ if ('speechSynthesis' in window) {
150
+ var utterance = new SpeechSynthesisUtterance(text);
151
+ utterance.voice = voice;
152
+ utterance.pitch = 0.6;
153
+ utterance.rate = 0.8;
154
+ utterance.volume = 0.8;
155
  synthesis.speak(utterance);
156
  } else {
157
+ console.log("Text to speech is now not supported")
158
  }
159
  }
160
  var word_list = []
 
170
 
171
  <script type="module">
172
 
173
+
174
+ //import { HandLandmarker, FilesetResolver } from "http://127.0.0.1:8125/assets/static/tasks-vision@0.10.0" // For Android
175
+ import { HandLandmarker, FilesetResolver } from "https://cdn.jsdelivr.net/npm/@mediapipe/tasks-vision@0.10.0"; // For Web
176
  let handLandmarker = undefined;
177
  let runningMode = "IMAGE";
178
  let enableWebcamButton;
 
184
  // loading. Machine Learning models can be large and take a moment to
185
  // get everything needed to run.
186
  const createHandLandmarker = async () => {
187
+ const vision = await FilesetResolver.forVisionTasks("https://cdn.jsdelivr.net/npm/@mediapipe/tasks-vision@0.10.0/wasm"); // This doesnt really matter as this is already imported somewhere else, and the code runs fine without the request
188
  handLandmarker = await HandLandmarker.createFromOptions(vision, {
189
  baseOptions: {
190
+ // modelAssetPath: `http://127.0.0.1:8125/assets/static/hand_landmarker.task`, // For Android
191
+ modelAssetPath: `https://storage.googleapis.com/mediapipe-models/hand_landmarker/hand_landmarker/float16/1/hand_landmarker.task`, // For Web
192
  delegate: "GPU"
193
  },
194
  runningMode: runningMode,
 
197
  };
198
  createHandLandmarker();
199
 
200
+ // const MODEL_PATH = "http://127.0.0.1:8125/assets/static/model.tflite" // For Android
201
+ const MODEL_PATH = "/exported" // For Web
202
  var objectDetector = tflite.loadTFLiteModel(MODEL_PATH);
203
 
204
  /********************************************************************
 
263
  }
264
  };
265
  // Activate the webcam stream.
266
+ navigator.mediaDevices.getUserMedia(constraints)
267
+ .then((stream) => {
268
+ video.srcObject = stream;
269
+ video.play();
270
+ video.addEventListener("loadeddata", predictWebcam);
271
+ })
272
+ .catch((error) => {
273
+ console.error("Error accessing the camera: ", error.name, error.message, error.code);
274
+ });
275
  }
276
  let lastVideoTime = -1;
277
  let results = undefined;
 
612
  }
613
  </script>
614
 
615
+ <script src="https://cdn.jsdelivr.net/npm/@mediapipe/tasks-vision@0.10.0/wasm/vision_wasm_internal.js"
616
+ crossorigin="anonymous"></script>
617
 
618
 
619
 
620
+
 
 
621
  </body>
622
 
623
  </html>