[英]python multiprocessing and opencv VideoCapture read() error
我對 python 多處理有疑問。
我想與 opencv 一起使用多處理
這是我的源代碼
--- 主要.py
if use_webcam:
print("[INFO] Using Webcam")
reader = cv2.VideoCapture(0)
ret, frame = reader.read()
if ret:
img_height, img_width, C = frame.shape
else:
img_height = 360
img_width = 640
fps = 1000 // DELAY
else:
reader = cv2.VideoCapture(video_path)
fps = reader.get(cv2.CAP_PROP_FPS)
shape = [T, img_height, img_width, 3]
frame_q = Queue()
frame_reader = Process(target=read_frames, args=(reader,frame_q,use_webcam)
--- def read_frames()
def read_frames(reader, frame_q, use_webcam, shape):
allframes = int(reader.get(cv2.CAP_PROP_FRAME_COUNT))
resize_height = shape[1]
resize_width = shape[2]
for ii in range(allframes):
if reader.isOpened():
print("[INFO] reader is opended")
# while frame_q.qsize() > 500:
# time.sleep(1)
ret, frame = reader.read()
print("[INFO] ret : ", ret)
resize_img = cv2.resize(frame, dsize=(resize_width, resize_height), interpolation=cv2.INTER_LINEAR)
cur_img = resize_img[:, :, ::-1]
frame_q.put(cur_img)
我嘗試運行源代碼
但是當我運行它時reader.read()
不起作用。
它沒有顯示任何錯誤,也沒有任何結果。
並且frame_q.qsize()
為 0。
我需要幫助。
如果 multiprocessing 和 opencv 不兼容,我應該如何處理它們?
- - - - - - - - - - - - 編輯
這個問題使用了 imageio 庫。
它適用於我的代碼
但我想知道這個問題。
為什么一起使用時停在'read()'?
在我看來,不要在主進程和子進程之間共享 object 是可能的。 所以 object 必須單獨聲明。
主文件
if use_webcam:
print("[INFO] Using Webcam")
reader = cv2.VideoCapture(0)
ret, frame = reader.read()
if ret:
img_height, img_width, C = frame.shape
else:
img_height = 360
img_width = 640
fps = 1000 // DELAY
else:
reader = cv2.VideoCapture(video_path)
fps = reader.get(cv2.CAP_PROP_FPS)
shape = [T, img_height, img_width, 3]
frame_q = Queue()
frame_reader = Process(target=read_frames, args=(reader,frame_q,use_webcam)
def read_frames()
def read_frames(video_path, frame_q, use_webcam, shape):
allframes = int(reader.get(cv2.CAP_PROP_FRAME_COUNT))
resize_height = shape[1]
resize_width = shape[2]
cap = cv2.VideoCapture(video_path)
for ii in range(allframes):
if reader.isOpened():
print("[INFO] reader is opended")
# while frame_q.qsize() > 500:
# time.sleep(1)
ret, frame = cap.read()
print("[INFO] ret : ", ret)
resize_img = cv2.resize(frame, dsize=(resize_width, resize_height), interpolation=cv2.INTER_LINEAR)
cur_img = resize_img[:, :, ::-1]
frame_q.put(cur_img)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.