-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
179 lines (101 loc) · 3.88 KB
/
main.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
from kivy.app import App
from kivy.graphics import Rectangle
from kivy.graphics import Color
from kivy.uix.widget import Widget
from kivy.graphics import Line
from kivy.uix.image import Image
from kivy.clock import Clock
from kivy.config import Config
import PIL.Image as pilm
import numpy as np
import matplotlib.pyplot as plt
import sklearn
from sklearn.externals import joblib
from kivy.core.window import Window
#Config.set('graphics','width','960')
#Config.set('graphics','height','540')
class Touch(Widget):
line_width = 6
l = []
r = 0
stops = []
initial = 0
d = dict()
character = ''
values = {0 : 48,1 : 49,2 : 50,3 : 51,4 : 52,5 : 53,6 : 54,7 : 55,8 : 56,9 : 57,10 : 65,11 : 66,12 : 67,13 : 68,14 : 69,15 : 70,16 : 71,17 : 72,18 : 73,19 : 74,20 : 75,21 : 76,22 : 77,23 : 78,24 : 79,25 : 80,26 : 81,27 : 82,28 : 83,29 : 84,30 : 85,31 : 86,32 : 87,33 : 88,34 : 89,35 : 90,36 : 97,37 : 98,38 : 100,39 : 101,40 : 102,41 : 103,42 : 104,43 : 110,44 : 113,45 : 114,46 : 116}
def on_touch_down(self, touch):
if Widget.on_touch_up(self,touch):
print('hii')
else:
with self.canvas:
touch.ud['line'] = Line(points = (touch.x,touch.y),width = self.line_width)
def on_touch_move(self, touch):
touch.ud['line'].points += (touch.x, touch.y)
def on_touch_up(self, touch):
if Widget.on_touch_down(self, touch):
print('hii')
else:
size = Window.size
#print(size[0])
x_mul = 100 / size[0]
y_mul = 100 / size[1]
#print(x_mul)
#print(y_mul)
self.r = self.r + 1
for i in touch.ud['line'].points:
if touch.ud['line'].points.index(i) % 2 == 0:
self.l.append(i * x_mul)
else:
self.l.append(i * y_mul)
#print(self.l)
s = len(self.l)
self.stops.append(s)
def export(self):
for k in self.stops:
self.d[k] = self.l[self.initial : k]
self.initial = k
#print(list(self.d.values()))
self.my_image = Image(source='black_bg2.jpg')
with self.my_image.canvas:
for l in list(self.d.values()):
self.line = Line(points=(l), width=5)
#Color(1, 0, 0, 0.5, mode="rgba")
Clock.schedule_once(self.exp, 1)
#self.my_image.export_as_image().save('cool3.jpg')
def clear(self):
saved = self.children[:]
self.clear_widgets()
self.canvas.clear()
for widget in saved:
self.add_widget(widget)
self.l = []
self.r = 0
self.initial = 0
self.stops = []
self.d = dict()
self.character = ''
my_label = self.ids.my_label
my_label.text = ''
def exp(self,dd):
self.my_image.export_as_image().save('cool3.jpg')
org = pilm.open('cool3.jpg').convert('L')
resized_img = org.resize((28, 28))
resized_arr = np.asarray(resized_img)
plt.imshow(resized_arr)
mlp = joblib.load('mlp_model')
#print(mlp.predict(resized_arr.reshape(1, 784)))
val = mlp.predict(resized_arr.reshape(1, 784))
num = self.values[val[0]]
#print(chr(num))
self.character = self.character + chr(num)
#print(self.character)
my_label = self.ids.my_label
my_label.text = self.character
#size = Window.size
#print(size)
plt.show()
class Testing7App(App):
def build(self):
return Touch()
touching = Testing7App()
touching.run()