Skip to content

Commit fb60cec

Browse files
author
Raven
committed
add(#6): gesture recognizer to identify "Thumb Up" to start game.
1 parent f694376 commit fb60cec

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

gesture_recognizer.task

7.99 MB
Binary file not shown.

main.py

+43-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import random
22
import time
3+
import os.path
34

45
import cv2
6+
import mediapipe as mp
57
import mediapipe.python.solutions.drawing_utils as mp_drawing
68
import mediapipe.python.solutions.face_detection as mp_face_detection
79
import mediapipe.python.solutions.hands as mp_hands
810
import numpy as np
911

12+
1013
SHOT_DELAY = 3
1114
SHOT_SIGNAL_TIME = 0.4
1215
SHOT_FIRE_TRACER_TIME = 0.2
@@ -100,6 +103,8 @@ def render(self, image):
100103
)
101104

102105

106+
game_is_on = False
107+
103108
hands = mp_hands.Hands(
104109
static_image_mode=False,
105110
max_num_hands=1,
@@ -112,13 +117,30 @@ def render(self, image):
112117
min_detection_confidence=0.5,
113118
)
114119

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+
115136
cap = cv2.VideoCapture(0) # 0 for default camera
116137

117138
# initializing things for the chasing rectangle
118139
rect_list = []
119140

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
122144
previous_shot_tracer = None
123145
previous_damaged_time = None
124146
previous_spawn_time = None
@@ -127,9 +149,19 @@ def render(self, image):
127149
health = INITIAL_HEALTH
128150
score = 0
129151

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+
131162

132163
while cap.isOpened():
164+
print(game_is_on)
133165
current_time = time.time()
134166
delta_time = current_time - previous_loop_time
135167
previous_loop_time = current_time
@@ -149,6 +181,14 @@ def render(self, image):
149181
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
150182
h, w, c = image.shape
151183

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+
152192
if game_is_on and (
153193
previous_spawn_time is None
154194
or spawn_delay is None

0 commit comments

Comments
 (0)