-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame_with_tts.py
More file actions
242 lines (193 loc) · 7.55 KB
/
game_with_tts.py
File metadata and controls
242 lines (193 loc) · 7.55 KB
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 cv2 as cv
import numpy as np
import mediapipe as mp
import math
import serial
import time
import vlc
ser=serial.Serial('com7',9600)
mp_face_mesh = mp.solutions.face_mesh
flag = True
write_flag = True
music_flag = True
NOSE_LANDMARK = 1
dest_x, dest_y, dest_y2 = 200, 100, 300
dest_x, dest_y, dest_y2 = 200, 100, 300
Bound= 30
Ki=0.001
Kp=40
dest = "90,45,135"
cap = cv.VideoCapture(0)
blue = (255, 0, 0)
green= (0, 255, 0)
red= (0, 0, 255)
white= (255, 255, 255)
def player_generator(filename):
instance = vlc.Instance()
player = instance.media_player_new()
media = instance.media_new(filename)
player.set_media(media)
return player
def draw_text(img, text, x, y,text_color):
font = cv.FONT_HERSHEY_SIMPLEX
font_scale = 1
font_thickness = 2
text_color=text_color
text_size, _ = cv.getTextSize(text, font, font_scale, font_thickness)
text_w, text_h = text_size
offset = 5
cv.putText(img, text, (x, y + text_h + font_scale - 1), font, font_scale, text_color, font_thickness)
def convert(x, max, min, dest_max, dest_min):
converted_x = dest_min + (x-min) * ((dest_max-dest_min) / (max - min))
return converted_x
with mp_face_mesh.FaceMesh(
max_num_faces=1,
refine_landmarks=True,
min_detection_confidence=0.5,
min_tracking_confidence=0.5
) as face_mesh:
start_flag = False
count = 0
sumx = 0
sumy = 0
player_generator("./music/audio_0_게임을_시작해볼까요_.mp3").play()
time.sleep(1)
start = time.time()
while True:
check = True
ret, frame = cap.read()
if not ret:
break
frame = cv.flip(frame, 1)
rgb_frame = cv.cvtColor(frame, cv.COLOR_BGR2RGB)
img_h, img_w = frame.shape[:2]
results = face_mesh.process(rgb_frame)
mask = np.zeros((img_h, img_w), dtype=np.uint8)
if results.multi_face_landmarks:
mesh_points=np.array([np.multiply([p.x, p.y], [img_w, img_h]).astype(int)
for p in results.multi_face_landmarks[0].landmark])
x, y = mesh_points[1]
left_nose_x, left_nose_y = mesh_points[331]
right_nose_x, right_nose_y = mesh_points[102]
x = x - img_w/2
y = -y + img_h/2
left_nose_x = left_nose_x - img_w/2
left_nose_y = -left_nose_y + img_h/2
right_nose_x = right_nose_x - img_w/2
right_nose_y = -right_nose_y + img_h/2
if(x > Bound and dest_x < 380):
if(sumx < 10000):
sumx += x
elif(sumx >= 10000):
sumx = 10000
dest_x=dest_x+int(x/Kp)+sumx*Ki
check = False
elif(x< -Bound and dest_x > 20):
if(sumx > -10000):
sumx += x
elif(sumx <=-10000):
sumx = -10000
dest_x=dest_x+int(x/Kp)+sumx*Ki
check = False
if(y> Bound and dest_y < 380):
if(sumy < 10000):
sumy += y
elif(sumy>=10000):
sumy = 10000
dest_y=dest_y+int(y/Kp)+sumy*Ki
check = False
elif(y < -Bound and dest_y > 20):
if(sumy > -10000):
sumy += y
elif(sumy<=-10000):
sumy = -10000
dest_y=dest_y+int(y/Kp)+sumy*Ki
check = False
dest_y2 = 200+dest_y
if dest_y2 > 300:
dest_y2 = 300
elif dest_y2 < 150:
dest_y2 = 150
dest = str(int(dest_x)) +" " + str(int(dest_y)) +" "+ str(int(dest_y2))
print(dest, sumx, sumy)
ser.write(dest.encode('utf-8'))
curr_time = time.time()
time_passed = curr_time - start
if(check):
text_color = green
else:
text_color = red
if check:
if(start_flag):
draw_text(frame,"IN",480,40,text_color)
if(time_passed < 13):
frame = cv.rectangle(frame,(310,250),(330,230),(0,255,0),3)
if(start_flag):
count = count + 1
else:
if(start_flag):
draw_text(frame,"OUT",480,40,text_color)
if(time_passed < 13):
frame = cv.rectangle(frame,(310,250),(330,230),(0,0,255),3)
if(time_passed < 1):
draw_text(frame,"3",310,230,text_color)
elif(time_passed < 2):
draw_text(frame,"2",310,230,text_color)
elif(time_passed<3):
draw_text(frame,"1",310,230,text_color)
else:
start_flag = True
if(time_passed < 4 and start_flag):
draw_text(frame,"10",480,100,text_color)
elif(time_passed < 5 and start_flag):
draw_text(frame,"9",480,100,text_color)
elif(time_passed < 6 and start_flag):
draw_text(frame,"8",480,100,text_color)
elif(time_passed < 7 and start_flag):
draw_text(frame,"7",480,100,text_color)
elif(time_passed < 8 and start_flag):
draw_text(frame,"6",480,100,text_color)
elif(time_passed < 9 and start_flag):
draw_text(frame,"5",480,100,text_color)
elif(time_passed < 10 and start_flag):
if(music_flag):
music_flag = False
player_generator("./music/audio_1_잘하고_있습니다.mp3").play()
draw_text(frame,"4",480,100,text_color)
elif(time_passed < 11 and start_flag):
draw_text(frame,"3",480,100,text_color)
elif(time_passed < 12 and start_flag):
draw_text(frame,"2",480,100,text_color)
elif(time_passed < 13 and start_flag):
draw_text(frame,"1",480,100,text_color)
elif(time_passed >= 13 and start_flag):
stayed_time = count/30
stayed_time = round(stayed_time,3)
draw_text(frame,"you stayed " + str(stayed_time)+ " seconds",100,140,red)
if(write_flag):
if(stayed_time >= 5):
player_generator("./music/audio_2_굉장한데요_.mp3").play()
else:
player_generator("./music/audio_3_아쉽지만_다음_기회에___.mp3").play()
write_flag = False
with open("result.txt", "a") as file:
file.write(str(stayed_time) + "\n")
file.close()
with open("result.txt", "r") as file:
lines = [float(line.strip()) for line in file]
lines.sort(reverse=True)
with open("result.txt", "w") as file:
for value in lines:
file.write(str(value) + "\n")
with open("result.txt", "r") as file:
lines = [float(line.strip()) for line in file]
ranking = 1
for values in lines[:5]:
draw_text(frame,str(ranking) +". " + str(values)+ " seconds",100,160 + 40 * ranking,red)
ranking = ranking + 1
cv.imshow('img', frame)
key = cv.waitKey(1)
if key ==ord('q'):
break
cap.release()
cv.destroyAllWindows()