-
Notifications
You must be signed in to change notification settings - Fork 0
/
draw.py
87 lines (68 loc) · 2.32 KB
/
draw.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# GUI for drawing
from tkinter import *
from PIL import Image, ImageDraw, ImageTk
from normalize import normalize
from ann import ANN
# variables
width = 500
height = 500
# functions
def gui():
global draw, image, photo, canvas
root = Tk()
root.title("Katakana Handwriting Recognition")
root.bind("<B1-Motion>", paint)
root.bind("<B3-Motion>", erase)
root.bind("<Return>", classify_draw)
root.bind("<BackSpace>", clear_draw)
frame = Frame(root, width = width, height = height)
frame.pack()
#img = PhotoImage(width = width, height = height)
#img.put("#EEEEEE", (50, 50))
#img = ImageTk.PhotoImage(Image.open("data\\computer (color)\\anime\\a.png"))
#img = ImageTk.PhotoImage('RGB', (width, height))
image = Image.new("RGB", (16, 16), 0xFFFFFF).resize((width, height))
photo = ImageTk.PhotoImage(image)
draw = ImageDraw.Draw(image) #pxs = image.load()
prev = (-1, -1)
canvas = Canvas(frame, bg = "white", width = width, height = height)
canvas.create_image(0, 0, image = photo, anchor = 'nw')
canvas.pack()
#img.put("#EEEEEE", (50, 50))
root.mainloop()
def refresh_draw():
global image, photo, canvas
photo = ImageTk.PhotoImage(image)
canvas.create_image(0, 0, image = photo, anchor = 'nw')
def paint(event):
global draw
(x, y) = (event.x, event.y)
if (x >= 0 and x < width and y >= 0 and y < height):
draw.ellipse((x - 10, y - 10, x + 10, y + 10), fill = (0, 0, 0))
refresh_draw()
def erase(event):
global draw
(x, y) = (event.x, event.y)
if (x >= 0 and x < width and y >= 0 and y < height):
draw.ellipse((x - 10, y - 10, x + 10, y + 10), fill = (0xFF, 0xFF, 0xFF))
refresh_draw()
def classify_draw(event):
print("classifying drawing")
ann.classify(ann.parse(normalize(image)))
def clear_draw(event):
global draw
draw.ellipse((-width, -height, width * 2, height * 2), fill = (0xFF, 0xFF, 0xFF))
refresh_draw()
def outweights(l, j):
return [ann.weights[l][i][j] for i in range(len(ann.weights[l]))]
def reconstruct(data):
img = Image.new("LA", (16, 16))
pxs = img.load()
for x in range(16):
for y in range(16):
pxs[x, y] = (int(data[y * 16 + x] * 128 + 128), 255)
return img
if __name__ == "__main__":
ann = ANN()
ann.load("256_iteration_30")
gui()