gpt4 book ai didi

python - Python OpenCV : Mutithreading with opecv video streaming

转载 作者:行者123 更新时间:2023-12-02 16:58:49 25 4
gpt4 key购买 nike

我想与opencv视频流一起运行一个多线程。如果在视频不断流传输的过程中检测到对象3秒钟,我想激活GPIO。我尝试使用多线程(join方法),但视频在线程调用期间暂停,因为它具有time.sleep()。有什么方法可以持续并行地运行线程流视频?下面是具有相同方式的代码。如果我删除连接,那么time.sleep根本不起作用。

import threading 
import time
import numpy as np
import cv2

def print_hello():
print("Hello")
time.sleep(3)
print ("World")

t1 = threading.Thread(target=print_hello)

cap = cv2.VideoCapture(0)

while(True):
# 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)

t1.start()
t1.join()

if cv2.waitKey(1) & 0xFF == ord('q'):
break

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

最佳答案

.join()等待线程结束并阻塞代码-因此它不发送任何内容以在循环内运行它,但是您应该在循环后或程序结束时运行它
另一个问题是循环中的.start(),因为.start()只能运行一次线程,因此在循环中多次使用它会产生错误。
您可以在循环之前启动线程,并在线程内部运行一些循环以使其始终运行。

import threading 
import time
import numpy as np
import cv2

# --- functions ---

running = True

def print_hello():
while running:
print("Hello World")
time.sleep(3)

# --- main ---

t1 = threading.Thread(target=print_hello)
t1.start()

# --- loop ---

cap = cv2.VideoCapture(0)

while True:
# 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)

if cv2.waitKey(1) & 0xFF == ord('q'):
break

# --- after loop ---

running = False # to stop loop in thread
t1.join()

cap.release()
cv2.destroyAllWindows()
如果必须在循环中启动线程,则还必须在循环中创建新线程。
在此示例中,我使用键 t来启动新线程-否则,它将在每个循环中创建新线程,因此它将在短时间内创建数百个线程,因此没有任何意义。
import threading 
import time
import numpy as np
import cv2

# --- functions ---

def print_hello():
print("Hello")
time.sleep(3)
print("World")

# --- main ---

all_threads = []

# --- loop ---

cap = cv2.VideoCapture(0)

while True:
# 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('q'):
break
if key == ord('t'):
t = threading.Thread(target=print_hello)
t.start()
all_threads.append(t)

# --- after loop ---

for t in all_threads:
t.join()

cap.release()
cv2.destroyAllWindows()
但是,即使多次按 t,您也可以同时创建多个线程,它们将一起工作。如果您不需要它,则必须控制威胁是否仍在起作用,并仅在不再起作用时才创建新威胁-使用 is_alive()-这样可以使其变得更加复杂。
import threading 
import time
import numpy as np
import cv2

# --- functions ---

def print_hello():
print("Hello")
time.sleep(3)
print("World")

# --- main ---

t = None

# --- loop ---

cap = cv2.VideoCapture(0)

while True:
# 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('q'):
break

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 t is not None:
t.join()

cap.release()
cv2.destroyAllWindows()

关于python - Python OpenCV : Mutithreading with opecv video streaming,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63345956/

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