def Align(image):
    # print('[Info] Input image size: {}'.format(image.shape))
    landmark = GetRetinaROI(image)

    # detected position
    facialPoints = [
        landmark[5], landmark[7], landmark[9], landmark[11], landmark[13],
        landmark[6], landmark[8], landmark[10], landmark[12], landmark[14]
    ]
    facialPoints = np.reshape(facialPoints, (2, 5))

    # ideal position
    referencePoint = get_reference_facial_points(config.output_size,
                                                 config.inner_padding_factor,
                                                 config.outer_padding,
                                                 config.default_square)

    alignedImg = warp_and_crop_face(image,
                                    facialPoints,
                                    reference_pts=referencePoint,
                                    crop_size=config.output_size)

    # draw landmark
    if config.draw_landmark:
        drawImg = draw(image, landmark)
    else:
        drawImg = None

    return alignedImg, drawImg
Beispiel #2
0
def image_loader(image_path, out_size, device, transform=None):
    global dst_img

    raw = cv.imread(image_path)
    img = Image.open(image_path).convert('RGB')
    _, facial5points = detect_faces(img)

    if len(facial5points) != 1:
        raise Exception("No face or multi faces...")

    facial5points = np.reshape(facial5points[0], (2, 5))

    # Default set
    crop_size = 224
    inner_padding_factor = 0.1
    outer_padding = 0
    output_size = 224

    # get the reference 5 landmarks position in the crop settings
    reference_5pts = get_reference_facial_points(
        (output_size, output_size), inner_padding_factor,
        (outer_padding, outer_padding), True)

    dst_img = warp_and_crop_face(raw,
                                 facial5points,
                                 reference_pts=reference_5pts,
                                 crop_size=(crop_size, crop_size))
    dst_img = cv.resize(dst_img, (out_size, out_size))[:, :, ::-1]
    dst_img_ = Image.fromarray(dst_img)
    im = transform(dst_img_).float()

    return im.unsqueeze(0).to(device)
Beispiel #3
0
def face_align_similarity_transformation(facial_5_points, dir):
    ##Load as RGB
    img = cv2.imread(dir + 'Test/data/test.png')
    facial_5_points = np.array([
        facial_5_points['left_eye'], facial_5_points['right_eye'],
        facial_5_points['nose'], facial_5_points['mouth_left'],
        facial_5_points['mouth_right']
    ],
                               dtype=np.float32)

    default_square = True
    inner_padding_factor = 0.001
    outer_padding = (0, 0)
    output_size = (48, 48)

    reference_5pts = get_reference_facial_points(output_size,
                                                 inner_padding_factor,
                                                 outer_padding, default_square)

    dst_img = warp_and_crop_face(img,
                                 facial_5_points,
                                 reference_pts=reference_5pts,
                                 crop_size=(48, 48))

    ##RGB to gray
    dst_img = cv2.cvtColor(dst_img, cv2.COLOR_BGR2GRAY)

    cv2.imwrite(dir + 'Test/data/face-align-test.png', dst_img)
def face_align_similarity_transformation(img, facial_5_points, dir_path,
                                         image_path, dataset, count):
    count += 1
    if facial_5_points != None:
        print(facial_5_points)

        # #facial_5_points = np.array(facial_5_points, dtype=np.float32)
        facial_5_points = np.array([
            facial_5_points['left_eye'], facial_5_points['right_eye'],
            facial_5_points['nose'], facial_5_points['mouth_left'],
            facial_5_points['mouth_right']
        ],
                                   dtype=np.float32)

        default_square = True
        inner_padding_factor = 0.001
        outer_padding = (0, 0)
        output_size = (48, 48)

        reference_5pts = get_reference_facial_points(output_size,
                                                     inner_padding_factor,
                                                     outer_padding,
                                                     default_square)

        dst_img = warp_and_crop_face(img,
                                     facial_5_points,
                                     reference_pts=reference_5pts,
                                     crop_size=(48, 48))

        ##RGB to gray
        dst_img = cv2.cvtColor(dst_img, cv2.COLOR_BGR2GRAY)
        folder = os.path.exists(
            "/home/ubuntu/Face-emotion-recognition-master/Preprocessing/data/face-alignment-{}/{}"
            .format(dataset, dir_path))
        if not folder:
            os.makedirs(
                "/home/ubuntu/Face-emotion-recognition-master/Preprocessing/data/face-alignment-{}/{}"
                .format(dataset, dir_path))
        cv2.imwrite(
            "/home/ubuntu/Face-emotion-recognition-master/Preprocessing/data/face-alignment-{}/{}/{}"
            .format(dataset, dir_path, image_path), dst_img)

    else:
        img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        folder = os.path.exists(
            "/home/ubuntu/Face-emotion-recognition-master/Preprocessing/data/face-alignment-{}/{}"
            .format(dataset, dir_path))
        if not folder:
            os.makedirs(
                "/home/ubuntu/Face-emotion-recognition-master/Preprocessing/data/face-alignment-{}/{}"
                .format(dataset, dir_path))
        cv2.imwrite(
            "/home/ubuntu/Face-emotion-recognition-master/Preprocessing/data/face-alignment-{}/{}/{}"
            .format(dataset, dir_path, image_path), img)

    return count
