-
Notifications
You must be signed in to change notification settings - Fork 1
/
painter.py
executable file
·140 lines (114 loc) · 4.89 KB
/
painter.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/env python3
import io
import json
from tkinter import *
import numpy as np
import tensorflow as tf
from PIL import Image, ImageDraw
from tensorflow.keras.models import load_model
gpus = tf.config.experimental.list_physical_devices('GPU')
[tf.config.experimental.set_memory_growth(gpu, True) for gpu in gpus if gpus]
scale = 12
with open("config.json", encoding="utf-8") as f:
config = json.load(f)
config["model"]["CNN"] = load_model(config["model"]["CNN"])
config["model"]["MLP"] = load_model(config["model"]["MLP"])
# Initialize model
_ = config["model"]["CNN"].predict(np.zeros(config["shape"]["CNN"]))[0]
_ = config["model"]["MLP"].predict(np.zeros(config["shape"]["MLP"]))[0]
class Paint(object):
def __init__(self, lang="en", width=600, height=600):
self.lang = lang
self.width = width
self.height = height
self.root = Tk()
self.NN_button = Button(self.root, command=self.choose_NN, font=("Courier", scale))
self._apply_nn("MLP")
self.NN_button.grid(row=0, column=0)
# self.eraser_button = Button(self.root, text='預測', command=self.use_predictor, font=("Courier", scale))
# self.eraser_button.grid(row=0, column=1)
self.stringvar = StringVar()
if lang == "zh":
self.label = Label(self.root, textvariable=self.stringvar, font=("Courier", 20))
elif lang == "en":
self.label = Label(self.root, textvariable=self.stringvar, font=("Courier", 12))
self.label.grid(row=0, column=1)
self.stringvar.set(config["stringvar"][self.lang])
self.choose_size_button = Scale(self.root, from_=30, to=150, orient=HORIZONTAL, font=("Courier", scale),
length=150, width=20)
self.choose_size_button.grid(row=0, column=2)
self.c = Canvas(self.root, bg='white', width=self.width, height=self.height)
self.image1 = Image.new("L", (self.width, self.height))
self.draw = ImageDraw.Draw(self.image1)
self.c.grid(row=1, columnspan=3)
self.item = None
self.setup()
self.root.mainloop()
def setup(self):
self.old_x = None
self.old_y = None
self.choose_size_button.set(60)
self.line_width = self.choose_size_button.get()
self.c.bind('<B1-Motion>', self._paint)
self.c.bind('<ButtonRelease-1>', self.reset)
self.c.bind("<Button-3>", self._right_click)
self.c.bind("<Button-2>", self._left_click)
self.c.bind("<Button-4>", self._on_mousewheel)
self.c.bind("<Button-5>", self._on_mousewheel)
def _on_mousewheel(self, event):
count = self.choose_size_button.get()
if event.num == 5 or event.delta == -120:
self.choose_size_button.set(count - 1)
if event.num == 4 or event.delta == 120:
self.choose_size_button.set(count + 1)
def choose_NN(self):
if self.mode == "MLP":
self._apply_nn("CNN")
else:
self._apply_nn("MLP")
self._use_predictor()
def _apply_nn(self, mode):
if mode == "CNN":
option_to = "MLP"
else:
option_to = "CNN"
self.mode = mode
print(config["use_what"][self.lang] % config["l8n"][mode])
self.model = config["model"][mode]
self.shape = config["shape"][mode]
self.NN_button.config(text=config["switch"][self.lang] % config["l8n"][option_to][self.lang])
self.root.title(config["title"][self.lang] % config["l8n"][mode][self.lang])
def _use_predictor(self):
filename = "my_drawing.jpg"
# self.image1.save(filename)
ps = self.c.postscript(colormode='gray')
img = Image.open(io.BytesIO(ps.encode('utf-8')))
gray = img.convert('L')
bw = gray.point(lambda x: 1 if x < 128 else 0)
bw.save(filename)
array = np.array(bw.resize((28, 28))).reshape(self.shape)
# array = array.astype("float32")
# array /= 255
ans = self.model.predict(array)[0]
print(np.around(ans, decimals=1))
self.stringvar.set(np.argmax(ans))
def _paint(self, event):
self.line_width = self.choose_size_button.get()
paint_color = 'black'
if self.old_x and self.old_y:
new = event.x, event.y
self.item = self.c.create_line(self.old_x, self.old_y, *new,
width=self.line_width, fill=paint_color,
capstyle=ROUND, smooth=TRUE, splinesteps=36)
self.old_x = event.x
self.old_y = event.y
def reset(self, event):
self.old_x, self.old_y = None, None
def _right_click(self, event):
self.c.delete("all")
self.label.config(font=("Courier", 30))
self.stringvar.set(config["Answer"][self.lang])
def _left_click(self, event):
self._use_predictor()
if __name__ == '__main__':
Paint()