Skip to content

Commit e299e05

Browse files
committed
automatically clear the canvas when drawing over old digit
1 parent 7618e80 commit e299e05

File tree

2 files changed

+35
-29
lines changed

2 files changed

+35
-29
lines changed

src/canvas.py

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,19 @@ def __init__(self, master, w=400, h=400):
2020
self.height = h
2121
self.file_name = "in.gif" # name of the screenshot file that self.save uses
2222
self.create_text(self.width/2, self.height/2, text="Write Your Digit Here", anchor="center")
23-
self.isNew = True
24-
self.isEmpty = True
23+
self.is_new = True
24+
self.is_empty = True
25+
self.has_been_predicted = False # True when the canvas drawing has been sent through the Neural Network
2526
self.delay_id = None # holds the id of the delay started since the pen has been lifted from canvas
2627
Pen(self) # used draw on canvas
2728

2829
def clear(self):
2930
self.delete(tk.ALL) # deletes all items on the canvas
30-
self.isEmpty = True
31+
self.is_empty = True
3132

3233
def grab(self):
3334
""" get current pixel data from canvas and save image to file"""
34-
if not self.isEmpty:
35+
if not self.is_empty:
3536
x = self.winfo_rootx()
3637
y = self.winfo_rooty()
3738
offset = self.border_w # needed because of the canvas' border
@@ -52,28 +53,29 @@ def grab(self):
5253

5354
def center_drawing(self):
5455
""" center drawing by calculating the center of mass https://stackoverflow.com/questions/37519238/"""
55-
if self.isEmpty:
56+
if not self.is_empty:
57+
x = self.winfo_rootx()
58+
y = self.winfo_rooty()
59+
offset = self.border_w # needed because of the canvas' border
60+
canvas_image = ImageGrab.grab((x+offset,y+offset,x+self.width+offset,y+self.height+offset)).convert('L')
61+
immat = canvas_image.load()
62+
m = np.zeros((self.width, self.height))
63+
# calculate center of mass (cx, cy)
64+
for x in range(0, self.width):
65+
for y in range(0, self.height):
66+
m[x, y] = immat[(x, y)] != 255
67+
m = m / np.sum(np.sum(m))
68+
# marginal distributions
69+
dx = np.sum(m, 1)
70+
dy = np.sum(m, 0)
71+
# expected values
72+
cx = np.sum(dx * np.arange(self.width))
73+
cy = np.sum(dy * np.arange(self.height))
74+
75+
self.move(tk.ALL, (self.width/2)-cx, (self.height/2)-cy) # use center of mass to center
76+
self.update() # force the canvas to update immediately
77+
else:
5678
raise ValueError('Canvas is empty.')
57-
x = self.winfo_rootx()
58-
y = self.winfo_rooty()
59-
offset = self.border_w # needed because of the canvas' border
60-
canvas_image = ImageGrab.grab((x+offset,y+offset,x+self.width+offset,y+self.height+offset)).convert('L')
61-
immat = canvas_image.load()
62-
m = np.zeros((self.width, self.height))
63-
# calculate center of mass (cx, cy)
64-
for x in range(0, self.width):
65-
for y in range(0, self.height):
66-
m[x, y] = immat[(x, y)] != 255
67-
m = m / np.sum(np.sum(m))
68-
# marginal distributions
69-
dx = np.sum(m, 1)
70-
dy = np.sum(m, 0)
71-
# expected values
72-
cx = np.sum(dx * np.arange(self.width))
73-
cy = np.sum(dy * np.arange(self.height))
74-
75-
self.move(tk.ALL, (self.width/2)-cx, (self.height/2)-cy) # use center of mass to center
76-
self.update() # force the canvas to update immediately
7779

7880
def predict_timeout(self):
7981
""" cancel old and set a new single timeout until prediction begins """
@@ -103,11 +105,14 @@ def hovered(self, event):
103105
def draw(self, event):
104106
""" draw a line for straight brush strokes and a circle for rounded corners """
105107
self.canvas.predict_timeout() # reset timeout
106-
self.canvas.isEmpty = False
108+
self.canvas.is_empty = False
107109
offset = self.width/2
108-
if self.canvas.isNew: # clear the initial text prompt
110+
if self.canvas.is_new: # clear the initial text prompt
111+
self.canvas.clear()
112+
self.canvas.is_new = False
113+
elif self.canvas.has_been_predicted:
109114
self.canvas.clear()
110-
self.canvas.isNew = False
115+
self.canvas.has_been_predicted = False
111116
self.canvas.create_line(event.x, event.y, self.previous_x, self.previous_y, fill=self.color, width=self.width+1)
112117
self.canvas.create_oval(event.x-offset, event.y-offset, event.x+offset, event.y+offset, fill=self.color)
113118
self.set_previous(event.x, event.y)

src/main.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def __init__(self, parent):
2323
self.cnv = None # canvas widget
2424

2525
self.build_ui()
26-
self.bind_actions()
26+
# self.bind_actions()
2727

2828
def build_ui(self):
2929
# using grid system to set the widgets in the window
@@ -43,6 +43,7 @@ def predict(self):
4343
try:
4444
self.cnv.center_drawing()
4545
image_data = self.cnv.grab()
46+
self.cnv.has_been_predicted = True
4647
except ValueError:
4748
print("Empty canvas. Will not predict.")
4849
return

0 commit comments

Comments
 (0)