def reload_images(self, group_method, img_list): """ Reloads the image list by replacing the comparative values with those that the chosen grouping method expects. :param group_method: str name of the grouping method that will be used. :param img_list: image list that has been sorted by one of the sort methods. :return: img_list but with the comparative values that the chosen grouping method expects. """ input_dir = self.args.input_dir print("Preparing to group...") if group_method == 'group_blur': temp_list = [[x, self.estimate_blur(cv2.imread(x))] for x in tqdm( self.find_images(input_dir), desc="Reloading", file=sys.stdout) ] elif group_method == 'group_face': temp_list = [[ x, face_recognition.face_encodings(cv2.imread(x)) ] for x in tqdm( self.find_images(input_dir), desc="Reloading", file=sys.stdout) ] elif group_method == 'group_face_cnn': temp_list = [] for x in tqdm(self.find_images(input_dir), desc="Reloading", file=sys.stdout): d = face_alignment.Extract( cv2.imread(x), 'dlib-cnn', True, input_is_predetected_face=True).landmarks temp_list.append([ x, np.array(d[0][1]) if len(d) > 0 else np.zeros((68, 2)) ]) elif group_method == 'group_face_yaw': temp_list = [] for x in tqdm(self.find_images(input_dir), desc="Reloading", file=sys.stdout): d = face_alignment.Extract( cv2.imread(x), 'dlib-cnn', True, input_is_predetected_face=True).landmarks temp_list.append( [x, self.calc_landmarks_face_yaw(np.array(d[0][1]))]) elif group_method == 'group_hist': temp_list = [[ x, cv2.calcHist([cv2.imread(x)], [0], None, [256], [0, 256]) ] for x in tqdm( self.find_images(input_dir), desc="Reloading", file=sys.stdout) ] else: raise ValueError("{} group_method not found.".format(group_method)) return self.splice_lists(img_list, temp_list)
def detect_faces(frame, detector, verbose, rotation=0, mtcnn_kwargs=None): """ Detect faces and draw landmarks in an image """ face_detect = face_alignment.Extract(frame, detector, mtcnn_kwargs, verbose) for face in face_detect.landmarks: ax_x, ax_y = face[0][0], face[0][1] right, bottom = face[0][2], face[0][3] landmarks = face[1] yield DetectedFace(frame[ax_y:bottom, ax_x:right], rotation, ax_x, right - ax_x, ax_y, bottom - ax_y, landmarksXY=landmarks)
def sort_face_yaw(self): input_dir = self.args.input_dir img_list = [] for x in tqdm(self.find_images(input_dir), desc="Loading", file=sys.stdout): d = face_alignment.Extract( cv2.imread(x), 'dlib-cnn', True, input_is_predetected_face=True).landmarks img_list.append( [x, self.calc_landmarks_face_yaw(np.array(d[0][1]))]) print("Sorting by face-yaw...") img_list = sorted(img_list, key=operator.itemgetter(1), reverse=True) return img_list
def detect_faces(frame, detector, verbose, rotation=0, mtcnn_kwargs=None): """ Detect faces and draw landmarks in an image """ face_detect = face_alignment.Extract(frame, detector, mtcnn_kwargs, verbose) # --- Changes --- # If we have a center, take the face that is closest to previous center if detect_faces.prev_center is None: print('WARNING: Static variable in detect_faces, only extract one ' 'sequence per faceswap.py call') # Find biggest face by looking at y bounding box size, 0 is biggest detect_landmarks = face_detect.landmarks detect_landmarks = sorted(detect_landmarks, key=lambda x: x[0][1] - x[0][3]) face_detect.landmarks = [detect_landmarks[0]] else: closest = float('inf') closest_face_idx = -1 closest_center = None for i, landmark in enumerate(face_detect.landmarks): center = compute_center(landmark) length = np.linalg.norm( np.array(center) - np.array(detect_faces.prev_center)) if length < closest: closest = length closest_face_idx = i closest_center = center detect_faces.prev_center = closest_center face_detect.landmarks = [face_detect.landmarks[closest_face_idx]] detect_faces.prev_center = compute_center(face_detect.landmarks[0]) # --- Changes --- for face in face_detect.landmarks: ax_x, ax_y = face[0][0], face[0][1] right, bottom = face[0][2], face[0][3] landmarks = face[1] yield DetectedFace(frame[ax_y:bottom, ax_x:right], rotation, ax_x, right - ax_x, ax_y, bottom - ax_y, landmarksXY=landmarks)
def sort_face_yaw(self): """ Sort by yaw of face """ input_dir = self.args.input_dir img_list = [] for img in tqdm(self.find_images(input_dir), desc="Loading", file=sys.stdout): landmarks = face_alignment.Extract( input_image_bgr=cv2.imread(img), detector='dlib-cnn', verbose=True, input_is_predetected_face=True).landmarks img_list.append( [img, self.calc_landmarks_face_yaw(np.array(landmarks[0][1]))]) print("Sorting by face-yaw...") img_list = sorted(img_list, key=operator.itemgetter(1), reverse=True) return img_list
def sort_face_cnn_dissim(self): """ Sort by dlib CNN dissimilarity """ input_dir = self.args.input_dir print("Sorting by face-cnn dissimilarity...") img_list = [] for img in tqdm(self.find_images(input_dir), desc="Loading", file=sys.stdout): landmarks = face_alignment.Extract( input_image_bgr=cv2.imread(img), detector='dlib-cnn', verbose=True, input_is_predetected_face=True).landmarks img_list.append([ img, np.array(landmarks[0][1]) if landmarks else np.zeros((68, 2)), 0 ]) img_list_len = len(img_list) for i in tqdm(range(0, img_list_len - 1), desc="Sorting", file=sys.stdout): score_total = 0 for j in range(i + 1, len(img_list)): if i == j: continue fl1 = img_list[i][1] fl2 = img_list[j][1] score_total += np.sum(np.absolute((fl2 - fl1).flatten())) img_list[i][2] = score_total print("Sorting...") img_list = sorted(img_list, key=operator.itemgetter(2), reverse=True) return img_list
def sort_face_cnn(self): """ Sort by dlib CNN similarity """ input_dir = self.args.input_dir print("Sorting by face-cnn similarity...") img_list = [] for img in tqdm(self.find_images(input_dir), desc="Loading", file=sys.stdout): landmarks = face_alignment.Extract( input_image_bgr=cv2.imread(img), detector='dlib-cnn', verbose=True, input_is_predetected_face=True).landmarks img_list.append([img, np.array(landmarks[0][1]) if landmarks else np.zeros((68, 2))]) img_list_len = len(img_list) for i in tqdm(range(0, img_list_len - 1), desc="Sorting", file=sys.stdout): min_score = float("inf") j_min_score = i + 1 for j in range(i + 1, len(img_list)): fl1 = img_list[i][1] fl2 = img_list[j][1] score = np.sum(np.absolute((fl2 - fl1).flatten())) if score < min_score: min_score = score j_min_score = j (img_list[i + 1], img_list[j_min_score]) = (img_list[j_min_score], img_list[i + 1]) return img_list
def sort_face_cnn(self): input_dir = self.args.input_dir print("Sorting by face-cnn similarity...") img_list = [] for x in tqdm(self.find_images(input_dir), desc="Loading", file=sys.stdout): d = face_alignment.Extract( cv2.imread(x), 'dlib-cnn', True, input_is_predetected_face=True).landmarks img_list.append( [x, np.array(d[0][1]) if len(d) > 0 else np.zeros((68, 2))]) img_list_len = len(img_list) for i in tqdm(range(0, img_list_len - 1), desc="Sorting", file=sys.stdout): min_score = float("inf") j_min_score = i + 1 for j in range(i + 1, len(img_list)): fl1 = img_list[i][1] fl2 = img_list[j][1] score = np.sum(np.absolute((fl2 - fl1).flatten())) if score < min_score: min_score = score j_min_score = j img_list[i + 1] = img_list[j_min_score] img_list[j_min_score] = img_list[i + 1] return img_list