我试图用Tkinter构建一个程序,让用户在画布上绘图,点击一个按钮"预测"然后使用画布上的任何内容为已经训练过的神经网络(MNIST数据库)提供信息。
如何将画布上的内容用作图像?有可能吗?
def paint(event):
x1, y1 = (event.x - 5), (event.y - 5)
x2, y2 = (event.x + 5), ( event.y + 5)
canvas.create_oval( x1, y1, x2, y2, fill = "black" )
root = Tk()
#///Menu///
menu = Menu(root)
root.config(menu = menu)
top_frame = Frame(root)
top_frame.pack(side = TOP)
middle_frame_L = Frame(root)
middle_frame_L.pack(side = LEFT)
middle_frame_R = Frame(root)
middle_frame_R.pack(side = LEFT)
submenu = Menu(menu)
menu.add_cascade(label = "Arquivo", menu = submenu)
submenu.add_command(label = "Sair", command = exit_program)
page_title = Label(top_frame, text = "MNIST Hand-Written Digit Classification", bg = "#adc9f7", font=("Helvetica", 30))
page_title.pack(side = TOP, fill = X)
instructions = Label(top_frame, text = "Desenhe no quadro abaixo um digito de 0 a 9. Após isso, clique em 'Prever' para a Rede Neural tentar acertar o seu número.", font = ("Helvetica", 15))
instructions.pack(side = BOTTOM)
canvas = Canvas(middle_frame_L, width = 200, height = 200, bg = "#8e8e8e")
canvas.pack()
prever_button = Button(middle_frame_L, text = "Prever", command = exit_program)
prever_button.pack()
photo = PhotoImage(file = "Neural Network aleatória.png")
photo_label = Label(middle_frame_R, image = photo)
photo_label.pack(side = LEFT)
# bind mouse dragging event to Canvas
canvas.bind("<B1-Motion>", paint)
root.mainloop()