![](/img/trans.png)
[英]How to run multiple videos simultaneously in python using OpenCV, multi threading, queues?
[英]Running two videos with OpenCV python with multi threading
我正在尝试同时使用 openCV 两个函数在 python 上运行。 一个功能应该显示本地视频,另一个应该显示来自我的网络摄像头的帧。 运行下面的代码时,两个窗口会冻结并变黑。 我在 Ubuntu 16.04 上运行它
import cv2
import numpy as np
from threading import Thread
def webcam_video():
cap = cv2.VideoCapture(0)
while(True):
ret, frame = cap.read()
if ret == True:
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else :
break
cap.release()
cv2.destroyAllWindows()
def local_video():
path = "video-1.mp4"
cap = cv2.VideoCapture(path)
while(True):
ret, frame = cap.read()
if ret == True:
cv2.imshow('frame_2',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else :
break
cap.release()
cv2.destroyAllWindows()
t1= Thread(target = webcam_video)
t2= Thread(target = local_video)
t1.start()
t2.start()
使用多处理对我有用!
import numpy as np
import cv2
from multiprocessing import Process
def webcam_video():
cap = cv2.VideoCapture(0)
while(True):
ret, frame = cap.read()
if ret == True:
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else :
break
cap.release()
cv2.destroyAllWindows()
def local_video():
path = r"C:\Users\bernad.peter\Downloads\Singapore Port.mp4"
cap = cv2.VideoCapture(path)
while(True):
ret, frame = cap.read()
if ret == True:
cv2.imshow('frame_2',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else :
break
cap.release()
cv2.destroyAllWindows()
if __name__ == '__main__':
p1= Process(target = local_video)
p2= Process(target = webcam_video)
p1.start()
p2.start()
p1.join()
p2.join()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.