Ask Your Question
1

How to get FPS in OpenCV live capture

asked 2015-10-06 10:30:09 -0600

ardidudidam gravatar image

updated 2016-02-03 20:29:28 -0600

Hi guys,

how to get frame rate per second in openCv live capture face detection. I have tried using capture property (VideoCapture.get(5)). but it doesnt work and give me 0 as the result. I also tried answer given in this question and i get approx 1000++ fps. i dont think this the correct fps because my live capture quite lag. by the way i am using java.

here is snipped of my code

    //-- 2. Read the video stream  
      Mat webcam_image=new Mat();  
      VideoCapture capture =new VideoCapture(0);   
      Thread.sleep(1000);
  if( capture.isOpened())  
         {  
          while( true )  
          {
              double t = (double)Core.getTickCount();  
               capture.read(webcam_image);  
            if( !webcam_image.empty() )  
             {   
                  frame.setSize(webcam_image.width()+40,webcam_image.height()+60);  
                  //-- 3. Apply the classifier to the captured image  
                  webcam_image=faceDetect.detect(webcam_image);  
                 //-- 4. Display the image  
                  framePanel.MatToBufferedImage(webcam_image);   
                  framePanel.repaint();   
             }  
             else  
             {   
                  System.out.println(" Image is empty. Break!");   
                  break;   
             }
            t = ((double)Core.getTickCount() - t) / Core.getTickFrequency();
            System.out.println("FPS "+ 1000.0/t);
            }  
           }  
           return;  
    }

I also tried technique from this blog, but i get 0.00 fps.

Any suggestion or what technique i should use to solve the problem?

edit retag flag offensive close merge delete

Comments

Do you get the right FPS in the first frame? Or does it detect the face?

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-10-06 11:03:30 -0600 )edit

@thdrksdfthmn i didnt get the right fps and yes it does detect the face. Do you know how to solve this?

ardidudidam gravatar imageardidudidam ( 2015-10-06 18:34:19 -0600 )edit

2 answers

Sort by ยป oldest newest most voted
2

answered 2015-10-06 20:39:02 -0600

updated 2016-02-03 20:26:42 -0600

while trying TickMeter class,i modified @Eduardo 's and @Pedro' s code from this post.

both code gives almost same results

#include <iostream>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>

class TickMeter
{
public:
    TickMeter();
    void start();
    void stop();

    int64 getTimeTicks() const;
    double getTimeMicro() const;
    double getTimeMilli() const;
    double getTimeSec()   const;
    int64 getCounter() const;

    void reset();
private:
    int64 counter;
    int64 sumTime;
    int64 startTime;
};

TickMeter::TickMeter()
{
    reset();
}
int64 TickMeter::getTimeTicks() const
{
    return sumTime;
}
double TickMeter::getTimeMicro() const
{
    return  getTimeMilli()*1e3;
}
double TickMeter::getTimeMilli() const
{
    return getTimeSec()*1e3;
}
double TickMeter::getTimeSec() const
{
    return (double)getTimeTicks()/cv::getTickFrequency();
}
int64 TickMeter::getCounter() const
{
    return counter;
}
void TickMeter::reset()
{
    startTime = 0;
    sumTime = 0;
    counter = 0;
}

void TickMeter::start()
{
    startTime = cv::getTickCount();
}
void TickMeter::stop()
{
    int64 time = cv::getTickCount();
    if ( startTime == 0 )
        return;
    ++counter;
    sumTime += ( time - startTime );
    startTime = 0;
}

