首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在mediapipe中获取面部mash地标的坐标?

如何在mediapipe中获取面部mash地标的坐标?
EN

Stack Overflow用户
提问于 2021-04-18 02:50:19
回答 4查看 6.6K关注 0票数 4

我正在尝试获取一个列表,其中包含带有mediapipe脸部混搭的标志性线缆-例如: Landmark6:(0.36116672,0.93204623,0.0019629495)...

我找不到这样做的方法,我会请求你的帮助。(在python中)

EN

回答 4

Stack Overflow用户

发布于 2021-04-21 02:22:44

Mediapipe的接口比您公开看到的大多数模型都要复杂。但是你想要的东西无论如何都是很容易实现的。

代码语言:javascript
运行
复制
import cv2
import mediapipe as mp
mp_drawing = mp.solutions.drawing_utils
mp_face_mesh = mp.solutions.face_mesh

file_list = ['test.png']
# For static images:
drawing_spec = mp_drawing.DrawingSpec(thickness=1, circle_radius=1)
with mp_face_mesh.FaceMesh(
    static_image_mode=True,
    min_detection_confidence=0.5) as face_mesh:
  for idx, file in enumerate(file_list):
    image = cv2.imread(file)
    # Convert the BGR image to RGB before processing.
    results = face_mesh.process(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))

    # Print and draw face mesh landmarks on the image.
    if not results.multi_face_landmarks:
      continue
    annotated_image = image.copy()
    for face_landmarks in results.multi_face_landmarks:
      print('face_landmarks:', face_landmarks)
      mp_drawing.draw_landmarks(
          image=annotated_image,
          landmark_list=face_landmarks,
          connections=mp_face_mesh.FACE_CONNECTIONS,
          landmark_drawing_spec=drawing_spec,
          connection_drawing_spec=drawing_spec)

在这个取自here的示例中,您可以看到它们正在遍历results.multi_face_landmarks

代码语言:javascript
运行
复制
for face_landmarks in results.multi_face_landmarks:

这里的每个迭代由图像中检测到的每个人脸的信息组成,results.multi_face_landmarks的长度是图像中检测到的人脸的数量。

当你打印第一个的属性时,你会看到地标的作为最后一个属性。

代码语言:javascript
运行
复制
dir(results.multi_face_landmarks[0])
>> ..., 'landmark']

我们需要地标属性来在进一步的步骤后获取像素坐标。

地标属性的长度为468,基本上是回归后预测的x,y,z关键点的个数。

如果我们取第一个关键点:

代码语言:javascript
运行
复制
results.multi_face_landmarks[0].landmark[0]

它将给出归一化的x,y,z值:

代码语言:javascript
运行
复制
x: 0.25341567397117615
y: 0.71121746301651
z: -0.03244325891137123

最后,xyz是每个关键点的属性。我们可以通过对关键点调用dir()来检查这一点。

现在,您可以轻松地获得标准化的像素坐标:

代码语言:javascript
运行
复制
results.multi_face_landmarks[0].landmark[0].x -> X coordinate
results.multi_face_landmarks[0].landmark[0].y -> Y coordinate
results.multi_face_landmarks[0].landmark[0].z -> Z coordinate

对于像素坐标的反规范化,我们应该将x坐标乘以宽度,将y坐标乘以高度。

示例代码:

代码语言:javascript
运行
复制
for face in results.multi_face_landmarks:
    for landmark in face.landmark:
        x = landmark.x
        y = landmark.y

        shape = image.shape 
        relative_x = int(x * shape[1])
        relative_y = int(y * shape[0])

        cv2.circle(image, (relative_x, relative_y), radius=1, color=(225, 0, 100), thickness=1)
cv2_imshow(image)

这将会给我们提供:

Click to see result image

票数 9
EN

Stack Overflow用户

发布于 2021-04-18 03:26:45

这是一个完整的解释-

Face Mesh MediaPipe

代码语言:javascript
运行
复制
import cv2
import mediapipe as mp
mp_drawing = mp.solutions.drawing_utils
mp_face_mesh = mp.solutions.face_mesh

# For static images:
drawing_spec = mp_drawing.DrawingSpec(thickness=1, circle_radius=1)
with mp_face_mesh.FaceMesh(
    static_image_mode=True,
    max_num_faces=1,
    min_detection_confidence=0.5) as face_mesh:
  for idx, file in enumerate(file_list):
    image = cv2.imread(file)
    # Convert the BGR image to RGB before processing.
    results = face_mesh.process(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))

    # Print and draw face mesh landmarks on the image.
    if not results.multi_face_landmarks:
      continue
    annotated_image = image.copy()
    for face_landmarks in results.multi_face_landmarks:
      print('face_landmarks:', face_landmarks)
票数 1
EN

Stack Overflow用户

发布于 2021-05-23 10:02:24

Mediapipe的地标值根据图像的宽度和高度进行标准化。之后,只需将地标的x与图像的宽度相乘,将地标的y与图像的高度相乘,即可获得地标值。您可以查看此link以获取有关mediapipe的完整教程。它正在制作中,但很快就会完成。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67141844

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档