[Thanks for your awesome work on this library.]
I'm using VideoCapture to detect faces in a webcam feed. If I perform detection a single time, it works fine. However, if I try to call my detectFaces() function in a loop (with setInterval), it crashes my host environment (NW.js, similar to Electron).
Do I need to do any sort of cleanup after each round of detection? Here is the code I'm using:
detectFaces() {
if (!this.vCap) this.vCap = new cv.VideoCapture(0);
// Grab frame and perform detection
const frame = this.vCap.read().bgrToGray();
const faceClassifier = new cv.CascadeClassifier(cv.HAAR_FRONTALFACE_DEFAULT);
const faceResult = faceClassifier.detectMultiScale(frame);
if (!faceResult.objects.length) return;
const sortByNumDetections = result => result.numDetections
.map((num, idx) => ({ num, idx }))
.sort(((n0, n1) => n1.num - n0.num))
.map(({ idx }) => idx);
// get best result
const faceRect = faceResult.objects[sortByNumDetections(faceResult)[0]];
// draw face detection
const blue = new cv.Vec(255, 0, 0);
const thickness = 2;
frame.drawRectangle(
new cv.Point(faceRect.x, faceRect.y),
new cv.Point(faceRect.x + faceRect.width, faceRect.y + faceRect.height),
blue,
cv.LINE_8,
thickness
);
// create new ImageData from raw mat data
const imgData = new ImageData(
new Uint8ClampedArray(frame.cvtColor(cv.COLOR_GRAY2RGBA).getData()),
frame.cols,
frame.rows
);
// set canvas dimensions
const canvas = document.getElementById('capture');
canvas.height = frame.rows;
canvas.width = frame.cols;
// set image data
canvas.getContext('2d').putImageData(imgData, 0, 0);
}
Obviously, this is not optimized code but I just want to get things rolling.
I updated NW.js to the last version and the problem is gone.
Oh okay great! Just a side note: I sometimes had trouble with reading frames from a capture via OpenCV. Sometimes it would fail to read a frame or something, leading to an empty Mat, which crashed my program. Simply checking for mat.empty did the trick for me.
Most helpful comment
Oh okay great! Just a side note: I sometimes had trouble with reading frames from a capture via OpenCV. Sometimes it would fail to read a frame or something, leading to an empty Mat, which crashed my program. Simply checking for
mat.emptydid the trick for me.