UNPKG

@webarkit/jsfeat-next

Version:

Typescript version of jsfeat for WebARKit

167 lines (140 loc) 6.6 kB
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <meta name="description" content="A JavaScript Computer Vision Library"> <title>JSFeatNext - JavaScript Computer Vision Library.</title> <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Droid+Sans:regular,bold|Inconsolata|PT+Sans:400,700"> <link rel="stylesheet" href="css/bootstrap.css"> <link rel="stylesheet" href="css/jsfeat.css"> </head> <body> <video id="webcam" width="640" height="480" style="display:none;"></video> <div style=" width:640px;height:480px;margin: 10px auto;"> <canvas id="canvas" width="640" height="480"></canvas> <div id="no_rtc" class="alert alert-error" style="display:none;"></div> <div id="log" class="alert alert-info"></div> </div> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script> <script src="../dist/jsfeatNext.js"></script> <script type="text/javascript" src="js/compatibility.js"></script> <script type="text/javascript" src="js/profiler.js"></script> <script type="text/javascript" src="js/dat.gui.min.js"></script> <script type="text/javascript"> $(window).on('load', function () { "use strict"; // lets do some fun var video = document.getElementById('webcam'); var canvas = document.getElementById('canvas'); var jsfeat = jsfeatNext.jsfeatNext; var imgproc = new jsfeat.imgproc(); try { var attempts = 0; var readyListener = function(event) { findVideoSize(); }; var findVideoSize = function() { if(video.videoWidth > 0 && video.videoHeight > 0) { video.removeEventListener('loadeddata', readyListener); onDimensionsReady(video.videoWidth, video.videoHeight); } else { if(attempts < 10) { attempts++; setTimeout(findVideoSize, 200); } else { onDimensionsReady(640, 480); } } }; var onDimensionsReady = function(width, height) { demo_app(width, height); compatibility.requestAnimationFrame(tick); }; video.addEventListener('loadeddata', readyListener); compatibility.getUserMedia({video: true}, function(stream) { if(video.srcObject !== undefined){ video.srcObject = stream } else { try { video.src = compatibility.URL.createObjectURL(stream); } catch (error) { video.src = stream; } } setTimeout(function() { video.play(); }, 500); }, function (error) { $('#canvas').hide(); $('#log').hide(); $('#no_rtc').html('<h4>WebRTC not available.</h4>'); $('#no_rtc').show(); }); } catch (error) { $('#canvas').hide(); $('#log').hide(); $('#no_rtc').html('<h4>Something goes wrong...</h4>'); $('#no_rtc').show(); } var stat = new profiler(); var gui,options,ctx,canvasWidth,canvasHeight; var img_u8; var demo_opt = function(){ this.blur_radius = 2; this.low_threshold = 20; this.high_threshold = 50; } function demo_app(videoWidth, videoHeight) { canvasWidth = canvas.width; canvasHeight = canvas.height; ctx = canvas.getContext('2d', {"willReadFrequently": true}); ctx.fillStyle = "rgb(0,255,0)"; ctx.strokeStyle = "rgb(0,255,0)"; img_u8 = new jsfeat.matrix_t(640, 480, jsfeat.U8C1_t); options = new demo_opt(); gui = new dat.GUI(); gui.add(options, 'blur_radius', 0, 4).step(1); gui.add(options, 'low_threshold', 1, 127).step(1); gui.add(options, 'high_threshold', 1, 127).step(1); stat.add("grayscale"); stat.add("gauss blur"); stat.add("canny edge"); } function tick() { compatibility.requestAnimationFrame(tick); stat.new_frame(); if (video.readyState === video.HAVE_ENOUGH_DATA) { ctx.drawImage(video, 0, 0, 640, 480); var imageData = ctx.getImageData(0, 0, 640, 480); stat.start("grayscale"); imgproc.grayscale(imageData.data, 640, 480, img_u8); stat.stop("grayscale"); var r = options.blur_radius|0; var kernel_size = (r+1) << 1; stat.start("gauss blur"); imgproc.gaussian_blur(img_u8, img_u8, kernel_size, 0); stat.stop("gauss blur"); stat.start("canny edge"); imgproc.canny(img_u8, img_u8, options.low_threshold|0, options.high_threshold|0); stat.stop("canny edge"); // render result back to canvas var data_u32 = new Uint32Array(imageData.data.buffer); var alpha = (0xff << 24); var i = img_u8.cols*img_u8.rows, pix = 0; while(--i >= 0) { pix = img_u8.data[i]; data_u32[i] = alpha | (pix << 16) | (pix << 8) | pix; } ctx.putImageData(imageData, 0, 0); $('#log').html(stat.log()); } } $(window).on('unload', function () { video.pause(); video.src = null; }); }); </script> </body> </html>