forked from aws/amazon-sagemaker-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_input.py
129 lines (108 loc) · 3.33 KB
/
get_input.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
import math
from tkinter import *
from tkinter import messagebox
import numpy as np
# track welcome status so we only display the welcome message once
welcome = False
# saved array size
width = 28.0
height = 28.0
# scale from array size to GUI size (for readability, ease of drawing/use)
scale = 10.0
# internal array data, initialize to zeros
array = np.zeros((int(width), int(height)))
def update(x, y):
"""
Update the internal array with given x/y coordinate
"""
global array
# compute location in array using scaling factor
real_x = math.floor(x / scale)
real_y = math.floor(y / scale)
# update array with value '1'
array[real_y][real_x] = 1
def paint(event):
"""
Event handler for mouse motion
Update the GUI with dots for given x/y coordinate
"""
global canvas
# compute size of dot based on scaling factor
x1, y1 = (event.x - scale), (event.y - scale)
x2, y2 = (event.x + scale), (event.y + scale)
# draw dot
canvas.create_oval(x1, y1, x2, y2, fill="black")
# update internal array
update(event.x, event.y)
def save(event):
"""
Event handler for mouse button release
Save the internal array to file
"""
global array
# save
np.save("input.npy", array)
# print array data to console (for understanding)
for y in range(int(height)):
s = ""
for x in range(int(width)):
s += str(int(array[y][x])) + " "
print(s)
# remind user of file name
print("saved to input.npy")
def clear(event):
"""
Event handler for mouse click
Clear internal array and drawing canvas to prepare for new digit
"""
global canvas
global array
# clear internal array
array = np.zeros((int(width), int(height)))
# clear drawing canvas
canvas.delete("all")
def focus(event):
"""
Event handler for gaining focus on window
Display welcome message the first time
"""
global welcome
# only display message once
if not welcome:
# open info pop up with instructions
messagebox.showinfo(
"Instructions",
"Click and drag to draw a digit as a single continuous stroke. Release the mouse to save 28x28 numpy array as 'input.npy' file on disk. Clicking and dragging again will reset and start over. Close the window to exit the python process",
parent=master,
)
# set flag to not repeat
welcome = True
#######################
# setup GUI
#######################
# setup window
try:
master = Tk()
except TclError as ex:
msg = ex.args[0]
if "display" in msg or "DISPLAY" in msg:
print(
"This script must be run in a terminal shell and on a machine with a GUI display like your local computer."
)
exit(1)
master.title("Draw a digit")
# register focus handler on window
master.bind("<FocusIn>", focus)
# setup drawing canvas
canvas = Canvas(master, width=width * scale, height=height * scale, background="gray75")
# if user resizes window, dont scale canvas
canvas.pack(expand=NO, fill=NONE)
# register handlers for canvas
canvas.bind("<B1-Motion>", paint)
canvas.bind("<ButtonRelease>", save)
canvas.bind("<Button-1>", clear)
# message at bottom of window
message = Label(master, text="Press and Drag the mouse to draw")
message.pack(side=BOTTOM)
# main GUI loop
mainloop()