One of the biggest library for real-time computer vision. Written in C/C++.
OpenCV compiled to asm.js or Webassembly with the help of Emscripten.
npm i
npm start
Visit: http://localhost:8080
You should see "runtime ready
" in the browser console after a couple
of seconds
window.cv
object
navigator.mediaDevices
.getUserMedia({ audio: false, video: true })
.then(stream => {
...
})
.catch(console.error);
const videoTrack = stream.getVideoTracks()[0];
const settings = videoTrack.getSettings();
// settings.width / settings.height
inputVideo.srcObject = stream;
let cap;
function startStreaming() {
...
cap = new cv.VideoCapture(inputVideo);
...
}
This is an OpenCV.js specific functionality
function startStreaming() {
...
outputCanvas.setAttribute('width', videoWidthPx);
outputCanvas.setAttribute('height', videoHeightPx);
...
}
let src;
let dst;
function startStreaming() {
...
src = new cv.Mat(settings.height, settings.width, cv.CV_8UC4);
dst = new cv.Mat(settings.height, settings.width, cv.CV_8UC1);
...
}
cap.read(src);
Read frame and write it to src-matrix
cv.cvtColor(src, dst, cv.COLOR_RGBA2GRAY);
Transform src- to dst-matrix reducing colors to grayscale
cv.imshow(outputCanvas, dst);
Draw dst-matrix to canvas. This is an OpenCV.js specific functionality.
window.requestAnimationFrame(processVideo);
gray = new cv.Mat(); // instantiate once
...
src.copyTo(dst);
cv.cvtColor(dst, gray, cv.COLOR_RGBA2GRAY);
faces = new cv.RectVector(); // instantiate once
...
classifier.detectMultiScale(gray, faces, 1.1, 3, 0);
size()
and get(i)
methods
for (let i = 0; i < faces.size(); i++) {
let face = faces.get(i);
...
}
cv.rectangle(topLeft, bottomRight, color)
let topLeft = new cv.Point(face.x, face.y);
...
cv.rectangle(dst, topLeft, bottomRight, [255, 0, 0, 255]);
Pick the right tool for the task!