|
| 1 | +# Copyright (c) Microsoft Corporation. |
| 2 | +# Licensed under the MIT License. |
| 3 | + |
| 4 | +import torch |
| 5 | +import numpy as np |
| 6 | +import skimage.io as io |
| 7 | + |
| 8 | +# from FaceSDK.face_sdk import FaceDetection |
| 9 | +# from face_sdk import FaceDetection |
| 10 | +import matplotlib.pyplot as plt |
| 11 | +from matplotlib.patches import Rectangle |
| 12 | +from skimage.transform import SimilarityTransform |
| 13 | +from skimage.transform import warp |
| 14 | +from PIL import Image |
| 15 | +import torch.nn.functional as F |
| 16 | +import torchvision as tv |
| 17 | +import torchvision.utils as vutils |
| 18 | +import time |
| 19 | +import cv2 |
| 20 | +import os |
| 21 | +from skimage import img_as_ubyte |
| 22 | +import json |
| 23 | +import argparse |
| 24 | +import dlib |
| 25 | + |
| 26 | + |
| 27 | +def _standard_face_pts(): |
| 28 | + pts = ( |
| 29 | + np.array([196.0, 226.0, 316.0, 226.0, 256.0, 286.0, 220.0, 360.4, 292.0, 360.4], np.float32) / 256.0 |
| 30 | + - 1.0 |
| 31 | + ) |
| 32 | + |
| 33 | + return np.reshape(pts, (5, 2)) |
| 34 | + |
| 35 | + |
| 36 | +def _origin_face_pts(): |
| 37 | + pts = np.array([196.0, 226.0, 316.0, 226.0, 256.0, 286.0, 220.0, 360.4, 292.0, 360.4], np.float32) |
| 38 | + |
| 39 | + return np.reshape(pts, (5, 2)) |
| 40 | + |
| 41 | + |
| 42 | +def get_landmark(face_landmarks, id): |
| 43 | + part = face_landmarks.part(id) |
| 44 | + x = part.x |
| 45 | + y = part.y |
| 46 | + |
| 47 | + return (x, y) |
| 48 | + |
| 49 | + |
| 50 | +def search(face_landmarks): |
| 51 | + |
| 52 | + x1, y1 = get_landmark(face_landmarks, 36) |
| 53 | + x2, y2 = get_landmark(face_landmarks, 39) |
| 54 | + x3, y3 = get_landmark(face_landmarks, 42) |
| 55 | + x4, y4 = get_landmark(face_landmarks, 45) |
| 56 | + |
| 57 | + x_nose, y_nose = get_landmark(face_landmarks, 30) |
| 58 | + |
| 59 | + x_left_mouth, y_left_mouth = get_landmark(face_landmarks, 48) |
| 60 | + x_right_mouth, y_right_mouth = get_landmark(face_landmarks, 54) |
| 61 | + |
| 62 | + x_left_eye = int((x1 + x2) / 2) |
| 63 | + y_left_eye = int((y1 + y2) / 2) |
| 64 | + x_right_eye = int((x3 + x4) / 2) |
| 65 | + y_right_eye = int((y3 + y4) / 2) |
| 66 | + |
| 67 | + results = np.array( |
| 68 | + [ |
| 69 | + [x_left_eye, y_left_eye], |
| 70 | + [x_right_eye, y_right_eye], |
| 71 | + [x_nose, y_nose], |
| 72 | + [x_left_mouth, y_left_mouth], |
| 73 | + [x_right_mouth, y_right_mouth], |
| 74 | + ] |
| 75 | + ) |
| 76 | + |
| 77 | + return results |
| 78 | + |
| 79 | + |
| 80 | +def compute_transformation_matrix(img, landmark, normalize, target_face_scale=1.0): |
| 81 | + |
| 82 | + std_pts = _standard_face_pts() # [-1,1] |
| 83 | + target_pts = (std_pts * target_face_scale + 1) / 2 * 512.0 |
| 84 | + |
| 85 | + # print(target_pts) |
| 86 | + |
| 87 | + h, w, c = img.shape |
| 88 | + if normalize == True: |
| 89 | + landmark[:, 0] = landmark[:, 0] / h * 2 - 1.0 |
| 90 | + landmark[:, 1] = landmark[:, 1] / w * 2 - 1.0 |
| 91 | + |
| 92 | + # print(landmark) |
| 93 | + |
| 94 | + affine = SimilarityTransform() |
| 95 | + |
| 96 | + affine.estimate(target_pts, landmark) |
| 97 | + |
| 98 | + return affine.params |
| 99 | + |
| 100 | + |
| 101 | +def show_detection(image, box, landmark): |
| 102 | + plt.imshow(image) |
| 103 | + print(box[2] - box[0]) |
| 104 | + plt.gca().add_patch( |
| 105 | + Rectangle( |
| 106 | + (box[1], box[0]), box[2] - box[0], box[3] - box[1], linewidth=1, edgecolor="r", facecolor="none" |
| 107 | + ) |
| 108 | + ) |
| 109 | + plt.scatter(landmark[0][0], landmark[0][1]) |
| 110 | + plt.scatter(landmark[1][0], landmark[1][1]) |
| 111 | + plt.scatter(landmark[2][0], landmark[2][1]) |
| 112 | + plt.scatter(landmark[3][0], landmark[3][1]) |
| 113 | + plt.scatter(landmark[4][0], landmark[4][1]) |
| 114 | + plt.show() |
| 115 | + |
| 116 | + |
| 117 | +def affine2theta(affine, input_w, input_h, target_w, target_h): |
| 118 | + # param = np.linalg.inv(affine) |
| 119 | + param = affine |
| 120 | + theta = np.zeros([2, 3]) |
| 121 | + theta[0, 0] = param[0, 0] * input_h / target_h |
| 122 | + theta[0, 1] = param[0, 1] * input_w / target_h |
| 123 | + theta[0, 2] = (2 * param[0, 2] + param[0, 0] * input_h + param[0, 1] * input_w) / target_h - 1 |
| 124 | + theta[1, 0] = param[1, 0] * input_h / target_w |
| 125 | + theta[1, 1] = param[1, 1] * input_w / target_w |
| 126 | + theta[1, 2] = (2 * param[1, 2] + param[1, 0] * input_h + param[1, 1] * input_w) / target_w - 1 |
| 127 | + return theta |
| 128 | + |
| 129 | + |
| 130 | +if __name__ == "__main__": |
| 131 | + |
| 132 | + parser = argparse.ArgumentParser() |
| 133 | + parser.add_argument("--url", type=str, default="/home/jingliao/ziyuwan/celebrities", help="input") |
| 134 | + parser.add_argument( |
| 135 | + "--save_url", type=str, default="/home/jingliao/ziyuwan/celebrities_detected_face_reid", help="output" |
| 136 | + ) |
| 137 | + opts = parser.parse_args() |
| 138 | + |
| 139 | + url = opts.url |
| 140 | + save_url = opts.save_url |
| 141 | + |
| 142 | + ### If the origin url is None, then we don't need to reid the origin image |
| 143 | + |
| 144 | + os.makedirs(url, exist_ok=True) |
| 145 | + os.makedirs(save_url, exist_ok=True) |
| 146 | + |
| 147 | + face_detector = dlib.get_frontal_face_detector() |
| 148 | + landmark_locator = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat") |
| 149 | + |
| 150 | + count = 0 |
| 151 | + |
| 152 | + map_id = {} |
| 153 | + for x in os.listdir(url): |
| 154 | + img_url = os.path.join(url, x) |
| 155 | + pil_img = Image.open(img_url).convert("RGB") |
| 156 | + |
| 157 | + image = np.array(pil_img) |
| 158 | + |
| 159 | + start = time.time() |
| 160 | + faces = face_detector(image) |
| 161 | + done = time.time() |
| 162 | + |
| 163 | + if len(faces) == 0: |
| 164 | + print("Warning: There is no face in %s" % (x)) |
| 165 | + continue |
| 166 | + |
| 167 | + print(len(faces)) |
| 168 | + |
| 169 | + if len(faces) > 0: |
| 170 | + for face_id in range(len(faces)): |
| 171 | + current_face = faces[face_id] |
| 172 | + face_landmarks = landmark_locator(image, current_face) |
| 173 | + current_fl = search(face_landmarks) |
| 174 | + |
| 175 | + affine = compute_transformation_matrix(image, current_fl, False, target_face_scale=1.3) |
| 176 | + aligned_face = warp(image, affine, output_shape=(512, 512, 3)) |
| 177 | + img_name = x[:-4] + "_" + str(face_id + 1) |
| 178 | + io.imsave(os.path.join(save_url, img_name + ".png"), img_as_ubyte(aligned_face)) |
| 179 | + |
| 180 | + count += 1 |
| 181 | + |
| 182 | + if count % 1000 == 0: |
| 183 | + print("%d have finished ..." % (count)) |
| 184 | + |
0 commit comments