Beispiel #5
0
def align_face(img_fn, facial5points):
    raw = cv.imread(img_fn, True)
    facial5points = np.reshape(facial5points, (2, 5))

    crop_size = (image_h, image_w)

    default_square = True
    inner_padding_factor = 0.25
    outer_padding = (0, 0)
    output_size = (image_h, image_w)

    # get the reference 5 landmarks position in the crop settings
    reference_5pts = get_reference_facial_points(
        output_size, inner_padding_factor, outer_padding, default_square)

    # dst_img = warp_and_crop_face(raw, facial5points)
    dst_img = warp_and_crop_face(raw, facial5points, reference_pts=reference_5pts, crop_size=crop_size)
    return dst_img
def crop_test(image,
              facial5points,
              reference_points=None,
              output_size=(96, 112),
              align_type='similarity'):
    # dst_img = transform_and_crop_face(image, facial5points, coord5points, imgSize)

    dst_img = warp_and_crop_face(image, facial5points, reference_points,
                                 output_size, align_type)

    print('warped image shape: ', dst_img.shape)

    # swap BGR to RGB to show image by pyplot
    dst_img_show = dst_img[..., ::-1]

    plt.figure()
    plt.title(align_type + ' transform ' + str(output_size))
    plt.imshow(dst_img_show)
    plt.show()
def process(img, output_size):
    _, facial5points = detector.detect_faces(img)
    facial5points = np.reshape(facial5points[0], (2, 5))

    default_square = True
    inner_padding_factor = 0.25
    outer_padding = (0, 0)

    # get the reference 5 landmarks position in the crop settings
    reference_5pts = get_reference_facial_points(output_size,
                                                 inner_padding_factor,
                                                 outer_padding, default_square)

    # dst_img = warp_and_crop_face(raw, facial5points, reference_5pts, crop_size)
    dst_img = warp_and_crop_face(raw,
                                 facial5points,
                                 reference_pts=reference_5pts,
                                 crop_size=output_size)
    cv.imwrite(
        'images/{}_mtcnn_aligned_{}x{}.jpg'.format(i, output_size[0],
                                                   output_size[1]), dst_img)
Beispiel #8
0
def embed_faces(annotations, filename=None, image=None):
    global face_model
    if len(annotations) < 1:
        return True
    with torch.no_grad():
        if image is None and filename is not None:
            image = cv2.imread(filename)
            if image is None:
                return
            image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        if image is None:
            return False
        face_model = store_arcface_model.get()
        device = store_arcface_model.loaded_device
        images = torch.zeros(len(annotations), 3, 112, 112).to(device)
        for i, annotation in enumerate(annotations):
            face = warp_and_crop_face(image, annotation['landmarks'], reference_pts=align_faces.REFERENCE_FACIAL_POINTS_112, crop_size=(112,112))
            images[i] = face_transforms(face).to(device)
        embedding = face_model(images)
        embedding = embedding / embedding.norm(dim=-1, keepdim=True)
        embedding = embedding.cpu().numpy()
        for i, annotation in enumerate(annotations):
            annotation['embedding'] = embedding[i]
        return True
