Ask Your Question
0

DNN CPP - TOO SLOW VIDEO - GENDERNET

asked 2019-06-11 15:29:11 -0600

hellykun gravatar image

updated 2019-06-12 04:05:45 -0600

supra56 gravatar image

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);
edit retag flag offensive close merge delete

Comments

why is this line repeated (double work here !) ? :

blob = blobFromImage(face, 1, Size(227, 227), MODEL_MEAN_VALUES, false);
berak gravatar imageberak ( 2019-06-12 00:43:56 -0600 )edit

try to profile it properly. i bet, the (cascade based) face detection is eating most of the time.

berak gravatar imageberak ( 2019-06-12 00:45:26 -0600 )edit

Can you give me some hints on how to profile it? thank you. It is my first time with computer vision.

hellykun gravatar imagehellykun ( 2019-06-12 02:18:50 -0600 )edit

something like this:

int64 t0 = cv::getTickCount();
// ... action !
int64 t1 = cv::getTickCount();
double seconds = (t1-t0) / cv::getTickFrequency();

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 gravatar imageberak ( 2019-06-12 03:10:48 -0600 )edit

@berak, could you estimate frame rates for these models? Is 25-30 fps on a full HD video feasible?

Erik Langskjegg gravatar imageErik Langskjegg ( 2019-10-14 06:56:12 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2019-06-11 18:25:12 -0600

Witek gravatar image

Try adding

genderNet.setPreferableTarget(DNN_TARGET_OPENCL);

If you have an Intel GPU it might speed up the calculations. Otherwise it will use the CPU which is rather slow with deep neural networks.

edit flag offensive delete link more

Comments

Thank you this solved the problem.

hellykun gravatar imagehellykun ( 2019-07-13 06:32:15 -0600 )edit

Question Tools

2 followers

Stats

Asked: 2019-06-11 15:29:11 -0600

Seen: 607 times

Last updated: Jun 12 '19