简体   繁体   中英

real time video processing using multithreading in Python

I am working on real time video processing using multithreading in Python. Here the processes are:

  1. I open the webcam and I capture frames.
  2. I create 10 threads for video processing (detection).
  3. Threads put these frames in a priority queue ( input_queue ). (I keep frames in sequential order)

  4. Threads begin to take frames from queue and process.

  5. Threads put frames to output_queue for showing.
  6. And finally one method reads frames from output_queue and shows. Here, output needs to be displayed instantaneously as processed video when capturing images from the camera. (maybe five seconds behind.)

Actually I do these processes. But I run my project, 10 threads process frames very quickly from queue and my output video closes after 5 secs. Because of output_queue is empty.

I try to put time.sleep() before processing or before reading frames or if queue is empty but in this time the output video begins very late and again closes or video is repeatedly opening and closing.

How should I go about this? Thank you for your help.

The Queue.get() method raises the Queue.Empty exception when the queue is empty. You probably need to catch and handle that, or prevent it from being raised.

try:
    image = output_queue.get()
    # display image
except Queue.Empty:
    pass

To prevent it from happening:

if not output_queue.empty():
    image = output_queue.get()
    # display image

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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