@@ -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 )
0 commit comments