我正在尝试使用tkinter创建一个gui,在这个gui中,在C:驱动器的子目录“raw”下创建一个文件夹,其中的条目中有一个用户指定的条目小工具.on创建文件夹,我想从我使用opencv的实时网络摄像头序列中单击图像。问题:只有一个图像被捕获!我想捕获同一个人的30张图像并存储在C:/raw/s中,其中s是用户指定的目录。在这里是代码。我不知道为什么img_counter变量没有更新。任何我们将非常感谢您的投入import tkinter as tk
import cv2
from tkinter import *
from PIL import Image, ImageTk
import os
import tkMessageBox
from multiprocessing import Process, Queue
width, height = 800, 600
cap = cv2.VideoCapture(0)
flag,frame=cap.read()
#cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
#cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
global img_counter
root = tk.Tk()
root.bind('', lambda e: root.quit())
lmain = tk.Label(root)
lmain.pack()
tk.Label(text='Enter your name')
entry = tk.Entry(root, width=10)
entry.pack(side=tk.TOP,padx=10,pady=10)
#e = Entry(root,width=10)
#e.pack(side=TOP,padx=10,pady=10)
entry.delete(0, tk.END)
entry.insert(0, "username")
def onok():
s = entry.get()
mypath='C:\\raw\\'
mypath=os.path.join(os.path.dirname(mypath), s)
os.mkdir(mypath, 0777)
tkMessageBox.showinfo(title=s,message="Your name is now entered.Start taking images")
entry.destroy()
#tk.Button(root,text='click',command=onclick).pack()
img_counter = 0
click_button = tk.Button(master=root, text='Click', command=lambda: saveImage(img_counter))
click_button.pack()
def saveImage(img_counter):
ret, frame = cap.read()
cv2.imshow("test", frame)
img_name = "opencv_frame_{}.png".format(img_counter)
cv2.imwrite(img_name, frame)
print("{} written!".format(img_name))
img_counter += 1
tk.Button(root, text='OK', command=onok).pack(side=tk.LEFT)
#tk.Button(root, text='CLOSE').pack(side= tk.RIGHT)
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()