def upgradeToFaceSamples ( samples ): sample_list = [] for s in io.progress_bar_generator(samples, "Loading"): s_filename_path = Path(s.filename) try: if s_filename_path.suffix == '.png': dflimg = DFLPNG.load ( str(s_filename_path) ) elif s_filename_path.suffix == '.jpg': dflimg = DFLJPG.load ( str(s_filename_path) ) else: dflimg = None if dflimg is None: print ("%s is not a dfl image file required for training" % (s_filename_path.name) ) continue pitch, yaw = LandmarksProcessor.estimate_pitch_yaw ( dflimg.get_landmarks() ) sample_list.append( s.copy_and_set(sample_type=SampleType.FACE, face_type=FaceType.fromString (dflimg.get_face_type()), shape=dflimg.get_shape(), landmarks=dflimg.get_landmarks(), pitch=pitch, yaw=yaw) ) except: print ("Unable to load %s , error: %s" % (str(s_filename_path), traceback.format_exc() ) ) return sample_list
def process_data(self, data): filepath = Path(data[0]) try: if filepath.suffix == '.png': dflimg = DFLPNG.load(str(filepath)) elif filepath.suffix == '.jpg': dflimg = DFLJPG.load(str(filepath)) else: dflimg = None if dflimg is None: self.log_err("%s is not a dfl image file" % (filepath.name)) return [1, [str(filepath)]] bgr = cv2_imread(str(filepath)) if bgr is None: raise Exception("Unable to load %s" % (filepath.name)) gray = cv2.cvtColor(bgr, cv2.COLOR_BGR2GRAY) sharpness = estimate_sharpness( gray) if self.include_by_blur else 0 pitch, yaw = LandmarksProcessor.estimate_pitch_yaw( dflimg.get_landmarks()) hist = cv2.calcHist([gray], [0], None, [256], [0, 256]) except Exception as e: self.log_err(e) return [1, [str(filepath)]] return [0, [str(filepath), sharpness, hist, yaw]]
def sort_by_face_pitch(input_path): io.log_info("Sorting by face pitch...") img_list = [] trash_img_list = [] for filepath in io.progress_bar_generator( Path_utils.get_image_paths(input_path), "Loading"): filepath = Path(filepath) if filepath.suffix == '.png': dflimg = DFLPNG.load(str(filepath)) elif filepath.suffix == '.jpg': dflimg = DFLJPG.load(str(filepath)) else: dflimg = None if dflimg is None: io.log_err("%s is not a dfl image file" % (filepath.name)) trash_img_list.append([str(filepath)]) continue pitch, yaw = LandmarksProcessor.estimate_pitch_yaw( dflimg.get_landmarks()) img_list.append([str(filepath), pitch]) io.log_info("Sorting...") img_list = sorted(img_list, key=operator.itemgetter(1), reverse=True) return img_list, trash_img_list
def onClientProcessData(self, data): filepath = Path(data[0]) try: if filepath.suffix == '.png': dflimg = DFLPNG.load(str(filepath), print_on_no_embedded_data=True) elif filepath.suffix == '.jpg': dflimg = DFLJPG.load(str(filepath), print_on_no_embedded_data=True) else: print("%s is not a dfl image file" % (filepath.name)) raise Exception("") bgr = cv2_imread(str(filepath)) if bgr is None: raise Exception("Unable to load %s" % (filepath.name)) gray = cv2.cvtColor(bgr, cv2.COLOR_BGR2GRAY) gray_masked = (gray * LandmarksProcessor.get_image_hull_mask( bgr.shape, dflimg.get_landmarks())[:, :, 0]).astype(np.uint8) sharpness = estimate_sharpness(gray_masked) pitch, yaw = LandmarksProcessor.estimate_pitch_yaw( dflimg.get_landmarks()) hist = cv2.calcHist([gray], [0], None, [256], [0, 256]) except Exception as e: print(e) return [1, [str(filepath)]] return [0, [str(filepath), sharpness, hist, yaw]]
def sort_by_face_pitch(input_path): print("Sorting by face pitch...") img_list = [] for filepath in tqdm(Path_utils.get_image_paths(input_path), desc="Loading", ascii=True): filepath = Path(filepath) if filepath.suffix == '.png': dflimg = DFLPNG.load(str(filepath), print_on_no_embedded_data=True) elif filepath.suffix == '.jpg': dflimg = DFLJPG.load(str(filepath), print_on_no_embedded_data=True) else: print("%s is not a dfl image file" % (filepath.name)) continue pitch, yaw = LandmarksProcessor.estimate_pitch_yaw( dflimg.get_landmarks()) img_list.append([str(filepath), pitch]) print("Sorting...") img_list = sorted(img_list, key=operator.itemgetter(1), reverse=True) return img_list
def batch_func(self, generator_id): gen_sq = self.generators_sq[generator_id] samples = self.samples samples_len = len(samples) samples_idxs = [ *range(samples_len) ] [generator_id::self.generators_count] repeat_samples_idxs = [] if len(samples_idxs) == 0: raise ValueError('No training data provided.') if self.sample_type == SampleType.FACE_YAW_SORTED or self.sample_type == SampleType.FACE_YAW_SORTED_AS_TARGET: if all ( [ samples[idx] == None for idx in samples_idxs] ): raise ValueError('Not enough training data. Gather more faces!') if self.sample_type == SampleType.FACE or self.sample_type == SampleType.FACE_WITH_CLOSE_TO_SELF: shuffle_idxs = [] elif self.sample_type == SampleType.FACE_YAW_SORTED or self.sample_type == SampleType.FACE_YAW_SORTED_AS_TARGET: shuffle_idxs = [] shuffle_idxs_2D = [[]]*samples_len while True: while not gen_sq.empty(): idxs = gen_sq.get() for idx in idxs: if idx in samples_idxs: repeat_samples_idxs.append(idx) batches = None for n_batch in range(self.batch_size): while True: sample = None if len(repeat_samples_idxs) > 0: idx = repeat_samples_idxs.pop() if self.sample_type == SampleType.FACE or self.sample_type == SampleType.FACE_WITH_CLOSE_TO_SELF: sample = samples[idx] elif self.sample_type == SampleType.FACE_YAW_SORTED or self.sample_type == SampleType.FACE_YAW_SORTED_AS_TARGET: sample = samples[(idx >> 16) & 0xFFFF][idx & 0xFFFF] else: if self.sample_type == SampleType.FACE or self.sample_type == SampleType.FACE_WITH_CLOSE_TO_SELF: if len(shuffle_idxs) == 0: shuffle_idxs = samples_idxs.copy() np.random.shuffle(shuffle_idxs) idx = shuffle_idxs.pop() sample = samples[ idx ] elif self.sample_type == SampleType.FACE_YAW_SORTED or self.sample_type == SampleType.FACE_YAW_SORTED_AS_TARGET: if len(shuffle_idxs) == 0: shuffle_idxs = samples_idxs.copy() np.random.shuffle(shuffle_idxs) idx = shuffle_idxs.pop() if samples[idx] != None: if len(shuffle_idxs_2D[idx]) == 0: shuffle_idxs_2D[idx] = random.sample( range(len(samples[idx])), len(samples[idx]) ) idx2 = shuffle_idxs_2D[idx].pop() sample = samples[idx][idx2] idx = (idx << 16) | (idx2 & 0xFFFF) if sample is not None: try: x = SampleProcessor.process (sample, self.sample_process_options, self.output_sample_types, self.debug) except: raise Exception ("Exception occured in sample %s. Error: %s" % (sample.filename, traceback.format_exc() ) ) if type(x) != tuple and type(x) != list: raise Exception('SampleProcessor.process returns NOT tuple/list') if batches is None: batches = [ [] for _ in range(len(x)) ] if self.add_sample_idx: batches += [ [] ] i_sample_idx = len(batches)-1 if self.add_pitch: batches += [ [] ] i_pitch = len(batches)-1 if self.add_yaw: batches += [ [] ] i_yaw = len(batches)-1 for i in range(len(x)): batches[i].append ( x[i] ) if self.add_sample_idx: batches[i_sample_idx].append (idx) if self.add_pitch or self.add_yaw: pitch, yaw = LandmarksProcessor.estimate_pitch_yaw (sample.landmarks) if self.add_pitch: batches[i_pitch].append ([pitch]) if self.add_yaw: batches[i_yaw].append ([yaw]) break yield [ np.array(batch) for batch in batches]