Hi, I’m using python api to read images from an usb camera on win11. Here is my code:
import time
import cv2
import numpy as np
from multiprocessing import Queue, Process
def collect_frames(que):
# camera settings
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_BUFFERSIZE, 0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
cap.set(cv2.CAP_PROP_FPS, 30)
time.sleep(1)
print(cap.get(cv2.CAP_PROP_FPS))
while cap.isOpened():
res, frame = cap.read()
if res:
que.put(frame)
time.sleep(1 / 30)
if __name__ == '__main__':
que = Queue()
collect_process = Process(target=collect_frames, args=(que,))
collect_process.start()
prev_frame = None
while True:
if not que.empty():
frame_que = que.get()
if np.all(prev_frame == frame_que):
print("get the same frame!")
prev_frame = frame_que
cv2.imshow("cam", frame_que)
c = cv2.waitKey(1)
if c == 27:
break
collect_process.join()
I expect cv2.VideoCapture.read() to return a new frame every time it gets called, but it turns out that a lot of frames are the same as the previous one. WHY?
Besides, multithreading implementation of above codes meets the same problem.
FYI, I’m trying to save specified number of frames from the camera, but I found there are same images saved. This is how I found this problem.
your room is dark. the camera compensates by extending the exposure beyond a single frame. it then emits frames at the originally requested frame rate, but the picture only changes once every exposure. this appears as “duplicates”.
or the webcam can’t move the data over the USB cable at the requested frame rate, so the driver compensates by duplicating some frames. it’s possible but unlikely. the video mode would have been rejected and a lesser one chosen automatically by the driver.
you should actually see a “stuttering” video from any program that reads your webcam.
also, time.sleep has no business being called there. the read() call already waits for a fresh frame from the device.
Hi Chan, did you figure out how to get around the duplicate frames? I’m getting the same and have tried different webcams, I have excellent lighting, and also other windows programs that capture video without any duplicate frames. It all points to opencv returning the same frame, and the higher the fps the more I get. Thanks
don’t set the buffer size to be 0. I’d bet that’s the reason things go wrong.
don’t use sleep(). you can’t choke the camera. if you do, you lose.
multiprocessing is a waste of resources for this. threading does the job just fine.
multithreading/multiprocessing is not trivial. API docs, even Python’s API docs, don’t thoroughly explain how to handle concurrency or how to combine the given primitives meaningfully.
if you have a specific issue, present a MRE and a description of the environment (anything weird you’re doing, brands and models of involved devices, what’s connected how, what operating system, …)