-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnewScript.py
185 lines (140 loc) · 4.23 KB
/
newScript.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
import mediapipe as mp
import cv2
import numpy as np
import time
from playsound import playsound
flg = False
while True:
cap = cv2.VideoCapture(0)
cPos = 0
startT = 0
endT = 0
userSum = 0
dur = 0
isAlive = 1
isInit = False
cStart, cEnd = 0,0
isCinit = False
tempSum = 0
winner = 0
inFrame = 0
inFramecheck = False
thresh = 180
ctimer = time.time()
# def countdown():
# t=3
# while t:
# mins, secs = divmod(t, 60)
# timer = '{:02d}:{:02d}'.format(mins, secs)
# print(timer, end="\r")
# cv2.putText(mainWin, timer, (100,400), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0,255,0), 4)
# time.sleep(1)
# t -= 1
def calc_sum(landmarkList):
tsum = 0
for i in range(11, 33):
tsum += (landmarkList[i].x * 480)
return tsum
def calc_dist(landmarkList):
return (landmarkList[28].y*640 - landmarkList[24].y*640)
def isVisible(landmarkList):
if (landmarkList[28].visibility > 0.7) and (landmarkList[24].visibility > 0.7):
return True
return False
mp_pose = mp.solutions.pose
pose = mp_pose.Pose()
drawing = mp.solutions.drawing_utils
im1 = cv2.imread('im1.png')
im2 = cv2.imread('im2.png')
currWindow = im1
while True:
_, frm = cap.read()
rgb = cv2.cvtColor(frm, cv2.COLOR_BGR2RGB)
res = pose.process(rgb)
frm = cv2.blur(frm, (5,5))
drawing.draw_landmarks(frm, res.pose_landmarks, mp_pose.POSE_CONNECTIONS)
if not(inFramecheck):
try:
if isVisible(res.pose_landmarks.landmark):
inFrame = 1
inFramecheck = True
else:
inFrame = 0
except:
print("You are not visible at all")
timer = int(180 - (time.time() - ctimer))
if inFrame == 1:
if not(isInit):
playsound('greenLight.mp3')
currWindow = im1
startT = time.time()
endT = startT
dur = np.random.randint(1, 5)
isInit = True
if (endT - startT) <= dur:
try:
m = calc_dist(res.pose_landmarks.landmark)
if m < thresh:
cPos += 1
print("current progress is : ", cPos)
except:
print("Not visible")
endT = time.time()
else:
if cPos >= 100:
print("WINNER")
winner = 1
else:
if not(isCinit):
isCinit = True
cStart = time.time()
cEnd = cStart
currWindow = im2
playsound('redLight.mp3')
userSum = calc_sum(res.pose_landmarks.landmark)
# time.sleep(1)
if (cEnd - cStart) <= 3:
tempSum = calc_sum(res.pose_landmarks.landmark)
cEnd = time.time()
if abs(tempSum - userSum) > 150:
print("DEAD ", abs(tempSum - userSum))
isAlive = 0
else:
isInit = False
isCinit = False
cv2.circle(currWindow, ((55 + 6*cPos),280), 15, (0,0,255), -1)
mainWin = np.concatenate((cv2.resize(frm, (800,400)), currWindow), axis=0)
cv2.putText(mainWin, str(timer), (100,400), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0,255,0), 4)
cv2.namedWindow("Main Window", cv2.WINDOW_FREERATIO)
cv2.setWindowProperty("Main Window", cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
cv2.imshow("Main Window", mainWin)
#cv2.imshow("window", frm)
#cv2.imshow("light", currWindow)
else:
cv2.putText(frm, "Please Make sure you are fully in frame", (20,200), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0,255,0), 4)
cv2.namedWindow("window", cv2.WINDOW_FREERATIO)
cv2.setWindowProperty("window", cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
cv2.imshow("window", frm)
if cv2.waitKey(1) == 27:
flg = True
break
if cv2.waitKey(1) == 27 or isAlive == 0 or winner == 1 or timer<=0:
cv2.destroyAllWindows()
cap.release()
break
if flg:
break
frm = cv2.blur(frm, (5,5))
if isAlive == 0 or timer == 0:
cv2.putText(frm, "Player Eliminated", (50,200), cv2.FONT_HERSHEY_SIMPLEX, 2, (0,0,255), 4)
cv2.namedWindow("Main Window", cv2.WINDOW_FREERATIO)
cv2.setWindowProperty("Main Window", cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
cv2.imshow("Main Window", frm)
playsound('eliminated.mp3')
if winner == 1:
cv2.putText(frm, "You are Winner", (40,200), cv2.FONT_HERSHEY_SIMPLEX, 2, (150,255,0), 4)
cv2.namedWindow("Main Window", cv2.WINDOW_FREERATIO)
cv2.setWindowProperty("Main Window", cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
cv2.imshow("Main Window", frm)
cv2.waitKey(0)
cv2.destroyAllWindows()