Spaces:
Runtime error
Runtime error
Update generator.py
Browse files- generator.py +54 -46
generator.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
"""
|
| 2 |
Generation logic for Pixagram AI Pixel Art Generator
|
| 3 |
"""
|
|
|
|
| 4 |
import torch
|
| 5 |
import numpy as np
|
| 6 |
import cv2
|
|
@@ -196,55 +197,62 @@ class RetroArtConverter:
|
|
| 196 |
print(f"[INFO] Verification skipped: {e}")
|
| 197 |
print("============================\n")
|
| 198 |
|
|
|
|
| 199 |
def get_depth_map(self, image):
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
|
|
|
| 245 |
gray = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2GRAY)
|
| 246 |
depth_colored = cv2.cvtColor(gray, cv2.COLOR_GRAY2RGB)
|
| 247 |
-
return Image.fromarray(depth_colored)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 248 |
|
| 249 |
|
| 250 |
def add_trigger_word(self, prompt):
|
|
|
|
| 1 |
"""
|
| 2 |
Generation logic for Pixagram AI Pixel Art Generator
|
| 3 |
"""
|
| 4 |
+
import gc
|
| 5 |
import torch
|
| 6 |
import numpy as np
|
| 7 |
import cv2
|
|
|
|
| 197 |
print(f"[INFO] Verification skipped: {e}")
|
| 198 |
print("============================\n")
|
| 199 |
|
| 200 |
+
|
| 201 |
def get_depth_map(self, image):
|
| 202 |
+
"""
|
| 203 |
+
Generate depth map using available depth detector.
|
| 204 |
+
Supports: LeresDetector, ZoeDetector, or MidasDetector.
|
| 205 |
+
"""
|
| 206 |
+
if self.depth_detector is not None:
|
| 207 |
+
try:
|
| 208 |
+
if image.mode != 'RGB':
|
| 209 |
+
image = image.convert('RGB')
|
| 210 |
+
|
| 211 |
+
orig_width, orig_height = image.size
|
| 212 |
+
orig_width = int(orig_width)
|
| 213 |
+
orig_height = int(orig_height)
|
| 214 |
+
|
| 215 |
+
target_width = int((orig_width // 64) * 64)
|
| 216 |
+
target_height = int((orig_height // 64) * 64)
|
| 217 |
+
|
| 218 |
+
target_width = int(max(64, target_width))
|
| 219 |
+
target_height = int(max(64, target_height))
|
| 220 |
+
|
| 221 |
+
size_for_depth = (int(target_width), int(target_height))
|
| 222 |
+
image_for_depth = image.resize(size_for_depth, Image.LANCZOS)
|
| 223 |
+
|
| 224 |
+
if target_width != orig_width or target_height != orig_height:
|
| 225 |
+
print(f"[DEPTH] Resized for {self.depth_type.upper()}Detector: {orig_width}x{orig_height} -> {target_width}x{target_height}")
|
| 226 |
+
|
| 227 |
+
# Use torch.no_grad() and clear cache
|
| 228 |
+
with torch.no_grad():
|
| 229 |
+
depth_image = self.depth_detector(image_for_depth)
|
| 230 |
+
|
| 231 |
+
# ADDED: Clear GPU cache after depth detection
|
| 232 |
+
if torch.cuda.is_available():
|
| 233 |
+
torch.cuda.empty_cache()
|
| 234 |
+
|
| 235 |
+
depth_width, depth_height = depth_image.size
|
| 236 |
+
if depth_width != orig_width or depth_height != orig_height:
|
| 237 |
+
depth_image = depth_image.resize((int(orig_width), int(orig_height)), Image.LANCZOS)
|
| 238 |
+
|
| 239 |
+
print(f"[DEPTH] {self.depth_type.upper()} depth map generated: {orig_width}x{orig_height}")
|
| 240 |
+
return depth_image
|
| 241 |
+
|
| 242 |
+
except Exception as e:
|
| 243 |
+
print(f"[DEPTH] {self.depth_type.upper()}Detector failed ({e}), falling back to grayscale depth")
|
| 244 |
+
# ADDED: Clear cache on error
|
| 245 |
+
if torch.cuda.is_available():
|
| 246 |
+
torch.cuda.empty_cache()
|
| 247 |
+
|
| 248 |
gray = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2GRAY)
|
| 249 |
depth_colored = cv2.cvtColor(gray, cv2.COLOR_GRAY2RGB)
|
| 250 |
+
return Image.fromarray(depth_colored)
|
| 251 |
+
else:
|
| 252 |
+
print("[DEPTH] No depth detector available, using grayscale fallback")
|
| 253 |
+
gray = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2GRAY)
|
| 254 |
+
depth_colored = cv2.cvtColor(gray, cv2.COLOR_GRAY2RGB)
|
| 255 |
+
return Image.fromarray(depth_colored)
|
| 256 |
|
| 257 |
|
| 258 |
def add_trigger_word(self, prompt):
|