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.