gpt4 book ai didi

python - 使用带有多线程的OpenCV python运行两个视频

转载 作者:行者123 更新时间:2023-12-02 16:36:09 27 4
gpt4 key购买 nike

我试图同时使用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()

关于python - 使用带有多线程的OpenCV python运行两个视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59365293/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com