-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest2.py
242 lines (215 loc) · 7.29 KB
/
test2.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import tkinter as tk
import tkinter.ttk as ttk
from PIL import Image, ImageTk
import random
import time
from accuracy_estimate import timeToTest
import cv2
import threading
video1 = cv2.VideoCapture(0)
# build the window
window = tk.Tk()
window.title("Vision Checker")
window_w = window.winfo_screenwidth()
window_h = window.winfo_screenheight()
window.geometry("%dx%d" % (window_w, window_h))
# window.rowconfigure(1, weight=1)
# window.rowconfigure(2, weight=1)
# window.rowconfigure(3, weight=1)
# window.rowconfigure(4, weight=1)
# window.rowconfigure(5, weight=1)
# window.columnconfigure(1, weight=1)
# window.columnconfigure(2, weight=1)
# window.columnconfigure(3, weight=1)
# window.columnconfigure(4, weight=1)
# window.columnconfigure(5, weight=1)
canvas = tk.Canvas(window, width=window_w, height=window_h)
canvas.grid(column=0, row=0)
# label candidates
pic = ["Up", "Down", "Right", "Left"]
picpos = random.randint(0,3)
# add lights
r = 20
x = int(window_w/2)
y = r + 2
up_light = canvas.create_oval(x-r, y-r, x+r, y+r, fill="")
y = int(window_h*0.85)
down_light = canvas.create_oval(x-r, y-r, x+r, y+r, fill="")
x = int(window_w*0.15)
y = int(window_h/2)
left_light = canvas.create_oval(x-r, y-r, x+r, y+r, fill="")
x = int(window_w*0.85)
right_light = canvas.create_oval(x-r, y-r, x+r, y+r, fill="")
# set the standard
w = 40
h = 40
ratio = [9.0, 4.5, 3.0, 2.25, 1.8, 1.5, 1.29, 1.11, 1.0, 0.9, 0.75, 0.6, 0.45]
level = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.2, 1.5, 2.0]
pos = 0
reverse = False
jump = 2
correct = 0
wrong = 0
# function for changing candidate
def change(dire):
# print("start change. dire is", dire)
if dire == 4:
return True
global picpos, correct, wrong, pos, reverse, jump
output = False
if picpos == dire:
# result.config(text="Correct!", fg="green")
canvas.itemconfig(result, text="Correct!", fill="green")
correct += 1
else:
# result.config(text="Wrong!", fg="red")
canvas.itemconfig(result, text="Wrong!", fill="red")
wrong += 1
change_light(dire)
canvas.itemconfig(instruct, text="hand back to the middle")
canvas.update()
# print("warning and light loaded.")
if correct >= 3:
if reverse or pos == 12:
output = True
else:
if wrong > 0 or pos == 11:
jump = 1
wrong = 0
pos += jump
update_pic_size()
correct = 0
elif wrong >= 2:
if pos == 1 or pos == 0:
pos = 0
output = True
else:
reverse = True
jump = 1
pos -= jump
update_pic_size()
correct = 0
wrong = 0
# window.update_idletasks() # update the warning text
# time.sleep(2) # hold for two seconds
time.sleep(1.5) # hold for one seconds
# result.config(text="")
canvas.itemconfig(result, text="")
canvas.itemconfig(instruct, text="")
reset_light()
if output:
output_result()
return False
# pick a new picture
picpos = random.randint(0,3)
# label.config(image=gifIm[picpos])
# label.image = gifIm[picpos]
canvas.itemconfig(label, image=gifIm[picpos])
canvas.update()
# print("picture changed.")
return True
def change_light(dire):
if dire == 0:
canvas.itemconfig(up_light, fill="red")
elif dire == 1:
canvas.itemconfig(down_light, fill="red")
elif dire == 2:
canvas.itemconfig(left_light, fill="red")
elif dire == 3:
canvas.itemconfig(right_light, fill="red")
def reset_light():
canvas.itemconfig(up_light, fill="")
canvas.itemconfig(down_light, fill="")
canvas.itemconfig(left_light, fill="")
canvas.itemconfig(right_light, fill="")
def output_result():
global window
for widget in window.winfo_children():
widget.destroy()
result_text = tk.Label(
window,
text = "The result is "+str(level[pos]),
font=("Arial", 50),
width=20, height=7
)
result_text.pack()
window.update_idletasks()
print("The result is", level[pos])
# load the pictures
imUp = Image.open("image/Up_9x9.gif")
imDown = Image.open("image/Down_9x9.gif")
imLeft = Image.open("image/Left_9x9.gif")
imRight = Image.open("image/Right_9x9.gif")
gifUp = ImageTk.PhotoImage(imUp.resize((int(w * ratio[pos]), int(h * ratio[pos])), Image.ANTIALIAS))
gifDown = ImageTk.PhotoImage(imDown.resize((int(w * ratio[pos]), int(h * ratio[pos])), Image.ANTIALIAS))
gifLeft = ImageTk.PhotoImage(imLeft.resize((int(w * ratio[pos]), int(h * ratio[pos])), Image.ANTIALIAS))
gifRight = ImageTk.PhotoImage(imRight.resize((int(w * ratio[pos]), int(h * ratio[pos])), Image.ANTIALIAS))
gifIm = [gifUp, gifDown, gifLeft, gifRight]
welcome_png = Image.open("image/welcome.png")
imWelcome = ImageTk.PhotoImage(welcome_png)
# scale_w = 2
# scale_h = 2
# gifUp = gifUp.subsample(scale_w, scale_h)
# gifDown = gifDown.subsample(scale_w, scale_h)
# gifLeft = gifLeft.subsample(scale_w, scale_h)
# gifRight = gifRight.subsample(scale_w, scale_h)
def update_pic_size():
# print("update!")
global gifUp, gifDown, gifLeft, gifRight, gifIm
gifUp = ImageTk.PhotoImage(imUp.resize((int(w * ratio[pos]), int(h * ratio[pos])), Image.ANTIALIAS))
gifDown = ImageTk.PhotoImage(imDown.resize((int(w * ratio[pos]), int(h * ratio[pos])), Image.ANTIALIAS))
gifLeft = ImageTk.PhotoImage(imLeft.resize((int(w * ratio[pos]), int(h * ratio[pos])), Image.ANTIALIAS))
gifRight = ImageTk.PhotoImage(imRight.resize((int(w * ratio[pos]), int(h * ratio[pos])), Image.ANTIALIAS))
gifIm = [gifUp, gifDown, gifLeft, gifRight]
# add the picture
# label = tk.Label(window, image=gifIm[picpos])
# label.image = gifIm[picpos]
# label.grid(column=2, row=2, columnspan=3, rowspan=2, sticky="NEWS")
label = canvas.create_image(int(window_w/2), int(window_h*0.45), anchor=tk.CENTER, image=gifIm[picpos])
# add text labels
picvar = tk.StringVar()
picvar.set(pic[picpos])
result = canvas.create_text(
int(window_w*0.85),
int(window_h*0.78),
text="",
font=("Arial 25 bold")
)
instruct = canvas.create_text(
int(window_w*0.15),
int(window_h*0.78),
text="",
fill="black",
font=("Arial 18 bold")
)
# result = tk.Label(
# window,
# text="",
# font=("Arial", 20),
# width=10, height=1
# )
# result.grid(column=3, row=4)
# add buttons
# buttonUp = ttk.Button(window, text="Up", command=lambda:change(0))
# buttonUp.grid(column=3, row=1)
# buttonDown = ttk.Button(window, text="Down", command=lambda:change(1))
# buttonDown.grid(column=3, row=5)
# buttonLeft = ttk.Button(window, text="Left", command=lambda:change(2))
# buttonLeft.grid(column=1, row=3)
# buttonRight = ttk.Button(window, text="Right", command=lambda:change(3))
# buttonRight.grid(column=5, row=3)
def start():
res = True
while(res):
temp = timeToTest(video1)
print("timeTotest is done, the direction is ",temp)
res = change(temp)
# bind the keyboard
# window.bind("<Up>", lambda event: change(0))
# window.bind("<Down>", lambda event: change(1))
# window.bind("<Left>", lambda event: change(2))
# window.bind("<Right>", lambda event: change(3))
window.bind("<Return>", lambda event: start())
window.mainloop()
video1.release()
cv2.destroyAllWindows()