简体   繁体   English

Python 两个函数线程

[英]Python two functions threading

I want to thread two functions, first function streaming a video and passing frames to second function, second function reading frames with Optical Character Recognition and converting frames to text.我想线程化两个函数,第一个函数流式传输视频并将帧传递给第二个函数,第二个函数使用光学字符识别读取帧并将帧转换为文本。 The question how to pass frames from first threaded function to second threaded function?问题如何将帧从第一个线程函数传递到第二个线程函数?

What I have done already, with first function saving video frames to local file 'frame.jpg' and at the same time reading with second function from 'frame.jpg'.我已经做了什么,第一个函数将视频帧保存到本地文件“frame.jpg”,同时从“frame.jpg”中读取第二个函数。 Is it possible to define video frames as global variable and pass to reading function?是否可以将视频帧定义为全局变量并传递给读取函数?

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()

Hope this answer can still be of some use.希望这个答案仍然有用。

I use multiprocessing.Pipe() to pass video frames from one processes to another with cv2.VideoCapture() to capture frames and write each image to the Pipe.我使用 multiprocessing.Pipe() 将视频帧从一个进程传递到另一个进程,并使用 cv2.VideoCapture() 捕获帧并将每个图像写入管道。

import multiprocessing导入多处理

multiprocessing.set_start_method('spawn') multiprocessing.set_start_method('spawn')

video_outfrompipe, video_intopipe = multiprocessing.Pipe() video_outfrompipe, video_intopipe = multiprocessing.Pipe()

vs = multiprocessing.Process(target=VideoSource, args=(video_intopipe)) vs = multiprocessing.Process(target=VideoSource, args=(video_intopipe))

vs.start()与开始()

vc = multiprocessing.Process(target=VideoConsumer, args=(video_outfrompipe)) vc = multiprocessing.Process(target=VideoConsumer, args=(video_outfrompipe))

vc.start() vc.start()

vs.join()与加入()

vc.join() vc.join()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM