示例#1
0
def detect_one_face_align(img):
    crop_size = 112
    scale = crop_size / 112.

    reference = get_reference_facial_points(default_square=True) * scale

    #img = Image.open(os.path.join(image_path))

    landmarks = []
    bounding_boxes = []
    try:
        bounding_boxes, landmarks = detect_faces(img)

    except Exception as e:
        print(e)
    if len(
            landmarks
    ) == 0:  # If the landmarks cannot be detected, the img will be discarded
        return None
    #print(landmarks)

    facial5point = [[landmarks[0][j], landmarks[0][j + 5]] for j in range(5)]
    #print(facial5points[i])
    warped_face = warp_and_crop_face(np.array(img),
                                     facial5point,
                                     reference,
                                     crop_size=(crop_size, crop_size))
    img_warped = Image.fromarray(warped_face)

    # img_warped.save("test.jpg")
    # img_warped.show()
    return img_warped, bounding_boxes
示例#2
0
def detect_face_align(img):
    crop_size = 112  #因为backbone的input_size要[112,112]or[224,224]
    scale = crop_size / 112.
    reference = get_reference_facial_points(default_square=True) * scale

    #img = Image.open(os.path.join(image_path))

    bounding_boxes = []
    warped_face = []
    img_warped = []
    facial5point = []
    landmarks = []
    try:
        bounding_boxes, landmarks = detect_faces(img)
    except Exception as e:
        print(e)
    if len(
            landmarks
    ) == 0:  # If the landmarks cannot be detected, the img will be discarded
        return img_warped, bounding_boxes
    #print(landmarks)
    for i in range(len(landmarks)):
        facial5point.append([[landmarks[i][j], landmarks[i][j + 5]]
                             for j in range(5)])
        #print(facial5points[i])
        warped_face.append(
            warp_and_crop_face(np.array(img),
                               facial5point[i],
                               reference,
                               crop_size=(crop_size, crop_size)))

        img_warped.append(Image.fromarray(warped_face[i]))
        # img_warped.save("test.jpg")
        # img_warped.show()
    return img_warped, bounding_boxes
示例#3
0
 def build(self, image):
     #1.add face detection
     bounding_boxes, landmarks = detect_faces(image)
     if len(bounding_boxes) == 0:
         return None
     #2.add face align
     wrapedfaces = self.warpface(landmarks,image)
     #3.compute face embedding
     return self.embedding(wrapedfaces)
示例#4
0
def main(img_path):
    img = Image.open(img_path)  # modify the image path to yours
    det_models = load_detect_faces_models()
    bounding_boxes, landmarks = detect_faces(
        det_models,
        img)  # detect bboxes and landmarks for all faces in the image

    viz_img = show_results(img, bounding_boxes,
                           landmarks)  # visualize the results
    viz_img.save('out.jpg')

    face_id_model = load_face_id_model()

    for bbox_idx, bbox in enumerate(bounding_boxes):
        tl_x, tl_y, br_x, br_y, prob = bbox.astype(int)
        cropped_img = np.array(img)[tl_y:br_y, tl_x:br_x, :]
        Image.fromarray(cropped_img).save('bbox-{}.jpg'.format(bbox_idx))
        features = extract_feature_for_img(img=cropped_img,
                                           backbone=face_id_model)
        print(features)
示例#5
0
def align_(img, crop_size, model_root, filter=False):

    # check if img is PIL
    if type(img) == np.ndarray:
        img = Image.fromarray(img)

    # path to detector models
    op = "{}/onet.npy".format(model_root)
    pp = "{}/pnet.npy".format(model_root)
    rp = "{}/rnet.npy".format(model_root)

    # settings
    scale = crop_size / 112.
    reference = get_reference_facial_points(default_square=True) * scale

    try:  # Handle exception
        _, landmarks = detect_faces(img, ppath=pp, opath=op, rpath=rp)
    except Exception:
        print("Image is discarded due to exception!")
        return 4
    if len(
            landmarks
    ) == 0:  # If the landmarks cannot be detected, the img will be discarded
        print("Image is discarded due to non-detected landmarks!")
        return 4
    if filter:
        return True
    else:
        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)

        return img_warped
