Created
January 21, 2024 11:52
-
-
Save aliirz/9a235a3973a511c13e04b0ff452b600b to your computer and use it in GitHub Desktop.
Gesture Recognition
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 | |
import mediapipe as mp | |
# Initialize MediaPipe Hands model | |
mp_hands = mp.solutions.hands | |
hands = mp_hands.Hands() | |
# Initialize MediaPipe drawing utilities | |
mp_drawing = mp.solutions.drawing_utils | |
def identify_gesture(hand_landmarks): | |
thumb_tip = hand_landmarks.landmark[mp_hands.HandLandmark.THUMB_TIP] | |
index_tip = hand_landmarks.landmark[mp_hands.HandLandmark.INDEX_FINGER_TIP] | |
middle_tip = hand_landmarks.landmark[mp_hands.HandLandmark.MIDDLE_FINGER_TIP] | |
ring_tip = hand_landmarks.landmark[mp_hands.HandLandmark.RING_FINGER_TIP] | |
pinky_tip = hand_landmarks.landmark[mp_hands.HandLandmark.PINKY_TIP] | |
wrist = hand_landmarks.landmark[mp_hands.HandLandmark.WRIST] | |
# Thumbs Up: Check if thumb is above other fingers and wrist | |
if thumb_tip.y < wrist.y and all(finger.y > wrist.y for finger in [index_tip, middle_tip, ring_tip, pinky_tip]): | |
return "Thumbs Up" | |
# Victory / Peace Sign: Check if index and middle fingers are up, others are down | |
if all(finger.y < wrist.y for finger in [index_tip, middle_tip]) and all(finger.y > wrist.y for finger in [ring_tip, pinky_tip]): | |
return "Victory / Peace" | |
# Hand Open: Check if all finger tips are above the wrist | |
if all(finger.y < wrist.y for finger in [thumb_tip, index_tip, middle_tip, ring_tip, pinky_tip]): | |
return "Hand Open" | |
# Hand Closed: If none of the above | |
return "Hand Closed" | |
# Capture video from the webcam | |
cap = cv2.VideoCapture(0) | |
while cap.isOpened(): | |
ret, frame = cap.read() | |
if not ret: | |
continue | |
# Convert the frame to RGB | |
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) | |
results = hands.process(frame_rgb) | |
# Draw hand landmarks and analyze gestures | |
if results.multi_hand_landmarks: | |
for hand_landmarks in results.multi_hand_landmarks: | |
mp_drawing.draw_landmarks(frame, hand_landmarks, mp_hands.HAND_CONNECTIONS) | |
# Gesture analysis | |
gesture = identify_gesture(hand_landmarks) | |
print(gesture) # Print the recognized gesture | |
# Display the frame | |
cv2.imshow('Gesture Recognition', frame) | |
# Break the loop with 'q' | |
if cv2.waitKey(1) & 0xFF == ord('q'): | |
break | |
# Release the webcam and close OpenCV window | |
cap.release() | |
cv2.destroyAllWindows() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment