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

General discussion • HQ Camera with OpenCV

$
0
0
I am using a Raspberry Pi 4 running Raspian and I'm trying to use the official HQ camera with OpenCV to run real-time image recognition and tracking. The problem is that when I try to use the OpenCV Python libraries using the following code (which should just take a picture)

Code:

import cv2video_capture = cv2.VideoCapture(0)assert video_capture.isOpened()ret, frame = video_capture.read()cv2.imshow('frame', frame)video_capture.release()cv2.destroyAllWindows()
I get an error saying the following

Code:

(opencvenv) main@raspberrypi:~/Documents/pythonFiles $ python captureFrame.py[ WARN:0@10.257] global cap_v4l.cpp:1134 tryIoctl VIDEOIO(V4L2:/dev/video0): select() timeout.Traceback (most recent call last):  File "/home/main/Documents/pythonFiles/captureFrame.py", line 12, in <module>    cv2.imshow('frame', frame)cv2.error: OpenCV(4.9.0) /io/opencv/modules/highgui/src/window.cpp:971: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'imshow'
From the error text, the fact that other cameras work with this code, the fact that this camera works with rpicam-hello, and the results of searching the error online, this sounds like a bandwidth problem, but one that I can not figure out how to fix. I have tried updating the python code to include the following lines

Code:

video_capture.set(cv2.CAP_PROP_FOURCC,cv2.VideoWriter_fourcc('M','J','P','G'))video_capture.set(cv2.CAP_PROP_FRAME_WIDTH, 1014)video_capture.set(cv2.CAP_PROP_FRAME_HEIGHT, 540)
Which did nothing. I even tried using opencv with C++ which I expect to be faster (and therefore would be my preferred language if possible) using the following code

Code:

#include <stdio.h>#include <stdlib.h>#include <assert.h>#include <opencv4/opencv2/opencv.hpp>using namespace cv;using namespace std;int main(int argv, char** argc) {  Mat frame; // current frame object  VideoCapture vid(0); // video object  assert(vid.isOpened());  vid.read(frame);  // vid >> frame; // this doest he same thing  imshow("Webcam", frame);  waitKey(0);    return 0;}
Unfortunately, this just sits in the

Code:

VideoCapture vid(0);
line without doing anything forever. I've tried:
  • I loaded a different program to open an image which worked fine, so I know the opencv libraries are installed correctly.
  • I tried the HQ camera with the built in Raspberry Pi test function like rpicam-hello which works, so I know the camera and physical connection are intact
  • I tried a different camera which worked, so I know the code isn't faulty
  • I tried capping the frame size as shown above
  • I tried adding the lines

    Code:

    nodrop=1 timeout=5000
    to the uvcvideo module, but I don't even know if this is the right module
Does anyone have advice on how to get OpenCV running on this Pi with the HQ camera, preferably at high resolution and speed, in C or C++ (though I'll take anything that works)? I've tried everything I can think of.

Statistics: Posted by klaytonme — Thu Apr 18, 2024 10:26 pm — Replies 0 — Views 26



Viewing all articles
Browse latest Browse all 4940

Trending Articles