我必须与多处理器并行地读取视频中的帧。在Python中使用opencv的队列,我的代码出错了。 这是我的代码,我不知道我的问题在哪里。
#! /usr/bin/python
import numpy as np
import cv2
import multiprocessing as mp
import time
def read_frames(q1,q2):
while True :
frame = q1.get()
if frame=='Done':
break
R=frame[:,:,0]
G=frame[:,:,1]
B=frame[:,:,2]
y = (np.uint8)((0.299 * R)+ (0.587 *G) +( 0.114 *B))
q2.put(y)
if __name__ == '__main__':
q1 = mp.Queue()
q2 = mp.Queue()
processes =[mp.Process(target=read_frames, args= (q1,q2)) for i in rang$
for p in processes:
p.start()
# feed the processes
# read input file and send to the processes the frames:
cap = cv2.VideoCapture('gou.avi')
lines = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
cols = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
fps = int(cap.get(cv2.CAP_PROP_FPS))
fourcc_ver = int(cap.get(cv2.CAP_PROP_FOURCC))
out = cv2.VideoWriter('output.avi',fourcc_ver, fps, (cols,lines),False)
# y = np.empty(shape=(lines,cols),dtype=np.uint8)
while(cap.isOpened()):
ret, frame = cap.read()
# as long as new frames are there
if ret==True:
q1.put(frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
q1.put('Done')
for p in processes:
p.join()
for p in processes:
result=[q2.get()]
# result.sort()
# results = [r[1] for r in results]
for i in result:
out.write(i)
# Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()
答案 0 :(得分:-1)
你可以做的是将所有帧添加到列表[]然后使用列表引用,例如frame = list [],然后按列表上的位置寻址帧:frame [0]或frame [1:4]