2013-06-12 30 views
5

現在、GUIマルチプロセスOpenCVビデオストリームを開発しようとしています。 (私は窓を使用していますマルチプロセスを使用してtkinterにOpenCVビデオを表示する

  • プログラムはpythonw.exeランタイムエラーが発生します。以下のコードは、それがビデオフィードを表示するので、ということに成功し、ボタンを「終了」が、奇妙な方法で実行されません)プログラムを言って(quit buttonによって、または'X'をクリックしてウィンドウを閉じてどちらか)を終了 「異例的に終了させるランタイムを要求し、」

任意のアイデアその問題を解決する方法として、上大変感謝しています!

マイコード:

#!/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のホームやPython 2.7.5、2.4 OpenCVの
  • 免責事項:上記this oneに触発されたコード。

答えて

5

quit_(root, process)機能ではprocess.join()の代わりにprocess.terminate()を使用して解決しました。

+0

次に、答えを受け入れて答えをマークします。 –

+2

SOは私が明日までそれをするのを待つことを頼みます、そして、私は... – Raoul

+0

は、それを行うために突然プロセスを突然終了させて​​いますか? – arvindh

関連する問題