real time video processing using multithreading in Python

时间:2018-12-27 12:58:10

标签: python multithreading real-time video-processing

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.

1 个答案:

答案 0 :(得分:0)

当队列为空时,Queue.get()方法引发Queue.Empty异常。您可能需要抓住并处理该问题,或者阻止它被提出。

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

为防止其发生:

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