我希望opencv中的VideoCapture函数在后台处理帧时工作平稳。由于处理需要3-4秒,所以当时拍摄视频挂起。因此,我需要运行两个线程,即一个捕获视频和其他处理它的输出和在屏幕上打印。我的代码到现在为止:
age = None
while True:
ret, frame = cam.read()
if ret == True:
# do some processing (3-4 seconds)
age = process(frame)
fr = update_age(frame, age)
cv2.imshow('hello', fr)
if cv2.waitKey(1) == 27:
break
else:
print "error"
break
cv2.destroyAllWindows()因此,我需要一种方法,使我的第3行不需要等待3-4秒来更新它的帧,而处理帧和打印其输出在后台运行。
更多的澄清(也在评论中提到):
发布于 2018-08-16 14:35:55
下面是multiprocessing的一个示例,您将在其中捕获框架并在以后处理它们:
from multiprocessing import Process
import time
frame_data = 0
def capture_video():
print('recording...')
return frame_data
def process_frame(frame_data):
time.sleep(3)
print(frame_data)
return frame_data
if __name__ == '__main__':
while True:
data = capture_video()
p = Process(target=process_frame, args=(frame_data,))
p.start()
time.sleep(0.1)
frame_data = frame_data + 1示例输出(删除了一些初始记录..。简约的印刷本):
recording...
recording...
recording...
recording...
recording...
recording...
0
recording...
1
recording...
2
recording...
3
recording...
4
recording...https://stackoverflow.com/questions/51878371
复制相似问题