primerz commited on
Commit
11f9790
·
verified ·
1 Parent(s): c4e4633

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +2 -2
  2. generator.py +39 -45
app.py CHANGED
@@ -106,7 +106,7 @@ def get_model_status():
106
  status_text += f"- Custom Checkpoint (Horizon): {'[OK] Loaded' if converter.models_loaded['custom_checkpoint'] else '[OK] Using SDXL base'}\n"
107
  status_text += f"- LORA (RetroArt): {'[OK] Loaded' if converter.models_loaded['lora'] else ' Disabled'}\n"
108
  status_text += f"- InstantID: {'[OK] Loaded' if converter.models_loaded['instantid'] else ' Disabled'}\n"
109
- status_text += f"- Leres Depth: {'[OK] Loaded' if converter.models_loaded['leres_depth'] else ' Fallback'}\n"
110
  status_text += f"- IP-Adapter (Face Embeddings): {'[OK] Loaded' if converter.models_loaded.get('ip_adapter', False) else ' Keypoints only'}\n"
111
  return status_text
112
  return "**Model status unavailable**"
@@ -452,4 +452,4 @@ if __name__ == "__main__":
452
  server_port=7860,
453
  share=True,
454
  show_api=True
455
- )
 
106
  status_text += f"- Custom Checkpoint (Horizon): {'[OK] Loaded' if converter.models_loaded['custom_checkpoint'] else '[OK] Using SDXL base'}\n"
107
  status_text += f"- LORA (RetroArt): {'[OK] Loaded' if converter.models_loaded['lora'] else ' Disabled'}\n"
108
  status_text += f"- InstantID: {'[OK] Loaded' if converter.models_loaded['instantid'] else ' Disabled'}\n"
109
+ status_text += f"- Depth: Grayscale (simple & reliable)\n"
110
  status_text += f"- IP-Adapter (Face Embeddings): {'[OK] Loaded' if converter.models_loaded.get('ip_adapter', False) else ' Keypoints only'}\n"
111
  return status_text
112
  return "**Model status unavailable**"
 
452
  server_port=7860,
453
  share=True,
454
  show_api=True
455
+ )
generator.py CHANGED
@@ -40,9 +40,9 @@ class RetroArtConverter:
40
  # Initialize face analysis
41
  self.face_app, self.face_detection_enabled = load_face_analysis()
42
 
43
- # Load Leres Depth detector
44
- self.leres_depth, leres_success = load_depth_detector()
45
- self.models_loaded['leres_depth'] = leres_success
46
 
47
  # Load ControlNets
48
  controlnet_depth, self.controlnet_instantid, instantid_success = load_controlnets()
@@ -151,37 +151,24 @@ class RetroArtConverter:
151
  print("============================\n")
152
 
153
  def get_depth_map(self, image):
