2013-06-12 46 views
5

Tôi hiện đang cố gắng phát triển một GUI cho một multiprocessed OpenCV Video suối. Đoạn code dưới đây không thành công trong việc làm điều đó, vì nó sẽ hiển thị các cấp dữ liệu video và một 'thoát' nút, nhưng chạy một cách kỳ lạ:hiển thị một video OpenCV trong Tkinter sử dụng đa xử

  • chương trình đặt ra một lỗi Runtime trong pythonw.exe (Tôi sử dụng cửa sổ) trên bỏ (hoặc bằng các quit button hoặc bằng cách đóng cửa sổ bằng cách nhấp vào 'X') nói rằng chương trình "yêu cầu Runtime để chấm dứt một cách bất thường"

Bất kỳ ý tưởng như thế nào để giải quyết vấn đề đó sẽ được đánh giá cao!

Mã của tôi:

#!/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' 
  • Config: Windows 7 Home, Python 2.7.5, OpenCV 2.4
  • Disclaimer: mã trên được lấy cảm hứng từ this one.

Trả lời

5

Tôi đã giải quyết nó bằng cách sử dụng process.terminate() thay vì process.join() trong chức năng quit_(root, process).

+0

Sau đó chấp nhận câu trả lời của bạn để đánh dấu câu trả lời. –

+2

SO yêu cầu tôi đợi đến ngày mai để làm điều đó, và tôi sẽ ... – Raoul

+0

đang chấm dứt một quá trình đột ngột một cách thích hợp để làm điều đó? – arvindh

Các vấn đề liên quan