本文主要是介绍【1】MediaPipe手部识别示例代码,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
上回说到,mediapipe如何安装,这回我们来看看mediapipe是如何识别手的位置和返回坐标的。
首先我们调用mediapipe库
import mediapipe as mp
import cv2
import numpy as np
之后我们使用此代码进行识别
mp_drawing = mp.solutions.drawing_utils
mp_drawing_styles = mp.solutions.drawing_utils.DrawingSpec
mp_hands = mp.solutions.hands
IMAGE_FILES = ["p2.jpg"]
with mp_hands.Hands(static_image_mode=True,max_num_hands=2,min_detection_confidence=0.5) as hands:for idx, file in enumerate(IMAGE_FILES):image = cv2.flip(cv2.imread(file), 1)results = hands.process(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))print('Handedness:', results.multi_handedness)if not results.multi_hand_landmarks:continueimage_height, image_width, _ = image.shapeannotated_image = image.copy()for hand_landmarks in results.multi_hand_landmarks:print(f'#5: (',f'{hand_landmarks.landmark[mp_hands.HandLandmark.INDEX_FINGER_MCP].x * image_width}, 'f'{hand_landmarks.landmark[mp_hands.HandLandmark.INDEX_FINGER_MCP].y * image_height})\n'f'#6: (',f'{hand_landmarks.landmark[mp_hands.HandLandmark.INDEX_FINGER_PIP].x * image_width}, 'f'{hand_landmarks.landmark[mp_hands.HandLandmark.INDEX_FINGER_PIP].y * image_height})\n'f'#7: (',f'{hand_landmarks.landmark[mp_hands.HandLandmark.INDEX_FINGER_DIP].x * image_width}, 'f'{hand_landmarks.landmark[mp_hands.HandLandmark.INDEX_FINGER_DIP].y * image_height})\n'f'#8: (',f'{hand_landmarks.landmark[mp_hands.HandLandmark.INDEX_FINGER_TIP].x * image_width}, 'f'{hand_landmarks.landmark[mp_hands.HandLandmark.INDEX_FINGER_TIP].y * image_height})\n'f'#12: (',f'{hand_landmarks.landmark[mp_hands.HandLandmark.MIDDLE_FINGER_TIP].x * image_width}, 'f'{hand_landmarks.landmark[mp_hands.HandLandmark.MIDDLE_FINGER_TIP].y * image_height})\n'f'#16: (',f'{hand_landmarks.landmark[mp_hands.HandLandmark.RING_FINGER_TIP].x * image_width}, 'f'{hand_landmarks.landmark[mp_hands.HandLandmark.RING_FINGER_TIP].y * image_height})\n'f'#20: (',f'{hand_landmarks.landmark[mp_hands.HandLandmark.PINKY_TIP].x * image_width}, 'f'{hand_landmarks.landmark[mp_hands.HandLandmark.PINKY_TIP].y * image_height})\n')mp_drawing.draw_landmarks(annotated_image,hand_landmarks,mp_hands.HAND_CONNECTIONS,mp_drawing_styles(),mp_drawing_styles())cv2.imwrite('annotated_image' + str(idx) + '.png', cv2.flip(annotated_image, 1))print(cv2.imread("hand.jpg").shape)
可能有朋友要问,怎么能知道返回的是哪个点的位置呢,我们看一看它手指关节点对照表,然后就可以返回它们的x,y,z的值了。但是要注意,mediapipe的坐标值都是经过归一化的,如果需要绝对坐标,需要分别对应地乘上图片的宽和高。
此图是示例代码返回的数值,因为示例图片为某非公开项目的图片,因此不予公布效果图。
这篇关于【1】MediaPipe手部识别示例代码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!