这是mediapipe python库的简单应用,比如人脸检测及3D人脸地标。
mediapipe是谷歌于2020年开源的一套支持跨平台机器学习解决方案。
直接pip下载极其缓慢,被迫使用国内镜像源下载,于实际发布的版本有差异。
pip install mediapipe
代码如下(人脸检测):
import cv2
import mediapipe as mp
import face_mesh_connections
def face_detection():
mp_face_detection = mp.solutions.face_detection
mp_drawing = mp.solutions.drawing_utils
mp_drawing_styles = mp.solutions.drawing_styles
mp_face_mesh = mp.solutions.face_mesh
# 人脸检测
# 对于静态图像:
IMAGE_FILES = ["boluo.jpg"]
with mp_face_detection.FaceDetection(min_detection_confidence=0.5) as face_detection:
for idx, file in enumerate(IMAGE_FILES):
image = cv2.imread(file)
# 将BGR图像转换为RGB,并使用MediaPipe人脸检测进行处理:
results = face_detection.process(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
# 绘制每个人脸的人脸检测
if not results.detections:
continue
annotated_image = image.copy()
for detection in results.detections:
print('Nose tip:')
print(mp_face_detection.get_key_point(
detection, mp_face_detection.FaceKeyPoint.NOSE_TIP))
mp_drawing.draw_detection(annotated_image, detection)
cv2.imwrite('annotated_image' + str(idx) + '.png', annotated_image)
cv2.namedWindow("MediaPipe Face Detection", 0)
cv2.imshow('MediaPipe Face Detection', cv2.flip(annotated_image, 1))
if cv2.waitKey(0) & 0xFF == 27:
break
效果如下:
代码如下(3D人脸地标):
def face_mesh():
mp_drawing = mp.solutions.drawing_utils
# mp_drawing_styles = mp.solutions.drawing_styles
mp_face_mesh = mp.solutions.face_mesh
mp_face_mesh_connection = face_mesh_connections
# For static images:
IMAGE_FILES = ["boluo.jpg"]
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(IMAGE_FILES):
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.landmark)
mp_drawing.draw_landmarks(
image=annotated_image,
landmark_list=face_landmarks,
# landmark_drawing_spec=mp_drawing.DrawingSpec(color=(255, 255, 255)),
connections=mp_face_mesh_connection.FACEMESH_TESSELATION,
connection_drawing_spec=mp_drawing.DrawingSpec(color=(0, 255, 0), thickness=3)
)
cv2.imwrite('face_mesh_image' + str(idx) + '.png', annotated_image)
cv2.namedWindow("MediaPipe Face Detection", 0)
cv2.imshow('MediaPipe Face Detection', cv2.flip(annotated_image, 1))
if cv2.waitKey(0) & 0xFF == 27:
break
效果如下:
mediapipe python库的安装和简单使用都比较简单,C++等其它版本安装较为复杂。
免责声明:本文系网络转载或改编,未找到原创作者,版权归原作者所有。如涉及版权,请联系删