使用多处理在tkinter中显示OpenCV视频 [英] Display an OpenCV video in tkinter using multiprocessing

查看:771
本文介绍了使用多处理在tkinter中显示OpenCV视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试为多处理的OpenCV视频流开发 GUI .下面的代码确实成功完成了此操作,因为它显示了视频供稿和一个退出"按钮,但运行方式很奇怪:

I am currently trying to develop a GUI for a multiprocessed OpenCV video stream. The code below does succeed in doing that, since it displays the video feed and a 'quit' button, but runs in a strange manner:

  • 程序在pythonw.exe(我正在使用Windows)中在退出时中引发运行时错误(通过quit button或通过单击以关闭窗口'X')说程序 要求运行时以异常方式终止"
  • the program raises a Runtime error in pythonw.exe (I am using windows) on quit (either by the quit button or by closing the window by clicking on 'X') saying the program "requested the Runtime to terminate in an unusual manner"

任何关于如何解决该问题的想法将不胜感激!

Any idea as to how to solve that issue would be greatly appreciated!

我的代码:

#!/usr/bin/python

import numpy as np
from multiprocessing import Process, Queue
from Queue import Empty
import cv2
import cv2.cv as cv
from PIL import Image, ImageTk
import time
import Tkinter as tk

#tkinter GUI functions----------------------------------------------------------
def quit_(root, process):
   process.join()
   root.destroy()

def update_image(image_label, queue):
   frame = queue.get()
   im = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
   a = Image.fromarray(im)
   b = ImageTk.PhotoImage(image=a)
   image_label.configure(image=b)
   image_label._image_cache = b  # avoid garbage collection
   root.update()

def update_all(root, image_label, queue):
   update_image(image_label, queue)
   root.after(0, func=lambda: update_all(root, image_label, queue))

#multiprocessing image processing functions-------------------------------------
def image_capture(queue):
   vidFile = cv2.VideoCapture(0)
   while True:
      try:
         flag, frame=vidFile.read()
         if flag==0:
            break
         queue.put(frame)
         cv2.waitKey(20)
      except:
         continue

if __name__ == '__main__':
   queue = Queue()
   print 'queue initialized...'
   root = tk.Tk()
   print 'GUI initialized...'
   image_label = tk.Label(master=root)# label for the video frame
   image_label.pack()
   print 'GUI image label initialized...'
   p = Process(target=image_capture, args=(queue,))
   p.start()
   print 'image capture process has started...'
   # quit button
   quit_button = tk.Button(master=root, text='Quit',command=lambda: quit_(root,p))
   quit_button.pack()
   print 'quit button initialized...'
   # setup the update callback
   root.after(0, func=lambda: update_all(root, image_label, queue))
   print 'root.after was called...'
   root.mainloop()
   print 'mainloop exit'
   p.join()
   print 'image capture process exit'

  • 配置:Windows 7 Home,Python 2.7.5,OpenCV 2.4
  • 免责声明:上面的代码受此代码的启发.
    • Config: Windows 7 Home, Python 2.7.5, OpenCV 2.4
    • Disclaimer: the code above was inspired by this one.
    • 推荐答案

      我通过在quit_(root, process)函数中使用process.terminate()而不是process.join()解决了该问题.

      I solved it by using process.terminate() instead of process.join() in the quit_(root, process) function.

      这篇关于使用多处理在tkinter中显示OpenCV视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