Created
March 28, 2021 15:24
-
-
Save RyanFleck/d48c786b04a7f63db093593fb972136c to your computer and use it in GitHub Desktop.
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 | |
from pprint import pprint | |
from easy_vector import Vector | |
from functools import reduce | |
from time import sleep | |
# 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", extended_value_ratio=0.3): | |
"""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. | |
args2 = list(map(lambda v: v - args[0], args)) | |
# Subtract the accumulated previous points from each point to isolate each vector. | |
points = [] | |
acc = Vector(0,0,0) | |
for index in range(len(args2)): | |
vec = args2[index] - acc | |
points.append(vec) | |
acc = acc + args2[index] | |
# print(f"Point {index} : {vec}") | |
#pprint(points) | |
# Next, add all the vectors together to create a final vector | |
length = 0 | |
vecTotal = Vector(0,0,0) | |
for point in points: | |
length = length + point.length | |
vecTotal = args[len(args)-1] - args[0] | |
#pprint(length) | |
#pprint(vecTotal) | |
difference = vecTotal.length/length | |
#pprint(difference) | |
return (digit, difference > extended_value_ratio, length, vecTotal.length, difference) | |
# 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 ", extended_value_ratio=1.0), | |
digitExtended(points[0], points[5], points[6], points[7], points[8], digit="index "), | |
digitExtended(points[0], points[9], points[10], points[11], points[12], digit="middle"), | |
digitExtended(points[0], points[13], points[14], points[15], points[16], digit="ring "), | |
digitExtended(points[0], 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]} : len: {finger[2]:.2f} vecLen: {finger[3]:.2f} diff: {finger[4]:.2f}" | |
print(debug_str) | |
#sleep(0.2) | |
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