I’m trying to capture an image from my Raspberry Pi camera module using OpenCV. The code runs without errors, but the frame I get is completely black.
When I use
`rpicam-still -o Desktop/image.jpg`,
I am able to get a still picture from the camera. However, when I run this code that I found on the internet (I’m not sure where I got it, but it’s a generic code that just tries to use OpenCV), I only get a completely black frame.
The code:
` import numpy as np
import cv2 as cv
cap = cv.VideoCapture(0)
if not cap.isOpened():
print("Cannot open camera")
exit()
while True:
# Capture frame-by-frame
ret, frame = cap.read()
# if frame is read correctly ret is True
if not ret:
print("Can't receive frame (stream end?). Exiting ...")
break
# Display the resulting frame
cv.imshow('frame', frame)
cv.waitKey()
if cv.waitKey(1) == ord('q'):
break
cap.release()
cv.destroyAllWindows() `
Also, I’m running this in Thonny IDE, and I’m using a Raspberry Pi 3 (with Raspberry Pi OS 64-bit) with a RaspiCam module. I already checked the OpenCV installation, and everything seems to be fine, but I’ll include the versions of Python and OpenCV that I’m using:
` pip show opencv-python
Name: opencv-python
Version: 4.10.0.84
Summary: Wrapper package for OpenCV python bindings.
Home-page: https://github.com/opencv/opencv-python
Author:
Author-email:
License: Apache 2.0
python3 --version
Python 3.11.2 `
(This is my first time posting here, and English is not my native language, so let me know if you have any questions about my writing or the data I provided.)
Lucas Flores Hinrichsen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2