DNN CPP - TOO SLOW VIDEO - GENDERNET
I have tried to create a program to recognize and playback gender on a video. The problem is that the video playback is really slow. When the gender network is not running the video playback is quick. Any ideas? Code:
string genderProto = "gender_deploy.prototxt";
string genderModel = "gender_net.caffemodel";
Scalar MODEL_MEAN_VALUES = Scalar(78.4263377603, 87.7689143744, 114.895847746);
vector<string> genderList = { "Man", "Woman" };
// Load Network
Net genderNet = readNet(genderModel, genderProto);
Net faceNet = readNet(faceModel, faceProto);
// Try opening camera
//cv::VideoCapture camera(0);
cv::VideoCapture camera("video.mp4");
if (!camera.isOpened()) {
fprintf(stderr, "Error getting camera...\n");
exit(1);
}
cv::namedWindow(WINDOW_NAME, cv::WINDOW_KEEPRATIO | cv::WINDOW_AUTOSIZE);
VideoFaceDetector detector(CASCADE_FILE, camera);
cv::Mat frame;
double fps = 0, time_per_frame;
while (true)
{
auto start = cv::getCPUTickCount();
detector >> frame;
auto end = cv::getCPUTickCount();
time_per_frame = (end - start) / cv::getTickFrequency();
fps = (15 * fps + (1 / time_per_frame)) / 16;
printf("Time per frame: %3.3f\tFPS: %3.3f\n", time_per_frame, fps);
if (detector.isFaceFound())
{
cv::rectangle(frame, detector.face(), cv::Scalar(255, 0, 0));
}
Mat face = frame; // take the ROI of box on the frame
Mat blob;
blob = blobFromImage(face, 1, Size(227, 227), MODEL_MEAN_VALUES, false);
blob = blobFromImage(face, 1, Size(227, 227), MODEL_MEAN_VALUES, false);
genderNet.setInput(blob);
// string gender_preds;
vector<float> genderPreds = genderNet.forward();
// printing gender here
// find max element index
// distance function does the argmax() work in C++
int max_index_gender = std::distance(genderPreds.begin(), max_element(genderPreds.begin(), genderPreds.end()));
string gender = genderList[max_index_gender];
string label = gender;
cv::putText(frame, label, Point(100, 200), cv::FONT_HERSHEY_SIMPLEX, 2, Scalar(0, 255, 255), 2, cv::LINE_AA);
cv::imshow(WINDOW_NAME, frame);
why is this line repeated (double work here !) ? :
try to profile it properly. i bet, the (cascade based) face detection is eating most of the time.
Can you give me some hints on how to profile it? thank you. It is my first time with computer vision.
something like this:
then, there are faster alternatives to the cascade detection, look for pico, pcn, mtcnn, last opencv has some pretrained dnn models which can be used with the dnn/objectdetection sample code.
@berak, could you estimate frame rates for these models? Is 25-30 fps on a full HD video feasible?