0

我目前正在尝试使用 Mediapipe 和 OpenCV 制作一个识别人脸并将其模糊的功能。

每当我尝试识别和模糊不止一张脸时,两张脸之间的空间也会变得模糊。我怎样才能做到只有面部模糊但不弄脏?

主文件

import cv2
import numpy as np

from facial_landmarks import FaceLandmarks

fl = FaceLandmarks()

image = cv2.imread("sampletest.jpg")
landmarks = fl.get_facial_landmarks(image)

convexhull = cv2.convexHull(landmarks)
h, w = image.shape[:2]
mask = np.zeros((h, w), np.uint8)

cv2.fillConvexPoly(mask, convexhull, 255)

image_copy = cv2.blur(image, (27, 27))
face_extracted = cv2.bitwise_and(image_copy, image_copy, mask=mask)

background_mask = cv2.bitwise_not(mask)
background = cv2.bitwise_and(image, image, mask=background_mask)

result = cv2.add(background, face_extracted)
cv2.imwrite('test.jpg', result)

地标.py

import mediapipe as mp
import cv2
import numpy as np


class FaceLandmarks:
    def __init__(self):
        mp_face_mesh = mp.solutions.face_mesh
        self.face_mesh = mp_face_mesh.FaceMesh(max_num_faces=6, min_detection_confidence=0.2)


    def get_facial_landmarks(self, frame):
        height, width, _ = frame.shape
        frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        result = self.face_mesh.process(frame_rgb)

        facelandmarks = []
        for facial_landmarks in result.multi_face_landmarks:
            for i in range(0, 468):
                pt1 = facial_landmarks.landmark[i]
                x = int(pt1.x * width)
                y = int(pt1.y * height)
                facelandmarks.append([x, y])


        return np.array(facelandmarks, np.int32)


这是max 4的样子

4

0 回答 0