def process(filename, type, savafilename, output_size):
    img = cv2.imread(filename)
    bbox, facial5points = detector.detect_faces(img)
    if bbox.size:
        # # visualization
        # draw_bbox(filename, bbox)
        # draw_lms(filename, facial5points)
        # draw_all(filename, bbox, facial5points)

        # # show ref_5pts
        if output_size[0] == 112 and output_size[1] == 112:
            tmp_pts = np.array([[
                38.29459953, 73.53179932, 56.02519989, 41.54930115,
                70.72990036, 51.69630051, 51.50139999, 71.73660278, 92.3655014,
                92.20410156
            ]])
            empty_face = np.zeros(
                (output_size[1], output_size[0], 3))  # output_size[0] == 列
            cv2.imwrite('empty_face.jpg', empty_face)
            draw_lms('empty_face.jpg', tmp_pts)
        elif output_size[0] == 96 and output_size[1] == 112:
            tmp_pts = np.array([[
                30.29459953, 65.53179932, 48.02519989, 33.54930115,
                62.72990036, 51.69630051, 51.50139999, 71.73660278, 92.3655014,
                92.20410156
            ]])
            empty_face = np.zeros((output_size[1], output_size[0], 3))
            cv2.imwrite(
                'empty_face_' + str(output_size[0]) + '_' +
                str(output_size[1]) + '.jpg', empty_face)
            draw_lms(
                'empty_face_' + str(output_size[0]) + '_' +
                str(output_size[1]) + '.jpg', tmp_pts)
        elif output_size[0] == 120 and output_size[1] == 120:
            tmp_pts = np.array([[
                42.29459953, 77.53179932, 60.02519989, 45.54930115,
                74.72990036, 63.69630051, 63.50139999, 83.73660278,
                104.3655014, 104.20410156
            ]])
            empty_face = np.zeros((output_size[1], output_size[0], 3))
            cv2.imwrite(
                'empty_face_' + str(output_size[0]) + '_' +
                str(output_size[1]) + '.jpg', empty_face)
            draw_lms(
                'empty_face_' + str(output_size[0]) + '_' +
                str(output_size[1]) + '.jpg', tmp_pts)

        default_square = True
        inner_padding_factor = 0.25
        outer_padding = (0, 0)
        # get the reference 5 landmarks position in the crop settings
        reference_5pts = get_reference_facial_points(output_size,
                                                     inner_padding_factor,
                                                     outer_padding,
                                                     default_square)

        if (len(bbox) > 0):
            if type == 'labeled':
                # find the max bbox
                max_bb = []
                for box in bbox:
                    x, y, r, b, _ = list(map(int, box))
                    w = r - x + 1
                    h = b - y + 1
                    max_bb.append(w * h)
                # 得到最大值的序号
                index = max_bb.index(max(max_bb))
                facial5points = facial5points[[index]]
                facial5point = np.reshape(facial5points, (2, 5))
                dst_img = warp_and_crop_face(img,
                                             facial5point,
                                             reference_pts=reference_5pts,
                                             crop_size=output_size)
                # dst_name = filename[: filename.rfind('.')]
                dst = filename.split("\\")
                dst_dir = savafilename + '/train/' + dst[1] + '/' + dst[2] + '/'
                if not os.path.exists(dst_dir):
                    os.makedirs(dst_dir)
                dst_dir = dst_dir + dst[3][:dst[3].rfind('.')]
                cv2.imwrite(
                    '{}_{}_mtcnn_aligned_{}x{}.jpg'.format(
                        dst_dir, type, output_size[0], output_size[1]),
                    dst_img)
            elif type == 'unlabelled':
                for i in range(bbox.shape[0]):
                    facial5point = np.reshape(facial5points[i], (2, 5))
                    dst_img = warp_and_crop_face(img,
                                                 facial5point,
                                                 reference_pts=reference_5pts,
                                                 crop_size=output_size)
                    dst = filename.split("\\")
                    dst_dir = savafilename + '/train/' + dst[1] + '/' + dst[
                        2] + '/'
                    if not os.path.exists(dst_dir):
                        os.makedirs(dst_dir)
                    dst_dir = dst_dir + dst[3][:dst[3].rfind('.')]
                    cv2.imwrite(
                        '{}_{}_mtcnn_aligned_{}x{}_{}.jpg'.format(
                            dst_dir, type, output_size[0], output_size[1], i),
                        dst_img)

    else:
        file_write_obj = open("bbox-none-name.txt", 'w')
        file_write_obj.writelines(filename)  # write 写入
        file_write_obj.write('\n')
        file_write_obj.close()
