I checked other solutions, but they weren't answering my question.
My issue is that whenever I try to capture just one frame from a video (I want to basically take a picture with my webcam) I just get a black window.
Code -
import cv2
cam = cv2.VideoCapture(0)
frame = cam.read()[1]
cv2.imwrite('img2.png', frame)
cv2.imshow("img1", frame)
My webcam is USB, 720p at 30fps.
Thanks.
解决方案
One of two things. It might be that you need to add a waitKey() after cv2.imshow(). Alternatively, you aren't checking the return from the camera for any errors. It could be a connection problem. Here are the two things to do.
import cv2
cam = cv2.VideoCapture(0)
retval, frame = cam.read()
if retval != True:
raise ValueError("Can't read frame")
cv2.imwrite('img2.png', frame)
cv2.imshow("img1", frame)
cv2.waitKey()
The waitKey() function halts the program until a user has entered a key in the window.