我用python编写了一个程序,导入了OpenCV的库。现在,我正在使用Tkinter进行GUI。我试图在GUI中显示网络摄像头,但我不能。我把代码放在函数中,因为我想用一个按钮看我的网络摄像头。
我的代码是:
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)
另外,我不知道如何在没有一个循环的情况下不断更新,因为我有另一个按钮来退出程序。
答案 0 :(得分:13)
使用OpenCv和Tkinter进行相机捕捉的简单版本:
import Tkinter as tk
import cv2
from PIL import Image, ImageTk
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.Tk()
root.bind('<Escape>', lambda e: root.quit())
lmain = tk.Label(root)
lmain.pack()
def show_frame():
_, frame = cap.read()
frame = cv2.flip(frame, 1)
cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
img = Image.fromarray(cv2image)
imgtk = ImageTk.PhotoImage(image=img)
lmain.imgtk = imgtk
lmain.configure(image=imgtk)
lmain.after(10, show_frame)
show_frame()
root.mainloop()
您需要下载并安装PIL!
答案 1 :(得分:3)
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()
答案 2 :(得分:2)
试试这段代码:
from collections import deque
import cv2
from PIL import Image, ImageTk
import time
import Tkinter as tk
def quit_(root):
root.destroy()
def update_image(image_label, cam):
(readsuccessful, f) = cam.read()
gray_im = cv2.cvtColor(f, cv2.COLOR_BGR2GRAY)
a = Image.fromarray(gray_im)
b = ImageTk.PhotoImage(image=a)
image_label.configure(image=b)
image_label._image_cache = b # avoid garbage collection
root.update()
def update_fps(fps_label):
frame_times = fps_label._frame_times
frame_times.rotate()
frame_times[0] = time.time()
sum_of_deltas = frame_times[0] - frame_times[-1]
count_of_deltas = len(frame_times) - 1
try:
fps = int(float(count_of_deltas) / sum_of_deltas)
except ZeroDivisionError:
fps = 0
fps_label.configure(text='FPS: {}'.format(fps))
def update_all(root, image_label, cam, fps_label):
update_image(image_label, cam)
update_fps(fps_label)
root.after(20, func=lambda: update_all(root, image_label, cam, fps_label))
if __name__ == '__main__':
root = tk.Tk()
image_label = tk.Label(master=root)# label for the video frame
image_label.pack()
cam = cv2.VideoCapture(1)
fps_label = tk.Label(master=root)# label for fps
fps_label._frame_times = deque([0]*5) # arbitrary 5 frame average FPS
fps_label.pack()
# quit button
quit_button = tk.Button(master=root, text='Quit',command=lambda: quit_(root))
quit_button.pack()
# setup the update callback
root.after(0, func=lambda: update_all(root, image_label, cam, fps_label))
root.mainloop()
答案 3 :(得分:1)
我已经完成并且成功地工作了。 这是Link
我没有指定按钮来显示框架,但您可以指定一个按钮并运行以获得答案。我知道使用全局变量是一种不好的做法,但我仍在使用它。希望它有所帮助。
答案 4 :(得分:0)
Kieleth提出的解决方案在Jupyter笔记本中对我来说效果很好。 但是,我建议您在主循环之后销毁root并释放cap。 否则,每次都必须重置内核。