Spaces:
Runtime error
Runtime error
HuggingFace-SK commited on
Commit ·
36fcd39
1
Parent(s): e60eb80
fixed image cropping issue and added result display
Browse files24 unmodified lines2526272829303132333435363738394041424334 unmodified lines78798081828384858687888990163 unmodified lines2542552562572582592607 unmodified lines2682692702712722732742752762772782796 unmodified lines28628728828929029129224 unmodified lines<style>/* Copyright 2023 The MediaPipe Authors.Licensed under the Apache License, Version 2.0 (the "License");you may not use this file except in compliance with the License.You may obtain a copy of the License athttp://www.apache.org/licenses/LICENSE-2.0Unless required by applicable law or agreed to in writing, softwaredistributed under the License is distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.See the License for the specific language governing permissions andlimitations under the License. */body {font-family: roboto;34 unmodified lines<div id="liveView" class="videoView"><img id="output_image"></img><button id="webcamButton"><span>ENABLE WEBCAM</span></button><div style="position: relative;"><video id="webcam" style="position: absolute" autoplay="" playsinline=""></video><canvas class="output_canvas" id="output_canvas" style="position: absolute; left: 0px; top: 0px; display:none"></canvas></div></div>163 unmodified lines});drawLandmarks(canvasCtx, landmarks, { color: "#FF0000", lineWidth: 2 });}var dataurl=cropCanvas(canvasElement,crop_left,crop_top,244,244).toDataURL("image/jpeg", 2);document.getElementById("output_image").src=dataurl//# sourceURL=pen.js7 unmodified linesconst cropCanvas = (sourceCanvas,left,top,width,height) => {let destCanvas = document.createElement('canvas');destCanvas.width = width;destCanvas.height = height;destCanvas.getContext("2d").drawImage(sourceCanvas,left,top,width,height, // source rect with content to crop0,0,width,height); // newCanvas, same size as sourcevar predictionInput=tf.browser.fromPixels(destCanvas.getContext("2d").getImageData(0, 0, 224, 224))predict(tf.expandDims(predictionInput,0))6 unmodified linesvar prediction = res.predict(inputTensor);var outputArray = prediction.dataSync(); // Get the output as an arrayvar predictedClass = outputArray.indexOf(Math.max(...outputArray)); // Get the indexconsole.log(letter_list[predictedClass]);}, function (err) {console.log(err);24 unmodified lines2526272829303134 unmodified lines6667686970717273747576777879163 unmodified lines2432442452462472482497 unmodified lines2572582592602612622632642652662672682692706 unmodified lines27727827928028128228324 unmodified lines<style>body {font-family: roboto;34 unmodified lines<div id="liveView" class="videoView"><img id="output_image" style="display:none"></img><button id="webcamButton"><span>ENABLE WEBCAM</span></button><span id="predicted_result"></span><div style="position: relative;"><video id="webcam" style="position: absolute; display:none" autoplay="" playsinline=""></video><canvas class="output_canvas" id="output_canvas" style="position: absolute; left: 0px; top: 0px; display:block"></canvas></div></div>163 unmodified lines});drawLandmarks(canvasCtx, landmarks, { color: "#FF0000", lineWidth: 2 });}var dataurl=cropCanvas(canvasElement,crop_left,crop_top,crop_right-crop_left, crop_bottom-crop_top).toDataURL("image/jpeg", 2);document.getElementById("output_image").src=dataurl//# sourceURL=pen.js7 unmodified linesconst cropCanvas = (sourceCanvas,left,top,width,height) => {let destCanvas = document.createElement('canvas');destCanvas.width = 244;var cropAspectRatio = width / height;destCanvas.height = 244 / cropAspectRatiodestCanvas.getContext("2d").drawImage(sourceCanvas,left,top,width,height, // source rect with content to crop0,0,244,destCanvas.height); // newCanvas, same size as sourcevar predictionInput=tf.browser.fromPixels(destCanvas.getContext("2d").getImageData(0, 0, 224, 224))predict(tf.expandDims(predictionInput,0))6 unmodified linesvar prediction = res.predict(inputTensor);var outputArray = prediction.dataSync(); // Get the output as an arrayvar predictedClass = outputArray.indexOf(Math.max(...outputArray)); // Get the indexdocument.getElementById("predicted_result").innerText=letter_list[predictedClass]console.log(letter_list[predictedClass]);}, function (err) {console.log(err);
templates/browser-detect.html+11-20