File size: 2,812 Bytes
8234608 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# refacer_bulk.py
#
# Example usage:
# python refacer_bulk.py --input_path ./input --dest_face myface.jpg --facetoreplace face1.jpg --threshold 0.3
#
# Or, to disable similarity check (i.e., just apply the destination face to all detected faces):
# python refacer_bulk.py --input_path ./input --dest_face myface.jpg
import argparse
import os
import cv2
from pathlib import Path
from refacer import Refacer
from PIL import Image
import time
import pyfiglet
def parse_args():
parser = argparse.ArgumentParser(description="Bulk Image Refacer")
parser.add_argument("--input_path", type=str, required=True, help="Directory containing input images")
parser.add_argument("--dest_face", type=str, required=True, help="Path to destination face image")
parser.add_argument("--facetoreplace", type=str, default=None, help="Path to face to replace (origin face)")
parser.add_argument("--threshold", type=float, default=0.2, help="Similarity threshold (default: 0.2)")
parser.add_argument("--force_cpu", action="store_true", help="Force CPU mode")
parser.add_argument("--colab_performance", action="store_true", help="Enable Colab performance tweaks")
return parser.parse_args()
def main():
print("\033[94m" + pyfiglet.Figlet(font='slant').renderText("NeoRefacer") + "\033[0m")
args = parse_args()
input_dir = Path(args.input_path)
refacer = Refacer(force_cpu=args.force_cpu, colab_performance=args.colab_performance)
# Load destination and origin face
dest_img = cv2.imread(args.dest_face)
if dest_img is None:
raise ValueError(f"Destination face image not found: {args.dest_face}")
origin_img = None
if args.facetoreplace:
origin_img = cv2.imread(args.facetoreplace)
if origin_img is None:
raise ValueError(f"Face to replace image not found: {args.facetoreplace}")
disable_similarity = origin_img is None
faces_config = [{
'origin': origin_img,
'destination': dest_img,
'threshold': args.threshold
}]
refacer.prepare_faces(faces_config, disable_similarity=disable_similarity)
print(f"Processing images from: {input_dir}")
image_files = list(input_dir.glob("*"))
supported_exts = {'.jpg', '.jpeg', '.png', '.bmp', '.webp'}
for image_path in image_files:
if image_path.suffix.lower() not in supported_exts:
print(f"Skipping non-image file: {image_path}")
continue
print(f"Refacing: {image_path}")
try:
refaced_path = refacer.reface_image(str(image_path), faces_config, disable_similarity=disable_similarity)
print(f"Saved to: {refaced_path}")
except Exception as e:
print(f"Failed to process {image_path}: {e}")
if __name__ == "__main__":
main()
|