def calculate_similarity(self, image1, image2): bboxes1 = self.face_detector.detect(image1) bboxes2 = self.face_detector.detect(image2) facial5points1 = [[bboxes1[0][2 * i - 1], bboxes1[0][2 * i]] for i in range(3, 8)] warped_face1 = warp_and_crop_face( cv2.cvtColor(image1, cv2.COLOR_BGR2RGB), facial5points1, self.reference, (self.crop_size, self.crop_size)) facial5points2 = [[bboxes2[0][2 * i - 1], bboxes2[0][2 * i]] for i in range(3, 8)] warped_face2 = warp_and_crop_face( cv2.cvtColor(image2, cv2.COLOR_BGR2RGB), facial5points2, self.reference, (self.crop_size, self.crop_size)) input1 = self.preprocess(warped_face1) input2 = self.preprocess(warped_face2) embedding1 = self.arcface_r50_asian(input1) embedding2 = self.arcface_r50_asian(input2) embedding1 = embedding1.detach().numpy() print(embedding1.shape) embedding2 = embedding2.detach().numpy() cosin = cosine_similarity(self.l2_normalize(embedding1), self.l2_normalize(embedding2)) distance = euclidean_distances(embedding1, embedding2) return cosin[0, 0], distance[0, 0], warped_face1, warped_face2
def warpface(self, landmarks, img): faces = [] for landmark in landmarks: facial5points = [[landmark[j], landmark[j + 5]] for j in range(5)] face = warp_and_crop_face(np.array(img), facial5points, self.reference, crop_size=(self.crop_size, self.crop_size)) faces.append(torchvision.transforms.ToTensor()(face)) return torch.stack(faces)
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
def process_faces(img, det_models, reference, crop_size): """ Note that img is a PIL Image! """ try: # Handle exception bounding_boxes, landmarks = detect_faces(det_models, img) except Exception as e: print("Image is discarded due to exception:\n{}".format(e)) return [] # If the landmarks cannot be detected, the img will be discarded if len(landmarks) == 0: print("No landmarks detected!") return [] else: warped_faces = [] for i, landmark in enumerate(landmarks): facial5points = [[landmark[j], landmark[j + 5]] for j in range(5)] warped_faces.append( warp_and_crop_face(np.array(img), facial5points, reference, crop_size=(crop_size, crop_size))) assert len(warped_faces) == len(bounding_boxes) assert len(warped_faces) == len(landmarks) return [ FaceResult( bounding_box=bounding_boxes[i], landmark=landmarks[i], warped_face=warped_faces[i], ) for i in range(len(warped_faces)) ]
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
def align_68(img, crop_size, model): # check if img is PIL bbox = model.face_detector.detect_from_image(img) if bbox is None: return 4 landmarks = model.get_landmarks(img, bbox) if landmarks is None: return 4 landmarks = landmarks[0] left_eye = np.mean(landmarks[36:42, :], axis=0).astype(np.int) right_eye = np.mean(landmarks[42:48, :], axis=0).astype(np.int) nose = np.mean(landmarks[28:35, :], axis=0).astype(np.int) left_mouth = landmarks[48].astype(np.int) right_mouth = landmarks[54].astype(np.int) # settings scale = crop_size / 112. reference = get_reference_facial_points(default_square=True) * scale facial5points = [left_eye, right_eye, nose, left_mouth, right_mouth] 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
def align_face(img, landmarks, crop_size=112): """Align face on the photo Arguments: img {PIL.Image} -- Image with face landmarks {np.array} -- Key points Keyword Arguments: crop_size {int} -- Size of face (default: {112}) Returns: PIL.Image -- Aligned face """ facial5points = [[landmarks[j], landmarks[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
def align(img, crop_size, img_landmarks): landmarks = img_landmarks[0] left_eye = np.mean(landmarks[36:42, :], axis=0).astype(np.int) right_eye = np.mean(landmarks[42:48, :], axis=0).astype(np.int) nose = np.mean(landmarks[28:35, :], axis=0).astype(np.int) left_mouth = landmarks[48].astype(np.int) right_mouth = landmarks[54].astype(np.int) # settings scale = crop_size / 112. reference = get_reference_facial_points(default_square=True) * scale facial5points = [left_eye, right_eye, nose, left_mouth, right_mouth] 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
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
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'))
arcface_r50_asian.to(device) crop_size = 112 scale = crop_size / 112. reference = get_reference_facial_points(default_square=True) * scale # face detector face_detector = FaceDetector() image_1 = cv2.imread('./4/id.png') image_2 = cv2.imread('./4/selfie.png') bboxes_1 = face_detector.detect(image_1) bboxes_2 = face_detector.detect(image_2) facial5points_1 = [[bboxes_1[0][2*i - 1], bboxes_1[0][2*i]] for i in range(3, 8)] warped_face_1 = warp_and_crop_face(cv2.cvtColor(image_1, cv2.COLOR_BGR2RGB), facial5points_1, reference, crop_size=(crop_size, crop_size)) facial5points_2 = [[bboxes_2[0][2*i - 1], bboxes_2[0][2*i]] for i in range(3, 8)] warped_face_2 = warp_and_crop_face(cv2.cvtColor(image_2, cv2.COLOR_BGR2RGB), facial5points_2, reference, crop_size=(crop_size, crop_size)) """ input_1 = preprocess(warped_face_1) input_2 = preprocess(warped_face_2) """ embedding_1 = arcface_r50_asian(warped_face_1) embedding_2 = arcface_r50_asian(warped_face_2) cosin = cosine_similarity(embedding_1, embedding_2) print("cosin:", cosin) distance = euclidean_distances(embedding_1, embedding_2) print('distance:', distance)
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 = cv2.imread(os.path.join(source_root, subfolder, image_name)) try: bboxes = face_detector.detect(img) except Exception: print("{} is discarded due to exception !".format( os.path.join(source_root, subfolder, image_name))) continue if len(bboxes) == 0: print("{} is discarded due to exception!".format( os.path.join(source_root, subfolder, image_name))) continue count += 1 facial5points = [[bboxes[0][2 * i - 1], bboxes[0][2 * i]] for i in range(3, 8)] warped_face = warp_and_crop_face(cv2.cvtColor( img, cv2.COLOR_BGR2RGB), facial5points, reference, crop_size=(crop_size, crop_size)) cv2.imwrite( os.path.join(dest_root, subfolder, str(count) + ".jpg"), cv2.cvtColor(warped_face, cv2.COLOR_RGB2BGR)) if count == 10: break