UNPKG

@webarkit/jsfeat-next

Version:

Typescript version of jsfeat for WebARKit

184 lines (153 loc) 6.88 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(); var fast_corners = new jsfeat.fast_corners(); 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, corners, threshold; var demo_opt = function () { this.threshold = 20; } 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.U8_t | jsfeat.C1_t); corners = []; var i = 640 * 480; while (--i >= 0) { corners[i] = new jsfeat.keypoint_t(0, 0, 0, 0); } threshold = 20; fast_corners.set_threshold(threshold); options = new demo_opt(); gui = new dat.GUI(); gui.add(options, 'threshold', 5, 100).step(1); stat.add("grayscale"); stat.add("fast corners"); } 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"); if (threshold != options.threshold) { threshold = options.threshold | 0; fast_corners.set_threshold(threshold); } stat.start("fast corners"); var count = fast_corners.detect(img_u8, corners, 5); stat.stop("fast corners"); // render result back to canvas var data_u32 = new Uint32Array(imageData.data.buffer); render_corners(corners, count, data_u32, 640); ctx.putImageData(imageData, 0, 0); $('#log').html(stat.log()); } } function render_corners(corners, count, img, step) { var pix = (0xff << 24) | (0x00 << 16) | (0xff << 8) | 0x00; for (var i = 0; i < count; ++i) { var x = corners[i].x; var y = corners[i].y; var off = (x + y * step); img[off] = pix; img[off - 1] = pix; img[off + 1] = pix; img[off - step] = pix; img[off + step] = pix; } } $(window).on('unload', function () { video.pause(); video.src = null; }); }); </script> </body> </html>