1

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'. 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()
3
  • 1
    en.wikipedia.org/wiki/Producer%E2%80%93consumer_problem Commented Feb 9, 2017 at 13:06
  • Thanks for answer, the link you posted is really useful. But my main problem is when video streaming function going with reading function experiencing big lag, so I decided to use multiprocessing to run this two functions separately in same time. But I do not know how to pass video frames from one function to another for reading when they are in separately processes. Commented Feb 9, 2017 at 13:37
  • Via queue? docs.python.org/2/library/… Commented Aug 5, 2018 at 20:12

1 Answer 1

1

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.

import multiprocessing

multiprocessing.set_start_method('spawn')

video_outfrompipe, video_intopipe = multiprocessing.Pipe()

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

vs.start()

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

vc.start()

vs.join()

vc.join()

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.