本系列文章介绍各类图像识别的使用方法,运用这些技巧采集更多的原始数据,训练我们的AI模型。本系列文章要有Python和OpenCV基础。
MediaPipe介绍
MediaPipe 是一款由 Google Research 开发并开源的多媒体机器学习模型应用框架。集成人脸识别,人体姿态评估,人手检测模型。MediaPipe安装也是很方便的,在控制端输出pip install MediaPipe即可。
默认情况下MediaPipe是基于CPU检测的,如果要GPU检测,那就要自己重新编译支持GPU的MediaPipe了。官网:https://google.github.io/mediapipe/
MediaPipe通过检测手掌上21个关键点来判断手掌的姿态
手掌21个检测点程序
import cv2
import mediapipe as mp
if __name__ == '__main__':
cap = cv2.VideoCapture(0);
mpHands = mp.solutions.hands
hands = mpHands.Hands()
mpDraw = mp.solutions.drawing_utils
while True:
ret,img=cap.read()
if ret:
img = cv2.flip(img,1)
imgrgb = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
result = hands.process(imgrgb)
#判断左右手的Index号
if result.multi_handedness:
for hand_label in result.multi_handedness:
print(hand_label)
#获取每个手掌21个点的信息
if result.multi_hand_landmarks:
for hand_no, hand_landmarks in enumerate(result.multi_hand_landmarks):
print("HAND Index:",hand_no)
print("-----------------------")
for i in range(0,len(mpHands.HandLandmark)):
print(i,mpHands.HandLandmark(i).name)
print(hand_landmarks.landmark[mpHands.HandLandmark(i).value])
#绘制手掌21个点
if result.multi_hand_landmarks:
for handLms in result.multi_hand_landmarks:
mpDraw.draw_landmarks(img,handLms)
cv2.imshow('img',img)
if cv2.waitKey(1) == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
运行结果