Skip to content

Instantly share code, notes, and snippets.

@pgmrDohan
Last active July 31, 2024 02:05
Show Gist options
  • Select an option

  • Save pgmrDohan/a94662e73b4a5f86bf545f1afed93dda to your computer and use it in GitHub Desktop.

Select an option

Save pgmrDohan/a94662e73b4a5f86bf545f1afed93dda to your computer and use it in GitHub Desktop.
import mediapipe as mp # type: ignore
BaseOptions = mp.tasks.BaseOptions
GestureRecognizer = mp.tasks.vision.GestureRecognizer
GestureRecognizerOptions = mp.tasks.vision.GestureRecognizerOptions
VisionRunningMode = mp.tasks.vision.RunningMode
options =GestureRecognizerOptions(
base_options=BaseOptions(model_asset_path='gesture_recognizer.task'),
running_mode=VisionRunningMode.VIDEO)
recognizer = GestureRecognizer.create_from_options(options)
import cv2
def opencv_to_mediapipe_image(cv_image):
return mp.Image(image_format=mp.ImageFormat.SRGB, data=cv_image)
cap = cv2.VideoCapture(0)
font = cv2.FONT_HERSHEY_SIMPLEX
fps = cap.get(cv2.CAP_PROP_FPS)
timestamps = [cap.get(cv2.CAP_PROP_POS_MSEC)]
calc_timestamps = [0.0]
while(cap.isOpened()):
ret,frame = cap.read()
if ret:
timestamps.append(cap.get(cv2.CAP_PROP_POS_MSEC))
calc_timestamps.append(calc_timestamps[-1] + 1000/fps)
recognition_result = recognizer.recognize_for_video(opencv_to_mediapipe_image(frame), int(calc_timestamps[-1]))
try:
top_gesture = recognition_result.gestures[0][0].category_name
frame = cv2.putText(frame, str(top_gesture), (10, int(cap.get(4)-10)), font, 1, (255, 0, 0), 5, cv2.LINE_AA)
except:
pass
f = open("test.txt", "a")
f.write(str(recognition_result))
f.close()
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
cap.release()
cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment