我正试图使用面网格从mediapipe库中提取所有特征的坐标,但对于我测试的每个图像,它都给出了相同的坐标。我不明白这里出了什么问题。如果有人能帮忙,那就太好了
import mediapipe as mp
import cv2
import matplotlib.pyplot as plt
# from mpl_toolkits.mplot3d import Axes3D
import json
import os
from os import path
file_name = "./json_output"
img_base = cv2.imread("./johnny-depp-sunglasses-hat-smile-wallpaper.jpg")
img = img_base.copy()
mp_face_mesh = mp.solutions.face_mesh
face_mesh = mp_face_mesh.FaceMesh(static_image_mode=True)
results = face_mesh.process(img)
landmarks = results.multi_face_landmarks[0]
xs, ys, zs = [], [], []
CONTOUR_LIST = ["FACEMESH_LIPS", "FACEMESH_FACE_OVAL", "FACEMESH_LEFT_IRIS",
"FACEMESH_LEFT_EYEBROW","FACEMESH_LEFT_EYE", "FACEMESH_RIGHT_IRIS",
"FACEMESH_RIGHT_EYEBROW", "FACEMESH_RIGHT_EYE"]
这是主要功能:
def extract_landmarks(inp):
img = img_base.copy()
for landmark in landmarks.landmark:
x = landmark.x
y = landmark.y
z = landmark.z
xs.append(x)
ys.append(y)
zs.append(z)
relative_x = int(x * img_base.shape[1])
relative_y = int(y * img_base.shape[0])
cv2.circle(img, (relative_x, relative_y),
radius=5, color=(0,0,255),
thickness=-1)
# fig = plt.figure(figsize=(15,15))
# plt.imshow(img[:,:,::-1])
# plt.show()
img = img_base.copy()
for i in inp:
for src_id, tar_id in i:
source = landmarks.landmark[src_id]
target = landmarks.landmark[tar_id]
relative_source = int(source.x * img.shape[1]), int(source.y * img.shape[0])
relative_target = int(target.x * img.shape[1]), int(target.y * img.shape[0])
cv2.line(img, relative_source, relative_target,
color=(255,255,255), thickness=2)
fig = plt.figure(figsize=(15,15))
plt.imshow(img[:,:,::-1])
plt.show()
result = inp
# print(result)
my_json = list(result)
# my_ans = [{f"{CONTOUR_LIST[k]}":{'x':x, 'y':y}} for k in range(len(CONTOUR_LIST)) for i in my_json for x,y in i]
my_ans = [{f"{CONTOUR_LIST[k]}":{'x':x, 'y':y}} for k in range(0, 8) for i in my_json for x,y in i]
#
# print(my_ans, sep="n", end="n")
# print("n")
# print("n")
# coordinates.append(my_ans)
# print(my_ans, end="n", sep="n")
if os.path.exists(file_name):
print("Already exists!")
# with open(file_name, 'w') as f:
# f.write(json.dumps(my_ans, indent=4, separators=(',',': ')))
else:
with open(file_name, 'w') as file:
json.dump(my_ans, file,
indent=4,
separators=(',',': '))
return len(my_json)
这是调用函数的代码:
features = []
features.append(mp_face_mesh.FACEMESH_LIPS)
features.append(mp_face_mesh.FACEMESH_FACE_OVAL)
features.append(mp_face_mesh.FACEMESH_LEFT_IRIS)
features.append(mp_face_mesh.FACEMESH_LEFT_EYEBROW)
features.append(mp_face_mesh.FACEMESH_LEFT_EYE)
features.append(mp_face_mesh.FACEMESH_RIGHT_IRIS)
features.append(mp_face_mesh.FACEMESH_RIGHT_EYEBROW)
features.append(mp_face_mesh.FACEMESH_RIGHT_EYE)
extract_landmarks(features)
对于每一张图像,我都得到相同的坐标。
json_output
中的坐标是固定的,请查看面网格。
json_output
的内容是图像中的数字。
CCD_ 3和CCD_ 4是相对于宽度的坐标图片的高度。