python调用opencv处理视频_python – 使用多处理在tkinter中显示OpenCV视频

本文档描述了一个在Python中使用Tkinter GUI和多处理展示OpenCV视频流的程序遇到的问题。当尝试退出程序时,会引发一个运行时错误。代码展示了如何捕获视频、更新GUI以及处理退出操作。问题在于程序在关闭时以不寻常的方式终止,可能需要调整退出逻辑以避免此错误。
摘要由CSDN通过智能技术生成

我目前正在尝试为多处理的OpenCV视频流开发GUI.下面的代码确实成功地实现了这一点,因为它显示了视频源和“退出”按钮,但以奇怪的方式运行:

>程序在退出时(通过退出按钮或通过单击“X”关闭窗口)在pythonw.exe(我正在使用Windows)中引发运行时错误,说程序

“要求运行时以不寻常的方式终止”

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

我的代码:

#!/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

>免责声明:上面的代码灵感来自this one.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值