示例#6
0
    source_root = args.source_root # specify your source dir
    dest_root = args.dest_root # specify your destination dir
    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 image_name in tqdm(glob("{}/*".format(source_root))):
        print("Processing\t{}".format(image_name))
        img = Image.open(image_name)
        try: # Handle exception
            _, landmarks = detect_faces(img)
        except Exception:
            print("{} is discarded due to exception!".format(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(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, image_name.split('.')[0] + '.jpg'))
示例#7
0
import numpy as np
import torch
from PIL import Image
from models import IR_50

from align.align_trans import warp_and_crop_face, get_reference_facial_points
from align.detector import detect_faces
from align.visualization_utils import show_results

img = Image.open('testImages/stp.jpeg') # modify the image path to yours
bounding_boxes, landmarks = detect_faces(img) # detect bboxes and landmarks for all faces in the image
image = show_results(img, bounding_boxes, landmarks) # visualize the results
crop_size = 112  # specify size of aligned faces, align and crop with padding
scale = crop_size / 112.
reference = get_reference_facial_points(default_square=True) * scale

model = IR_50((112, 112)).eval()
model.load_state_dict(torch.load('backbone_ir50_ms1m_epoch120.pth'))

for landmark in landmarks:
    facial5points = [[landmark[j], landmark[j + 5]] for j in range(5)]
    warped_face = warp_and_crop_face(np.array(img), facial5points, reference, crop_size=(112, 112))
    img_warped = torch.Tensor(warped_face).unsqueeze(0).transpose(1, 3)
    out = model(img_warped).detach().cpu().numpy()
    norm = np.linalg.norm(out, axis=1)
    out = out / norm
    i = 5



示例#8
0
def main(request):
    """Displays the main page
    
    Arguments:
        request {request} -- GET or POST request
    """
    # SHOW MAIN PAGE
    if request.method == 'GET':
        context = {
            'title': 'Main',
            }
        return render(request, 'index.html', context)
    # GOT NEW PHOTO
    elif request.method == 'POST':
        # INITIAL SEARCH
        if request.POST.get('refine') == "False":
            image = request.FILES.get('photo')
            image_type = image.name.split('.')[-1] #png/jpg/jpeg
            now = datetime.datetime.now()
            image_path = f'{now.day}{now.month}{now.year}_{now.hour}:{now.minute}:{now.second}.{image_type}'
            full_path = os.path.join(MEDIA_PATH, image_path)
            with open(full_path, 'wb+') as destination:
                destination.write(image.read())
            img = Image.open(full_path)
            _, landmarks = detect_faces(img) #TODO: change onet/rnet/pnet path
            if landmarks == []:
                # there are no faces on the photo
                # TODO: send message
                return redirect('Main Page')
            count = landmarks.shape[0]
            if count == 1:
                img = align_face(img, landmarks[0]) # cropped aligned face, ready for search
                D, I = search(img) # distances and indexes
                results_dict = results(D,I)

                context = {
                    'title': 'Search Results',
                    'results_dict': results_dict
                }
                return render(request, 'results.html', context)

            else:
                face_urls = refine_face(img, landmarks, image_path)
                context = {
                    'title': 'Choose face',
                    'faces_list': face_urls,
                }
                return render(request, 'refine.html', context)
        # SEARCH AFTER REFINING
        elif request.POST.get('refine') == "True":
            image_path = request.POST.get('imagecrop') # number selected face
            full_path = os.path.join(MEDIA_PATH, image_path)
            img = Image.open(full_path) # cropped aligned face, ready for search
            D, I = search(img) # distances and indexes
            results_dict = results(D,I)

            context = {
                'title': 'Search Results',
                'results_dict': results_dict
            }
            return render(request, 'results.html', context)
示例#9
0
cv2.setWindowProperty(window, cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)

while (True):
    #Capture frame
    ret, img = capture.read()

    # Get frameRate
    frameRate_counter += 1
    if (time.time() - frameRate_start_time) > frameRate_refresh:
        frameRate_fps = frameRate_counter / (time.time() -
                                             frameRate_start_time)
        frameRate_counter = 0
        frameRate_start_time = time.time()

    # Faces location
    bounding_boxes, landmarks = detector.detect_faces(Image.fromarray(img))
    for i, box in enumerate(bounding_boxes):
        # Set color
        color = colors.astype('int')[i % 10].tolist()
        # Copy just the face
        box = box.astype('int')
        face = img[box[1]:box[3], box[0]:box[2]]

        # Build the face frame
        corner_top_left = (int(box[0]), int(box[1]))
        corner_buttom_right = (int(box[2]), int(box[3]))
        cv2.rectangle(img, corner_top_left, corner_buttom_right, color, 2)

        with torch.no_grad():
            # Fake batch
            face = prepare_face(face)