当前位置:网站首页>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 .





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)
边栏推荐
- 2022.6.22-----leetcode. five hundred and thirteen
- S5P4418裸机编程的实现(替换2ndboot)
- Redis learning notes - client communication protocol resp
- Redis learning notes - AOF of persistence mechanism
- [SUCTF 2019]CheckIn
- Redis学习笔记—redis-benchmark详解
- [网鼎杯 2020 青龙组]AreUSerialz
- 如何在 FlowUs、Notion 等笔记软件中使用矩阵分析法建立你的思维脚手架
- JSP入门总结
- [MRCTF2020]Ez_bypass
猜你喜欢
Redis学习笔记—数据类型:字符串(string)
Redis learning notes - data type: hash
Redis学习笔记—发布订阅
Redis learning notes - slow query analysis

线性表(SequenceList)的顺序表示与实现----线性结构

Map接口的注意事项

Redis学习笔记—持久化机制之RDB

Flink error --caused by: org apache. calcite. sql. parser. SqlParseException: Encountered “time“

微信小程序:点击按钮频繁切换,重叠自定义markers,但是值不改变

UEFI 源码学习4.1 - PciHostBridgeDxe
随机推荐
[网鼎杯 2020 青龙组]AreUSerialz
Basic use of lua
June 22, 2022: golang multiple choice question, what does the following golang code output? A:3; B:1; C:4; D: Compilation failed.
Pizza ordering design - simple factory model
JSP入门总结
36 krypton launched | cloud native database company "tuoshupai" completed a new round of strategic financing, and the valuation has reached the level of quasi Unicorn
Chain representation and implementation of linklist ---- linear structure
Mysql 数据库入门总结
[GXYCTF2019]BabyUpload
[极客大挑战 2019]HardSQL
多线程初学
在小程序中实现视频通话及互动直播的一种方法
[event registration] sofastack × CSDN jointly held the open source series meetup, which was launched on June 24
Servlet-02 生命周期
扫码登录基本流程
ARM处理器与51单片机程序编写的区别
ucosii(学习笔记)
General paging (1)
Correspondence between three-tier architecture and SSM
One of the 12 balls is different from the others. Provide a balance and find it three times