Spaces:
Runtime error
Runtime error
HuggingFace-SK
commited on
Commit
·
7b3c42a
1
Parent(s):
ecf406d
resize and center
Browse files- templates/browser-detect.html +31 -23
templates/browser-detect.html
CHANGED
|
@@ -9,11 +9,16 @@
|
|
| 9 |
|
| 10 |
body {
|
| 11 |
font-family: roboto;
|
| 12 |
-
margin: 2em;
|
| 13 |
color: #3d3d3d;
|
| 14 |
--mdc-theme-primary: #007f8b;
|
| 15 |
--mdc-theme-on-primary: #f1f3f4;
|
|
|
|
| 16 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
</style>
|
| 18 |
|
| 19 |
<script>
|
|
@@ -32,17 +37,17 @@ body {
|
|
| 32 |
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs-tflite/dist/tf-tflite.min.js"></script>
|
| 33 |
|
| 34 |
|
| 35 |
-
|
| 36 |
<img id="output_image" style="display:none"></img>
|
| 37 |
<button id="webcamButton">
|
| 38 |
<span>ENABLE WEBCAM</span>
|
| 39 |
</button>
|
| 40 |
<span id="predicted_result"></span>
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
|
| 47 |
|
| 48 |
|
|
@@ -120,11 +125,7 @@ let lastVideoTime = -1;
|
|
| 120 |
let results = undefined;
|
| 121 |
console.log(video);
|
| 122 |
async function predictWebcam() {
|
| 123 |
-
canvasElement.
|
| 124 |
-
;
|
| 125 |
-
canvasElement.style.height = video.videoHeight;
|
| 126 |
-
canvasElement.width = video.videoWidth;
|
| 127 |
-
canvasElement.height = video.videoHeight;
|
| 128 |
// Now let's start detecting the stream.
|
| 129 |
if (runningMode === "IMAGE") {
|
| 130 |
runningMode = "VIDEO";
|
|
@@ -137,7 +138,7 @@ async function predictWebcam() {
|
|
| 137 |
}
|
| 138 |
canvasCtx.save();
|
| 139 |
canvasCtx.clearRect(0, 0, canvasElement.width, canvasElement.height);
|
| 140 |
-
canvasCtx.drawImage(video, 0, 0, canvasElement.width, canvasElement.
|
| 141 |
if (results.landmarks) {
|
| 142 |
annotateImage()
|
| 143 |
//detectSign()
|
|
@@ -156,8 +157,8 @@ if(results.landmarks[0]){
|
|
| 156 |
y_array=[]
|
| 157 |
results.landmarks[0].forEach(iterate)
|
| 158 |
//console.log(x_array)
|
| 159 |
-
var image_height = video.videoHeight
|
| 160 |
-
var image_width=
|
| 161 |
var min_x = Math.min(...x_array)*image_width
|
| 162 |
var min_y = Math.min(...y_array)*image_height
|
| 163 |
var max_x = Math.max(...x_array)*image_width
|
|
@@ -286,8 +287,8 @@ drawLandmarks(canvasCtx, hand[17], '#ff3030'); // Pinky palm (17)
|
|
| 286 |
}
|
| 287 |
// Add more drawing calls for each landmark collection as needed
|
| 288 |
|
| 289 |
-
var dataurl=cropCanvas(canvasElement,crop_left,crop_top,crop_right-crop_left, crop_bottom-crop_top).toDataURL("image/jpeg", 2);
|
| 290 |
-
|
| 291 |
|
| 292 |
//# sourceURL=pen.js
|
| 293 |
}
|
|
@@ -300,14 +301,14 @@ function iterate(x,y){
|
|
| 300 |
|
| 301 |
const cropCanvas = (sourceCanvas,left,top,width,height) => {
|
| 302 |
let destCanvas = document.createElement('canvas');
|
| 303 |
-
destCanvas.width =
|
| 304 |
var cropAspectRatio = width / height;
|
| 305 |
|
| 306 |
-
destCanvas.height =
|
| 307 |
destCanvas.getContext("2d").drawImage(
|
| 308 |
sourceCanvas,
|
| 309 |
left,top,width,height, // source rect with content to crop
|
| 310 |
-
0,0,
|
| 311 |
var predictionInput=tf.browser.fromPixels(destCanvas.getContext("2d").getImageData(0, 0, 224, 224))
|
| 312 |
|
| 313 |
predict(tf.expandDims(predictionInput,0))
|
|
@@ -329,11 +330,14 @@ async function predict(inputTensor){
|
|
| 329 |
}
|
| 330 |
|
| 331 |
function drawLandmarks(canvasCtx, landmarks, color) {
|
|
|
|
|
|
|
|
|
|
| 332 |
canvasCtx.fillStyle = color;
|
| 333 |
canvasCtx.strokeStyle = 'white';
|
| 334 |
canvasCtx.lineWidth = 1;
|
| 335 |
canvasCtx.beginPath();
|
| 336 |
-
canvasCtx.arc(landmarks.x *
|
| 337 |
canvasCtx.fill();
|
| 338 |
canvasCtx.stroke();
|
| 339 |
|
|
@@ -341,14 +345,18 @@ function drawLandmarks(canvasCtx, landmarks, color) {
|
|
| 341 |
|
| 342 |
function drawConnection(startNode, endNode, strokeColor, strokeWidth) {
|
| 343 |
|
|
|
|
|
|
|
|
|
|
| 344 |
canvasCtx.strokeStyle = strokeColor;
|
| 345 |
canvasCtx.lineWidth = strokeWidth;
|
| 346 |
canvasCtx.beginPath();
|
| 347 |
-
canvasCtx.moveTo(startNode.x *
|
| 348 |
-
canvasCtx.lineTo(endNode.x *
|
| 349 |
canvasCtx.stroke();
|
| 350 |
}
|
| 351 |
|
|
|
|
| 352 |
</script>
|
| 353 |
|
| 354 |
|
|
|
|
| 9 |
|
| 10 |
body {
|
| 11 |
font-family: roboto;
|
|
|
|
| 12 |
color: #3d3d3d;
|
| 13 |
--mdc-theme-primary: #007f8b;
|
| 14 |
--mdc-theme-on-primary: #f1f3f4;
|
| 15 |
+
box-sizing: border-box;
|
| 16 |
}
|
| 17 |
+
|
| 18 |
+
canvas {
|
| 19 |
+
background-color: lightgray; /* Just for visibility */
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
</style>
|
| 23 |
|
| 24 |
<script>
|
|
|
|
| 37 |
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs-tflite/dist/tf-tflite.min.js"></script>
|
| 38 |
|
| 39 |
|
| 40 |
+
<center>
|
| 41 |
<img id="output_image" style="display:none"></img>
|
| 42 |
<button id="webcamButton">
|
| 43 |
<span>ENABLE WEBCAM</span>
|
| 44 |
</button>
|
| 45 |
<span id="predicted_result"></span>
|
| 46 |
+
|
| 47 |
+
</center>
|
| 48 |
+
<video id="webcam" style="display:none" autoplay="" playsinline=""></video>
|
| 49 |
+
<canvas class="output_canvas" id="output_canvas" style="display:block; position:absolute; left:0px" width="100%" height="30%"></canvas>
|
| 50 |
+
|
| 51 |
|
| 52 |
|
| 53 |
|
|
|
|
| 125 |
let results = undefined;
|
| 126 |
console.log(video);
|
| 127 |
async function predictWebcam() {
|
| 128 |
+
canvasElement.width = window.innerWidth;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
// Now let's start detecting the stream.
|
| 130 |
if (runningMode === "IMAGE") {
|
| 131 |
runningMode = "VIDEO";
|
|
|
|
| 138 |
}
|
| 139 |
canvasCtx.save();
|
| 140 |
canvasCtx.clearRect(0, 0, canvasElement.width, canvasElement.height);
|
| 141 |
+
canvasCtx.drawImage(video, 0, 0, canvasElement.width, (video.videoHeight/video.videoWidth)*canvasElement.width)
|
| 142 |
if (results.landmarks) {
|
| 143 |
annotateImage()
|
| 144 |
//detectSign()
|
|
|
|
| 157 |
y_array=[]
|
| 158 |
results.landmarks[0].forEach(iterate)
|
| 159 |
//console.log(x_array)
|
| 160 |
+
var image_height = (video.videoHeight/video.videoWidth)*canvasElement.width
|
| 161 |
+
var image_width= canvasElement.width
|
| 162 |
var min_x = Math.min(...x_array)*image_width
|
| 163 |
var min_y = Math.min(...y_array)*image_height
|
| 164 |
var max_x = Math.max(...x_array)*image_width
|
|
|
|
| 287 |
}
|
| 288 |
// Add more drawing calls for each landmark collection as needed
|
| 289 |
|
| 290 |
+
var dataurl=cropCanvas(canvasElement,crop_left,crop_top,crop_right-crop_left, crop_bottom-crop_top).toDataURL("image/jpeg", 0.2);
|
| 291 |
+
|
| 292 |
|
| 293 |
//# sourceURL=pen.js
|
| 294 |
}
|
|
|
|
| 301 |
|
| 302 |
const cropCanvas = (sourceCanvas,left,top,width,height) => {
|
| 303 |
let destCanvas = document.createElement('canvas');
|
| 304 |
+
destCanvas.width = 224;
|
| 305 |
var cropAspectRatio = width / height;
|
| 306 |
|
| 307 |
+
destCanvas.height = 224 / cropAspectRatio
|
| 308 |
destCanvas.getContext("2d").drawImage(
|
| 309 |
sourceCanvas,
|
| 310 |
left,top,width,height, // source rect with content to crop
|
| 311 |
+
0,0,224,destCanvas.height); // newCanvas, same size as source
|
| 312 |
var predictionInput=tf.browser.fromPixels(destCanvas.getContext("2d").getImageData(0, 0, 224, 224))
|
| 313 |
|
| 314 |
predict(tf.expandDims(predictionInput,0))
|
|
|
|
| 330 |
}
|
| 331 |
|
| 332 |
function drawLandmarks(canvasCtx, landmarks, color) {
|
| 333 |
+
var image_height = (video.videoHeight/video.videoWidth)*canvasElement.width
|
| 334 |
+
var image_width= canvasElement.width
|
| 335 |
+
|
| 336 |
canvasCtx.fillStyle = color;
|
| 337 |
canvasCtx.strokeStyle = 'white';
|
| 338 |
canvasCtx.lineWidth = 1;
|
| 339 |
canvasCtx.beginPath();
|
| 340 |
+
canvasCtx.arc(landmarks.x * image_width, landmarks.y*image_height, 6, 0, 2 * Math.PI);
|
| 341 |
canvasCtx.fill();
|
| 342 |
canvasCtx.stroke();
|
| 343 |
|
|
|
|
| 345 |
|
| 346 |
function drawConnection(startNode, endNode, strokeColor, strokeWidth) {
|
| 347 |
|
| 348 |
+
var image_height = (video.videoHeight/video.videoWidth)*canvasElement.width
|
| 349 |
+
var image_width= canvasElement.width
|
| 350 |
+
|
| 351 |
canvasCtx.strokeStyle = strokeColor;
|
| 352 |
canvasCtx.lineWidth = strokeWidth;
|
| 353 |
canvasCtx.beginPath();
|
| 354 |
+
canvasCtx.moveTo(startNode.x * image_width, startNode.y*image_height);
|
| 355 |
+
canvasCtx.lineTo(endNode.x * image_width, endNode.y*image_height);
|
| 356 |
canvasCtx.stroke();
|
| 357 |
}
|
| 358 |
|
| 359 |
+
|
| 360 |
</script>
|
| 361 |
|
| 362 |
|