def get_face_hashes(self): """ Check for the existence of an aligned directory for identifying which faces in the target frames should be swapped. If it exists, obtain the hashes of the faces in the folder """ face_hashes = list() input_aligned_dir = self.args.input_aligned_dir if input_aligned_dir is None: logger.verbose( "Aligned directory not specified. All faces listed in the " "alignments file will be converted") elif not os.path.isdir(input_aligned_dir): logger.warning( "Aligned directory not found. All faces listed in the " "alignments file will be converted") else: file_list = [path for path in get_image_paths(input_aligned_dir)] logger.info("Getting Face Hashes for selected Aligned Images") for face in tqdm(file_list, desc="Hashing Faces"): face_hashes.append(hash_image_file(face)) logger.debug("Face Hashes: %s", (len(face_hashes))) if not face_hashes: logger.error( "Aligned directory is empty, no faces will be converted!") exit(1) elif len(face_hashes) <= len(self.input_images) / 3: logger.warning( "Aligned directory contains far fewer images than the input " "directory, are you sure this is the right folder?") return face_hashes
def get_face_hashes(self): """ Check for the existence of an aligned directory for identifying which faces in the target frames should be swapped. If it exists, obtain the hashes of the faces in the folder """ face_hashes = list() input_aligned_dir = self.args.input_aligned_dir if input_aligned_dir is None: logger.verbose("Aligned directory not specified. All faces listed in the " "alignments file will be converted") elif not os.path.isdir(input_aligned_dir): logger.warning("Aligned directory not found. All faces listed in the " "alignments file will be converted") else: file_list = [path for path in get_image_paths(input_aligned_dir)] logger.info("Getting Face Hashes for selected Aligned Images") for face in tqdm(file_list, desc="Hashing Faces"): face_hashes.append(hash_image_file(face)) logger.debug("Face Hashes: %s", (len(face_hashes))) if not face_hashes: logger.error("Aligned directory is empty, no faces will be converted!") exit(1) elif len(face_hashes) <= len(self.input_images) / 3: logger.warning("Aligned directory contains far fewer images than the input " "directory, are you sure this is the right folder?") return face_hashes
def process_folder(self): """ Iterate through the faces dir pulling out various information """ logger.info("Loading file list from %s", self.folder) for face in tqdm(os.listdir(self.folder), desc="Reading Face Hashes"): if not self.valid_extension(face): continue filename = os.path.splitext(face)[0] file_extension = os.path.splitext(face)[1] face_hash = hash_image_file(os.path.join(self.folder, face)) retval = {"face_fullname": face, "face_name": filename, "face_extension": file_extension, "face_hash": face_hash} logger.trace(retval) yield retval
def add_hashes(self, hashes, faces_dir): """ Add Face Hashes to the alignments file """ all_faces = dict() face_files = sorted(face for face in os.listdir(faces_dir) if "_" in face) for face in face_files: filename, extension = os.path.splitext(face) index = filename[filename.rfind("_") + 1:] if not index.isdigit(): continue orig_frame = filename[:filename.rfind("_")] + extension all_faces.setdefault(orig_frame, dict())[int(index)] = os.path.join(faces_dir, face) for frame in tqdm(hashes): if frame not in all_faces.keys(): logger.warning("Skipping missing frame: '%s'", frame) continue hash_faces = all_faces[frame] for index, face_path in hash_faces.items(): hash_faces[index] = hash_image_file(face_path) self.alignments.add_face_hashes(frame, hash_faces)