简体   繁体   中英

Remove Blur or unmask an image

I am using MTCNN to detect face in an image, FAC.NET to extract and save features from each detected face and OpenCV Gaussian Blur filter to mask the detected faces.

原图

蒙版图像

目标脸

    def face_and_features(img):
    boxes, _ = mtcnn.detect(img)
    print(boxes)

    for i, box in enumerate(boxes):
        a,b,c,d = box
        x1,y1,x2,y2 = int(a), int(b), int(c), int(d)
        face = img[y1:y2, x1:x2]
        cv2.imwrite(f"face{i}.jpg",face)
        
        face = cv2.resize(face, (160, 160))
        face = face.transpose((2, 0, 1))
        face = torch.from_numpy(face).float()
        face = face.unsqueeze(0)

        features = facenet(face)

        filename = "face_{}.npy".format(i)
        np.save(filename, features.detach().numpy())
        with open("bounding_boxes.txt", "a") as f:
            f.write("{}, {}, {}, {}, {}, {}, {} \n".format(x1, y1, x2, y2, filename, datetime.datetime.now(), frame_number))
    return features
       
def masking(img):
    filename = "bounding_boxes.txt"
    masked_img = img.copy()
    with open(filename,'r') as file:
        for line in file:
            x,y,w,h,f_name,time, f_no = line.split(",")
            x,y,w,h = int(x), int(y), int(w), int(h)
            roi_color = masked_img[y:h, x:w]
            masked_img[y:h, x:w] = cv2.GaussianBlur(roi_color, (51,51), 0)
    return masked_img, f_name, time, f_no

My end goal is to find the target face in the masked image by comparing saved facial features(or any other method) and unmask target face only. Any idea or advice?

Your code is basically detecting faces using MTCNN, extracting features from each face using FAC.NET and saving them, then blurring the faces in the image using OpenCV Gaussian Blur filter.

To find the target face in the masked image, you can compare the saved features with the features extracted from the target face. You can use cosine similarity to calculate the similarity between the saved features and the target face features. The face with the highest similarity score will be the target face.

In the masking function, you can add a step to unmask the target face by replacing the blurred area with the original face region.

Here's a sample code that implements the steps I mentioned above:

def compare_features(target_face, saved_features):
    target_face = cv2.resize(target_face, (160, 160))
    target_face = target_face.transpose((2, 0, 1))
    target_face = torch.from_numpy(target_face).float()
    target_face = target_face.unsqueeze(0)

    target_features = facenet(target_face)
    similarity_scores = []
    for i, feature in enumerate(saved_features):
        score = cosine_similarity(target_features, feature)
        similarity_scores.append((i, score))
    return max(similarity_scores, key=lambda x: x[1])

def unmask_target_face(img, target_index, x, y, w, h):
    target_face = img[y:h, x:w]
    masked_img = img.copy()
    masked_img[y:h, x:w] = target_face
    return masked_img

def find_target_face(img, saved_features):
    target_face, target_coords = detect_target_face(img)
    target_index, score = compare_features(target_face, saved_features)
    x, y, w, h = target_coords[0], target_coords[1], target_coords[2], target_coords[3]
    unmasked_img = unmask_target_face(img, target_index, x, y, w, h)
    return unmasked_img

saved_features = [np.load(f"face_{i}.npy") for i in range(len(boxes))]
masked_img, _, _, _ = masking(img)
unmasked_img = find_target_face(masked_img, saved_features)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM