Python 使用多处理在tkinter中显示OpenCV视频
我目前正在尝试为多处理OpenCV视频流开发一个GUI。下面的代码确实成功地做到了这一点,因为它显示视频提要和“退出”按钮,但运行方式很奇怪:Python 使用多处理在tkinter中显示OpenCV视频,python,opencv,tkinter,multiprocessing,Python,Opencv,Tkinter,Multiprocessing,我目前正在尝试为多处理OpenCV视频流开发一个GUI。下面的代码确实成功地做到了这一点,因为它显示视频提要和“退出”按钮,但运行方式很奇怪: 退出时,程序在pythonw.exe(我正在使用windows)中引发运行时错误(通过退出按钮或通过单击'X'关闭窗口),显示程序 “请求运行时以异常方式终止” 任何关于如何解决这个问题的想法都将不胜感激 我的代码: #!/usr/bin/python import numpy as np from multiprocessing import P
- 退出时,程序在
(我正在使用windows)中引发运行时错误(通过pythonw.exe
退出按钮或通过单击
关闭窗口),显示程序 “请求运行时以异常方式终止”'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'
- 配置:Windows7Home、Python 2.7.5、OpenCV 2.4
- 免责声明:以上代码的灵感来自
process.terminate()
而不是process.join()
在quit(root,process)
函数中解决了这个问题。突然终止一个进程是正确的方法吗?