Ask Your Question
0

FPS for USB WEB cam

asked 2015-10-23 11:25:12 -0600

Gosha_DE gravatar image

updated 2015-10-23 11:25:39 -0600

Please help me. I'am beginer? :(

For this code (win 7) i have only about 6.8 FPS. i need 25. How to сapture at higher FPS. thank you.

cap.get(CV_CAP_PROP_FPS)=0

int main() 
    {
        VideoCapture cap(0); // open the video file for reading

        if ( !cap.isOpened() )  // if not success, exit program
        {
             cout << "Cannot open the video file" << endl;
             return -1;
        }

        double fps = cap.get(CV_CAP_PROP_FPS); //get the frames per seconds of the video
        if (fps==0) 
        {
            fps=25;   
        }

        cout << "Frame per seconds : " << fps << endl;
        CvSize size = cvSize( cap.get( CV_CAP_PROP_FRAME_WIDTH), cap.get(  CV_CAP_PROP_FRAME_HEIGHT));
        VideoWriter writer = VideoWriter("cap.avi",CV_FOURCC('M','J','P','G'), fps, size, 1);

        int cc=0;

        while(1)
        {
            Mat frame;

            bool bSuccess = cap.read(frame); // read a new frame from video

            if (!bSuccess) //if not success, break loop
            {
                            cout << "Cannot read the frame from video file" << endl;
                           break;
            }
            writer.write(frame);

            cc++;

            if(waitKey(1) == 27) 

           {
                    cout << "esc key is pressed by user" << endl; 
                    cout << cc << endl; 
                    waitKey(10000) ;
                     break; 
           }
        }
        writer.release();
        return 0;
    }
edit retag flag offensive close merge delete

Comments

btw, waitKey() will have no effect without a window shown or imshow() called

berak gravatar imageberak ( 2015-10-24 05:09:36 -0600 )edit
1

not only... he can't exit from while loop if waitKey() has no effect

pklab gravatar imagepklab ( 2015-10-24 05:19:31 -0600 )edit

i have namedWindow("MyVideo",CV_WINDOW_AUTOSIZE);, but not imshow("MyVideo", frame);. i see gray window. and waitKey work.

Gosha_DE gravatar imageGosha_DE ( 2015-10-26 03:24:00 -0600 )edit

2 answers

Sort by » oldest newest most voted
0

answered 2015-10-26 03:24:29 -0600

Gosha_DE gravatar image

updated 2015-10-26 04:22:03 -0600

well. thank you.

1) i capture in minute and see "cout << cc << endl;" - 408. 408/60=6.8fps and cap.avi played with acceleration.

2) without writer.write(frame);, with imshow("MyVideo", frame); i have 480 frame per minute = 7.7fps without writer.write(frame); and imshow("MyVideo", frame); i have 8fps

Sistem idle in task manager - 98%

3)with VideoCapture cap(0 + CV_CAP_DSHOW); and cap.set(CV_CAP_PROP_FPS,25); without writer.write(frame); and imshow("MyVideo", frame); i have 8.1fps

