Spaces:
Runtime error
Runtime error
HuggingFace-SK
commited on
Commit
·
17c3f55
1
Parent(s):
8f21c12
add switch camera button
Browse files- templates/browser-detect.html +45 -18
templates/browser-detect.html
CHANGED
|
@@ -29,12 +29,12 @@
|
|
| 29 |
<div class="container">
|
| 30 |
<video id="webcam" style="display:none" autoplay="" playsinline=""></video>
|
| 31 |
<div class="canvas_wrapper" id="canvas_wrapper">
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
</div>
|
| 39 |
</div>
|
| 40 |
<center>
|
|
@@ -45,7 +45,10 @@
|
|
| 45 |
<div class="wrapper_text">
|
| 46 |
<textarea id="text" onkeyup="set_output_array(this.value)"></textarea>
|
| 47 |
<button id="text-to-speech" onclick="speak(document.getElementById('text').value)">
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
| 49 |
</button>
|
| 50 |
</div>
|
| 51 |
<center>
|
|
@@ -86,11 +89,11 @@
|
|
| 86 |
var word_list = []
|
| 87 |
|
| 88 |
|
| 89 |
-
function set_output_array(text) {
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
}
|
| 94 |
|
| 95 |
</script>
|
| 96 |
|
|
@@ -104,7 +107,7 @@ function set_output_array(text) {
|
|
| 104 |
let webcamRunning = false;
|
| 105 |
var time_since_letter = 0
|
| 106 |
var last_letter_time = 0
|
| 107 |
-
var is_first_run=1
|
| 108 |
// Before we can use HandLandmarker class we must wait for it to finish
|
| 109 |
// loading. Machine Learning models can be large and take a moment to
|
| 110 |
// get everything needed to run.
|
|
@@ -133,6 +136,7 @@ function set_output_array(text) {
|
|
| 133 |
const canvasCtx = canvasElement.getContext("2d");
|
| 134 |
var x_array = []
|
| 135 |
var y_array = []
|
|
|
|
| 136 |
// Check if webcam access is supported.
|
| 137 |
const hasGetUserMedia = () => { var _a; return !!((_a = navigator.mediaDevices) === null || _a === void 0 ? void 0 : _a.getUserMedia); };
|
| 138 |
// If webcam supported, add event listener to button for when user
|
|
@@ -140,10 +144,25 @@ function set_output_array(text) {
|
|
| 140 |
if (hasGetUserMedia()) {
|
| 141 |
enableWebcamButton = document.getElementById("webcamButton");
|
| 142 |
enableWebcamButton.addEventListener("click", enableCam);
|
|
|
|
| 143 |
}
|
| 144 |
else {
|
| 145 |
console.warn("getUserMedia() is not supported by your browser");
|
| 146 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
// Enable the live webcam view and start detection.
|
| 148 |
function enableCam(event) {
|
| 149 |
if (!handLandmarker) {
|
|
@@ -157,11 +176,16 @@ function set_output_array(text) {
|
|
| 157 |
else {
|
| 158 |
webcamRunning = true;
|
| 159 |
enableWebcamButton.style = "display:none"
|
| 160 |
-
|
| 161 |
}
|
| 162 |
// getUsermedia parameters.
|
|
|
|
|
|
|
|
|
|
| 163 |
const constraints = {
|
| 164 |
-
video:
|
|
|
|
|
|
|
| 165 |
};
|
| 166 |
// Activate the webcam stream.
|
| 167 |
navigator.mediaDevices.getUserMedia(constraints).then((stream) => {
|
|
@@ -173,6 +197,9 @@ function set_output_array(text) {
|
|
| 173 |
let results = undefined;
|
| 174 |
console.log(video);
|
| 175 |
async function predictWebcam() {
|
|
|
|
|
|
|
|
|
|
| 176 |
canvasElement.width = window.innerWidth;
|
| 177 |
// Now let's start detecting the stream.
|
| 178 |
if (runningMode === "IMAGE") {
|
|
@@ -187,12 +214,12 @@ function set_output_array(text) {
|
|
| 187 |
canvasCtx.save();
|
| 188 |
canvasCtx.clearRect(0, 0, canvasElement.width, canvasElement.height);
|
| 189 |
canvasCtx.drawImage(video, 0, 0, canvasElement.width, (video.videoHeight / video.videoWidth) * canvasElement.width)
|
| 190 |
-
if (is_first_run == 1){
|
| 191 |
var elem_rect = document.getElementById("output_canvas").getBoundingClientRect()
|
| 192 |
console.log(elem_rect.height | 0);
|
| 193 |
-
document.getElementById("canvas_wrapper").style.height = (elem_rect.height | 0).toString()+"px"
|
| 194 |
|
| 195 |
-
is_first_run=0
|
| 196 |
}
|
| 197 |
|
| 198 |
if (results.landmarks && results.handednesses[0]) {
|
|
|
|
| 29 |
<div class="container">
|
| 30 |
<video id="webcam" style="display:none" autoplay="" playsinline=""></video>
|
| 31 |
<div class="canvas_wrapper" id="canvas_wrapper">
|
| 32 |
+
<canvas class="output_canvas" id="output_canvas" width="100%" height="300%"></canvas>
|
| 33 |
+
<center>
|
| 34 |
+
<button id="webcamButton">
|
| 35 |
+
<span>ENABLE WEBCAM</span>
|
| 36 |
+
</button>
|
| 37 |
+
</center>
|
| 38 |
</div>
|
| 39 |
</div>
|
| 40 |
<center>
|
|
|
|
| 45 |
<div class="wrapper_text">
|
| 46 |
<textarea id="text" onkeyup="set_output_array(this.value)"></textarea>
|
| 47 |
<button id="text-to-speech" onclick="speak(document.getElementById('text').value)">
|
| 48 |
+
<span>Listen to the text 🔊</span>
|
| 49 |
+
</button>
|
| 50 |
+
<button id="switch-camera">
|
| 51 |
+
<span>Switch CM</span>
|
| 52 |
</button>
|
| 53 |
</div>
|
| 54 |
<center>
|
|
|
|
| 89 |
var word_list = []
|
| 90 |
|
| 91 |
|
| 92 |
+
function set_output_array(text) {
|
| 93 |
+
console.log(text)
|
| 94 |
+
word_list = text.split("");
|
| 95 |
+
console.log(word_list)
|
| 96 |
+
}
|
| 97 |
|
| 98 |
</script>
|
| 99 |
|
|
|
|
| 107 |
let webcamRunning = false;
|
| 108 |
var time_since_letter = 0
|
| 109 |
var last_letter_time = 0
|
| 110 |
+
var is_first_run = 1
|
| 111 |
// Before we can use HandLandmarker class we must wait for it to finish
|
| 112 |
// loading. Machine Learning models can be large and take a moment to
|
| 113 |
// get everything needed to run.
|
|
|
|
| 136 |
const canvasCtx = canvasElement.getContext("2d");
|
| 137 |
var x_array = []
|
| 138 |
var y_array = []
|
| 139 |
+
var video_facing_mode = "user"
|
| 140 |
// Check if webcam access is supported.
|
| 141 |
const hasGetUserMedia = () => { var _a; return !!((_a = navigator.mediaDevices) === null || _a === void 0 ? void 0 : _a.getUserMedia); };
|
| 142 |
// If webcam supported, add event listener to button for when user
|
|
|
|
| 144 |
if (hasGetUserMedia()) {
|
| 145 |
enableWebcamButton = document.getElementById("webcamButton");
|
| 146 |
enableWebcamButton.addEventListener("click", enableCam);
|
| 147 |
+
document.getElementById("switch-camera").addEventListener("click", switch_camera);
|
| 148 |
}
|
| 149 |
else {
|
| 150 |
console.warn("getUserMedia() is not supported by your browser");
|
| 151 |
}
|
| 152 |
+
async function switch_camera(){
|
| 153 |
+
if (video_facing_mode=='user'){
|
| 154 |
+
webcamRunning= false
|
| 155 |
+
video_facing_mode = 'environment'
|
| 156 |
+
await load_camera()
|
| 157 |
+
webcamRunning= true
|
| 158 |
+
}
|
| 159 |
+
else{
|
| 160 |
+
webcamRunning = false
|
| 161 |
+
video_facing_mode = 'user'
|
| 162 |
+
await load_camera()
|
| 163 |
+
webcamRunning= true
|
| 164 |
+
}
|
| 165 |
+
}
|
| 166 |
// Enable the live webcam view and start detection.
|
| 167 |
function enableCam(event) {
|
| 168 |
if (!handLandmarker) {
|
|
|
|
| 176 |
else {
|
| 177 |
webcamRunning = true;
|
| 178 |
enableWebcamButton.style = "display:none"
|
| 179 |
+
|
| 180 |
}
|
| 181 |
// getUsermedia parameters.
|
| 182 |
+
load_camera()
|
| 183 |
+
}
|
| 184 |
+
function load_camera(){
|
| 185 |
const constraints = {
|
| 186 |
+
video: {
|
| 187 |
+
facingMode: video_facing_mode
|
| 188 |
+
}
|
| 189 |
};
|
| 190 |
// Activate the webcam stream.
|
| 191 |
navigator.mediaDevices.getUserMedia(constraints).then((stream) => {
|
|
|
|
| 197 |
let results = undefined;
|
| 198 |
console.log(video);
|
| 199 |
async function predictWebcam() {
|
| 200 |
+
if (video.videoHeight == 0){
|
| 201 |
+
return
|
| 202 |
+
}
|
| 203 |
canvasElement.width = window.innerWidth;
|
| 204 |
// Now let's start detecting the stream.
|
| 205 |
if (runningMode === "IMAGE") {
|
|
|
|
| 214 |
canvasCtx.save();
|
| 215 |
canvasCtx.clearRect(0, 0, canvasElement.width, canvasElement.height);
|
| 216 |
canvasCtx.drawImage(video, 0, 0, canvasElement.width, (video.videoHeight / video.videoWidth) * canvasElement.width)
|
| 217 |
+
if (is_first_run == 1) {
|
| 218 |
var elem_rect = document.getElementById("output_canvas").getBoundingClientRect()
|
| 219 |
console.log(elem_rect.height | 0);
|
| 220 |
+
document.getElementById("canvas_wrapper").style.height = (elem_rect.height | 0).toString() + "px"
|
| 221 |
|
| 222 |
+
is_first_run = 0
|
| 223 |
}
|
| 224 |
|
| 225 |
if (results.landmarks && results.handednesses[0]) {
|