I am running Python 3.7.3 on Debian 10 Buster, and I am trying to capture one image and save it using opencv-python and a Logitech C270 Webcam.
I am trying to achieve this using the simple program below:
import cv2
cap = cv2.VideoCapture(0) # video capture source camera (Here webcam of laptop)
ret,frame = cap.read() # return a single frame in variable `frame`
cv2.imwrite('images/c1.png',frame)
cap.release()
when I run this code, I keep on getting !_img.empty() in function 'imwrite'
. My webcam has a little indicator light, and when the code runs it turns on, so I am confused as to why the program isn’t capturing the image.
Troubleshooting steps taken:
I tested my webcam using guvcview, and it works just fine. A v2l2-ctl --list-devices
command returns: /dev/video0 /dev/video1
Thanks in advance for your help.
Hello,
I am by far a OpenCV master of any sort but…
- Have you tried to manually add the file and then run the source?
-
touch c1.png
&& python3 YourFile.py
Please let me know if that works.
Seth
Update: The resulting warning from the terminal prompted me to Google it, and I found this thread:
https://forum.opencv.org/t/videoio-v4l2-dev-video0-select-timeout/8822
I added this line of code before ret,frame = cap.read()
:
camera.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'))
and now the program works. Thanks!
1 Like
Thanks for your suggestions.
1.) I ran the code below, manually providing an image:
import cv2
img = cv2.imread("sample.png", cv2.IMREAD_COLOR)
cv2.imshow("image", img)
cv2.imwrite('test.png', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
The program was able to read the image, show it in a window, and save it with a different name. This file that was saved contained the original image with no issues.
2.) When I ran this in the terminal using the original program I asked about in this post, I got the following in the terminal:
[WARN:0] VIDEOIO(V4L2:/dev/video0): select() timeout.
The image “c1.png” that was produced after running your command contained no image data when I tried to open it. I still saw the light on my Webcam turn on, but again no image data could be obtained from it.