def add_landmarks_debug_images(input_path): print("Adding landmarks debug images...") for filepath in tqdm(Path_utils.get_image_paths(input_path), desc="Processing", ascii=True): filepath = Path(filepath) img = cv2_imread(str(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 if not (dflimg is None or img is None): face_landmarks = dflimg.get_landmarks() LandmarksProcessor.draw_landmarks(img, face_landmarks) 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(input_path): print ("Sorting by face similarity...") 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 img_list.append( [str(filepath), dflimg.get_landmarks()] ) img_list_len = len(img_list) for i in tqdm ( range(0, img_list_len-1), desc="Sorting", ascii=True): 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 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)) 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)) continue if img is not None: face_landmarks = dflimg.get_landmarks() LandmarksProcessor.draw_landmarks(img, face_landmarks) output_file = '{}{}'.format( str(Path(str(input_path)) / filepath.stem), '_debug.jpg') cv2_imwrite(output_file, img, [int(cv2.IMWRITE_JPEG_QUALITY), 50])
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 sort_by_hist_dissim(input_path): print("Sorting by histogram dissimilarity...") 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 image = cv2.imread(str(filepath)) face_mask = LandmarksProcessor.get_image_hull_mask( image.shape, dflimg.get_landmarks()) image = (image * face_mask).astype(np.uint8) img_list.append([ str(filepath), 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_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 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) 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)) 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 onClientProcessData(self, data): filename_path = Path(data) files_processed = 1 faces_processed = 0 output_filename_path = self.output_path / (filename_path.stem + '.png') if self.converter.get_mode() == ConverterBase.MODE_FACE and filename_path.stem not in self.alignments.keys(): if not self.debug: print ( 'no faces found for %s, copying without faces' % (filename_path.name) ) shutil.copy ( str(filename_path), str(output_filename_path) ) else: image = (cv2_imread(str(filename_path)) / 255.0).astype(np.float32) if self.converter.get_mode() == ConverterBase.MODE_IMAGE: image = self.converter.convert_image(image, None, self.debug) if self.debug: for img in image: cv2.imshow ('Debug convert', img ) cv2.waitKey(0) faces_processed = 1 elif self.converter.get_mode() == ConverterBase.MODE_IMAGE_WITH_LANDMARKS: if filename_path.suffix == '.png': dflimg = DFLPNG.load( str(filename_path), throw_on_no_embedded_data=True ) elif filename_path.suffix == '.jpg': dflimg = DFLJPG.load ( str(filename_path), throw_on_no_embedded_data=True ) else: raise Exception ("%s is not a dfl image file" % (filename_path.name) ) image_landmarks = dflimg.get_landmarks() image = self.converter.convert_image(image, image_landmarks, self.debug) if self.debug: for img in image: cv2.imshow ('Debug convert', img ) cv2.waitKey(0) faces_processed = 1 elif self.converter.get_mode() == ConverterBase.MODE_FACE: faces = self.alignments[filename_path.stem] for face_num, image_landmarks in enumerate(faces): try: if self.debug: print ( '\nConverting face_num [%d] in file [%s]' % (face_num, filename_path) ) image = self.converter.convert_face(image, image_landmarks, self.debug) if self.debug: for img in image: cv2.imshow ('Debug convert', (img*255).astype(np.uint8) ) cv2.waitKey(0) except Exception as e: print ( 'Error while converting face_num [%d] in file [%s]: %s' % (face_num, filename_path, str(e)) ) traceback.print_exc() faces_processed = len(faces) if not self.debug: cv2_imwrite (str(output_filename_path), (image*255).astype(np.uint8) ) return (files_processed, faces_processed)
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) 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, dflimg.get_yaw_value()]]
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", 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 img_list.append( [str(filepath), dflimg.get_landmarks(), 0 ] ) img_list_len = len(img_list) for i in tqdm( range(0, img_list_len-1), desc="Sorting", ascii=True): 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 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) 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: self.log_err (e) return [ 1, [str(filepath)] ] return [ 0, [str(filepath), sharpness, hist, yaw ] ]
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) if filepath.suffix == '.png': dflimg = DFLPNG.load(str(filepath)) elif filepath.suffix == '.jpg': dflimg = DFLJPG.load(str(filepath)) else: dflimg = None 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_origname(input_path): io.log_info("Sort by original filename...") 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 img_list.append([str(filepath), dflimg.get_source_filename()]) io.log_info("Sorting...") img_list = sorted(img_list, key=operator.itemgetter(1)) return img_list, trash_img_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, roll = LandmarksProcessor.estimate_pitch_yaw_roll( 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_roll = dflimg.get_pitch_yaw_roll() if pitch_yaw_roll is not None: pitch, yaw, roll = pitch_yaw_roll else: pitch, yaw, roll = LandmarksProcessor.estimate_pitch_yaw_roll( 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 sort_by_hist_dissim(input_path): io.log_info("Sorting by histogram dissimilarity...") 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 image = cv2_imread(str(filepath)) if dflimg is not None: face_mask = LandmarksProcessor.get_image_hull_mask( image.shape, dflimg.get_landmarks()) image = (image * face_mask).astype(np.uint8) img_list.append([ str(filepath), cv2.calcHist([cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)], [0], None, [256], [0, 256]), 0 ]) img_list = HistDissimSubprocessor(img_list).run() io.log_info("Sorting...") img_list = sorted(img_list, key=operator.itemgetter(2), reverse=True) return img_list, trash_img_list
def show_landmarks(path): import cv from utils.DFLJPG import DFLJPG jpg = DFLJPG.load(path) img = cv.cv_load(path) lm = jpg.get_landmarks() for (x, y) in lm: cv.cv_point(img, (x, y), (255, 0, 0)) cv.cv_show(img)
def load_fanseg_mask(self): if self.fanseg_mask_exist: filepath = Path(self.filename) if filepath.suffix == '.png': dflimg = DFLPNG.load ( str(filepath) ) elif filepath.suffix == '.jpg': dflimg = DFLJPG.load ( str(filepath) ) else: dflimg = None return dflimg.get_fanseg_mask() return None
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 dfl_load_img(path): from pathlib import Path from utils.DFLPNG import DFLPNG from utils.DFLJPG import DFLJPG filepath = Path(path) 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)) return dflimg
def process_data(self, data): filepath = Path(data[0]) if filepath.suffix == '.png': dflimg = DFLPNG.load(str(filepath)) elif filepath.suffix == '.jpg': dflimg = DFLJPG.load(str(filepath)) else: dflimg = None if dflimg is not None: image = cv2_imread(str(filepath)) return [str(filepath), estimate_sharpness(image)] else: self.log_err("%s is not a dfl image file" % (filepath.name)) return [str(filepath), 0]
def remove_fanseg_file(filepath): filepath = Path(filepath) if filepath.suffix == '.png': dflimg = DFLPNG.load(str(filepath)) elif filepath.suffix == '.jpg': dflimg = DFLJPG.load(str(filepath)) else: return if dflimg is None: io.log_err("%s is not a dfl image file" % (filepath.name)) return dflimg.remove_fanseg_mask() dflimg.embed_and_set(str(filepath))
def upgradeToFaceSamples(samples, silent=False): sample_list = [] for s in (samples if silent else 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 landmarks = dflimg.get_landmarks() pitch_yaw_roll = dflimg.get_pitch_yaw_roll() eyebrows_expand_mod = dflimg.get_eyebrows_expand_mod() if pitch_yaw_roll is None: pitch_yaw_roll = LandmarksProcessor.estimate_pitch_yaw_roll( 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=landmarks, ie_polys=dflimg.get_ie_polys(), pitch_yaw_roll=pitch_yaw_roll, eyebrows_expand_mod=eyebrows_expand_mod, source_filename=dflimg.get_source_filename(), fanseg_mask_exist=dflimg.get_fanseg_mask() is not None, )) except: print("Unable to load %s , error: %s" % (str(s_filename_path), traceback.format_exc())) return sample_list
def process_data(self, data): filename = data[0] filepath = Path(filename) if filepath.suffix == '.png': dflimg = DFLPNG.load(str(filepath)) elif filepath.suffix == '.jpg': dflimg = DFLJPG.load(str(filepath)) else: dflimg = None image_to_face_mat = dflimg.get_image_to_face_mat() src_filename = dflimg.get_source_filename() img = cv2_imread(filename) h, w, c = img.shape fanseg_mask = LandmarksProcessor.get_image_hull_mask( img.shape, dflimg.get_landmarks()) idx_name = '%.5d' % int(src_filename.split('.')[0]) idx_files = [x for x in self.masks_files_paths if idx_name in x] skin_files = [x for x in idx_files if 'skin' in x] eye_glass_files = [x for x in idx_files if 'eye_g' in x] for files, is_invert in [(skin_files, False), (eye_glass_files, True)]: if len(files) > 0: mask = cv2_imread(files[0]) mask = mask[..., 0] mask[mask == 255] = 1 mask = mask.astype(np.float32) mask = cv2.resize(mask, (1024, 1024)) mask = cv2.warpAffine(mask, image_to_face_mat, (w, h), cv2.INTER_LANCZOS4) if not is_invert: fanseg_mask *= mask[..., None] else: fanseg_mask *= (1 - mask[..., None]) dflimg.embed_and_set(filename, fanseg_mask=fanseg_mask) return 1
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 onClientProcessData(self, data): filepath = Path(data[0]) 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)) dflimg = None if dflimg is not None: image = cv2.imread(str(filepath)) image = ( image * \ LandmarksProcessor.get_image_hull_mask (image.shape, dflimg.get_landmarks()) \ ).astype(np.uint8) return [str(filepath), estimate_sharpness(image)] else: return [str(filepath), 0]
def process_data(self, data): filepath = Path( data[0] ) if filepath.suffix == '.png': dflimg = DFLPNG.load( str(filepath) ) elif filepath.suffix == '.jpg': dflimg = DFLJPG.load ( str(filepath) ) else: dflimg = None if dflimg is not None: image = cv2_imread( str(filepath) ) image = ( image * \ LandmarksProcessor.get_image_hull_mask (image.shape, dflimg.get_landmarks()) \ ).astype(np.uint8) return [ str(filepath), estimate_sharpness( image ) ] else: self.log_err ("%s is not a dfl image file" % (filepath.name) ) return [ str(filepath), 0 ]
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", 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 img_list.append( [str(filepath), dflimg.get_source_filename()] ) print ("Sorting...") img_list = sorted(img_list, key=operator.itemgetter(1)) return img_list
def sort_by_face_dissim(input_path): io.log_info("根据差异度[dissimilarity]排序...") 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(), 0]) img_list_len = len(img_list) for i in io.progress_bar_generator(range(img_list_len - 1), "Sorting"): 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 io.log_info("排序...") img_list = sorted(img_list, key=operator.itemgetter(2), reverse=True) return img_list, trash_img_list
def Align(filepath, imgDim): #filepath = Path('F:/DeepFaceLab/workspace/data_src/aligned/00776.jpg') 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)) return landmark = dflimg.get_landmarks() npLandmarks = np.float32(landmark) npLandmarkIndices = np.array(INNER_EYES_AND_BOTTOM_LIP) rgbImg = cv2.imread(str(filepath)) H = cv2.getAffineTransform(npLandmarks[npLandmarkIndices], imgDim * MINMAX_TEMPLATE[npLandmarkIndices]) thumbnail = cv2.warpAffine(rgbImg, H, (imgDim, imgDim)) return thumbnail