显示网络摄像头序列 TkInter [英] Show webcam sequence TkInter

查看:28
本文介绍了显示网络摄像头序列 TkInter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用 python 做了一个程序,导入 OpenCV 的库.现在,我正在 Tkinter 中制作 GUI.我试图在 GUI 中显示网络摄像头,但我不能.我将代码放在函数中,因为我想通过按钮查看我的网络摄像头.

I did a program with python, importing the OpenCV's libraries. Now, I'm doing the GUI in Tkinter. I'm trying to show the webcam in the GUI but I couldn't. I put the code in the Function because I would like with a push button see my webcam.

我的代码是:

def webcam():
   img= cv.QueryFrame(cap)
   cam= PhotoImage(img)
   label1 = Label(root, image=cam)
   label1.image = cam
   label1.pack()
   label1.place(x=0, y=400)

另外,我不知道如何在没有一段时间循环的情况下不断更新,因为我有另一个按钮可以退出程序.

Also, I don't know how update constantly without a while cycle, because I have another push button to quit the program.

推荐答案

Kieleth 更新的 py3.7 代码

import PIL
from PIL import Image,ImageTk
import pytesseract
import cv2
from tkinter import *
width, height = 800, 600
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)

root = Tk()
root.bind('<Escape>', lambda e: root.quit())
lmain = Label(root)
lmain.pack()

def show_frame():
    _, frame = cap.read()
    frame = cv2.flip(frame, 1)
    cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
    img = PIL.Image.fromarray(cv2image)
    imgtk = ImageTk.PhotoImage(image=img)
    lmain.imgtk = imgtk
    lmain.configure(image=imgtk)
    lmain.after(10, show_frame)

show_frame()
root.mainloop()

这篇关于显示网络摄像头序列 TkInter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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