int main(int argc, char**argv)
{
    cv::VideoCapture capture;

    // Read the video stream or open the web cam
    if( argc > 1)
        capture.open( argv[1] );
    else
        capture.open( 0 );

    if (!capture.isOpened())
    {
        std::cout << "Problem connecting to cam " << std::endl;
        return -1;
    }
    else
    if( argc == 1 )
    {
        std::cout << "Successfuly connected to camera " << std::endl;
        //capture.set(cv::CAP_PROP_FRAME_WIDTH,320);
        //capture.set(cv::CAP_PROP_FRAME_HEIGHT,240);
    }

    cv::Mat frame;
    int c;
    TickMeter tm;

    do
    {
        tm.stop();
        tm.start();
        capture.read(frame);

        if( frame.empty() )
        {
            break;
        }

        if(tm.getCounter() > 0)
        {
            cv::putText(frame, cv::format("Average FPS=%d",cvRound( tm.getCounter()/tm.getTimeSec())), cv::Point(30, 30), cv::FONT_HERSHEY_SIMPLEX, 0.8, cv::Scalar(0,0,255));
        }

        cv::imshow("Video Capture Test", frame);

        c = cv::waitKey(1);
    }
    while(c != 27);

    return 0;
}

another way

( thanks to Pedro Batista )

#include <iostream>
#include <ctime>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>

int main(int argc, char**argv)
{
    cv::VideoCapture capture;

    // Read the video stream or open the web cam
    if( argc > 1)
        capture.open( argv[1] );
    else
        capture.open( 0 );

    if (!capture.isOpened())
    {
        std::cout << "Problem connecting to cam " << std::endl;
        return -1;
    }
    else
    if( argc == 1 )
    {
        std::cout << "Successfuly connected to camera " << std::endl;
        //capture.set(cv::CAP_PROP_FRAME_WIDTH,320);
        //capture.set(cv::CAP_PROP_FRAME_HEIGHT,240);
    }

    int frameCounter = 0;
    int tick = 0;
    int fps;
    std::time_t timeBegin = std::time(0);

    cv::Mat frame;

    while (1)
    {
        capture.read(frame);

        if( frame.empty() )
        {
            break;
        }

        frameCounter++;

        std::time_t timeNow = std::time(0) - timeBegin;

        if (timeNow - tick >= 1)
        {
            tick++;
            fps = frameCounter;
            frameCounter = 0;
        }

        cv::putText(frame, cv::format("Average FPS=%d", fps ), cv::Point(30, 30), cv::FONT_HERSHEY_SIMPLEX, 0.8, cv::Scalar(0,0,255));
        cv::imshow("FPS test", frame);
        cv::waitKey(1);
    }

    return 0;
}
edit flag offensive delete link more

Comments

hi @sturkmen thanks for your answer, i tried your answer and i get the average FPS, but somehow the average FPS is decreasing, is it supposed to decreasing or increasing?

ardidudidam gravatar imageardidudidam ( 2015-10-06 23:30:48 -0600 )edit
1

i know in the begining FPS is changing. later it must be stabil on correct value.

sturkmen gravatar imagesturkmen ( 2015-10-07 03:10:58 -0600 )edit

it is possible of you are using an older OpenCV version that there is still some memory loss, and then yes over time, the FPS will decrease. Make sure you use latest 2.4 or 3.0 branch of OpenCV repository. Many problems related to this were fixed in the last year.

StevenPuttemans gravatar imageStevenPuttemans ( 2015-10-07 04:49:11 -0600 )edit
1

hi @StevenPuttemans , i am currently using 2.4.9, i will try use the latest version of opencv, thanks a lot Steven

ardidudidam gravatar imageardidudidam ( 2015-10-07 08:06:59 -0600 )edit

hi @StevenPuttemans, i tried to use opencv3, but the i got an exception error in videocapture, i cant find any solution yet for that error so i change to 2.4.11 but the fps still decreasing.

And @sturkmen, for pedro suggestion, i got FPS = 1.

ardidudidam gravatar imageardidudidam ( 2015-10-08 03:01:14 -0600 )edit

Hmm strange that it is still decreasing ... could be that fixes were only applied to master branch.

StevenPuttemans gravatar imageStevenPuttemans ( 2015-10-08 03:37:38 -0600 )edit

i think there was a problem on my first code. i updated the answer.

sturkmen gravatar imagesturkmen ( 2016-02-03 20:32:50 -0600 )edit
0

answered 2016-03-13 11:17:20 -0600

gseif gravatar image

Try this link. It helped me out.

http://stackoverflow.com/questions/22...

edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2015-10-06 10:30:09 -0600

Seen: 22,386 times

Last updated: Mar 13 '16