Spaces:
Runtime error
Runtime error
Update generator.py
Browse files- generator.py +26 -5
generator.py
CHANGED
|
@@ -766,14 +766,26 @@ class RetroArtConverter:
|
|
| 766 |
if hasattr(self.pipe, 'text_encoder'):
|
| 767 |
pipe_kwargs["clip_skip"] = 2
|
| 768 |
|
| 769 |
-
# --- FIX START: Configure ControlNet inputs dynamically ---
|
| 770 |
control_images = []
|
| 771 |
conditioning_scales = []
|
| 772 |
scale_debug_str = []
|
| 773 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 774 |
# 1. InstantID (Identity)
|
| 775 |
if self.instantid_active:
|
| 776 |
if has_detected_faces and face_kps_image is not None:
|
|
|
|
|
|
|
| 777 |
control_images.append(face_kps_image)
|
| 778 |
conditioning_scales.append(identity_control_scale)
|
| 779 |
scale_debug_str.append(f"Identity: {identity_control_scale:.2f}")
|
|
@@ -816,24 +828,33 @@ class RetroArtConverter:
|
|
| 816 |
|
| 817 |
# 2. Depth
|
| 818 |
if self.depth_active:
|
|
|
|
|
|
|
| 819 |
control_images.append(depth_image)
|
| 820 |
conditioning_scales.append(depth_control_scale)
|
| 821 |
scale_debug_str.append(f"Depth: {depth_control_scale:.2f}")
|
| 822 |
|
| 823 |
# 3. OpenPose (Expression)
|
| 824 |
if self.openpose_active:
|
| 825 |
-
|
|
|
|
|
|
|
| 826 |
conditioning_scales.append(expression_control_scale)
|
| 827 |
scale_debug_str.append(f"Expression: {expression_control_scale:.2f}")
|
| 828 |
|
|
|
|
| 829 |
if control_images:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 830 |
pipe_kwargs["control_image"] = control_images
|
| 831 |
pipe_kwargs["controlnet_conditioning_scale"] = conditioning_scales
|
| 832 |
-
print(f"Active ControlNets: {len(control_images)}")
|
| 833 |
else:
|
| 834 |
print("No active ControlNets, running standard Img2Img")
|
| 835 |
-
# --- FIX END ---
|
| 836 |
-
|
| 837 |
|
| 838 |
# Generate
|
| 839 |
print(f"Generating with LCM: Steps={num_inference_steps}, CFG={guidance_scale}, Strength={strength}")
|
|
|
|
| 766 |
if hasattr(self.pipe, 'text_encoder'):
|
| 767 |
pipe_kwargs["clip_skip"] = 2
|
| 768 |
|
|
|
|
| 769 |
control_images = []
|
| 770 |
conditioning_scales = []
|
| 771 |
scale_debug_str = []
|
| 772 |
|
| 773 |
+
# Helper function to ensure control image has correct dimensions
|
| 774 |
+
def ensure_correct_size(img, target_w, target_h, name="control"):
|
| 775 |
+
"""Ensure image matches target dimensions exactly"""
|
| 776 |
+
if img is None:
|
| 777 |
+
return Image.new("RGB", (target_w, target_h), (0,0,0))
|
| 778 |
+
|
| 779 |
+
if img.size != (target_w, target_h):
|
| 780 |
+
print(f" [RESIZE] {name}: {img.size} -> ({target_w}, {target_h})")
|
| 781 |
+
img = img.resize((target_w, target_h), Image.LANCZOS)
|
| 782 |
+
return img
|
| 783 |
+
|
| 784 |
# 1. InstantID (Identity)
|
| 785 |
if self.instantid_active:
|
| 786 |
if has_detected_faces and face_kps_image is not None:
|
| 787 |
+
# Ensure face keypoints image has correct size
|
| 788 |
+
face_kps_image = ensure_correct_size(face_kps_image, target_width, target_height, "InstantID")
|
| 789 |
control_images.append(face_kps_image)
|
| 790 |
conditioning_scales.append(identity_control_scale)
|
| 791 |
scale_debug_str.append(f"Identity: {identity_control_scale:.2f}")
|
|
|
|
| 828 |
|
| 829 |
# 2. Depth
|
| 830 |
if self.depth_active:
|
| 831 |
+
# Ensure depth image has correct size
|
| 832 |
+
depth_image = ensure_correct_size(depth_image, target_width, target_height, "Depth")
|
| 833 |
control_images.append(depth_image)
|
| 834 |
conditioning_scales.append(depth_control_scale)
|
| 835 |
scale_debug_str.append(f"Depth: {depth_control_scale:.2f}")
|
| 836 |
|
| 837 |
# 3. OpenPose (Expression)
|
| 838 |
if self.openpose_active:
|
| 839 |
+
# Ensure openpose image has correct size
|
| 840 |
+
openpose_image = ensure_correct_size(openpose_image, target_width, target_height, "OpenPose")
|
| 841 |
+
control_images.append(openpose_image)
|
| 842 |
conditioning_scales.append(expression_control_scale)
|
| 843 |
scale_debug_str.append(f"Expression: {expression_control_scale:.2f}")
|
| 844 |
|
| 845 |
+
# Final validation: ensure all control images have identical dimensions
|
| 846 |
if control_images:
|
| 847 |
+
expected_size = (target_width, target_height)
|
| 848 |
+
for idx, img in enumerate(control_images):
|
| 849 |
+
if img.size != expected_size:
|
| 850 |
+
print(f" [WARNING] Control image {idx} size mismatch: {img.size} vs expected {expected_size}")
|
| 851 |
+
control_images[idx] = img.resize(expected_size, Image.LANCZOS)
|
| 852 |
+
|
| 853 |
pipe_kwargs["control_image"] = control_images
|
| 854 |
pipe_kwargs["controlnet_conditioning_scale"] = conditioning_scales
|
| 855 |
+
print(f"Active ControlNets: {len(control_images)} (all {target_width}x{target_height})")
|
| 856 |
else:
|
| 857 |
print("No active ControlNets, running standard Img2Img")
|
|
|
|
|
|
|
| 858 |
|
| 859 |
# Generate
|
| 860 |
print(f"Generating with LCM: Steps={num_inference_steps}, CFG={guidance_scale}, Strength={strength}")
|