1
1
import random
2
2
import time
3
+ import os .path
3
4
4
5
import cv2
6
+ import mediapipe as mp
5
7
import mediapipe .python .solutions .drawing_utils as mp_drawing
6
8
import mediapipe .python .solutions .face_detection as mp_face_detection
7
9
import mediapipe .python .solutions .hands as mp_hands
8
10
import numpy as np
9
11
12
+
10
13
SHOT_DELAY = 3
11
14
SHOT_SIGNAL_TIME = 0.4
12
15
SHOT_FIRE_TRACER_TIME = 0.2
@@ -100,6 +103,8 @@ def render(self, image):
100
103
)
101
104
102
105
106
+ game_is_on = False
107
+
103
108
hands = mp_hands .Hands (
104
109
static_image_mode = False ,
105
110
max_num_hands = 1 ,
@@ -112,13 +117,30 @@ def render(self, image):
112
117
min_detection_confidence = 0.5 ,
113
118
)
114
119
120
+ # Initialize Hand Gesture Recognizer
121
+ BaseOptions = mp .tasks .BaseOptions
122
+ GestureRecognizer = mp .tasks .vision .GestureRecognizer
123
+ GestureRecognizerOptions = mp .tasks .vision .GestureRecognizerOptions
124
+ GestureRecognizerResult = mp .tasks .vision .GestureRecognizerResult
125
+ VisionRunningMode = mp .tasks .vision .RunningMode
126
+
127
+
128
+ options = GestureRecognizerOptions (
129
+ base_options = BaseOptions (model_asset_path = "gesture_recognizer.task" ),
130
+ running_mode = VisionRunningMode .LIVE_STREAM ,
131
+ result_callback = lambda result , image , timestamp_ms : process_results (result ),
132
+ )
133
+ recognizer = GestureRecognizer .create_from_options (options )
134
+ recognizer_is_on = True
135
+
115
136
cap = cv2 .VideoCapture (0 ) # 0 for default camera
116
137
117
138
# initializing things for the chasing rectangle
118
139
rect_list = []
119
140
120
- previous_loop_time = time .time ()
121
- previous_shot_time = previous_loop_time
141
+ start_time = time .time ()
142
+ previous_loop_time = start_time
143
+ previous_shot_time = start_time
122
144
previous_shot_tracer = None
123
145
previous_damaged_time = None
124
146
previous_spawn_time = None
@@ -127,9 +149,19 @@ def render(self, image):
127
149
health = INITIAL_HEALTH
128
150
score = 0
129
151
130
- game_is_on = False
152
+
153
+ def process_results (result ):
154
+ if result .gestures :
155
+ for gesture in result .gestures :
156
+ print (gesture )
157
+ if gesture [0 ].category_name == "Thumb_Up" and gesture [0 ].score > 0.7 :
158
+ print ("Thumbs_Up" )
159
+ global game_is_on
160
+ game_is_on = True
161
+
131
162
132
163
while cap .isOpened ():
164
+ print (game_is_on )
133
165
current_time = time .time ()
134
166
delta_time = current_time - previous_loop_time
135
167
previous_loop_time = current_time
@@ -149,6 +181,14 @@ def render(self, image):
149
181
image = cv2 .cvtColor (image , cv2 .COLOR_RGB2BGR )
150
182
h , w , c = image .shape
151
183
184
+ if game_is_on :
185
+ if recognizer_is_on :
186
+ recognizer .close ()
187
+ recognizer_is_on = False
188
+ else :
189
+ mp_image = mp .Image (image_format = mp .ImageFormat .SRGB , data = image )
190
+ recognizer .recognize_async (mp_image , int ((current_time - start_time ) * 1000 ))
191
+
152
192
if game_is_on and (
153
193
previous_spawn_time is None
154
194
or spawn_delay is None
0 commit comments