HuggingFace-SK commited on
Commit
36fcd39
·
1 Parent(s): e60eb80

fixed image cropping issue and added result display

Browse files
Files changed (1) hide show
  1. templates/browser-detect.html +11 -20
templates/browser-detect.html CHANGED
@@ -25,19 +25,7 @@
25
 
26
 
27
  <style>
28
- /* Copyright 2023 The MediaPipe Authors.
29
 
30
- Licensed under the Apache License, Version 2.0 (the "License");
31
- you may not use this file except in compliance with the License.
32
- You may obtain a copy of the License at
33
-
34
- http://www.apache.org/licenses/LICENSE-2.0
35
-
36
- Unless required by applicable law or agreed to in writing, software
37
- distributed under the License is distributed on an "AS IS" BASIS,
38
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
39
- See the License for the specific language governing permissions and
40
- limitations under the License. */
41
 
42
  body {
43
  font-family: roboto;
@@ -78,13 +66,14 @@ limitations under the License. -->
78
 
79
 
80
  <div id="liveView" class="videoView">
81
- <img id="output_image"></img>
82
  <button id="webcamButton">
83
  <span>ENABLE WEBCAM</span>
84
  </button>
 
85
  <div style="position: relative;">
86
- <video id="webcam" style="position: absolute" autoplay="" playsinline=""></video>
87
- <canvas class="output_canvas" id="output_canvas" style="position: absolute; left: 0px; top: 0px; display:none"></canvas>
88
  </div>
89
  </div>
90
 
@@ -254,7 +243,7 @@ for (const landmarks of results.landmarks) {
254
  });
255
  drawLandmarks(canvasCtx, landmarks, { color: "#FF0000", lineWidth: 2 });
256
  }
257
- var dataurl=cropCanvas(canvasElement,crop_left,crop_top,244,244).toDataURL("image/jpeg", 2);
258
  document.getElementById("output_image").src=dataurl
259
 
260
  //# sourceURL=pen.js
@@ -268,12 +257,14 @@ function iterate(x,y){
268
 
269
  const cropCanvas = (sourceCanvas,left,top,width,height) => {
270
  let destCanvas = document.createElement('canvas');
271
- destCanvas.width = width;
272
- destCanvas.height = height;
 
 
273
  destCanvas.getContext("2d").drawImage(
274
  sourceCanvas,
275
  left,top,width,height, // source rect with content to crop
276
- 0,0,width,height); // newCanvas, same size as source
277
  var predictionInput=tf.browser.fromPixels(destCanvas.getContext("2d").getImageData(0, 0, 224, 224))
278
 
279
  predict(tf.expandDims(predictionInput,0))
@@ -286,7 +277,7 @@ async function predict(inputTensor){
286
  var prediction = res.predict(inputTensor);
287
  var outputArray = prediction.dataSync(); // Get the output as an array
288
  var predictedClass = outputArray.indexOf(Math.max(...outputArray)); // Get the index
289
-
290
  console.log(letter_list[predictedClass]);
291
  }, function (err) {
292
  console.log(err);
 
25
 
26
 
27
  <style>
 
28
 
 
 
 
 
 
 
 
 
 
 
 
29
 
30
  body {
31
  font-family: roboto;
 
66
 
67
 
68
  <div id="liveView" class="videoView">
69
+ <img id="output_image" style="display:none"></img>
70
  <button id="webcamButton">
71
  <span>ENABLE WEBCAM</span>
72
  </button>
73
+ <span id="predicted_result"></span>
74
  <div style="position: relative;">
75
+ <video id="webcam" style="position: absolute; display:none" autoplay="" playsinline=""></video>
76
+ <canvas class="output_canvas" id="output_canvas" style="position: absolute; left: 0px; top: 0px; display:block"></canvas>
77
  </div>
78
  </div>
79
 
 
243
  });
244
  drawLandmarks(canvasCtx, landmarks, { color: "#FF0000", lineWidth: 2 });
245
  }
246
+ var dataurl=cropCanvas(canvasElement,crop_left,crop_top,crop_right-crop_left, crop_bottom-crop_top).toDataURL("image/jpeg", 2);
247
  document.getElementById("output_image").src=dataurl
248
 
249
  //# sourceURL=pen.js
 
257
 
258
  const cropCanvas = (sourceCanvas,left,top,width,height) => {
259
  let destCanvas = document.createElement('canvas');
260
+ destCanvas.width = 244;
261
+ var cropAspectRatio = width / height;
262
+
263
+ destCanvas.height = 244 / cropAspectRatio
264
  destCanvas.getContext("2d").drawImage(
265
  sourceCanvas,
266
  left,top,width,height, // source rect with content to crop
267
+ 0,0,244,destCanvas.height); // newCanvas, same size as source
268
  var predictionInput=tf.browser.fromPixels(destCanvas.getContext("2d").getImageData(0, 0, 224, 224))
269
 
270
  predict(tf.expandDims(predictionInput,0))
 
277
  var prediction = res.predict(inputTensor);
278
  var outputArray = prediction.dataSync(); // Get the output as an array
279
  var predictedClass = outputArray.indexOf(Math.max(...outputArray)); // Get the index
280
+ document.getElementById("predicted_result").innerText=letter_list[predictedClass]
281
  console.log(letter_list[predictedClass]);
282
  }, function (err) {
283
  console.log(err);