コード例 #1
0
def align_multi(cfg, image, min_confidence=0.97, limits=None):
    boxes = []
    landmarks = []
    detector = MTCNN()
    faces = detector.detect_faces(image)
    refrence = get_reference_facial_points(default_square=True)
    for face in faces:
        if face['confidence'] < min_confidence:
            continue
        boxes.append(face['box'])
        landmark = []
        for name, points in face['keypoints'].items():
            landmark.append(list(points))
        landmarks.append(landmark)
    if limits:
        boxes = boxes[:limits]
        landmarks = landmarks[:limits]

    faces = []
    for landmark in landmarks:
        warped_face = warp_and_crop_face(image,
                                         landmark,
                                         reference_pts=refrence,
                                         crop_size=(cfg['input_size'],
                                                    cfg['input_size']))
        faces.append(warped_face)

    return np.array(boxes), np.array(landmarks), np.array(faces)
コード例 #2
0
def align_face(img_path, resize_face_size=112):
    img = Image.open(img_path)
    bounding_boxes, landmarks = detect_faces(img)
    scale = resize_face_size / 112.
    reference = get_reference_facial_points(default_square = True) * scale
    facial5points = [[landmarks[0][j], landmarks[0][j + 5]] for j in range(5)]
    warped_face = warp_and_crop_face(np.array(img), facial5points, reference, crop_size=(resize_face_size, resize_face_size))
    return warped_face
コード例 #3
0
def prepare_facebank(cfg, model):
    names = ['Unknown']
    embeddings = []
    detector = MTCNN()
    for name in os.listdir(cfg['face_bank']):
        if os.path.isfile(name):
            continue
        else:
            emb = []
            for file in tqdm(os.listdir(os.path.join(cfg['face_bank'], name))):
                if not os.path.isfile(
                        os.path.join(cfg['face_bank'], name, file)):
                    continue
                else:
                    image = cv2.imread(
                        os.path.join(cfg['face_bank'], name, file))
                    image = cv2.resize(image,
                                       (cfg['input_size'], cfg['input_size']))
                    face = detector.detect_faces(image)
                    if len(face) > 0:
                        face = face[0]
                        refrence = get_reference_facial_points(
                            default_square=True)

                        landmark = []
                        for _, points in face['keypoints'].items():
                            landmark.append(list(points))

                        warped_face = warp_and_crop_face(
                            image,
                            landmark,
                            reference_pts=refrence,
                            crop_size=(cfg['input_size'], cfg['input_size']))
                        image = np.array(warped_face)

                        image = image.astype(np.float32) / 255.
                        if len(image.shape) == 3:
                            image = np.expand_dims(image, 0)
                        emb.append(l2_norm(model(image)).numpy())
            if len(emb) == 0:
                continue
            emb = np.array(emb)
            mean = np.mean(emb, axis=0)
            embeddings.append(mean)
        names.append(name)
    embeddings = np.array(embeddings)
    names = np.array(names)

    np.save(os.path.join('data', 'facebank.npy'), embeddings)
    np.save(os.path.join('data', 'names.npy'), names)

    return embeddings, names
コード例 #4
0
def align(cfg, image):
    detector = MTCNN()
    face = detector.detect_faces(image)[0]
    refrence = get_reference_facial_points(default_square=True)

    landmark = []
    for name, points in face['keypoints'].items():
        landmark.append(list(points))

    warped_face = warp_and_crop_face(image,
                                     landmark,
                                     reference_pts=refrence,
                                     crop_size=(cfg['input_size'],
                                                cfg['input_size']))
    return warped_face
