-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpaintApp_my_save.py
144 lines (118 loc) · 5.23 KB
/
paintApp_my_save.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
from tkinter import *
from PIL import ImageDraw, Image, ImageGrab
import numpy as np
#import scipy.misc
from skimage import color
from skimage import io
from keras.models import Sequential, model_from_json
import os
import io
from keras_cnn import trainModel, getData
from tkinter import filedialog
# class Paint(object):
class Paint():
def __init__(self):
self.root = Tk()
self.root.title('Handwriting Recognition')
#defining Canvas
self.c = Canvas(self.root, bg='white', width=280, height=280)
self.image1 = Image.new('RGB', (280, 280), color = 'white')
self.draw = ImageDraw.Draw(self.image1)
self.c.grid(row=1, columnspan=6)
# self.classify_button = Button(self.root, text='辨識', command=lambda:self.classify(self.c))
self.classify_button = Button(self.root, text='Recognize', command=lambda:self.classify(self.c))
self.classify_button.grid(row=0, column=0, columnspan=2, sticky='EWNS')
# self.clear = Button(self.root, text='清畫面', command=self.clear)
self.clear = Button(self.root, text='Clear', command=self.clear)
self.clear.grid(row=0, column=2, columnspan=2, sticky='EWNS')
# self.savefile = Button(self.root, text='存檔', command=self.savefile)
self.savefile = Button(self.root, text='SaveAs', command=self.savefile)
self.savefile.grid(row=0, column=4, columnspan=2, sticky='EWNS')
self.prediction_text = Text(self.root, height=2, width=10)
self.prediction_text.grid(row=2, column=4, columnspan=2)
self.model = self.loadModel()
self.setup()
self.root.mainloop()
def setup(self):
self.old_x = None
self.old_y = None
self.line_width = 15
self.color = 'black'
self.c.bind('<B1-Motion>', self.paint)
self.c.bind('<ButtonRelease-1>', self.reset)
def paint(self, event):
paint_color = self.color
if self.old_x and self.old_y:
self.c.create_line(self.old_x, self.old_y, event.x, event.y,
width=self.line_width, fill=paint_color,
capstyle=ROUND, smooth=TRUE, splinesteps=36)
# 畫圖同時寫到記憶體,避免螢幕字型放大,造成抓到的畫布區域不足
self.draw.line((self.old_x, self.old_y, event.x, event.y), fill='black', width=5)
self.old_x = event.x
self.old_y = event.y
def reset(self, event):
self.old_x, self.old_y = None, None
def rgb2gray(rgb):
return np.dot(rgb[...,:3], [0.2989, 0.5870, 0.1140])
def clear(self):
self.c.delete("all")
self.image1 = Image.new('RGB', (280, 280), color = 'white')
self.draw = ImageDraw.Draw(self.image1)
self.prediction_text.delete("1.0", END)
def savefile(self):
f = filedialog.asksaveasfilename(defaultextension=".png", filetypes = [("png file",".png")])
if f is None: # asksaveasfile return `None` if dialog closed with "cancel".
return
#print(f)
self.image1.save(f)
def classify(self, widget):
# 顯示設定>100%,會造成辨識不佳,因為抓到的區域會變小
#getting pixel information
# x=self.root.winfo_rootx()+widget.winfo_x()
# y=self.root.winfo_rooty()+widget.winfo_y()
# x1=x+widget.winfo_width()
# y1=y+widget.winfo_height()
# ImageGrab.grab().crop((x,y,x1,y1)).resize((28, 28)).save('classify.png')
#save drawing
#img = scipy.misc.imread('classify.png', flatten=False, mode='P')
# widget.postscript(file = './classify.eps')
# # use PIL to convert to PNG
# img = ImageGrab.Image.open('./classify.eps')
# img.save('./classify.png')
# img = color.rgb2gray(io.imread('./classify.png')).resize( (28, 28), ImageGrab.Image.ANTIALIAS)
# img = color.rgb2gray(self.image1.resize( (28, 28), ImageGrab.Image.ANTIALIAS))
img = self.image1.resize((28, 28), ImageGrab.Image.ANTIALIAS).convert('L')
#img.save('1.png')
img = np.array(img)
# Change pixels to work with our classifier
# img[img==0] = 255
# img[img==225] = 0
img[img==255] = 0
# img[img>100] = 255
img[img>0] = 255
# img2=Image.fromarray(img)
#img2.save('2.png')
img = np.reshape(img, (1, 28, 28, 1))
# Predict digit
pred = self.model.predict([img])
# Get index with highest probability
pred = np.argmax(pred)
#print(pred)
self.prediction_text.delete("1.0", END)
self.prediction_text.insert(END, pred)
def loadModel(self):
if (os.path.exists('mnist_model.h5')):
print('loading model')
json_file = open('model.json', 'r')
model_json = json_file.read()
json_file.close()
model = model_from_json(model_json)
model.load_weights("mnist_model.h5")
return model
else:
print('train model')
X_train, y_train, X_test, y_test = getData()
model = trainModel(X_train, y_train, X_test, y_test)
return model
if __name__ == '__main__':
Paint()