justadudewhohacks / justadudewhohacks/opencv4nodejs
Wrong output while using Caffe Models
- Dominant language
- C++
- Stars
- 5.1k
- Forks
- 826
- PR merge metrics
- No merged PRs in 30d
Description
Hi, I'm trying to convert [this](https://github.com/raunaqness/Gender-and-Age-Detection-OpenCV-Caffe) python example of predicting a users age and gender from the webcam. It works pretty well and the expected output from `net.forward()` is an array of confidence values. Gender would be for example `[0.682727, 0.837272]`
My attempt at converting that example to run in node using opencv4nodejs is :
```
const cv = require('opencv4nodejs');
const MODEL_MEAN_VALUES = new cv.Vec(78.4263377603, 87.7689143744, 114.895847746)
const age_list = ['(0, 2)', '(4, 6)', '(8, 12)', '(15, 20)', '(25, 32)', '(38, 43)', '(48, 53)', '(60, 100)']
const gender_list = ['Male', 'Female']
const age_net = cv.readNetFromCaffe(
'./models/deploy_age.prototxt',
'./models/age_net.caffemodel')
const gender_net = cv.readNetFromCaffe(
'./models/deploy_gender.prototxt',
'./models/gender_net.caffemodel')
const grabFrames = () => {
const cap = new cv.VideoCapture(0);
cap.set(cv.CAP_PROP_FPS, 1)
let done = false;
const intvl = setInterval(() => {
let frame = cap.read();
// loop back to start on end of stream reached
if (frame.empty) {
cap.reset();
frame = cap.read();
}
let face_cascade = new cv.CascadeClassifier('data/haarcascade_frontalface_alt.xml')
let gray = frame.bgrToGray();
const faceRects = face_cascade.detectMultiScale(gray, 1.1, 5).objects;
if (faceRects.length > 0) {
console.log("Found "+faceRects.length+" face(s)")
faceRects.forEach(faceRect => {
drawBlueRect(frame, faceRect)
var face_img = frame.getRegion(faceRect);
var blob = cv.blobFromImage(face_img, 1, new cv.Size(227, 227), MODEL_MEAN_VALUES, swapRB=false);
console.log("blob + " + JSON.stringify(blob));
gender_net.setInput(blob);
let gender_preds = gender_net.forward();
console.log("gender " + JSON.stringify(gender_preds));
age_net.setInput(blob);
let age_preds = age_net.forward();
console.log("age " + JSON.stringify(age_preds));
cv.imshow('face detection', frame);
});
}
const key = cv.waitKey(1);
done = key !== -1 && key !== 255;
if (done) {
clearInterval(intvl);
console.log('Key pressed, exiting.');
}
}, 0);
}
grabFrames();
const drawRect = (image, rect, color, opts = { thickness: 2 }) =>
image.drawRectangle(
rect,
color,
opts.thickness,
cv.LINE_8
);
const drawBlueRect = (image, rect, opts = { thickness: 2 }) =>
drawRect(image, rect, new cv.Vec(255, 0, 0), opts);
const drawGreenRect = (image, rect, opts = { thickness: 2 }) =>
drawRect(image, rect, new cv.Vec(0, 255, 0), opts);
const drawRedRect = (image, rect, opts = { thickness: 2 }) =>
drawRect(image, rect, new cv.Vec(0, 0, 255), opts);
```
I am new to both openCV and this node module, but I can't really see any obvious issues within the code, maybe I am missing something silly? The output of the gender model from the above code is a Mat. So I'm doing something wrong?
Contributor guide
Assessment
This issue has not been assessed yet.