Beispiel #10
0
import numpy as np
from PIL import Image

from align_faces import warp_and_crop_face, get_reference_facial_points
from mtcnn.detector import detect_faces

if __name__ == "__main__":
    for i in range(10):
        img_fn = 'images/{}_raw.jpg'.format(i)
        print('Loading image {}'.format(img_fn))
        raw = cv.imread(img_fn, True)
        img = Image.open(img_fn).convert('RGB')
        _, facial5points = detect_faces(img)
        facial5points = np.reshape(facial5points[0], (2, 5))
        crop_size = (224, 224)

        default_square = True
        inner_padding_factor = 0.25
        outer_padding = (0, 0)
        output_size = (224, 224)

        # get the reference 5 landmarks position in the crop settings
        reference_5pts = get_reference_facial_points(
            output_size, inner_padding_factor, outer_padding, default_square)

        # dst_img = warp_and_crop_face(raw, facial5points, reference_5pts, crop_size)
        dst_img = warp_and_crop_face(raw, facial5points, reference_pts=reference_5pts, crop_size=crop_size)
        cv.imwrite('images/{}_warped.jpg'.format(i), dst_img)
        img = cv.resize(raw, (224, 224))
        cv.imwrite('images/{}_img.jpg'.format(i), img)
Beispiel #11
0
def face_align_similarity_transformation(img, facial_5_points, dir_path,
                                         image_path, dataset):
    if facial_5_points != None:
        print(facial_5_points)
        # #facial_5_points = np.array(facial_5_points, dtype=np.float32)
        facial_5_points = np.array([
            facial_5_points['left_eye'], facial_5_points['right_eye'],
            facial_5_points['nose'], facial_5_points['mouth_left'],
            facial_5_points['mouth_right']
        ],
                                   dtype=np.float32)

        #
        # coord_5_points = np.array([
        #     [30.2946, 51.6963],
        #     [65.5318, 51.5014],
        #     [48.0252, 71.7366],
        #     [33.5493, 92.3655],
        #     [62.7299, 92.2041]], dtype=np.float32)
        #
        # transform = trans.SimilarityTransform()
        # transform.estimate(facial_5_points, coord_5_points)
        # M = transform.params[0:2,:]
        # width = img.shape[1]
        # height = img.shape[0]
        # warped = cv2.warpAffine(img, M, (48,48), borderValue=(255,255,255))

        default_square = True
        inner_padding_factor = 0.001
        outer_padding = (0, 0)
        output_size = (48, 48)

        reference_5pts = get_reference_facial_points(output_size,
                                                     inner_padding_factor,
                                                     outer_padding,
                                                     default_square)

        dst_img = warp_and_crop_face(img,
                                     facial_5_points,
                                     reference_pts=reference_5pts,
                                     crop_size=(48, 48))
        # transform = trans.SimilarityTransform()
        # transform.estimate(facial_5_points, reference_5pts)
        # M = transform.params[0:2,:]
        #
        # warped = cv2.warpAffine(img, M, output_size, borderValue=0)

        ##RGB to gray
        dst_img = cv2.cvtColor(dst_img, cv2.COLOR_BGR2GRAY)
        folder = os.path.exists(
            "/home/ubuntu/face-emotion-detection/data/face-alignment-{}/{}".
            format(dataset, dir_path))
        if not folder:
            os.makedirs(
                "/home/ubuntu/face-emotion-detection/data/face-alignment-{}/{}"
                .format(dataset, dir_path))
        cv2.imwrite(
            "/home/ubuntu/face-emotion-detection/data/face-alignment-{}/{}/{}".
            format(dataset, dir_path, image_path), dst_img)

    else:
        img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        folder = os.path.exists(
            "/home/ubuntu/face-emotion-detection/data/face-alignment-{}/{}".
            format(dataset, dir_path))
        if not folder:
            os.makedirs(
                "/home/ubuntu/face-emotion-detection/data/face-alignment-{}/{}"
                .format(dataset, dir_path))
        cv2.imwrite(
            "/home/ubuntu/face-emotion-detection/data/face-alignment-{}/{}/{}".
            format(dataset, dir_path, image_path), img)