Skip to content

Instantly share code, notes, and snippets.

@RyanFleck
Created March 27, 2021 13:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RyanFleck/4ee3ba379bc4f232a867eb6f4c521d4c to your computer and use it in GitHub Desktop.
Save RyanFleck/4ee3ba379bc4f232a867eb6f4c521d4c to your computer and use it in GitHub Desktop.
import cv2
import mediapipe as mp
from pprint import pprint
from easy_vector import Vector
from functools import reduce
# https://pypi.org/project/vectors/
mp_drawing = mp.solutions.drawing_utils
mp_hands = mp.solutions.hands
landmark = mp_hands.HandLandmark
def digitExtended(*args, digit="unidentified"):
"""Calculates whether or not the digits of a finger are in a straight line."""
if len(args) < 2:
return 0
# First, normalize by subtracting the first vector from all other vectors.
args = list(map(lambda v: v - args[0], args[1:]))
# Next, add all the vectors together to create a final vector
length = 0
vecTotal = Vector(0,0,0)
for arg in args:
vecTotal = vecTotal + arg
length = length + arg.length
difference = vecTotal.length/length
return (digit, difference > 0.99, length, vecTotal.length)
# For webcam input:
cap = cv2.VideoCapture(0)
with mp_hands.Hands(
min_detection_confidence=0.6,
min_tracking_confidence=0.6) as hands:
while cap.isOpened():
success, image = cap.read()
if not success:
print("Ignoring empty camera frame.")
# If loading a video, use 'break' instead of 'continue'.
continue
# Flip the image horizontally for a later selfie-view display, and convert
# the BGR image to RGB.
image = cv2.cvtColor(cv2.flip(image, 1), cv2.COLOR_BGR2RGB)
# To improve performance, optionally mark the image as not writeable to
# pass by reference.
image.flags.writeable = False
results = hands.process(image)
# Draw the hand annotations on the image.
image.flags.writeable = True
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
if results.multi_hand_landmarks:
for hand_landmarks in results.multi_hand_landmarks:
mp_drawing.draw_landmarks(image, hand_landmarks, mp_hands.HAND_CONNECTIONS)
points = list(map(lambda v: Vector(v.x, v.y, v.z), hand_landmarks.landmark))
# Fingers are 0-thumb 1-index, 2- middle, etc. Creates boolean list of 'extended' or not.
fingers_extended = [
digitExtended(points[1], points[2], points[3], points[4], digit="thumb "),
digitExtended(points[5], points[6], points[7], points[8], digit="index "),
digitExtended(points[9], points[10], points[11], points[12], digit="middle"),
digitExtended(points[13], points[14], points[15], points[16], digit="ring "),
digitExtended(points[17], points[18], points[19], points[20], digit="pinkie"),
]
#pprint(fingers_extended)
debug_str = "\n\n============================"
for finger in fingers_extended:
debug_str = debug_str + f"\n{finger[0]} => {finger[1]}"
print(debug_str)
cv2.imshow('MediaPipe Hands', image)
if cv2.waitKey(5) & 0xFF == 27:
break
cap.release()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment