Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 4863

C/C++ • OpenCV VideoCapture not working with ZeroCam

$
0
0
Hi,
I'm having some trouble getting the zerocam to work with OpenCV's VideoCapture. Tools such as rpicam-still are working fine. It's worth noting that ffmpeg is also not working.
I'm compiling the C++ stuff on another machine using WSL with a Raspberry Pi OS image, so it should be the same as the Pi that I'm running the code on.
This is the code that I'm using to try get OpenCV to work:

Code:

#include <opencv2/opencv.hpp>#include <iostream>#include <string>#include <ctime>int main() {    // Open the default camera with V4L2 backend    cv::VideoCapture cap(0, cv::CAP_V4L2);    // Check if the camera opened successfully    if (!cap.isOpened()) {        std::cerr << "Error: Could not open camera." << std::endl;        return -1;    }    // Set camera properties    cap.set(cv::CAP_PROP_FRAME_WIDTH, 640);    cap.set(cv::CAP_PROP_FRAME_HEIGHT, 480);    cap.set(cv::CAP_PROP_FPS, 30);    // Print camera properties    std::cout << "Frame width: " << cap.get(cv::CAP_PROP_FRAME_WIDTH) << std::endl;    std::cout << "Frame height: " << cap.get(cv::CAP_PROP_FRAME_HEIGHT) << std::endl;    std::cout << "FPS: " << cap.get(cv::CAP_PROP_FPS) << std::endl;    // Skip a few initial frames to allow the camera to adjust    for (int i = 0; i < 10; ++i) {        cv::Mat temp;        cap >> temp;        if (temp.empty()) {            std::cerr << "Warning: Initial frame " << i << " is empty." << std::endl;        }    }    // Capture a single frame    cv::Mat frame;    cap >> frame;    // Check if the frame is empty    if (frame.empty()) {        std::cerr << "Error: Blank frame grabbed." << std::endl;        return -1;    }    // Generate a filename with timestamp    std::time_t t = std::time(nullptr);    std::tm* now = std::localtime(&t);    char filename[128];    strftime(filename, sizeof(filename), "capture_%Y%m%d_%H%M%S.jpg", now);    // Save the image    bool success = cv::imwrite(filename, frame);    if (success) {        std::cout << "Image saved as '" << filename << "'" << std::endl;    } else {        std::cerr << "Error: Could not save the image." << std::endl;    }    // Release the camera    cap.release();    return 0;}
And the output of that code:

Code:

jorda@opendashcam:~ $ sudo ./dashcamFrame width: 640Frame height: 480FPS: [ WARN:0@0.015] global cap_v4l.cpp:1938 getProperty VIDEOIO(V4L2:/dev/video0): Unable to get camera FPS-1Warning: Initial frame 0 is empty.Warning: Initial frame 1 is empty.Warning: Initial frame 2 is empty.Warning: Initial frame 3 is empty.Warning: Initial frame 4 is empty.Warning: Initial frame 5 is empty.Warning: Initial frame 6 is empty.Warning: Initial frame 7 is empty.Warning: Initial frame 8 is empty.Warning: Initial frame 9 is empty.Error: Blank frame grabbed. 
ffmpeg output:

Code:

jorda@opendashcam:~ $ ffmpeg -f video4linux2 -i /dev/video0 -vframes 1 test.jpg                                         ffmpeg version 5.1.5-0+rpt1+deb12u1 Copyright (c) 2000-2024 the FFmpeg developers                                         built with gcc 12 (Debian 12.2.0-14)                     configuration: --prefix=/usr --extra-version=0+rpt1+deb12u1 --toolchain=hardened --incdir=/usr/include/aarch64-linux-gnu --enable-gpl --disable-stripping --disable-mmal --enable-gnutls --enable-ladspa --enable-libaom --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libcodec2 --enable-libdav1d --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libglslang --enable-libgme --enable-libgsm --enable-libjack --enable-libmp3lame --enable-libmysofa --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librabbitmq --enable-librist --enable-librubberband --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libsrt --enable-libssh --enable-libsvtav1 --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx265 --enable-libxml2 --enable-libxvid --enable-libzimg --enable-libzmq --enable-libzvbi --enable-lv2 --enable-omx --enable-openal --enable-opencl --enable-opengl --enable-sand --enable-sdl2 --disable-sndio --enable-libjxl --enable-neon --enable-v4l2-request --enable-libudev --enable-epoxy --libdir=/usr/lib/aarch64-linux-gnu --arch=arm64 --enable-pocketsphinx --enable-librsvg --enable-libdc1394 --enable-libdrm --enable-vout-drm --enable-libiec61883 --enable-chromaprint --enable-frei0r --enable-libx264 --enable-libplacebo --enable-librav1e --enable-shared                        libavutil      57. 28.100 / 57. 28.100                   libavcodec     59. 37.100 / 59. 37.100                   libavformat    59. 27.100 / 59. 27.100                   libavdevice    59.  7.100 / 59.  7.100                   libavfilter     8. 44.100 /  8. 44.100                   libswscale      6.  7.100 /  6.  7.100                   libswresample   4.  7.100 /  4.  7.100                   libpostproc    56.  6.100 / 56.  6.100                 [video4linux2,v4l2 @ 0x559321d5c0] ioctl(VIDIOC_G_PARM): Inappropriate ioctl for device                 [video4linux2,v4l2 @ 0x559321d5c0] Time per frame unknown[video4linux2,v4l2 @ 0x559321d5c0] ioctl(VIDIOC_STREAMON): Invalid argument/dev/video0: Invalid argument   
Which doesn't seem good. /dev/video0 does exist, and is used by rpicam-still.

If it's of use, this is the command I'm using to compile OpenCV.

Code:

cmake -D CMAKE_BUILD_TYPE=RELEASE \      -D CMAKE_INSTALL_PREFIX=/usr/local \      -D BUILD_TESTS=OFF \      -D BUILD_PERF_TESTS=OFF \      -D BUILD_EXAMPLES=OFF \      -D BUILD_opencv_python2=OFF \      -D BUILD_opencv_python3=OFF \      -D BUILD_SHARED_LIBS=OFF \      -D OPENCV_GENERATE_PKGCONFIG=YES \      -D WITH_GTK=ON \      -D WITH_OPENGL=ON \      -D WITH_QT=ON \      -D WITH_V4L=ON \      -D WITH_TBB=OFF \      ..
OpenCV is statically linked in this case so that it does not have to be installed on the Pi to work. (potentially the problem? would not explain why ffmpeg is also not working)

Any help is greatly appreciated!

Statistics: Posted by Zalosath — Sat Jul 27, 2024 5:54 pm



Viewing all articles
Browse latest Browse all 4863

Trending Articles