How to use a RaspberryPi camera with the cv2.videoCapture() function?

32 views Asked by At

I am building a face-id detection program on my raspberry pi, for which I need to use OpenCV along with my RaspberryPi camera. However, when I run my code, I keep on getting an error which I am guessing is because the cv2.VideoCapture() function is not able to take input from my RaspberryPi camera.

Whenever I run the following code, I get the following error: Code

import cv2

cap = cv2.VideoCapture("/dev/video12")
print(cap)

while True:
    ret, frame = cap.read()
    
    cv2.imshow("frame", frame)
    
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows() 

Error

[ WARN:[email protected]] global ./modules/videoio/src/cap_gstreamer.cpp (1127) open OpenCV | GStreamer warning: Error opening bin: no element "dev"
[ WARN:[email protected]] global ./modules/videoio/src/cap_gstreamer.cpp (862) isPipelinePlaying OpenCV | GStreamer warning: GStreamer: pipeline have not been created
< cv2.VideoCapture 0x7f80b53750>
Traceback (most recent call last):
  File "/home/pi/Desktop/test.py", line 9, in <module>
    cv2.imshow("frame", frame)
cv2.error: OpenCV(4.6.0) ./modules/highgui/src/window.cpp:967: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'imshow'

Whenever I use 0 instead of "dev/video12," I get a black window without the

live feed. I also get this warning:
[ WARN:[email protected]] global ./modules/videoio/src/cap_gstreamer.cpp (1405) open OpenCV | GStreamer warning: Cannot query video position: status=0, value=-1, duration=-1
< cv2.VideoCapture 0x7f91381bb0>
qt.qpa.xcb: QXcbConnection: XCB error: 148 (Unknown), sequence: 191, resource id: 0, major code: 140 (Unknown), minor code: 20
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile

I am guessing this has something to do with the cv2.VideoCapture() function not taking input from my camera. I am using a RaspberryPi 3B with Bookworm. Please help. Thank you.

0

There are 0 answers