我试图同时使用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()
答案 0 :(得分:1)
使用多处理对我有用!
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()