显示网络摄像头序列TkInter [英] Show webcam sequence TkInter
本文介绍了显示网络摄像头序列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屋!
查看全文