Ask Your Question

Revision history [back]

Can't get > 10 FPS (Java)

Hey all, I've been working on building a Java application using OpenCV that grabs data from the webcam and records it. However, I've been unable to achieve frame rates greater than 10 FPS (Intel i5-5200u, 8 GB RAM), which doesn't seem right to me. My webcam supports up to 30 FPS and 1280 x 720 resolution. Recording at 640 x 480 only increases to about 12 FPS. Recording at 720p (which I need to do for my application) nets the aforementioned 10. My first idea was that the VideoCapture.read() function takes a relatively long time to process, so I tried moving those calls to a thread pool, but it gave no gain. I've read all about how fast this library is supposed to be, so I must be doing something wrong. Below is the loop for capturing data:

VideoCapture cam = new VideoCapture();
    cam.open(0);

    // set proper resolution
    cam.set(Videoio.CV_CAP_PROP_FRAME_WIDTH, CAMERA_WIDTH);
    cam.set(Videoio.CV_CAP_PROP_FRAME_HEIGHT, CAMERA_HEIGHT);

    // Matrix for storing camera images, provided by OpenCV
    ArrayList<Mat> framesList = new ArrayList<>();

    long startTime = System.currentTimeMillis();

    if( ! cam.isOpened()) {
        System.err.println("Camera could not be opened.");
    }

    else {
        System.out.println("Capturing...");

        while(System.currentTimeMillis() - startTime < 1000 * SECONDS_TO_RUN) {

            Mat frame = new Mat();
            cam.read(frame);
            framesList.add(frame);

        }

        cam.release();

        System.out.println("Done recording. " + framesList.size() + " frames captured.");
        System.out.println("Framerate : " + framesList.size() / SECONDS_TO_RUN);