如何解决Python:opencv视频和多线程功能
在我的应用程序中,我有一个功能可以传输opencv视频。在视频流传输期间,如果发生事件,则应该启动一个具有time.sleep()的线程。 我尝试使用下面提到的代码。问题在于它仅显示一帧,然后视频流终止。我究竟做错了什么?我也没有自由使用“ While True”循环进行视频流传输。
import threading
import time
import numpy as np
import cv2
# --- functions ---
def print_hello():
print("Hello")
time.sleep(3)
print("World")
# --- loop ---
cap = cv2.VideoCapture(0)
def show_frame():
global t
t = None
# Capture frame-by-frame
ret,frame = cap.read()
# Our operations on the frame come here
gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
# Display the resulting frame
cv2.imshow('frame',gray)
key = cv2.waitKey(1) & 0xFF
if key == ord('t'):
if t is None or not t.is_alive():
t = threading.Thread(target=print_hello)
t.start()
else:
print('previous thread is still running')
# --- after loop ---
if __name__ == '__main__':
show_frame()
if t is not None:
t.join()
cap.release()
cv2.destroyAllWindows()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。