def sort_by_blur(input_path): io.log_info("Sorting by blur...") img_list = [(filename, []) for filename in Path_utils.get_image_paths(input_path)] img_list, trash_img_list = BlurEstimatorSubprocessor(img_list).run() io.log_info("Sorting...") img_list = sorted(img_list, key=operator.itemgetter(1), reverse=True) return img_list, trash_img_list
def sort_by_blur(input_path): img_list = [] print ("Sorting by blur...") for filepath in tqdm( Path_utils.get_image_paths(input_path), desc="Loading"): #never mask it by face hull, it worse than whole image blur estimate img_list.append ( [filepath, estimate_blur (cv2.imread( filepath ))] ) print ("Sorting...") img_list = sorted(img_list, key=operator.itemgetter(1), reverse=True) return img_list
def sort_by_hue(input_path): io.log_info("Sorting by hue...") img_list = [[ x, np.mean( cv2.cvtColor(cv2_imread(x), cv2.COLOR_BGR2HSV)[..., 0].flatten()) ] for x in io.progress_bar_generator( Path_utils.get_image_paths(input_path), "Loading")] io.log_info("Sorting...") img_list = sorted(img_list, key=operator.itemgetter(1), reverse=True) return img_list
def load(sample_type, samples_path): samples_cache = SampleHost.samples_cache if str(samples_path) not in samples_cache.keys(): samples_cache[str(samples_path)] = [None] * SampleType.QTY samples = samples_cache[str(samples_path)] if sample_type == SampleType.IMAGE: if samples[sample_type] is None: samples[sample_type] = [ Sample(filename=filename) for filename in io.progress_bar_generator( Path_utils.get_image_paths(samples_path), "Loading") ] elif sample_type == SampleType.FACE: if samples[sample_type] is None: try: result = samplelib.PackedFaceset.load(samples_path) except: io.log_err( f"Error occured while loading samplelib.PackedFaceset.load {str(samples_dat_path)}, {traceback.format_exc()}" ) if result is not None: io.log_info( f"Loaded {len(result)} packed faces from {samples_path}" ) if result is None: result = SampleHost.load_face_samples( Path_utils.get_image_paths(samples_path)) samples[sample_type] = result elif sample_type == SampleType.FACE_TEMPORAL_SORTED: result = SampleHost.load(SampleType.FACE, samples_path) result = SampleHost.upgradeToFaceTemporalSortedSamples(result) samples[sample_type] = result return samples[sample_type]
def get_training_data(self, dtype): if not isinstance(dtype, TrainingDataType): raise Exception('get_training_data dtype is not TrainingDataType') if dtype == TrainingDataType.SRC: if self.training_datas[dtype] is None: self.training_datas[dtype] = X_LOAD([ TrainingDataSample(filename=filename) for filename in Path_utils.get_image_paths(self.training_data_src_path) ]) return self.training_datas[dtype] elif dtype == TrainingDataType.DST: if self.training_datas[dtype] is None: self.training_datas[dtype] = X_LOAD([ TrainingDataSample(filename=filename) for filename in Path_utils.get_image_paths(self.training_data_dst_path) ]) return self.training_datas[dtype] elif dtype == TrainingDataType.SRC_WITH_NEAREST: if self.training_datas[dtype] is None: self.training_datas[dtype] = X_WITH_NEAREST_Y( self.get_training_data(TrainingDataType.SRC), self.get_training_data(TrainingDataType.DST)) return self.training_datas[dtype] elif dtype == TrainingDataType.SRC_ONLY_10_NEAREST_TO_DST_ONLY_1: if self.training_datas[dtype] is None: self.training_datas[dtype] = X_ONLY_n_NEAREST_TO_Y_ONLY_1( self.get_training_data(TrainingDataType.SRC), 10, self.get_training_data(TrainingDataType.DST_ONLY_1)) return self.training_datas[dtype] elif dtype == TrainingDataType.DST_ONLY_1: if self.training_datas[dtype] is None: self.training_datas[dtype] = X_ONLY_1( self.get_training_data(TrainingDataType.DST)) return self.training_datas[dtype] return None
def sort_by_black(input_path): io.log_info ("Sorting by amount of black pixels...") img_list = [] for x in io.progress_bar_generator( Path_utils.get_image_paths(input_path), "Loading"): img = cv2_imread(x) img_list.append ([x, img[(img == 0)].size ]) io.log_info ("Sorting...") img_list = sorted(img_list, key=operator.itemgetter(1), reverse=False) return img_list
def recover_original_aligned_filename(input_path): io.log_info ("Recovering original aligned filename...") files = [] for filepath in io.progress_bar_generator( Path_utils.get_image_paths(input_path), "Processing"): filepath = Path(filepath) dflimg = DFLIMG.load (filepath) if dflimg is None: io.log_err ("%s is not a dfl image file" % (filepath.name) ) continue files += [ [filepath, None, dflimg.get_source_filename(), False] ] files_len = len(files) for i in io.progress_bar_generator( range(files_len), "Sorting" ): fp, _, sf, converted = files[i] if converted: continue sf_stem = Path(sf).stem files[i][1] = fp.parent / ( sf_stem + '_0' + fp.suffix ) files[i][3] = True c = 1 for j in range(i+1, files_len): fp_j, _, sf_j, converted_j = files[j] if converted_j: continue if sf_j == sf: files[j][1] = fp_j.parent / ( sf_stem + ('_%d' % (c)) + fp_j.suffix ) files[j][3] = True c += 1 for file in io.progress_bar_generator( files, "Renaming", leave=False ): fs, _, _, _ = file dst = fs.parent / ( fs.stem + '_tmp' + fs.suffix ) try: fs.rename (dst) except: io.log_err ('fail to rename %s' % (fs.name) ) for file in io.progress_bar_generator( files, "Renaming" ): fs, fd, _, _ = file fs = fs.parent / ( fs.stem + '_tmp' + fs.suffix ) try: fs.rename (fd) except: io.log_err ('fail to rename %s' % (fs.name) )
def get_pitch_yaw_roll(input_path, r=0.05): import os import numpy as np import cv2 from shutil import copyfile from pathlib import Path from utils import Path_utils from utils.DFLPNG import DFLPNG from utils.DFLJPG import DFLJPG from facelib import LandmarksProcessor from joblib import Subprocessor import multiprocessing from interact import interact as io from imagelib import estimate_sharpness io.log_info("Sorting by face yaw...") 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, roll = LandmarksProcessor.estimate_pitch_yaw_roll( dflimg.get_landmarks()) img_list.append([str(filepath), pitch, yaw, roll]) img_list.sort(key=lambda item: item[1]) with open(os.path.join(input_path, "_pitch_yaw_roll.csv"), "w") as f: for i in img_list: f.write("%s,%f,%f,%f\n" % (os.path.basename(i[0]), i[1], i[2], i[3])) import cv width = 800 img = cv.cv_new((width, width)) xs = [i[1] for i in img_list] ys = [i[2] for i in img_list] cs = [(128, 128, 128)] * len(xs) rs = [int(r * width / 2)] * len(xs) cv.cv_scatter(img, xs, ys, [-1, 1], [-1, 1], cs, rs) cs = [(0xcc, 0x66, 0x33)] * len(xs) rs = [2] * len(xs) cv.cv_scatter(img, xs, ys, [-1, 1], [-1, 1], cs, rs) cv.cv_save(img, os.path.join(input_path, "_pitch_yaw_roll.bmp")) return img_list
def sort_by_black(input_path): print("Sorting by amount of black pixels...") img_list = [] for x in tqdm(Path_utils.get_image_paths(input_path), desc="Loading"): img = cv2.imread(x) img_list.append([x, img[(img == 0)].size]) print("Sorting...") img_list = sorted(img_list, key=operator.itemgetter(1), reverse=False) return img_list
def extract_video(input_file, output_dir, output_ext=None, fps=None): input_file_path = Path(input_file) output_path = Path(output_dir) if not output_path.exists(): output_path.mkdir(exist_ok=True) if input_file_path.suffix == '.*': input_file_path = Path_utils.get_first_file_by_stem( input_file_path.parent, input_file_path.stem) else: if not input_file_path.exists(): input_file_path = None if input_file_path is None: io.log_err("input_file not found.") return if fps is None: fps = io.input_int( "Enter FPS ( ?:help skip:fullfps ) : ", 0, help_message= "How many frames of every second of the video will be extracted.") if output_ext is None: output_ext = io.input_str( "Output image format? ( jpg png ?:help skip:png ) : ", "png", ["png", "jpg"], help_message= "png is lossless, but extraction is x10 slower for HDD, requires x10 more disk space than jpg." ) for filename in Path_utils.get_image_paths(output_path, ['.' + output_ext]): Path(filename).unlink() job = ffmpeg.input(str(input_file_path)) kwargs = {'pix_fmt': 'rgb24'} if fps != 0: kwargs.update({'r': str(fps)}) if output_ext == 'jpg': kwargs.update({'q:v': '2'}) #highest quality for jpg job = job.output(str(output_path / ('%5d.' + output_ext)), **kwargs) try: job = job.run() except: io.log_err("ffmpeg fail, job commandline:" + str(job.compile()))
def pack(samples_path): samples_dat_path = samples_path / packed_faceset_filename if samples_dat_path.exists(): io.log_info(f"{samples_dat_path} : file already exists !") io.input_bool("Press enter to continue and overwrite.", False) of = open(samples_dat_path, "wb") image_paths = Path_utils.get_image_paths(samples_path) samples = samplelib.SampleHost.load_face_samples(image_paths) samples_len = len(samples) samples_configs = [] for sample in samples: sample.filename = str(Path(sample.filename).relative_to(samples_path)) samples_configs.append ( sample.get_config() ) samples_bytes = pickle.dumps(samples_configs, 4) of.write ( struct.pack ("Q", PackedFaceset.VERSION ) ) of.write ( struct.pack ("Q", len(samples_bytes) ) ) of.write ( samples_bytes ) sample_data_table_offset = of.tell() of.write ( bytes( 8*(samples_len+1) ) ) #sample data offset table data_start_offset = of.tell() offsets = [] for sample in io.progress_bar_generator(samples, "Packing"): try: with open( samples_path / sample.filename, "rb") as f: b = f.read() offsets.append ( of.tell() - data_start_offset ) of.write(b) except: raise Exception(f"error while processing sample {sample.filename}") offsets.append ( of.tell() ) of.seek(sample_data_table_offset, 0) for offset in offsets: of.write ( struct.pack("Q", offset) ) of.seek(0,2) of.close() for filename in io.progress_bar_generator(image_paths,"Deleting"): Path(filename).unlink()
def extract_video(input_file, output_dir, output_ext=None, fps=None): input_file_path = Path(input_file) output_path = Path(output_dir) if not output_path.exists(): output_path.mkdir(exist_ok=True) if input_file_path.suffix == '.*': input_file_path = Path_utils.get_first_file_by_stem( input_file_path.parent, input_file_path.stem) else: if not input_file_path.exists(): input_file_path = None if input_file_path is None: io.log_err("input_file not found.") return if fps is None: fps = io.input_int("输入帧率[FPS] ( ?:帮助 跳过:默认帧率 ) : ", 0, help_message="FPS是指每秒多少张图片,一般视频为24,推荐输入12") if output_ext is None: output_ext = io.input_str( "输出格式? ( jpg还是png ?:帮助 默认为png ) : ", "png", ["png", "jpg"], help_message="png 为无损格式, 但是比JPG慢10倍, 空间也比JPG大十倍,建议使用JPG格式.") for filename in Path_utils.get_image_paths(output_path, ['.' + output_ext]): Path(filename).unlink() job = ffmpeg.input(str(input_file_path)) kwargs = {'pix_fmt': 'rgb24'} if fps != 0: kwargs.update({'r': str(fps)}) if output_ext == 'jpg': kwargs.update({'q:v': '2'}) #highest quality for jpg job = job.output(str(output_path / ('%5d.' + output_ext)), **kwargs) try: job = job.run() except: io.log_err("ffmpeg 调用失败, 错误提示:" + str(job.compile()))
def final_process(input_path, img_list, trash_img_list): if len(trash_img_list) != 0: parent_input_path = input_path.parent trash_path = parent_input_path / (input_path.stem + '_trash') trash_path.mkdir(exist_ok=True) io.log_info("Trashing %d items to %s" % (len(trash_img_list), str(trash_path))) for filename in Path_utils.get_image_paths(trash_path): Path(filename).unlink() for i in io.progress_bar_generator(range(len(trash_img_list)), "Moving trash", leave=False): src = Path(trash_img_list[i][0]) dst = trash_path / src.name try: src.rename(dst) except: io.log_info('fail to trashing %s' % (src.name)) io.log_info("") if len(img_list) != 0: for i in io.progress_bar_generator([*range(len(img_list))], "Renaming", leave=False): src = Path(img_list[i][0]) dst = "%s/%.5d_%s" % (input_path, i, src.name) # dst = input_path / ('%.5d_%s' % (i, src.name )) try: src.rename(dst) except: io.log_info('fail to rename %s' % (src.name)) for i in io.progress_bar_generator([*range(len(img_list))], "Renaming"): src = Path(img_list[i][0]) src = "%s/%.5d_%s" % (input_path, i, src.name) src = Path(src) dst = "%s/%.5d%s" % (input_path, i, src.suffix) # src = input_path / ('%.5d_%s' % (i, src.name)) # dst = input_path / ('%.5d%s' % (i, src.suffix)) try: src.rename(dst) except: io.log_info('fail to rename %s' % (src.name))
def dev_test(input_dir): input_path = Path(input_dir) dir_names = Path_utils.get_all_dir_names(input_path) for dir_name in io.progress_bar_generator(dir_names, desc="Processing"): img_paths = Path_utils.get_image_paths (input_path / dir_name) for filename in img_paths: filepath = Path(filename) dflimg = DFLIMG.load (filepath) if dflimg is None: raise ValueError dflimg.embed_and_set(filename, person_name=dir_name)
def sort_by_oneface_in_image(input_path): io.log_info ("Sort by one face in images...") image_paths = Path_utils.get_image_paths(input_path) a = np.array ([ ( int(x[0]), int(x[1]) ) \ for x in [ Path(filepath).stem.split('_') for filepath in image_paths ] if len(x) == 2 ]) if len(a) > 0: idxs = np.ndarray.flatten ( np.argwhere ( a[:,1] != 0 ) ) idxs = np.unique ( a[idxs][:,0] ) idxs = np.ndarray.flatten ( np.argwhere ( np.array([ x[0] in idxs for x in a ]) == True ) ) if len(idxs) > 0: io.log_info ("Found %d images." % (len(idxs)) ) img_list = [ (path,) for i,path in enumerate(image_paths) if i not in idxs ] trash_img_list = [ (image_paths[x],) for x in idxs ] return img_list, trash_img_list return [], []
def sort_by_face(input_path): print("Sorting by face similarity...") img_list = [] for filepath in tqdm(Path_utils.get_image_paths(input_path), desc="Loading"): filepath = Path(filepath) if filepath.suffix != '.png': print("%s is not a png file required for sort_by_face" % (filepath.name)) continue a_png = AlignedPNG.load(str(filepath)) if a_png is None: print("%s failed to load" % (filepath.name)) continue d = a_png.getFaceswapDictData() if d is None or d['landmarks'] is None: print("%s - no embedded data found required for sort_by_face" % (filepath.name)) continue img_list.append([str(filepath), np.array(d['landmarks'])]) 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 delete_relighted(input_dir): input_path = Path(input_dir) image_paths = [Path(x) for x in Path_utils.get_image_paths(input_path)] files_to_delete = [] for filepath in io.progress_bar_generator(image_paths, "Loading"): dflimg = DFLIMG.load(Path(filepath)) if dflimg is None: io.log_err("%s is not a dfl image file" % (filepath.name)) continue else: if dflimg.get_relighted(): files_to_delete += [filepath] for file in io.progress_bar_generator(files_to_delete, "Deleting"): file.unlink()
def final_process(input_path, img_list, trash_img_list): if len(trash_img_list) != 0: parent_input_path = input_path.parent trash_path = parent_input_path / (input_path.stem + '_trash') trash_path.mkdir(exist_ok=True) print("Trashing %d items to %s" % (len(trash_img_list), str(trash_path))) for filename in Path_utils.get_image_paths(trash_path): Path(filename).unlink() for i in tqdm(range(len(trash_img_list)), desc="Moving trash", leave=False, ascii=True): src = Path(trash_img_list[i][0]) dst = trash_path / src.name try: src.rename(dst) except: print('fail to trashing %s' % (src.name)) print("") if len(img_list) != 0: for i in tqdm(range(len(img_list)), desc="Renaming", leave=False, ascii=True): src = Path(img_list[i][0]) dst = input_path / ('%.5d_%s' % (i, src.name)) try: src.rename(dst) except: print('fail to rename %s' % (src.name)) for i in tqdm(range(len(img_list)), desc="Renaming", ascii=True): src = Path(img_list[i][0]) src = input_path / ('%.5d_%s' % (i, src.name)) dst = input_path / ('%.5d%s' % (i, src.suffix)) try: src.rename(dst) except: print('fail to rename %s' % (src.name))
def sort_by_face_vector(input_path, source_image): print("Sorting by face vector...") source_vector = GetFaceVector(source_image) img_list = [] for filepath in tqdm(Path_utils.get_image_paths(input_path), desc="Loading", ascii=True): dst_vector = GetFaceVector(filepath) dSim = np.linalg.norm(source_vector - dst_vector) print("sim : %f , %s" % (dSim, filepath)) img_list.append([str(filepath), dSim]) img_list = sorted(img_list, key=operator.itemgetter(1), reverse=False) return img_list
def dev_test(input_dir): input_path = Path(input_dir) dir_names = Path_utils.get_all_dir_names(input_path) for dir_name in dir_names: img_paths = Path_utils.get_image_paths (input_path / dir_name) for filename in img_paths: filepath = Path(filename) dflimg = DFLIMG.load (filepath) if dflimg is None: raise ValueError import code code.interact(local=dict(globals(), **locals()))
def extract_video(input_file, output_dir, output_ext=None, fps=None): input_file_path = Path(input_file) output_path = Path(output_dir) if not output_path.exists(): output_path.mkdir(exist_ok=True) if input_file_path.suffix == '.*': input_file_path = Path_utils.get_first_file_by_stem( input_file_path.parent, input_file_path.stem) else: if not input_file_path.exists(): input_file_path = None if input_file_path is None: io.log_err("input_file not found.") return if output_ext is None: output_ext = io.input_str( "Output image format (extension)? ( default:png ) : ", "png") if fps is None: fps = io.input_int( "Enter FPS ( ?:help skip:fullfps ) : ", 0, help_message= "How many frames of every second of the video will be extracted.") for filename in Path_utils.get_image_paths(output_path, ['.' + output_ext]): Path(filename).unlink() job = ffmpeg.input(str(input_file_path)) kwargs = {} if fps != 0: kwargs.update({'r': str(fps)}) job = job.output(str(output_path / ('%5d.' + output_ext)), **kwargs) try: job = job.run() except: io.log_err("ffmpeg fail, job commandline:" + str(job.compile()))
def extract_vggface2_dataset(input_dir, device_args={}): multi_gpu = device_args.get('multi_gpu', False) cpu_only = device_args.get('cpu_only', False) input_path = Path(input_dir) if not input_path.exists(): raise ValueError('Input directory not found. Please ensure it exists.') output_path = input_path.parent / (input_path.name + '_out') dir_names = Path_utils.get_all_dir_names(input_path) if not output_path.exists(): output_path.mkdir(parents=True, exist_ok=True) for dir_name in dir_names: cur_input_path = input_path / dir_name cur_output_path = output_path / dir_name l = len(Path_utils.get_image_paths(cur_input_path)) if l < 250 or l > 350: continue io.log_info(f"Processing: {str(cur_input_path)} ") if not cur_output_path.exists(): cur_output_path.mkdir(parents=True, exist_ok=True) Extractor.main(str(cur_input_path), str(cur_output_path), detector='s3fd', image_size=256, face_type='full_face', max_faces_from_image=1, device_args=device_args) io.log_info(f"Sorting: {str(cur_input_path)} ") Sorter.main(input_path=str(cur_output_path), sort_by_method='hist') try: io.log_info(f"Removing: {str(cur_input_path)} ") shutil.rmtree(cur_input_path) except: io.log_info(f"unable to remove: {str(cur_input_path)} ")
def sort_by_face_dissim(input_path): print("Sorting by face dissimilarity...") img_list = [] for filepath in tqdm(Path_utils.get_image_paths(input_path), desc="Loading"): filepath = Path(filepath) if filepath.suffix != '.png': print("%s is not a png file required for sort_by_face_dissim" % (filepath.name)) continue a_png = AlignedPNG.load(str(filepath)) if a_png is None: print("%s failed to load" % (filepath.name)) continue d = a_png.getFaceswapDictData() if d is None or d['landmarks'] is None: print( "%s - no embedded data found required for sort_by_face_dissim" % (filepath.name)) continue img_list.append([str(filepath), np.array(d['landmarks']), 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_by_face(input_path): io.log_info("根据相似度[similarity]排序...") 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 不是DeepFaceLab的图片格式,请使用DeepFaceLab提取脸图" % (filepath.name)) trash_img_list.append([str(filepath)]) continue img_list.append([str(filepath), dflimg.get_landmarks()]) img_list_len = len(img_list) for i in io.progress_bar_generator(range(0, img_list_len - 1), "Sorting"): 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, trash_img_list
def extract_video(input_file, output_dir, output_ext=None, fps=None): input_file_path = Path(input_file) output_path = Path(output_dir) if not output_path.exists(): output_path.mkdir(exist_ok=True) if input_file_path.suffix == ".*": input_file_path = Path_utils.get_first_file_by_stem( input_file_path.parent, input_file_path.stem) else: if not input_file_path.exists(): input_file_path = None if input_file_path is None: logger.error("input_file not found.") return if fps is None: fps = 0 if output_ext is None: output_ext = "png" for filename in Path_utils.get_image_paths(output_path, ["." + output_ext]): Path(filename).unlink() job = ffmpeg.input(str(input_file_path)) kwargs = {"pix_fmt": "rgb24"} if fps != 0: kwargs.update({"r": str(fps)}) if output_ext == "jpg": kwargs.update({"q:v": "2"}) # highest quality for jpg job = job.output(str(output_path / ("%5d." + output_ext)), **kwargs) try: job = job.run() except: logger.error("ffmpeg fail, job commandline:" + str(job.compile()))
def extract_fanseg(input_dir, device_args={} ): multi_gpu = device_args.get('multi_gpu', False) cpu_only = device_args.get('cpu_only', False) input_path = Path(input_dir) if not input_path.exists(): raise ValueError('Input directory not found. Please ensure it exists.') paths_to_extract = [] for filename in Path_utils.get_image_paths(input_path) : filepath = Path(filename) dflimg = DFLIMG.load ( filepath ) if dflimg is not None: paths_to_extract.append (filepath) paths_to_extract_len = len(paths_to_extract) if paths_to_extract_len > 0: io.log_info ("Performing extract fanseg for %d files..." % (paths_to_extract_len) ) data = ExtractSubprocessor ([ ExtractSubprocessor.Data(filename) for filename in paths_to_extract ], 'fanseg', multi_gpu=multi_gpu, cpu_only=cpu_only).run()
def sort_by_hist_dissim(input_path): print ("Sorting by histogram dissimilarity...") img_list = [] for filename_path in tqdm( Path_utils.get_image_paths(input_path), desc="Loading"): image = cv2.imread(filename_path) dflpng = DFLPNG.load( str(filename_path) ) if dflpng is not None: face_mask = LandmarksProcessor.get_image_hull_mask (image, dflpng.get_landmarks()) image = (image*face_mask).astype(np.uint8) img_list.append ([filename_path, cv2.calcHist([cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)], [0], None, [256], [0, 256]), 0 ]) img_list = HistDissimSubprocessor(img_list).process() print ("Sorting...") img_list = sorted(img_list, key=operator.itemgetter(2), reverse=True) return img_list
def sort_by_origname(input_path): print ("Sort by original filename...") img_list = [] for filepath in tqdm( Path_utils.get_image_paths(input_path), desc="Loading"): filepath = Path(filepath) if filepath.suffix != '.png': print ("%s is not a png file required for sort_by_origname" % (filepath.name) ) continue dflpng = DFLPNG.load (str(filepath), print_on_no_embedded_data=True) if dflpng is None: continue img_list.append( [str(filepath), dflpng.get_source_filename()] ) print ("Sorting...") img_list = sorted(img_list, key=operator.itemgetter(1)) return img_list
def add_landmarks_debug_images(input_path): io.log_info ("Adding landmarks debug images...") for filepath in io.progress_bar_generator( Path_utils.get_image_paths(input_path), "Processing"): filepath = Path(filepath) img = cv2_imread(str(filepath)) dflimg = DFLIMG.load (filepath) if dflimg is None: io.log_err ("%s is not a dfl image file" % (filepath.name) ) continue if img is not None: face_landmarks = dflimg.get_landmarks() LandmarksProcessor.draw_landmarks(img, face_landmarks, transparent_mask=True, ie_polys=IEPolys.load(dflimg.get_ie_polys()) ) output_file = '{}{}'.format( str(Path(str(input_path)) / filepath.stem), '_debug.jpg') cv2_imwrite(output_file, img, [int(cv2.IMWRITE_JPEG_QUALITY), 50] )
def sort_by_face_yaw(input_path): print ("Sorting by face yaw...") img_list = [] for filepath in tqdm( Path_utils.get_image_paths(input_path), desc="Loading"): filepath = Path(filepath) if filepath.suffix != '.png': print ("%s is not a png file required for sort_by_face_dissim" % (filepath.name) ) continue dflpng = DFLPNG.load (str(filepath), print_on_no_embedded_data=True) if dflpng is None: continue img_list.append( [str(filepath), np.array( dflpng.get_yaw_value() ) ] ) print ("Sorting...") img_list = sorted(img_list, key=operator.itemgetter(1), reverse=True) return img_list