Spaces:
Running
Running
Update bald_processor.py
Browse files- bald_processor.py +9 -9
bald_processor.py
CHANGED
|
@@ -10,7 +10,7 @@ import traceback
|
|
| 10 |
|
| 11 |
# Global model load with error handling
|
| 12 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 13 |
-
print(f"Using device:
|
| 14 |
|
| 15 |
print("Loading SegFormer face-parsing model...")
|
| 16 |
try:
|
|
@@ -20,7 +20,7 @@ try:
|
|
| 20 |
model.eval()
|
| 21 |
print("Model loaded successfully!")
|
| 22 |
except Exception as e:
|
| 23 |
-
print(f"CRITICAL: Model loading failed!
|
| 24 |
traceback.print_exc()
|
| 25 |
|
| 26 |
hair_class_id = 13
|
|
@@ -36,7 +36,7 @@ def make_realistic_bald(image_bytes: bytes) -> bytes:
|
|
| 36 |
except UnidentifiedImageError:
|
| 37 |
raise ValueError("Invalid image format or corrupt bytes")
|
| 38 |
except Exception as e:
|
| 39 |
-
raise ValueError(f"Image open failed:
|
| 40 |
|
| 41 |
orig_w, orig_h = image.size
|
| 42 |
original_np = np.array(image)
|
|
@@ -65,7 +65,7 @@ def make_realistic_bald(image_bytes: bytes) -> bytes:
|
|
| 65 |
logits = outputs.logits
|
| 66 |
|
| 67 |
upsampled_logits = torch.nn.functional.interpolate(
|
| 68 |
-
logits, size=(working_h, working_w), mode="bilinear", align_corners=
|
| 69 |
)
|
| 70 |
parsing = upsampled_logits.argmax(dim=1).squeeze(0).cpu().numpy()
|
| 71 |
|
|
@@ -114,7 +114,7 @@ def make_realistic_bald(image_bytes: bytes) -> bytes:
|
|
| 114 |
if len(skin_pixels_full) > 100:
|
| 115 |
mean_color_rgb = np.mean(skin_pixels_full, axis=0).astype(int)
|
| 116 |
except Exception as skin_err:
|
| 117 |
-
print(f"Skin detection error (fallback used):
|
| 118 |
|
| 119 |
# Make detected skin color 30% brighter
|
| 120 |
mean_color_rgb = np.array(mean_color_rgb, dtype=float)
|
|
@@ -123,7 +123,7 @@ def make_realistic_bald(image_bytes: bytes) -> bytes:
|
|
| 123 |
|
| 124 |
# Clear forehead color print (updated one)
|
| 125 |
hex_color = '#%02x%02x%02x' % tuple(mean_color_rgb)
|
| 126 |
-
print(f"Adjusted (30% brighter) skin color → RGB:
|
| 127 |
|
| 128 |
hair_mask = (parsing == hair_class_id).astype(np.uint8)
|
| 129 |
|
|
@@ -173,9 +173,9 @@ def make_realistic_bald(image_bytes: bytes) -> bytes:
|
|
| 173 |
hair_pixels = np.sum(hair_mask_final)
|
| 174 |
|
| 175 |
final_mask = hair_mask_final.copy()
|
| 176 |
-
use_extended_mask =
|
| 177 |
if hair_pixels > 380000:
|
| 178 |
-
use_extended_mask =
|
| 179 |
big_kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (25, 25))
|
| 180 |
extended = cv2.dilate(hair_mask_final, big_kernel, iterations=1)
|
| 181 |
upper = np.zeros_like(hair_mask_final)
|
|
@@ -238,4 +238,4 @@ def make_realistic_bald(image_bytes: bytes) -> bytes:
|
|
| 238 |
except Exception as main_err:
|
| 239 |
print("ERROR in make_realistic_bald:")
|
| 240 |
traceback.print_exc()
|
| 241 |
-
raise RuntimeError(f"Bald processing failed:
|
|
|
|
| 10 |
|
| 11 |
# Global model load with error handling
|
| 12 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 13 |
+
print(f"Using device: {device} | CUDA available: {torch.cuda.is_available()}")
|
| 14 |
|
| 15 |
print("Loading SegFormer face-parsing model...")
|
| 16 |
try:
|
|
|
|
| 20 |
model.eval()
|
| 21 |
print("Model loaded successfully!")
|
| 22 |
except Exception as e:
|
| 23 |
+
print(f"CRITICAL: Model loading failed! {str(e)}")
|
| 24 |
traceback.print_exc()
|
| 25 |
|
| 26 |
hair_class_id = 13
|
|
|
|
| 36 |
except UnidentifiedImageError:
|
| 37 |
raise ValueError("Invalid image format or corrupt bytes")
|
| 38 |
except Exception as e:
|
| 39 |
+
raise ValueError(f"Image open failed: {str(e)}")
|
| 40 |
|
| 41 |
orig_w, orig_h = image.size
|
| 42 |
original_np = np.array(image)
|
|
|
|
| 65 |
logits = outputs.logits
|
| 66 |
|
| 67 |
upsampled_logits = torch.nn.functional.interpolate(
|
| 68 |
+
logits, size=(working_h, working_w), mode="bilinear", align_corners=False
|
| 69 |
)
|
| 70 |
parsing = upsampled_logits.argmax(dim=1).squeeze(0).cpu().numpy()
|
| 71 |
|
|
|
|
| 114 |
if len(skin_pixels_full) > 100:
|
| 115 |
mean_color_rgb = np.mean(skin_pixels_full, axis=0).astype(int)
|
| 116 |
except Exception as skin_err:
|
| 117 |
+
print(f"Skin detection error (fallback used): {str(skin_err)}")
|
| 118 |
|
| 119 |
# Make detected skin color 30% brighter
|
| 120 |
mean_color_rgb = np.array(mean_color_rgb, dtype=float)
|
|
|
|
| 123 |
|
| 124 |
# Clear forehead color print (updated one)
|
| 125 |
hex_color = '#%02x%02x%02x' % tuple(mean_color_rgb)
|
| 126 |
+
print(f"Adjusted (30% brighter) skin color → RGB: {mean_color_rgb.tolist()} | Hex: {hex_color}")
|
| 127 |
|
| 128 |
hair_mask = (parsing == hair_class_id).astype(np.uint8)
|
| 129 |
|
|
|
|
| 173 |
hair_pixels = np.sum(hair_mask_final)
|
| 174 |
|
| 175 |
final_mask = hair_mask_final.copy()
|
| 176 |
+
use_extended_mask = False
|
| 177 |
if hair_pixels > 380000:
|
| 178 |
+
use_extended_mask = True
|
| 179 |
big_kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (25, 25))
|
| 180 |
extended = cv2.dilate(hair_mask_final, big_kernel, iterations=1)
|
| 181 |
upper = np.zeros_like(hair_mask_final)
|
|
|
|
| 238 |
except Exception as main_err:
|
| 239 |
print("ERROR in make_realistic_bald:")
|
| 240 |
traceback.print_exc()
|
| 241 |
+
raise RuntimeError(f"Bald processing failed: {str(main_err)}")
|