4 Star 3 Fork 3

小脚丫 / 虚拟主播-vtuber-python

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
facial_landmark.py 5.34 KB
一键复制 编辑 原始数据 按行查看 历史
"""
For finding the face and face landmarks for further manipulication
"""
import cv2
import mediapipe as mp
import numpy as np
class FaceMeshDetector:
def __init__(self):
# 使用Mediapipe提供的绘图方法来绘制面部标识
self.mp_drawing = mp.solutions.drawing_utils
self.drawing_spec = self.mp_drawing.DrawingSpec(thickness=1, circle_radius=1)
# def __init__(self,
# model_complexity=1,
# static_image_mode=False,
# # enable_segmentation=False,
# refine_face_landmarks=True,
# min_detection_confidence=0.5,
# min_tracking_confidence=0.5):
#
# self.model_complexity=model_complexity
# self.static_image_mode = static_image_mode
# self.refine_face_landmarks = refine_face_landmarks
# self.min_detection_confidence=min_detection_confidence
# self.min_tracking_confidence=min_tracking_confidence
#
# self.mp_holistic = mp.solutions.holistic
# # The object to do the stuffs
# self.holistic = self.mp_holistic.Holistic(
# self.model_complexity,
# self.static_image_mode,
# self.refine_face_landmarks,)
# # self.min_detection_confidence,
# # self.min_tracking_confidence)
#
# self.mp_drawing = mp.solutions.drawing_utils
# self.drawing_spec = self.mp_drawing.DrawingSpec(thickness=1, circle_radius=1)
# def findFaceMesh(self, img, draw=True):
# # convert the img from BRG to RGB 并镜像翻转图像
# # img = cv2.cvtColor(cv2.flip(img, 1), cv2.COLOR_BGR2RGB)
# #
# # # To improve performance, optionally mark the image as not writeable to
# # # pass by reference.
# # img.flags.writeable = False
# # # self.results = self.face_mesh.process(img)
# #
# # # Draw the face mesh annotations on the image.
# # img.flags.writeable = True
# # img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
#
#
# # 若有多张脸 则将每个人脸图像的468个像素坐标传入矩阵中
# # if self.results.multi_face_landmarks:
# # for face_landmarks in self.results.multi_face_landmarks:
# # self.results = self.face_mesh.process(img)
# # face_landmarks = self.results.face_landmarks
#
# img = cv2.cvtColor(cv2.flip(img, 1), cv2.COLOR_BGR2RGB)
# img.flags.writeable = False
# self.results = self.holistic.process(img)
# img.flags.writeable = True
# img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
#
# self.imgH, self.imgW, self.imgC = img.shape
# self.faces = []
#
# face_landmarks = self.results.face_landmarks
# if face_landmarks:
# # print(face_landmarks)
# if draw:
# self.mp_drawing.draw_landmarks(
# image = img,
# landmark_list = face_landmarks,
# # connections = self.mp_face_mesh.FACEMESH_TESSELATION,
# landmark_drawing_spec = self.drawing_spec,
# connection_drawing_spec = self.drawing_spec)
#
# # 一个人脸图像的468个像素坐标传入face矩阵之中
# # print(len(face_landmarks.landmark))
# face = []
# for id, lmk in enumerate(face_landmarks.landmark):
# x, y = int(lmk.x * self.imgW), int(lmk.y * self.imgH)
# face.append([x, y])
#
# # show the id of each point on the image
# # cv2.putText(img, str(id), (x-4, y-4), cv2.FONT_HERSHEY_SIMPLEX, 0.3, (255, 255, 255), 1, cv2.LINE_AA)
#
# self.faces.append(face)
#
# return img, self.faces
def findFaceMesh(self, img,face_landmarks, draw=True):
self.imgH, self.imgW, self.imgC = img.shape
self.faces = []
if face_landmarks:
# print(face_landmarks)
if draw:
self.mp_drawing.draw_landmarks(
image = img,
landmark_list = face_landmarks,
# connections = self.mp_face_mesh.FACEMESH_TESSELATION,
landmark_drawing_spec = self.drawing_spec,
connection_drawing_spec = self.drawing_spec)
# 一个人脸图像的468个像素坐标传入face矩阵之中
face = []
for id, lmk in enumerate(face_landmarks.landmark):
x, y = int(lmk.x * self.imgW), int(lmk.y * self.imgH)
face.append([x, y])
# show the id of each point on the image
# cv2.putText(img, str(id), (x-4, y-4), cv2.FONT_HERSHEY_SIMPLEX, 0.3, (255, 255, 255), 1, cv2.LINE_AA)
self.faces.append(face)
return img, self.faces
# sample run of the module
def main():
detector = FaceMeshDetector()
cap = cv2.VideoCapture(0)
while cap.isOpened():
success, img = cap.read()
if not success:
print("Ignoring empty camera frame.")
continue
img, faces = detector.findFaceMesh(img)
# if faces:
# print(faces[0])
cv2.imshow('MediaPipe FaceMesh', img)
# press "q" to leave
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
if __name__ == "__main__":
# demo code
main()
Python
1
https://gitee.com/little-feet/vtuber.git
git@gitee.com:little-feet/vtuber.git
little-feet
vtuber
虚拟主播-vtuber-python
master

搜索帮助