[英]Python two functions threading
我想線程化兩個函數,第一個函數流式傳輸視頻並將幀傳遞給第二個函數,第二個函數使用光學字符識別讀取幀並將幀轉換為文本。 問題如何將幀從第一個線程函數傳遞到第二個線程函數?
我已經做了什么,第一個函數將視頻幀保存到本地文件“frame.jpg”,同時從“frame.jpg”中讀取第二個函數。 是否可以將視頻幀定義為全局變量並傳遞給讀取函數?
import cv2
import pytesseract
from multiprocessing import Process
def video_straming(): #Video streaming function, First Function
vc = cv2.VideoCapture(0)
cv2.namedWindow("preview")
if vc.isOpened():
rval, frame = vc.read()
else:
rval = False
while rval:
rval, frame = vc.read()
cv2.imwrite('frame.jpg',frame)
key = cv2.waitKey(20)
if key == 27: # exit on ESC
break
cv2.destroyWindow("preview")
def reading(): #Reading from frame.jpg function, Second Function
while:
frame = cv2.imread('frame.jpg')
read = Image.fromarray(frame)
read = pytesseract.image_to_string(read)
if len(read) > 80:
break
if __name__ == '__main__':
video_stream = Process(target=video_streaming)
video_stream.start()
frame_read = Process(target=reading)
frame_read.start()
video_stream.join()
frame_read.join()
希望這個答案仍然有用。
我使用 multiprocessing.Pipe() 將視頻幀從一個進程傳遞到另一個進程,並使用 cv2.VideoCapture() 捕獲幀並將每個圖像寫入管道。
導入多處理
multiprocessing.set_start_method('spawn')
video_outfrompipe, video_intopipe = multiprocessing.Pipe()
vs = multiprocessing.Process(target=VideoSource, args=(video_intopipe))
與開始()
vc = multiprocessing.Process(target=VideoConsumer, args=(video_outfrompipe))
vc.start()
與加入()
vc.join()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.