3

I'm using CodeBlock IDE on raspberry pi 2 with raspberry pi camera module to run face detection program (via Haar Cascade). Now I've compared its performance using Microsoft LifeCam (USB connection) and as the title suggests, the usb webcam was visibly faster.

Since the image processing was same in both cases, I'm thinking it comes down to the webcam. The only other reason might be the library I'm using to access the raspberry pi camera in my C++ code. How can I get better performance out of the raspberry pi camera module?

Raspicam Library?

Webcam (Microsoft LifeCam)

#include "opencv2/objdetect.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"

#include <iostream>
#include <stdio.h>

using namespace std;
using namespace cv;

void detectAndDisplay(Mat frame );
String face_cascade_name = "haarcascade_frontalface_alt.xml";
String eyes_cascade_name = "haarcascade_eye_tree_eyeglasses.xml";
CascadeClassifier face_cascade;
CascadeClassifier eyes_cascade;
String window_name = "Capture - Face detection";

int main( void )
{
    //--VideoCapture capture;
    raspicam::RaspiCam_Cv capture;
    Mat frame;

    //-- 1. Load the cascades
    if( !face_cascade.load( face_cascade_name ) ){ printf("--(!)Error loading face cascade\n"); return -1; };
    if( !eyes_cascade.load( eyes_cascade_name ) ){ printf("--(!)Error loading eyes cascade\n"); return -1; };

    //-- 2. Read the video stream
    capture.open();
    if ( ! capture.isOpened() ) { printf("--(!)Error opening video capture\n"); return -1; }

    while ( capture.grab() )
    {
        capture.retrieve(frame);
        if( frame.empty() )
        {
            printf(" --(!) No captured frame -- Break!");
            break;
        }

        //-- 3. Apply the classifier to the frame
        detectAndDisplay( frame );

        int c = waitKey(10);
        if( (char)c == 27 ) { break; } // escape
    }
    capture.release();
    return 0;
}

void detectAndDisplay( Mat frame )
{
    std::vector<Rect> faces;
    Mat frame_gray;

    cvtColor( frame, frame_gray, COLOR_BGR2GRAY );
    equalizeHist( frame_gray, frame_gray );

    //-- Detect faces
    face_cascade.detectMultiScale( frame_gray, faces, 1.4, 2, 0|CASCADE_SCALE_IMAGE, Size(30, 30) );

    for( size_t i = 0; i < faces.size(); i++ )
    {
        Point center( faces[i].x + faces[i].width/2, faces[i].y + faces[i].height/2 );
        ellipse( frame, center, Size( faces[i].width/2, faces[i].height/2), 0, 0, 360, Scalar( 255, 0, 255 ), 4, 8, 0 );

        Mat faceROI = frame_gray( faces[i] );
        std::vector<Rect> eyes;

        //-- In each face, detect eyes
        eyes_cascade.detectMultiScale( faceROI, eyes, 1.4, 2, 0 |CASCADE_SCALE_IMAGE, Size(30, 30) );

        for( size_t j = 0; j < eyes.size(); j++ )
        {
            Point eye_center( faces[i].x + eyes[j].x + eyes[j].width/2, faces[i].y + eyes[j].y + eyes[j].height/2 );
            int radius = cvRound( (eyes[j].width + eyes[j].height)*0.25 );
            circle( frame, eye_center, radius, Scalar( 255, 0, 0 ), 4, 8, 0 );
        }
    }
    //-- Show what you got
    imshow( window_name, frame );
}

Without the raspicam module, the standard VideoCapture object was used for accessing the usb webcam.

  • What do you mean by "slower"? And what do you mean by "image processing was same in both cases"? If the camera acquires a higher resolution the processing will take longer. – Luca Carlon Feb 5 '16 at 22:22
1

The speed in which the processing of the images from the camera is affected by the resolution used by the camera. The official Raspberry Pi camera has a full 1080p resolution of 1920x1080. While the Microsoft LifeCam has a resolution of 1280 x 720. Therefore because the resolution of the LifeCam is lower the pi processes this much quicker. To get a higher performance level you would have to adjust the resolution of the Pi camera to a lower level.

0

You could try threading. It significantly reduces the I/O latency and your pipeline can process much more frames per second.

I agree with @JWise, you have to lower your resolution of you want a good enough FPS for image processing. 1080p is a bit too much for the Pi.

You can follow the threading tutorial here. I used it in my python script and saw significant improvements... Since you're using C/C++ your performance gains will be higher

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.