功能:基于MediaPipe通过PC摄像头获取不同手势控制EP运动
硬件:RoboMaster EP,PC
系统平台:Windows10
开发软件:PyCharm
语言及功能包:python3.8.6,mediapipe库,robomaster sdk
完成效果:
EP-手势识别
软件界面:左边为PC摄像头画面,用于手势识别;右边为EP摄像头画面,监控EP运动。
重点思路:
利用MediaPipe识别手掌的返回信息,标识不同的手势样式,来控制EP旋转和平移(或者其他功能)。
下图引用参考文章,标出了手部关节的21个主要点的说明。
def fingerStatus(self, lmList):
fingerDict = dict()
fingerstaList = list()
id, originx, originy = lmList[0]
if self.results.multi_handedness:
for hand_label in self.results.multi_handedness:
try:
for score, label in enumerate(hand_label.classification):
if lmList[0][2] < lmList[17][2] or lmList[0][2] < lmList[5][2]: # 判断手掌是否横向情况
fingerDict["Updown"] = True
if label.label == "Right": # 横向-右手
fingerDict["Right"] = True
if lmList[0][2] > lmList[1][2]: # 横向-右手-手背
hand_palm = False
if lmList[3][2] > lmList[4][2]:
thumb_sta = True
else:
thumb_sta = False
else: # 横向-右手-手心
hand_palm = True
if lmList[3][2] > lmList[4][2]:
thumb_sta = False
else:
thumb_sta = True
else: # 横向-左手
fingerDict["Right"] = False
if lmList[0][2] > lmList[1][2]: # 横向-左手-手背
hand_palm = False
if lmList[3][2] > lmList[4][2]:
thumb_sta = True
else:
thumb_sta = False
else: # 横向-左手-手心
hand_palm = True
if lmList[3][2] > lmList[4][2]:
thumb_sta = False
else:
thumb_sta = True
else: # 竖向
fingerDict["Updown"] = False
if label.label == "Right": # 竖向-右手
fingerDict["Right"] = True
if lmList[0][1] > lmList[1][1]: # 竖向-右手-手心
hand_palm = True
if lmList[3][1] > lmList[4][1]:
thumb_sta = True
else:
thumb_sta = False
else: # 竖向-右手-手背
hand_palm = False
if lmList[3][1] > lmList[4][1]:
thumb_sta = False
else:
thumb_sta = True
else: # 竖向-左手
fingerDict["Right"] = False
if lmList[0][1] > lmList[1][1]: # 竖向-左手-手背
hand_palm = False
if lmList[3][1] > lmList[4][1]:
thumb_sta = True
else:
thumb_sta = False
else: # 竖向-左手-手心
hand_palm = True
if lmList[3][1] > lmList[4][1]:
thumb_sta = False
else:
thumb_sta = True
except Exception as e:
print(e)
fingerDict["Hand_palm"] = hand_palm
fingerDict["Thumb"] = thumb_sta
fingerstaList.append(thumb_sta)
keypoint_dict = {"indexfinger": (6, 8), "midfinger": (10, 12), "ringfinger": (14, 16), "littlefinger": (18, 20)}
for key, point in keypoint_dict.items():
id, x1, y1 = lmList[point[0]]
id, x2, y2 = lmList[point[1]]
if math.hypot(x2 - originx, y2 - originy) > math.hypot(x1 - originx, y1 - originy):
fingerDict[key] = True
fingerstaList.append(True)
else:
fingerDict[key] = False
fingerstaList.append(False)
return fingerDict, fingerstaList
上面的函数返回的字典结果为:包含手掌方向、左右手、手心方向及5个手指曲伸状态,例如:{'Updown': False, 'Right': False, 'Hand_palm': True, 'Thumb': True, 'indexfinger': True, 'midfinger': False, 'ringfinger': False, 'littlefinger': False}, 通过以上四类关键信息,得出手势。
参考链接:基于MediaPipe、opencv和python实现21点手势识别和手势运动方向识别_在于探索的博客-CSDN博客_mediapipe 手势识别