I do not understand... :(

   VideoCapture cap(0 + CV_CAP_DSHOW); // open the video file for reading
    if ( !cap.isOpened() )  // if not success, exit program
    {
         cout << "Cannot open the video file" << endl;
         return -1;
    }

    cap.set(CV_CAP_PROP_FPS,25);
    double fps = cap.get(CV_CAP_PROP_FPS); //get the frames per seconds of the video
    cout << "Frame per seconds : " << fps << endl;
    namedWindow("MyVideo",CV_WINDOW_AUTOSIZE); //create a window called "MyVideo"
    int cc=0;
    while(1)
    {
        Mat frame;
        bool bSuccess = cap.read(frame); // read a new frame from video
         if (!bSuccess) //if not success, break loop
        {
                        cout << "Cannot read the frame from video file" << endl;
                       break;
        }
       cc++;
        if(waitKey(1) == 32) 
       {
                cout << "esc key is pressed by user" << endl; 
                cout << cc << endl; 
                waitKey(10000) ;
                 break; 
       }
    }
edit flag offensive delete link more

Comments

I tested (with VideoCapture cap(0 + CV_CAP_DSHOW); and cap.set(CV_CAP_PROP_FPS,25); without writer.write(frame); and imshow("MyVideo", frame);) on another computer/cam ...

notebook with int web - 7fps

ws with ather usb cam -18fps

win8 tablet -19fps

win8 tablet (with VideoCapture cap(0 + CV_CAP_DSHOW); cap.set(CV_CAP_PROP_FPS,25); writer.write(frame); and imshow("MyVideo", frame );-15fps (waitKey(1))

Gosha_DE gravatar imageGosha_DE ( 2015-10-26 04:14:05 -0600 )edit

@Gosha_DE see my answer in EDIT2.

pklab gravatar imagepklab ( 2015-10-26 06:33:07 -0600 )edit
0

answered 2015-10-24 04:27:48 -0600

pklab gravatar image

updated 2015-10-26 06:31:08 -0600

  • I don't understand why you do if (fps==0)... and how do you measure your fps as 6.8 ?
  • Despite of you can cap.set(CV_CAP_PROP_FPS,25), this is ignored by VideoCapture because you will get a new frame each time you will call cap.read ... you ask for a frame and the cam will give you back a frame immediately

On the output side, FPS for output video will be exactly as requested at VideoWriter opening/construction time.

Than:

  • With VideoCapture the grab loop is controlled by user and not by cam, so you grab frames at FPS given by your grab loop timing
  • With VideoWriter you write video at requested FPS, the codec will do the hard work like frame mapping, compression, decimation etc...

Anyway, because your grab loop is just grab frame, save frame (timing should be at minimum), than you should reach highest FPS but for sure higher than 6.8.

Try showing the grabbed frame with imshow("frame", frame); to have a visual feedback of grabbing fps, in case try without video creation or different codec.

At the end, VideoCapture depends from drivers, OS and additional libs and above statement might be unconfirmed on different on environment

EDIT: Above infos aren't completely true because if you open the camera using DirectShow instead of default driver (may be Video For Windows) you can set CV_CAP_PROP_FPS effectively (see here for more info)

// VideoCapture cap(0);              // open the default camera using default driver
// VideoCapture cap(0 + CV_CAP_VFW); // open the default camera using VFW
VideoCapture cap(0 + CV_CAP_DSHOW);  // open the default camera using DirectShow

cap.set(CV_CAP_PROP_FPS,25);         // this seems effective with DSHOW

EDIT2 VideoCapture is a bit oscure and depends from drivers :( anyway, try declaring the frame once out of for loop than grab one to open the channel and reserve the memory.

On my laptop I can control fps up to 30 using DSHOW. With VFW, I can get only 15 fps regardless of CV_CAP_PROP_FPS. Try this to estimate fps:

#include <time.h>
int EstimateFps()
{
    //VideoCapture cap(0 + CV_CAP_VFW); // open the default camera using VFW driver
    VideoCapture cap(0 + CV_CAP_DSHOW); // open the default camera using DirectShow driver

    if (!cap.isOpened()) // check if we succeeded
        return -1;

    bool bSuccess = false;
    double fpsWanted = 30;
    bSuccess = cap.set(CV_CAP_PROP_FRAME_WIDTH, 640);
    bSuccess = cap.set(CV_CAP_PROP_FRAME_HEIGHT, 480);
    bSuccess = cap.set(CV_CAP_PROP_FPS, fpsWanted);
    if (!bSuccess)
        cout << endl << fpsWanted << "is not supported";

    Mat frame; //create frame once
    bSuccess = cap.read(frame); // grab a frame to reserve memory
    imshow("frame", frame);

    clock_t start, stop;
    int cc;
    start = clock();
    for (cc = 0; cc<100; cc++) {
        cap.read(frame); // grab a new frame
    }
    stop = clock();
    double timeLoop = (stop - start) / (double)CLOCKS_PER_SEC;
    double fpsLoop = cc / timeLoop;
    cout << endl << "Average FPS:" << fpsLoop << endl;
    cout << endl << "Press Enter to terminate "; cin.get();
    return 0;
}
edit flag offensive delete link more

Comments

@Gosha_DE do you have some success with VideoCapture cap(0 + CV_CAP_DSHOW); and cap.set(CV_CAP_PROP_FPS,25); ? please let us know

pklab gravatar imagepklab ( 2015-10-24 12:08:16 -0600 )edit

to EDIT2 Average FPS:6.74127 :( driveer? but webcam7 capture file with 25 fps.

Gosha_DE gravatar imageGosha_DE ( 2015-10-26 11:43:08 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-10-23 11:25:12 -0600

Seen: 4,089 times

Last updated: Oct 26 '15