mediapipe系列之一脸部特征点标记

基于mediapipe进行的脸部特征点标记

采用方法

主要使用google 的 mediapipe 工具包中的face_mesh模块和opencv中的显示标识模块

使用事项

文中的1.flv 可以换成其他格式的视频。

代码如下

import cv2
import mediapipe as mp
import time


class FaceMashDetector(object):
    def __init__(self, mode=False, max_num=2, detection=0.5, tracking=0.5):
        self.mode = mode
        self.max_num = max_num
        self.detection = detection
        self.tracking = tracking
        self.mpFaceMesh = mp.solutions.face_mesh
        self.face = self.mpFaceMesh.FaceMesh(self.mode, self.max_num, self.detection, self.tracking)
        self.mpDraw = mp.solutions.drawing_utils
        self.drawSpec = self.mpDraw.DrawingSpec(thickness=1, circle_radius=2)

    def findPose(self, img, draw=True):
        imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        self.results = self.face.process(imgRGB)
        if self.results.multi_face_landmarks:
            for faceLms in self.results.multi_face_landmarks:
                if draw:
                    self.mpDraw.draw_landmarks(img, faceLms, self.mpFaceMesh.FACE_CONNECTIONS, self.drawSpec,
                                               self.drawSpec)
        return img

    def findPosition(self, img, handNo=0, draw=True):
        lmList = []
        if self.results.multi_face_landmarks:
            myFace = self.results.multi_face_landmarks[handNo]
            for id, lm in enumerate(myFace.landmark):
                h, w, c = img.shape
                cx, cy = int(lm.x * w), int(lm.y * h)
                lmList.append((id, cx, cy))
                if draw:
                    cv2.circle(img, (cx, cy), 10, (255, 0, 255), cv2.FILLED)
        return lmList


def main():
    pTime = 0
    cap = cv2.VideoCapture('1.flv')
    detector = FaceMashDetector()
    while True:
        success, img = cap.read()
        img = detector.findPose(img)
        lmList = detector.findPosition(img, draw=False)
        cTime = time.time()
        fps = 1 / (cTime - pTime)
        pTime = cTime
        cv2.putText(img, f"fps:{int(fps)}", (10, 70), cv2.FONT_HERSHEY_PLAIN, 3, (255, 0, 255), 3)
        cv2.imshow('Image', img)
        cv2.waitKey(10)


if __name__ == '__main__':
    main()

你可能感兴趣的:(opencv,图像识别)