コード例 #5
0
def large_pose_crop(img, lmrks, pose, reference, args):

    scale_size = math.ceil(args.crop_size / 144 *
                           48)  # According to LightCNN cropping strategy

    if args.WarpAffine:
        warped_face = warp_and_crop_face(np.array(img),
                                         lmrks,
                                         reference,
                                         crop_size=(args.crop_size,
                                                    args.crop_size))

    elif args.Align_Eyes_Crop:
        if pose == '110' or pose == '120' or pose == '010' or pose == '240':
            eye_middle_x = (lmrks[0][0] + lmrks[1][0]) / 2
            eye_middle_y = (lmrks[0][1] + lmrks[1][1]) / 2
            mouth_middle_y = (lmrks[3][1] + lmrks[4][1]) / 2

            Scale_thresh = scale_size / (mouth_middle_y - eye_middle_y)
            resize_img = cv2.resize(img, (int(img.shape[1] * Scale_thresh),
                                          int(img.shape[0] * Scale_thresh)))
            new_eye_mid_x, new_eye_mid_y = int(
                eye_middle_x * Scale_thresh), int(eye_middle_y * Scale_thresh)

            facial_boundary = facial_bound(new_eye_mid_y, new_eye_mid_x,
                                           args.crop_size)
            [upper_bound, lower_bound, left_bound,
             right_bound], img = facial_pad(facial_boundary, resize_img)

        else:
            scale = scale_size / (((lmrks[2][0] - lmrks[3][0])**2 +
                                   (lmrks[2][1] - lmrks[3][1])**2)**0.5)
            atan = math.atan2(lmrks[0][1] - lmrks[1][1],
                              lmrks[0][0] - lmrks[1][0]) * 57.2957795 + 180
            center = (lmrks[2][0], lmrks[2][1])
            M = cv2.getRotationMatrix2D(center, atan, scale)
            rotated_face = cv2.warpAffine(img, M, (img.shape[1], img.shape[0]))

            facial_boundary = facial_bound(lmrks[2][1], lmrks[2][0],
                                           args.crop_size)
            [upper_bound, lower_bound, left_bound,
             right_bound], img = facial_pad(facial_boundary, rotated_face)

        warped_face = img[upper_bound:lower_bound, left_bound:right_bound, :]

    return warped_face
コード例 #6
0
    for subfolder in tqdm(os.listdir(source_root)):
        if not os.path.isdir(os.path.join(dest_root, subfolder)):
            os.mkdir(os.path.join(dest_root, subfolder))
        for image_name in os.listdir(os.path.join(source_root, subfolder)):
            print("Processing\t{}".format(
                os.path.join(source_root, subfolder, image_name)))
            img = Image.open(os.path.join(source_root, subfolder, image_name))
            try:  # Handle exception
                _, landmarks = detect_faces(img)
            except Exception:
                print("{} is discarded due to exception!".format(
                    os.path.join(source_root, subfolder, image_name)))
                continue
            if len(
                    landmarks
            ) == 0:  # If the landmarks cannot be detected, the img will be discarded
                print("{} is discarded due to non-detected landmarks!".format(
                    os.path.join(source_root, subfolder, image_name)))
                continue
            facial5points = [[landmarks[0][j], landmarks[0][j + 5]]
                             for j in range(5)]
            warped_face = warp_and_crop_face(np.array(img),
                                             facial5points,
                                             reference,
                                             crop_size=(crop_size, crop_size))
            img_warped = Image.fromarray(warped_face)
            if image_name.split('.')[-1].lower() not in ['jpg', 'jpeg'
                                                         ]:  #not from jpg
                image_name = '.'.join(image_name.split('.')[:-1]) + '.jpg'
            img_warped.save(os.path.join(dest_root, subfolder, image_name))
コード例 #7
0
    crop_size = args.crop_size # specify size of aligned faces, align and crop with padding
    scale = crop_size / 112.
    reference = get_reference_facial_points(default_square = True) * scale

    cwd = os.getcwd() # delete '.DS_Store' existed in the source_root
    os.chdir(source_root)
    os.system("find . -name '*.DS_Store' -type f -delete")
    os.chdir(cwd)

    if not os.path.isdir(dest_root):
        os.mkdir(dest_root)

    for subfolder in tqdm(os.listdir(source_root)):
        if not os.path.isdir(os.path.join(dest_root, subfolder)):
            os.mkdir(os.path.join(dest_root, subfolder))
        for image_name in os.listdir(os.path.join(source_root, subfolder)):
            print("Processing\t{}".format(os.path.join(source_root, subfolder, image_name)))
            img = Image.open(os.path.join(source_root, subfolder, image_name))
            try: # Handle exception
                _, landmarks = detect_faces(img)
            except Exception:
                print("{} is discarded due to exception!".format(os.path.join(source_root, subfolder, image_name)))
                continue
            if len(landmarks) == 0: # If the landmarks cannot be detected, the img will be discarded
                print("{} is discarded due to non-detected landmarks!".format(os.path.join(source_root, subfolder, image_name)))
                continue
            facial5points = [[landmarks[0][j], landmarks[0][j + 5]] for j in range(5)]
            warped_face = warp_and_crop_face(np.array(img), facial5points, reference, crop_size=(crop_size, crop_size))
            img_warped = Image.fromarray(warped_face)
            img_warped.save(os.path.join(dest_root, subfolder, image_name.split('.')[0] + '.jpg'))