154
- """Generate depth map using Leres Depth for better quality"""
155
- if self.leres_depth is not None:
156
- try:
157
- # Ensure RGB mode
158
- if image.mode != 'RGB':
159
- image = image.convert('RGB')
160
-
161
- # Get original dimensions
162
- orig_width, orig_height = image.size
163
-
164
- # Ensure dimensions are multiples of 64 for better processing
165
- detect_width = int((orig_width // 64) * 64) or 64
166
- detect_height = int((orig_height // 64) * 64) or 64
167
-
168
- # Resize if needed
169
- if detect_width != orig_width or detect_height != orig_height:
170
- detect_image = image.resize((detect_width, detect_height), Image.LANCZOS)
171
- print(f"[DEPTH] Resized for detection: {orig_width}x{orig_height} -> {detect_width}x{detect_height}")
172
- else:
173
- detect_image = image
174
-
175
- # Generate depth map with Leres (better quality than Zoe)
176
- with torch.no_grad():
177
- depth_image = self.leres_depth(detect_image)
178
-
179
- # Resize back to original dimensions
180
- if depth_image.size != (orig_width, orig_height):
181
- depth_image = depth_image.resize((orig_width, orig_height), Image.LANCZOS)
182
-
183
- print(f"[DEPTH] Leres depth map generated: {orig_width}x{orig_height}")
184
- return depth_image
185
 
186
  except Exception as e:
187
  print(f"[DEPTH] LeresDetector failed ({e}), falling back to grayscale depth")
@@ -466,7 +453,7 @@ class RetroArtConverter:
466
  resized_image = input_image.resize((int(target_width), int(target_height)), Image.LANCZOS)
467
 
468
  # Generate depth map
469
- print("Generating Leres depth map...")
470
  depth_image = self.get_depth_map(resized_image)
471
  if depth_image.size != (target_width, target_height):
472
  depth_image = depth_image.resize((int(target_width), int(target_height)), Image.LANCZOS)
@@ -547,17 +534,24 @@ class RetroArtConverter:
547
  print(f"Face info: bbox={face.bbox}, age={age if age else 'N/A'}, gender={gender_str}")
548
  print(f"Face crop size: {face_crop.size}, enhanced: {face_crop_enhanced.size if face_crop_enhanced else 'N/A'}")
549
 
550
- # Set LORA scale
551
- if hasattr(self.pipe, 'set_adapters') and self.models_loaded['lora']:
552
  try:
553
- # Try retroart first (if loaded with that name), then default_0
554
- for adapter_name in ["retroart", "default_0"]:
555
- try:
556
- self.pipe.set_adapters([adapter_name], adapter_weights=[lora_scale])
557
- print(f"[LORA] Set adapter '{adapter_name}' with scale: {lora_scale}")
558
- break
559
- except:
560
- continue
 
 
 
 
 
 
 
561
 
562
  except Exception as e:
563
  print(f"[WARNING] LORA set_adapters failed: {e}")
 
40
  # Initialize face analysis
41
  self.face_app, self.face_detection_enabled = load_face_analysis()
42
 
43
+ # Skip depth detector - using grayscale conversion instead
44
+ self.leres_depth = None
45
+ self.models_loaded['leres_depth'] = False
46
 
47
  # Load ControlNets
48
  controlnet_depth, self.controlnet_instantid, instantid_success = load_controlnets()
 
151
  print("============================\n")
152
 
153
  def get_depth_map(self, image):
154
+ """Generate depth map using grayscale conversion for reliability"""
155
+ try:
156
+ # Ensure RGB mode
157
+ if image.mode != 'RGB':
158
+ image = image.convert('RGB')
159
+
160
+ # Convert to grayscale for depth
161
+ gray = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2GRAY)
162
+
163
+ # Apply some enhancement to make depth more pronounced
164
+ gray = cv2.equalizeHist(gray)
165
+
166
+ # Convert back to RGB format (ControlNet expects RGB)
167
+ depth_colored = cv2.cvtColor(gray, cv2.COLOR_GRAY2RGB)
168
+ depth_image = Image.fromarray(depth_colored)
169
+
170
+ print(f"[DEPTH] Grayscale depth map generated: {image.size}")
171
+ return depth_image
 
 
 
 
 
 
 
 
 
 
 
 
 
172
 
173
  except Exception as e:
174
  print(f"[DEPTH] LeresDetector failed ({e}), falling back to grayscale depth")
 
453
  resized_image = input_image.resize((int(target_width), int(target_height)), Image.LANCZOS)
454
 
455
  # Generate depth map
456
+ print("Generating grayscale depth map...")
457
  depth_image = self.get_depth_map(resized_image)
458
  if depth_image.size != (target_width, target_height):
459
  depth_image = depth_image.resize((int(target_width), int(target_height)), Image.LANCZOS)
 
534
  print(f"Face info: bbox={face.bbox}, age={age if age else 'N/A'}, gender={gender_str}")
535
  print(f"Face crop size: {face_crop.size}, enhanced: {face_crop_enhanced.size if face_crop_enhanced else 'N/A'}")
536
 
537
+ # Set LORA scale - use fuse_lora for immediate effect
538
+ if hasattr(self.pipe, 'fuse_lora') and self.models_loaded['lora']:
539
  try:
540
+ self.pipe.fuse_lora(lora_scale=lora_scale)
541
+ print(f"[LORA] Fused with scale: {lora_scale}")
542
+ except Exception as e:
543
+ print(f"[WARNING] LORA fuse failed: {e}")
544
+ # Try set_adapters as fallback
545
+ try:
546
+ for adapter_name in ["retroart", "default_0"]:
547
+ try:
548
+ self.pipe.set_adapters([adapter_name], adapter_weights=[lora_scale])
549
+ print(f"[LORA] Set adapter '{adapter_name}' with scale: {lora_scale}")
550
+ break
551
+ except:
552
+ continue
553
+ except Exception as e2:
554
+ print(f"[WARNING] LORA set_adapters also failed: {e2}")
555
 
556
  except Exception as e:
557
  print(f"[WARNING] LORA set_adapters failed: {e}")