'OpenCV: My camera opens but it won't return any frames
I am working with OpenCV to record some videos. I have this very simple test code that records a 20 seconds video:
import cv2
import os
import time
base_dir = os.path.dirname(os.path.abspath(__file__))
# Set the video camera:
cap = cv2.VideoCapture(-1)
# Here, we are letting it warm-up:
time.sleep(2.0)
if not cap.isOpened():
print("Cannot open webcam") # This returns True
# Setting the codec to jpg image sequences:
cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'))
frameSize = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
fps = 12.5
fourcc = cv2.VideoWriter_fourcc(*'MJPG')
out = cv2.VideoWriter(os.path.join(base_dir, 'videos', f'test_video.avi'), fourcc, fps, frameSize)
print("Recording...")
t_end = time.time() + 20
while time.time() < t_end:
ret, frame = cap.read()
print(ret) # This returns False
print(frame) #... and None
if ret:
out.write(frame)
else:
break
cap.release()
out.release()
cv2.destroyAllWindows()
print("Done!)
The problem is that my camera is opening, but it doesn't read the frame correctly as ret, frame returns False and None respectively.
Any ideas on what may be causing this, and/or how to solve it?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|