You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

112 lines
4.1 KiB

from tkinter import *
import cv2
from PIL import ImageGrab
from tkinter import filedialog
import read_image
model=3 # 默认使用模型三
# 此函数用于选择模型一
def model_1():
global model
model = 1
print(model)
# 此函数用于选择模型二
def model_2():
global model
model = 2
print(model)
# 此函数用于选择模型三
def model_3():
global model
model = 3
print(model)
# 此函数用于调节笔的粗细以及颜色
def paint(event):
x1, y1 = (event.x - 20), (event.y - 20)
x2, y2 = (event.x + 20), (event.y + 20)
w.create_oval(x1, y1, x2, y2, fill="white", outline='white')
# 此函数用于打开本地的图片
def open_image():
image_name = filedialog.askopenfilename(title='打开图片', filetypes=[('jpg,jpeg', '*.jpg')])
image_show = cv2.imread(image_name, cv2.IMREAD_GRAYSCALE)
cv2.imshow("image", image_show)
print(model)
result = read_image.predeiction(image_name, model)
text.set(str(result))
# 此函数用于截取画板图片并进行识别
def screenshot(*args):
a = root.winfo_x()
b = root.winfo_y()
a = a + 10
b = b + 35
bbox = (a, b, a + 395, b + 395)
im = ImageGrab.grab(bbox)
im.save('1.jpg')
print("在使用%d模型"%model)
result = read_image.predeiction('1.jpg', model)
text.set(str(result))
# 此函数用于清空画布
def clear_canvas(event):
x1, y1 = (event.x - 2800), (event.y - 2800)
x2, y2 = (event.x + 2800), (event.y + 2800)
w.create_oval(x1, y1, x2, y2, fill="black", outline='black')
# 此函数用于清空画布
def reset_canvas():
a = root.winfo_x()
b = root.winfo_y()
x1, y1 = (a - 2800), (b - 2800)
x2, y2 = (a + 2800), (b + 2800)
w.create_oval(x1, y1, x2, y2, fill="black", outline='black')
root = Tk()
root.geometry('600x400') # 规定窗口大小600*400像素
root.resizable(False, False) # 规定窗口不可缩放
root.title('数字识别')
# 调节位置
col_count, row_count = root.grid_size()
for col in range(col_count):
root.grid_columnconfigure(col, minsize=10)
for row in range(row_count):
root.grid_rowconfigure(row, minsize=20)
text = StringVar()
text.set('')
w = Canvas(root, width=400, height=400, bg='black') # 定义画布大小及其颜色
w.grid(row=0, column=0, rowspan=6) # 按键定义几行几列
label_1 = Label(root, text=' 识别的结果为:', font=('', 20)) # 字体显示大小
label_1.grid(row=0, column=1) # 该字体处于的位置
result_label = Label(root, textvariable=text, font=('', 25), height=2, fg='red') # 识别结果的大小和颜色定义
result_label.grid(row=1, column=1) # 识别结果处于的位置
try_button = Button(root, text='模型1', width=7, height=2, command=model_1) # 调用模型一函数
try_button.grid(row=2, column=1,sticky=W) # 定义模型一按钮所处的位置
try_button = Button(root, text='模型2', width=7, height=2, command=model_2) # 调用模型二函数
try_button.grid(row=2, column=1) # 定义模型二所处的位置
try_button = Button(root, text='模型3', width=7, height=2, command=model_3) # 调用模型三函数
try_button.grid(row=2, column=1,sticky=E) # 定义模型三按钮所处的位置
try_button = Button(root, text='开始识别', width=15, height=2, command=screenshot) # 调用识别数字函数
try_button.grid(row=3, column=1) # 定义开始识别按钮所处的位置
clear_button = Button(root, text='清空画布', width=15, height=2, command=reset_canvas) # 调用清空画布函数
clear_button.grid(row=4, column=1) # 定义清空画布按钮所处的位置
load_image_button = Button(root, text='来自图片', width=15, height=2, command=open_image) # 调用打开本地图片函数
load_image_button.grid(row=5, column=1) # 定义来自图片按钮所处的位置
w.bind("<B1-Motion>", paint) # 鼠标左键进行写数字
w.bind("<Button-3>", screenshot) # 鼠标右键进行截屏识别
w.bind("<Double-Button-1>", clear_canvas) # 双击鼠标左键进行清空画布
mainloop()