Raspberry Camera OpenCV Grabbing Low FPS

  c++, opencv, raspberry-pi, raspberry-pi4

I written test code in Raspberry Pi 4 with OpenCV and C++ and I’m tried to get 1640×1232 40 FPS but just get 5-6 FPS. And CPU usage up to %70-%80.
I’m tried to get 1640×1232 40 FPS with Raspivid. And I did. Also the CPU usage up to %1.
So what could be the reason for this?

C++ Code :

#include <iostream>
#include <sstream>
#include <opencv2/imgproc.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/video.hpp>

int main()
{
    cv::VideoCapture capture(0);

    capture.set(cv::CAP_PROP_FRAME_WIDTH, 1640);
    capture.set(cv::CAP_PROP_FRAME_HEIGHT, 1232);
    capture.set(cv::CAP_PROP_FPS, 40);

    cv::Mat frame;

    if(!capture.isOpened())
    {
        std::cout << "Capture is not open" << std::endl;
    }

    auto start = std::chrono::system_clock::now();
    int hitCount = 0;
    while(capture.grab())
    {
        capture >> frame;

        auto end = std::chrono::system_clock::now();
        std::chrono::duration<double> elapsed_seconds = end - start;
        hitCount++;

        if(elapsed_seconds.count() >= 1.0)
        {
            std::cout << "Current FPS : " << (double)hitCount / (double) elapsed_seconds.count() << std::endl;
            start = std::chrono::system_clock::now();
            hitCount = 0;
        }
    }

    cv::destroyAllWindows();
    return 0;
}

Raspivid command :

raspivid -t 10000 -w 1640 -h 1232 -fps 40 -o video.h264

Source: Windows Questions C++

LEAVE A COMMENT