当前位置:网站首页>Gesture recognition based on mediapipe

Gesture recognition based on mediapipe

2022-06-23 09:22:00 Crispy eggplant strips

be based on mediapipe Recognize the number corresponding to the gesture ( One 、 Two 、 3、 ... and 、 Four 、 5、 ... and ).
mediapipe Its official website
General idea :mediapipe You can identify the key points of the palm , My idea is to identify whether a single finger is bent , Then, according to the bending degree of the five fingers, the number corresponding to the gesture is judged .

How to judge whether a single finger is bent ?
I am based on the relative positions of the four key points of the fingers . For example, identify the bending degree of the thumb , Calculate the point first 4 Sum point 3 The angle of a, Calculate a little more 2 Sum point 1 The angle of b, Finally, calculate the angle a And angle b The absolute value of the difference between , If the absolute value is less than 12 degree , Think the thumb is straight . The same goes for other fingers .

How to judge the corresponding number of the gesture according to the bending degree of the five fingers ?
Suppose you know how much five fingers bend , If all five fingers are straight , Then the gesture is the number five ; If index finger 、 Middle finger 、 ring finger 、 Keep your fingers straight , And the thumb bends , The gesture is the number four . Other gestures are the same .
 Insert picture description here
 Insert picture description here
 Insert picture description here  Insert picture description here
 Insert picture description here
 Insert picture description here

The code is as follows :

import cv2
import mediapipe as mp
import time
import math


#  Judge whether the finger is straight according to the four joints of the finger 
def get_angleError(point_4,point_3,point_2,point_1):
    try:
        point_4_cx, point_4_cy = int(point_4.x * w), int(point_4.y * h)
        point_3_cx, point_3_cy = int(point_3.x * w), int(point_3.y * h)
        point_2_cx, point_2_cy = int(point_2.x * w), int(point_2.y * h)
        point_1_cx, point_1_cy = int(point_1.x * w), int(point_1.y * h)

        angle_1 = math.degrees(math.atan((point_3_cx - point_4_cx) / (point_3_cy - point_4_cy)))
        angle_2 = math.degrees(math.atan((point_1_cx - point_2_cx) / (point_1_cy - point_2_cy)))
        angle_error = abs(angle_1 - angle_2)
        if angle_error<12:
            isStraight = 1
        else:
            isStraight = 0
    except:
        angle_error = 1000
        isStraight = 0

    return angle_error, isStraight


#  Recognize gestures according to the straightness of five fingers 
def getGesture(isStraight_list):
    if isStraight_list[0]==0 and isStraight_list[1]==1 and isStraight_list[2]==0 and isStraight_list[3]==0 and isStraight_list[4]==0:
        gesture = "one"
    elif isStraight_list[0]==0 and isStraight_list[1]==1 and isStraight_list[2]==1 and isStraight_list[3]==0 and isStraight_list[4]==0:
        gesture = "two"
    elif isStraight_list[0]==0 and isStraight_list[1]==0 and isStraight_list[2]==1 and isStraight_list[3]==1 and isStraight_list[4]==1:
        gesture = "three"
    elif isStraight_list[0]==0 and isStraight_list[1]==1 and isStraight_list[2]==1 and isStraight_list[3]==1 and isStraight_list[4]==1:
        gesture = "four"
    elif isStraight_list[0]==1 and isStraight_list[1]==1 and isStraight_list[2]==1 and isStraight_list[3]==1 and isStraight_list[4]==1:
        gesture = "five"
    else:
        gesture = "None"

    return gesture

cap = cv2.VideoCapture(0)
mpHands = mp.solutions.hands
hands = mpHands.Hands(static_image_mode=False,
                      max_num_hands=2,
                      min_detection_confidence=0.5,
                      min_tracking_confidence=0.5)

mpDraw = mp.solutions.drawing_utils
pTime = 0
cTime = 0
while True:
    success, img = cap.read()
    img = cv2.flip(img, 1)
    imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    results = hands.process(imgRGB)
    #print(results.multi_hand_landmarks)
    if results.multi_hand_landmarks:

        for handLms in results.multi_hand_landmarks:
            for id, lm in enumerate(handLms.landmark):
                #print(id,lm)
                h, w, c = img.shape
                cx, cy = int(lm.x *w), int(lm.y*h)
                # print(cx,cy)
                #if id ==0:
                cv2.circle(img, (cx,cy), 3, (255,0,255), cv2.FILLED)
            mpDraw.draw_landmarks(img, handLms, mpHands.HAND_CONNECTIONS)

        #  Judge the direction of thumb gesture :
        isStraight_list = []
        point_4 = handLms.landmark[4]
        point_3 = handLms.landmark[3]
        point_2 = handLms.landmark[2]
        point_1 = handLms.landmark[1]
        angle_error_1, isStraight_1 = get_angleError(point_4,point_3,point_2,point_1)
        print("isStraight_1:",isStraight_1)
        isStraight_list.append(isStraight_1)

        #  Judging the direction of forefinger gesture :
        point_4 = handLms.landmark[8]
        point_3 = handLms.landmark[7]
        point_2 = handLms.landmark[6]
        point_1 = handLms.landmark[5]
        angle_error_2, isStraight_2 = get_angleError(point_4,point_3,point_2,point_1)
        print("isStraight_2:",isStraight_2)
        isStraight_list.append(isStraight_2)

        #  Judge the direction of the middle finger gesture :
        point_4 = handLms.landmark[12]
        point_3 = handLms.landmark[11]
        point_2 = handLms.landmark[10]
        point_1 = handLms.landmark[9]
        angle_error_3, isStraight_3 = get_angleError(point_4,point_3,point_2,point_1)
        print("isStraight_3:",isStraight_3)
        isStraight_list.append(isStraight_3)

        #  Judge the direction of ring finger gesture :
        point_4 = handLms.landmark[16]
        point_3 = handLms.landmark[15]
        point_2 = handLms.landmark[14]
        point_1 = handLms.landmark[13]
        angle_error_4, isStraight_4 = get_angleError(point_4,point_3,point_2,point_1)
        print("isStraight_4:",isStraight_4)
        isStraight_list.append(isStraight_4)

        #  Judge the direction of the little finger gesture :
        point_4 = handLms.landmark[20]
        point_3 = handLms.landmark[19]
        point_2 = handLms.landmark[18]
        point_1 = handLms.landmark[17]
        angle_error_5, isStraight_5 = get_angleError(point_4,point_3,point_2,point_1)
        print("isStraight_5:",isStraight_5)
        isStraight_list.append(isStraight_5)

        #  Judge the number corresponding to the gesture according to the straightening degree of the five fingers 
        gesture  = getGesture(isStraight_list)
        print("gesture:",gesture)

        cv2.putText(img, gesture, (10, 100), cv2.FONT_HERSHEY_PLAIN, 3, (255, 0, 255), 3)


    cTime = time.time()
    fps = 1/(cTime-pTime)
    pTime = cTime
    # cv2.putText(img,str(int(fps)), (10,70), cv2.FONT_HERSHEY_PLAIN, 3, (255,0,255), 3)
    cv2.imshow("Image", img)
    cv2.waitKey(1)
原网站

版权声明
本文为[Crispy eggplant strips]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206230909339840.html