-
Notifications
You must be signed in to change notification settings - Fork 29
/
drawing_classifier.py
261 lines (204 loc) · 10.2 KB
/
drawing_classifier.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
import pickle
import os.path
import tkinter.messagebox
from tkinter import *
from tkinter import simpledialog, filedialog
import PIL
import PIL.Image, PIL.ImageDraw
import cv2 as cv
import numpy as np
from sklearn.svm import LinearSVC
from sklearn.naive_bayes import GaussianNB
from sklearn.tree import DecisionTreeClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.linear_model import LogisticRegression
class DrawingClassifier:
def __init__(self):
self.class1, self.class2, self.class3 = None, None, None
self.class1_counter, self.class2_counter, self.class3_counter = None, None, None
self.clf = None
self.proj_name = None
self.root = None
self.image1 = None
self.status_label = None
self.canvas = None
self.draw = None
self.brush_width = 15
self.classes_prompt()
self.init_gui()
def classes_prompt(self):
msg = Tk()
msg.withdraw()
self.proj_name = simpledialog.askstring("Project Name", "Please enter your project name down below!", parent=msg)
if os.path.exists(self.proj_name):
with open(f"{self.proj_name}/{self.proj_name}_data.pickle", "rb") as f:
data = pickle.load(f)
self.class1 = data['c1']
self.class2 = data['c2']
self.class3 = data['c3']
self.class1_counter = data['c1c']
self.class2_counter = data['c2c']
self.class3_counter = data['c3c']
self.clf = data['clf']
self.proj_name = data['pname']
else:
self.class1 = simpledialog.askstring("Class 1", "What is the first class called?", parent=msg)
self.class2 = simpledialog.askstring("Class 2", "What is the second class called?", parent=msg)
self.class3 = simpledialog.askstring("Class 3", "What is the third class called?", parent=msg)
self.class1_counter = 1
self.class2_counter = 1
self.class3_counter = 1
self.clf = LinearSVC()
os.mkdir(self.proj_name)
os.chdir(self.proj_name)
os.mkdir(self.class1)
os.mkdir(self.class2)
os.mkdir(self.class3)
os.chdir("..")
def init_gui(self):
WIDTH = 500
HEIGHT = 500
WHITE = (255, 255, 255)
self.root = Tk()
self.root.title(f"NeuralNine Drawing Classifier Alpha v0.2 - {self.proj_name}")
self.canvas = Canvas(self.root, width=WIDTH-10, height=HEIGHT-10, bg="white")
self.canvas.pack(expand=YES, fill=BOTH)
self.canvas.bind("<B1-Motion>", self.paint)
self.image1 = PIL.Image.new("RGB", (WIDTH, HEIGHT), WHITE)
self.draw = PIL.ImageDraw.Draw(self.image1)
btn_frame = tkinter.Frame(self.root)
btn_frame.pack(fill=X, side=BOTTOM)
btn_frame.columnconfigure(0, weight=1)
btn_frame.columnconfigure(1, weight=1)
btn_frame.columnconfigure(2, weight=1)
class1_btn = Button(btn_frame, text=self.class1, command=lambda: self.save(1))
class1_btn.grid(row=0, column=0, sticky=W + E)
class2_btn = Button(btn_frame, text=self.class2, command=lambda: self.save(2))
class2_btn.grid(row=0, column=1, sticky=W + E)
class3_btn = Button(btn_frame, text=self.class3, command=lambda: self.save(3))
class3_btn.grid(row=0, column=2, sticky=W + E)
bm_btn = Button(btn_frame, text="Brush-", command=self.brushminus)
bm_btn.grid(row=1, column=0, sticky=W + E)
clear_btn = Button(btn_frame, text="Clear", command=self.clear)
clear_btn.grid(row=1, column=1, sticky=W + E)
bp_btn = Button(btn_frame, text="Brush+", command=self.brushplus)
bp_btn.grid(row=1, column=2, sticky=W + E)
train_btn = Button(btn_frame, text="Train Model", command=self.train_model)
train_btn.grid(row=2, column=0, sticky=W + E)
save_btn = Button(btn_frame, text="Save Model", command=self.save_model)
save_btn.grid(row=2, column=1, sticky=W + E)
load_btn = Button(btn_frame, text="Load Model", command=self.load_model)
load_btn.grid(row=2, column=2, sticky=W + E)
change_btn = Button(btn_frame, text="Change Model", command=self.rotate_model)
change_btn.grid(row=3, column=0, sticky=W + E)
predict_btn = Button(btn_frame, text="Predict", command=self.predict)
predict_btn.grid(row=3, column=1, sticky=W + E)
save_everything_btn = Button(btn_frame, text="Save Everything", command=self.save_everything)
save_everything_btn.grid(row=3, column=2, sticky=W + E)
self.status_label = Label(btn_frame, text=f"Current Model: {type(self.clf).__name__}")
self.status_label.config(font=("Arial", 10))
self.status_label.grid(row=4, column=1, sticky=W + E)
self.root.protocol("WM_DELETE_WINDOW", self.on_closing)
self.root.attributes("-topmost", True)
self.root.mainloop()
def paint(self, event):
x1, y1 = (event.x - 1), (event.y - 1)
x2, y2 = (event.x + 1), (event.y + 1)
self.canvas.create_rectangle(x1, y1, x2, y2, fill="black", width=self.brush_width)
self.draw.rectangle([x1, y2, x2 + self.brush_width, y2 + self.brush_width], fill="black", width=self.brush_width)
def save(self, class_num):
self.image1.save("temp.png")
img = PIL.Image.open("temp.png")
img.thumbnail((50, 50), PIL.Image.ANTIALIAS)
if class_num == 1:
img.save(f"{self.proj_name}/{self.class1}/{self.class1_counter}.png", "PNG")
self.class1_counter += 1
elif class_num == 2:
img.save(f"{self.proj_name}/{self.class2}/{self.class2_counter}.png", "PNG")
self.class2_counter += 1
elif class_num == 3:
img.save(f"{self.proj_name}/{self.class3}/{self.class3_counter}.png", "PNG")
self.class3_counter += 1
self.clear()
def brushminus(self):
if self.brush_width > 1:
self.brush_width -= 1
def brushplus(self):
self.brush_width += 1
def clear(self):
self.canvas.delete("all")
self.draw.rectangle([0, 0, 1000, 1000], fill="white")
def train_model(self):
img_list = np.array([])
class_list = np.array([])
for x in range(1, self.class1_counter):
img = cv.imread(f"{self.proj_name}/{self.class1}/{x}.png")[:, :, 0]
img = img.reshape(2500)
img_list = np.append(img_list, [img])
class_list = np.append(class_list, 1)
for x in range(1, self.class2_counter):
img = cv.imread(f"{self.proj_name}/{self.class2}/{x}.png")[:, :, 0]
img = img.reshape(2500)
img_list = np.append(img_list, [img])
class_list = np.append(class_list, 2)
for x in range(1, self.class3_counter):
img = cv.imread(f"{self.proj_name}/{self.class3}/{x}.png")[:, :, 0]
img = img.reshape(2500)
img_list = np.append(img_list, [img])
class_list = np.append(class_list, 3)
img_list = img_list.reshape(self.class1_counter - 1 + self.class2_counter - 1 + self.class3_counter - 1, 2500)
self.clf.fit(img_list, class_list)
tkinter.messagebox.showinfo("NeuralNine Drawing Classifier", "Model successfully trained!", parent=self.root)
def predict(self):
self.image1.save("temp.png")
img = PIL.Image.open("temp.png")
img.thumbnail((50, 50), PIL.Image.ANTIALIAS)
img.save("predictshape.png", "PNG")
img = cv.imread("predictshape.png")[:, :, 0]
img = img.reshape(2500)
prediction = self.clf.predict([img])
if prediction[0] == 1:
tkinter.messagebox.showinfo("NeuralNine Drawing Classifier", f"The drawing is probably a {self.class1}", parent=self.root)
elif prediction[0] == 2:
tkinter.messagebox.showinfo("NeuralNine Drawing Classifier", f"The drawing is probably a {self.class2}", parent=self.root)
elif prediction[0] == 3:
tkinter.messagebox.showinfo("NeuralNine Drawing Classifier", f"The drawing is probably a {self.class3}", parent=self.root)
def rotate_model(self):
if isinstance(self.clf, LinearSVC):
self.clf = KNeighborsClassifier()
elif isinstance(self.clf, KNeighborsClassifier):
self.clf = LogisticRegression()
elif isinstance(self.clf, LogisticRegression):
self.clf = DecisionTreeClassifier()
elif isinstance(self.clf, DecisionTreeClassifier):
self.clf = RandomForestClassifier()
elif isinstance(self.clf, RandomForestClassifier):
self.clf = GaussianNB()
elif isinstance(self.clf, GaussianNB):
self.clf = LinearSVC()
self.status_label.config(text=f"Current Model: {type(self.clf).__name__}")
def save_model(self):
file_path = filedialog.asksaveasfilename(defaultextension="pickle")
with open(file_path, "wb") as f:
pickle.dump(self.clf, f)
tkinter.messagebox.showinfo("NeuralNine Drawing Classifier", "Model successfully saved!", parent=self.root)
def load_model(self):
file_path = filedialog.askopenfilename()
with open(file_path, "rb") as f:
self.clf = pickle.load(f)
tkinter.messagebox.showinfo("NeuralNine Drawing Classifier", "Model successfully loaded!", parent=self.root)
def save_everything(self):
data = {"c1": self.class1, "c2": self.class2, "c3": self.class3, "c1c": self.class1_counter,
"c2c": self.class2_counter, "c3c": self.class3_counter, "clf": self.clf, "pname": self.proj_name}
with open(f"{self.proj_name}/{self.proj_name}_data.pickle", "wb") as f:
pickle.dump(data, f)
tkinter.messagebox.showinfo("NeuralNine Drawing Classifier", "Project successfully saved!", parent=self.root)
def on_closing(self):
answer = tkinter.messagebox.askyesnocancel("Quit?", "Do you want to save your work?", parent=self.root)
if answer is not None:
if answer:
self.save_everything()
self.root.destroy()
exit()
DrawingClassifier()