我已将facemesh和handmesh实时放入单帧中。
我一直在尝试获取每个面网格顶点的坐标,但遇到了麻烦。
我能够通过
打印出整个坐标print (results.multi_face_landmarks[0])
但它似乎包含整个顶点的坐标。
例如,我只想获取鼻尖的坐标,即 478 个顶点中的第 4 个顶点。
将来我想测量该点与食指指尖的距离,以提醒用户习惯性触摸鼻子。
**无法得知整个坐标是以表格还是列表的形式保存和加载的。
请帮我如何找到坐标。**
这是到目前为止我的代码。我已导入数学以供将来计算距离。
import cv2
import numpy as np
import mediapipe as mp
#################################################################
import math
def calculate_distance(point1, point2):
x1, y1, z1 = point1
x2, y2, z2 = point2
distance = math.sqrt((x2 - x1)**2 + (y2 - y1)**2 + (z2 - z1)**2)
return distance
##################################################################
cap = cv2.VideoCapture(0)
mpDraw = mp.solutions.drawing_utils
mpFaceMesh = mp.solutions.face_mesh
mpHands = mp.solutions.hands
faceMesh = mpFaceMesh.FaceMesh(max_num_faces=2)
handsMesh = mpHands.Hands(max_num_hands=2)
drawSpecFace = mpDraw.DrawingSpec(thickness=1, circle_radius=1)
drawSpecHands = mpDraw.DrawingSpec(thickness=1, circle_radius=1)
both_detected = False # Initialize detection flag
while True:
success, img = cap.read()
imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
results = faceMesh.process(imgRGB)
if results.multi_face_landmarks:
for faceLms in results.multi_face_landmarks:
mpDraw.draw_landmarks(img, faceLms, mpFaceMesh.FACEMESH_CONTOURS, drawSpecFace, drawSpecFace)
face = results.multi_face_landmarks[0];
results2 = handsMesh.process(imgRGB)
if results2.multi_hand_landmarks:
for handLms in results2.multi_hand_landmarks:
mpDraw.draw_landmarks(img, handLms, mpHands.HAND_CONNECTIONS, drawSpecHands, drawSpecHands)
# Check if both face and hands are detected
if results.multi_face_landmarks and results2.multi_hand_landmarks:
both_detected = True
cv2.putText(img, "Why Are Your Hands Near Face?", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (34,139,34), 2, cv2.LINE_AA)
else:
both_detected = False
print (results.multi_face_landmarks[0])
cv2.imshow("Image", img)
cv2.waitKey(1)
这是我到目前为止的结果:
我自己发现了。
结果.multi_face_landmarks[0]
解析出整个坐标的结果。
为了获得顶点的一定协调,
我不得不这样做。
结果.multi_face_landmarks[0].landmark[x]
x 是您可以在面网格映射图像上找到的顶点编号。