简体   繁体   English

在 Python 中使用多线程进行实时视频处理

[英]real time video processing using multithreading in Python

I am working on real time video processing using multithreading in Python.我正在使用 Python 中的多线程进行实时视频处理。 Here the processes are:这里的过程是:

  1. I open the webcam and I capture frames.我打开网络摄像头并捕获帧。
  2. I create 10 threads for video processing (detection).我为视频处理(检测)创建了 10 个线程。
  3. Threads put these frames in a priority queue ( input_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.线程将帧放入output_queue进行显示。
  6. And finally one method reads frames from output_queue and shows.最后一种方法从output_queue读取帧并显示。 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.但是我运行我的项目,10 个线程从队列中非常快速地处理帧,我的输出视频在 5 秒后关闭。 Because of output_queue is empty.因为output_queue是空的。

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.我尝试将time.sleep()放在处理之前或读取帧之前,或者如果队列为空但此时输出视频开始很晚并再次关闭或视频反复打开和关闭。

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. Queue.get()方法在队列为空时引发Queue.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

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

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