-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdcam_tests.py
169 lines (143 loc) · 6.05 KB
/
dcam_tests.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
import datetime
import time
from threading import Thread
from tkinter import Canvas, ALL
import pyttsx3
import random
engine = pyttsx3.init()
engine.setProperty('rate', engine.getProperty('rate') - 10)
def basic_attention_test(label_frame, result_list: list, rounds_num = 1, mode = 'vision', seconds_per_round = 60
, choices = ('1', '2', '3'), interval = 0.2, showing_timeleft_switch = False):
waiting = True
current_input = ''
round_ending = False
a = ''
n = 0
text = ''
showing_timeleft = True
result_list.append((datetime.timedelta(), 0, 0))
def paint(canvas: Canvas, n: int, text: str, text_timeleft='', text_size=32, text_color=(0, 0, 0),
text_floating=False):
canvas.delete(ALL)
canvas.create_text(50, 15, text='进度:' + str(n) + '/' + str(rounds_num), font='微软雅黑 11')
if text_floating:
x_offset = random.randint(-10, 10)
y_offset = random.randint(-10, 10)
else:
x_offset = y_offset = 0
canvas.create_text(canvas.winfo_width() / 2 + x_offset, canvas.winfo_height() / 2 + y_offset
, text=text, font='微软雅黑 ' + str(text_size), fill="#%02x%02x%02x" % text_color)
canvas.create_text(80, 36, text=text_timeleft, font='微软雅黑 11')
def show_timeleft(canvas: Canvas, start_time):
if not showing_timeleft_switch:
return
while showing_timeleft:
paint(canvas, n, text,
text_timeleft='剩余时间:' + str(start_time + datetime.timedelta(seconds=seconds_per_round)
- datetime.datetime.now()).split('.')[0])
time.sleep(0.9)
label_frame.config(text='DPsycho-4注意测试')
canvas = Canvas(label_frame, bg='white')
canvas.pack(fill='both', expand='yes')
canvas.update()
canvas.focus_set()
canvas.bind('<Button-1>', func=lambda event: canvas.focus_set())
def process_keyboard_event(k):
nonlocal waiting, current_input
if k.keysym == 'Return':
waiting = False
if current_input:
return
if k.keysym == '1':
current_input = '1'
elif k.keysym == '2':
current_input = '2'
elif k.keysym == '3':
current_input = '3'
else:
return
if current_input == a and not round_ending:
paint(canvas, n, current_input, text_color=(0, 255, 0), text_floating=False)
else:
paint(canvas, n, current_input, text_color=(255, 0, 0), text_floating=False)
canvas.bind(sequence="<Key>", func=process_keyboard_event)
canvas.focus_set()
if mode == 'listening':
mode_str = '听觉'
else:
mode_str = '视觉'
print('下面开始DPsycho-4' + mode_str + '注意力测试。')
paint(canvas, 0, '下面开始DPsycho-4' + mode_str + '注意力测试。按下Enter键开始测试。', text_size=12)
'''engine.say('下面开始DPsycho4' + mode_str + '注意力测试。按下Enter键开始测试。')
engine.runAndWait()'''
while waiting:
time.sleep(0.1)
record = ''
duration_in_all = datetime.datetime.now() - datetime.datetime.now()
correct_count_in_all = 0
count_in_all = 0
for n in range(1, rounds_num + 1):
round_ending = False
paint(canvas, n, '3')
time.sleep(1)
paint(canvas, n, '2')
time.sleep(1)
paint(canvas, n, '1')
time.sleep(1)
print('进度:' + str(n) + '/' + str(rounds_num))
print('请听……')
testee_answers = []
keys = []
start_time = datetime.datetime.now()
showing_timeleft_thread = Thread(target=show_timeleft, args=(canvas, start_time))
showing_timeleft = True
showing_timeleft_thread.start()
count = 0
while (datetime.datetime.now() - start_time).total_seconds() < seconds_per_round:
a = random.choice(choices)
rgb = random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)
if mode == 'listening':
text = '请听'
paint(canvas, n, text)
engine.say(a)
engine.runAndWait()
else:
text = a
paint(canvas, n, a, text_color=rgb, text_floating=True)
time.sleep(random.uniform(1, 1.5))
time.sleep(interval)
testee_answers.append(current_input)
keys.append(a)
current_input = ''
count += 1
end_time = datetime.datetime.now()
showing_timeleft = False
round_ending = True
correct_count = 0
for index, answer in enumerate(testee_answers):
if keys[index] == answer:
correct_count += 1
duration = end_time - start_time
rec = '时长:' + str(duration) + ' 正确数:' + str(correct_count) + '/' + str(count)\
+ ' 正确率:' + '{:.1%}'.format(correct_count / count)
print(rec)
record += str(n) + '. ' + rec + '\n'''
duration_in_all += duration
correct_count_in_all += correct_count
count_in_all += count
result_list[0] = duration_in_all, correct_count_in_all, count_in_all
paint(canvas, n, rec + '\n\n按下Enter键继续。', text_size=18)
'''engine.say('按下Enter键继续。')
engine.runAndWait()'''
waiting = True
while waiting:
time.sleep(0.1)
rec_in_all = '总计:\n时长:' + str(duration_in_all)\
+ ' 错误次数:' + str(count_in_all - correct_count_in_all) + '/' + str(count_in_all)\
+ ' 错误率:' + '{:.1%}'.format((count_in_all - correct_count_in_all) / count_in_all)\
+ '\n错误发生频率:'\
+ '{:.2%}'.format((count_in_all - correct_count_in_all) / (duration_in_all.total_seconds() / 60)) + '次/min\n'
paint(canvas, rounds_num, rec_in_all, text_size=12)
time.sleep(5)
canvas.destroy()
label_frame.pack_forget()