File size: 4,004 Bytes
8308bbd
 
ee3c968
 
8308bbd
 
 
 
ae826ca
8308bbd
 
 
a125be2
8308bbd
 
 
 
 
 
ee3c968
 
 
 
8308bbd
 
 
 
 
 
 
ee3c968
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8308bbd
a125be2
ee3c968
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8308bbd
ee3c968
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a125be2
ee3c968
 
 
 
 
 
 
 
 
 
 
 
 
a125be2
 
ee3c968
8308bbd
 
 
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import argparse
import os
import shutil
from FaceEnhancementProd import enhance_face

def parse_args():
    parser = argparse.ArgumentParser(description='Face Enhancement Tool')
    parser.add_argument('--input', type=str, required=True, help='Path to the input image')
    parser.add_argument('--ref', type=str, required=True, help='Path to the reference image')
    parser.add_argument('--crop', action='store_true', help='Whether to crop the image')
    parser.add_argument('--upscale', action='store_true', help='Whether to upscale the image')
    parser.add_argument('--output', type=str, required=True, help='Path to save the output image')
    parser.add_argument('--id_weight', type=float, default=0.75, help='face ID weight')
    args = parser.parse_args()
    
    # Validate input file exists
    if not os.path.exists(args.input):
        parser.error(f"Input file does not exist: {args.input}")
    
    # Validate reference file exists
    if not os.path.exists(args.ref):
        parser.error(f"Reference file does not exist: {args.ref}")
    
    # Validate output directory exists
    output_dir = os.path.dirname(args.output)
    if output_dir and not os.path.exists(output_dir):
        parser.error(f"Output directory does not exist: {output_dir}")
    
    return args

def create_scratch_dir():
    """Create a new numbered directory in ./ComfyUI/input/scratch"""
    base_dir = "./ComfyUI/input/scratch"
    
    # Create base directory if it doesn't exist
    os.makedirs(base_dir, exist_ok=True)
    
    # Get existing directories and find the next number
    existing_dirs = [d for d in os.listdir(base_dir) if os.path.isdir(os.path.join(base_dir, d)) and d.isdigit()]
    next_dir_num = 1
    if existing_dirs:
        next_dir_num = max([int(d) for d in existing_dirs]) + 1
    
    # Create the new directory
    new_dir = os.path.join(base_dir, str(next_dir_num))
    os.makedirs(new_dir, exist_ok=True)
    
    return new_dir

def process_face(input_path, ref_path, crop=False, upscale=False, output_path=None, id_weight=0.75):
    """
    Process a face image using the given parameters.
    
    Args:
        input_path (str): Path to the input image
        ref_path (str): Path to the reference image
        crop (bool): Whether to crop the image
        upscale (bool): Whether to upscale the image
        output_path (str): Path to save the output image
        
    Returns:
        str: Path to the scratch directory used for processing
    """
    print(f"Processing image: {input_path}")
    print(f"Reference image: {ref_path}")
    print(f"Output will be saved to: {output_path}")
    
    # Create a new scratch directory for this run
    scratch_dir = create_scratch_dir()
    print(f"Created scratch directory: {scratch_dir}")
    
    # Copy input and reference images to scratch directory
    input_filename = os.path.basename(input_path)
    ref_filename = os.path.basename(ref_path)
    
    scratch_input = os.path.join(scratch_dir, input_filename)
    scratch_ref = os.path.join(scratch_dir, ref_filename)
    
    shutil.copy(input_path, scratch_input)
    shutil.copy(ref_path, scratch_ref)
    
    # Convert paths to ComfyUI format (relative to ComfyUI/input/)
    # For example: "./ComfyUI/input/scratch/1/image.png" becomes "scratch/1/image.png"
    comfy_ref_path = os.path.relpath(scratch_ref, "./ComfyUI/input")
    comfy_input_path = os.path.relpath(scratch_input, "./ComfyUI/input")
    
    enhance_face(comfy_ref_path, comfy_input_path, output_path, dist_image=f"{output_path}_dist.png", id_weight=id_weight)
    
    print(f"Enhanced image saved to: {output_path}")
    print(f"Working files are in: {scratch_dir}")
    
    return scratch_dir

def main():
    args = parse_args()
    return process_face(
        input_path=args.input, 
        ref_path=args.ref, 
        crop=args.crop, 
        upscale=args.upscale, 
        output_path=args.output,
        id_weight=args.id_weight
    )

if __name__ == "__main__":
    main()