创建手部检测模型
import cv2
import mediapipe as mp
mp_drawing = mp.solutions.drawing_utils
mp_hands = mp.solutions.hands
hands = mp_hands.Hands(
static_image_mode=True,
max_num_hands=2,
min_detection_confidence=0.75,
min_tracking_confidence=0.5)
1、static_image_mode:默认为False,如果设置为false, 就是把输入看作一个视频流,在检测到手之后对手加了一个目标跟踪(目标检测+跟踪),无需调用另一次检测,直到失去对任何手的跟踪为止。如果设置为True,则手部检测将在每个输入图像上运行(目标检测),非常适合处理一批静态的,可能不相关的图像。(如果检测的是图片就要设置成True)
2、max_num_hands:可以检测到的手的数量最大值,默认是2
3、min_detection_confidence: 手部检测的最小置信度值,大于这个数值被认为是成功的检测。默认为0.5
4、min_tracking_confidence:目标踪模型的最小置信度值,大于这个数值将被视为已成功跟踪的手部,默认为0.5,如果static_image_mode设置为true,则忽略此操作。
输出结果
results = hands.process(frame)
print(results.multi_handedness)
print(results.multi_hand_landmarks)
results.multi_handedness: 包括label和score,label是字符串"Left"或"Right",score是置信度
results.multi_hand_landmarks: 手部21个关键点的位置信息,包括x,y,z 其中x,y是归一化后的坐标。z代表地标深度,以手腕处的深度为原点,值越小,地标就越靠近相机
检测代码
import cv2
import mediapipe as mp
import numpy as np
fight_list = [0, 0, 0, 0, 0]
fight_num = 0
# mp.solutions.drawing_utils用于绘制
mp_drawing = mp.solutions.drawing_utils
# 参数:1、颜色,2、线条粗细,3、点的半径
DrawingSpec_point = mp_drawing.DrawingSpec((0, 255, 0), 1, 1)
DrawingSpec_line = mp_drawing.DrawingSpec((0, 0, 255), 1, 1)
# mp.solutions.hands,是人的手
mp_hands = mp.solutions.hands
# 参数:1、是否检测静态图片,2、手的数量,3、检测阈值,4、跟踪阈值
hands_mode = mp_hands.Hands(max_num_hands=2)
cap = cv2.VideoCapture(1)
while cap.isOpened():
success, image = cap.read()
image_hight, image_width, _ = image.shape
if not success:
print("Ignoring empty camera frame.")
continue
image1 = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# 处理RGB图像
results = hands_mode.process(image1)
# 绘制
if results.multi_hand_landmarks:
for hand_landmarks in results.multi_hand_landmarks:
x0 = hand_landmarks.landmark[mp_hands.HandLandmark.WRIST].x * image_width
y0 = hand_landmarks.landmark[mp_hands.HandLandmark.WRIST].y * image_hight
x3 = hand_landmarks.landmark[mp_hands.HandLandmark.THUMB_IP].x * image_width
y3 = hand_landmarks.landmark[mp_hands.HandLandmark.THUMB_IP].y * image_hight
x4 = hand_landmarks.landmark[mp_hands.HandLandmark.THUMB_TIP].x * image_width
y4 = hand_landmarks.landmark[mp_hands.HandLandmark.THUMB_TIP].y * image_hight
x5 = hand_landmarks.landmark[mp_hands.HandLandmark.INDEX_FINGER_MCP].x * image_width
y5 = hand_landmarks.landmark[mp_hands.HandLandmark.INDEX_FINGER_MCP].y * image_hight
x7 = hand_landmarks.landmark[mp_hands.HandLandmark.INDEX_FINGER_DIP].x * image_width
y7 = hand_landmarks.landmark[mp_hands.HandLandmark.INDEX_FINGER_DIP].y * image_hight
x8 = hand_landmarks.landmark[mp_hands.HandLandmark.INDEX_FINGER_TIP].x * image_width
y8 = hand_landmarks.landmark[mp_hands.HandLandmark.INDEX_FINGER_TIP].y * image_hight
x11 = hand_landmarks.landmark[mp_hands.HandLandmark.MIDDLE_FINGER_DIP].x * image_width
y11 = hand_landmarks.landmark[mp_hands.HandLandmark.MIDDLE_FINGER_DIP].y * image_hight
x12 = hand_landmarks.landmark[mp_hands.HandLandmark.MIDDLE_FINGER_TIP].x * image_width
y12 = hand_landmarks.landmark[mp_hands.HandLandmark.MIDDLE_FINGER_TIP].y * image_hight
x15 = hand_landmarks.landmark[mp_hands.HandLandmark.RING_FINGER_DIP].x * image_width
y15 = hand_landmarks.landmark[mp_hands.HandLandmark.RING_FINGER_DIP].y * image_hight
x16 = hand_landmarks.landmark[mp_hands.HandLandmark.RING_FINGER_TIP].x * image_width
y16 = hand_landmarks.landmark[mp_hands.HandLandmark.RING_FINGER_TIP].y * image_hight
x19 = hand_landmarks.landmark[mp_hands.HandLandmark.PINKY_DIP].x * image_width
y19 = hand_landmarks.landmark[mp_hands.HandLandmark.PINKY_DIP].y * image_hight
x20 = hand_landmarks.landmark[mp_hands.HandLandmark.PINKY_TIP].x * image_width
y20 = hand_landmarks.landmark[mp_hands.HandLandmark.PINKY_TIP].y * image_hight
points = np.array([[x4, y4], [x12, y12], [x20, y20], [x0, y0]], np.float32)
x, y, w, h = cv2.boundingRect(points)
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
# rect = cv2.minAreaRect(points)
# box = cv2.boxPoints(rect) # 获取最小外接矩形的4个顶点坐标(ps: cv2.boxPoints(rect) for OpenCV 3.x)
# box = np.int0(box)
# # 画出来
# cv2.drawContours(image, [box], 0, (255, 0, 0), 1)
# x, y, w, h = cv2.boundingRect((x4, y4), (x8, y8), (x12, y12), (x16, y16), (x20, y20))
distance200 = np.sqrt(abs(x20 - x0) ** 2 + abs(y20 - y0) ** 2)
distance190 = np.sqrt(abs(x19 - x0) ** 2 + abs(y19 - y0) ** 2)
distance160 = np.sqrt(abs(x16 - x0) ** 2 + abs(y16 - y0) ** 2)
distance150 = np.sqrt(abs(x15 - x0) ** 2 + abs(y15 - y0) ** 2)
distance120 = np.sqrt(abs(x12 - x0) ** 2 + abs(y12 - y0) ** 2)
distance110 = np.sqrt(abs(x11 - x0) ** 2 + abs(y11 - y0) ** 2)
distance80 = np.sqrt(abs(x8 - x0) ** 2 + abs(y8 - y0) ** 2)
distance70 = np.sqrt(abs(x7 - x0) ** 2 + abs(y7 - y0) ** 2)
distance40 = np.sqrt(abs(x4 - x0) ** 2 + abs(y4 - y0) ** 2)
distance30 = np.sqrt(abs(x3 - x0) ** 2 + abs(y3 - y0) ** 2)
distance50 = np.sqrt(abs(x5 - x0) ** 2 + abs(y5 - y0) ** 2)
if abs(x4 - x0) < abs(x3 - x0) or abs(y4 - y0) < abs(y3 - y0):
fight_list[0] = 0
else:
fight_list[0] = 1
if distance80 < distance70:
fight_list[1] = 0
else:
fight_list[1] = 1
if distance120 < distance110:
fight_list[2] = 0
else:
fight_list[2] = 1
if distance160 < distance150:
fight_list[3] = 0
else:
fight_list[3] = 1
if distance200 < distance190:
fight_list[4] = 0
else:
fight_list[4] = 1
mp_drawing.draw_landmarks(
image, hand_landmarks, mp_hands.HAND_CONNECTIONS, DrawingSpec_point, DrawingSpec_line)
# print(fight_list)
if fight_list[1] == 1 and fight_list[2] == 0 and fight_list[3] == 0 and fight_list[4] == 0:
fight_num = 1
elif fight_list[1] == 1 and fight_list[2] == 1 and fight_list[3] == 0 and fight_list[4] == 0:
fight_num = 2
elif fight_list[1] == 1 and fight_list[2] == 1 and fight_list[3] == 1 and fight_list[4] == 0:
fight_num = 3
elif fight_list[1] == 1 and fight_list[2] == 1 and fight_list[3] == 1 and fight_list[4] == 1:
fight_num = 4
elif fight_list[1] == 0 and fight_list[2] == 0 and fight_list[3] == 0 and fight_list[4] == 0:
fight_num = 0
print(fight_num)
cv2.imshow('MediaPipe Hands', image)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
hands_mode.close()
cv2.destroyAllWindows()
cap.release()