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. """ import_face_recognition() 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': import_FaceLandmarksExtractor() temp_list = [] for x in tqdm(self.find_images(input_dir), desc="Reloading", file=sys.stdout): d = FaceLandmarksExtractor.extract( cv2.imread(x), 'cnn', True, input_is_predetected_face=True) temp_list.append([ x, np.array(d[0][1]) if len(d) > 0 else np.zeros((68, 2)) ]) elif group_method == 'group_face_yaw': import_FaceLandmarksExtractor() temp_list = [] for x in tqdm(self.find_images(input_dir), desc="Reloading", file=sys.stdout): d = FaceLandmarksExtractor.extract( cv2.imread(x), 'cnn', True, input_is_predetected_face=True) 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 sort_face_yaw(self): def calc_landmarks_face_pitch(fl): #unused t = ((fl[6][1] - fl[8][1]) + (fl[10][1] - fl[8][1])) / 2.0 b = fl[8][1] return b - t def calc_landmarks_face_yaw(fl): l = ((fl[27][0] - fl[0][0]) + (fl[28][0] - fl[1][0]) + (fl[29][0] - fl[2][0])) / 3.0 r = ((fl[16][0] - fl[27][0]) + (fl[15][0] - fl[28][0]) + (fl[14][0] - fl[29][0])) / 3.0 return r - l import_FaceLandmarksExtractor() input_dir = self.arguments.input_dir img_list = [] for x in tqdm(self.find_images(input_dir), desc="Loading", file=sys.stdout): d = FaceLandmarksExtractor.extract(cv2.imread(x), 'cnn', True, input_is_predetected_face=True) img_list.append([x, calc_landmarks_face_yaw(np.array(d[0][1]))]) print("Sorting...") img_list = sorted(img_list, key=operator.itemgetter(1), reverse=True) return img_list
def sort_face_cnn_dissim(self): from lib import FaceLandmarksExtractor input_dir = self.arguments.input_dir print("Sorting by face-cnn dissimilarity...") img_list = [] for x in tqdm(self.find_images(input_dir), desc="Loading", file=sys.stdout): d = FaceLandmarksExtractor.extract(cv2.imread(x), 'cnn', True) img_list.append( [x, np.array(d[0][1]) if len(d) > 0 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_dissim(self): import_FaceLandmarksExtractor() input_dir = self.args.input_dir print("Sorting by face-cnn dissimilarity...") img_list = [] for x in tqdm(self.find_images(input_dir), desc="Loading", file=sys.stdout): d = FaceLandmarksExtractor.extract(cv2.imread(x), 'cnn', True, input_is_predetected_face=True) img_list.append([x, np.array(d[0][1]) if len(d) > 0 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): import_FaceLandmarksExtractor() 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 = FaceLandmarksExtractor.extract(cv2.imread(x), 'cnn', True, input_is_predetected_face=True) 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
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. """ import_face_recognition() 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': import_FaceLandmarksExtractor() temp_list = [] for x in tqdm(self.find_images(input_dir), desc="Reloading", file=sys.stdout): d = FaceLandmarksExtractor.extract(cv2.imread(x), 'cnn', True, input_is_predetected_face=True) temp_list.append([x, np.array(d[0][1]) if len(d) > 0 else np.zeros((68, 2))]) elif group_method == 'group_face_yaw': import_FaceLandmarksExtractor() temp_list = [] for x in tqdm(self.find_images(input_dir), desc="Reloading", file=sys.stdout): d = FaceLandmarksExtractor.extract(cv2.imread(x), 'cnn', True, input_is_predetected_face=True) 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, model="hog"): fd = FaceLandmarksExtractor.extract(frame, True if model == "cnn" else False) for face in fd: x, y, right, bottom, landmarks = face[0][0], face[0][1], face[0][ 2], face[0][3], face[1] yield DetectedFace(frame[y:bottom, x:right], x, right - x, y, bottom - y, landmarksXY=landmarks)
def sort_face_yaw(self): import_FaceLandmarksExtractor() input_dir = self.args.input_dir img_list = [] for x in tqdm(self.find_images(input_dir), desc="Loading", file=sys.stdout): d = FaceLandmarksExtractor.extract(cv2.imread(x), 'cnn', True, input_is_predetected_face=True) 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 sort_face_yaw(self): import_FaceLandmarksExtractor() input_dir = self.args.input_dir img_list = [] for x in tqdm(self.find_images(input_dir), desc="Loading", file=sys.stdout): d = FaceLandmarksExtractor.extract(cv2.imread(x), 'cnn', True, input_is_predetected_face=True) 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 sort_face_cnn(self): import_FaceLandmarksExtractor() input_dir = self.arguments.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 = FaceLandmarksExtractor.extract(cv2.imread(x), 'cnn', True, input_is_predetected_face=True) 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
def sort_face_yaw(self): def calc_landmarks_face_pitch(fl): # unused t = ((fl[6][1] - fl[8][1]) + (fl[10][1] - fl[8][1])) / 2.0 b = fl[8][1] return b - t def calc_landmarks_face_yaw(fl): l = ((fl[27][0] - fl[0][0]) + (fl[28][0] - fl[1][0]) + (fl[29][0] - fl[2][0])) / 3.0 r = ((fl[16][0] - fl[27][0]) + (fl[15][0] - fl[28][0]) + (fl[14][0] - fl[29][0])) / 3.0 return r - l import_FaceLandmarksExtractor() input_dir = self.args.input_dir img_list = [] for x in tqdm(self.find_images(input_dir), desc="Loading", file=sys.stdout): d = FaceLandmarksExtractor.extract(cv2.imread(x), 'cnn', True, input_is_predetected_face=True) img_list.append([x, calc_landmarks_face_yaw(np.array(d[0][1]))]) print("Sorting...") img_list = sorted(img_list, key=operator.itemgetter(1), reverse=True) return img_list
def detect_faces(frame, detector, verbose, rotation=0): fd = FaceLandmarksExtractor.extract (frame, detector, verbose) for face in fd: x, y, right, bottom, landmarks = face[0][0], face[0][1], face[0][2], face[0][3], face[1] yield DetectedFace(frame[y: bottom, x: right], rotation, x, right - x, y, bottom - y, landmarksXY=landmarks)