Spaces:
Running
Running
pkoooo
commited on
Commit
·
c033455
1
Parent(s):
8ea5ea0
Patch Gradio api_info to ignore schema parsing errors
Browse files
app.py
CHANGED
|
@@ -490,7 +490,55 @@ def convert_image_to_stl(
|
|
| 490 |
final_min, final_max, final_range, final_std
|
| 491 |
)
|
| 492 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 493 |
# Calculate expected z-range in the final mesh
|
|
|
|
| 494 |
expected_z_min = final_min * height_scale_val + base_thickness_val
|
| 495 |
expected_z_max = final_max * height_scale_val + base_thickness_val
|
| 496 |
expected_z_range = expected_z_max - expected_z_min
|
|
|
|
| 490 |
final_min, final_max, final_range, final_std
|
| 491 |
)
|
| 492 |
|
| 493 |
+
# Detect and flatten background to base level
|
| 494 |
+
# This prevents the background from creating unwanted 3D structure
|
| 495 |
+
# Background is typically the most common/brightest value
|
| 496 |
+
background_threshold = 0.85 # Pixels above this are considered background
|
| 497 |
+
background_mask = normalized_depth >= background_threshold
|
| 498 |
+
|
| 499 |
+
if np.any(background_mask):
|
| 500 |
+
background_count = np.sum(background_mask)
|
| 501 |
+
total_pixels = normalized_depth.size
|
| 502 |
+
background_ratio = background_count / total_pixels
|
| 503 |
+
logger.info(
|
| 504 |
+
"Detected background: %.1f%% of pixels above threshold %.2f",
|
| 505 |
+
background_ratio * 100, background_threshold
|
| 506 |
+
)
|
| 507 |
+
|
| 508 |
+
# Set background pixels to 0 (base level)
|
| 509 |
+
normalized_depth[background_mask] = 0.0
|
| 510 |
+
|
| 511 |
+
# Re-normalize non-background pixels to use full height range
|
| 512 |
+
non_bg_mask = ~background_mask
|
| 513 |
+
if np.any(non_bg_mask):
|
| 514 |
+
non_bg_values = normalized_depth[non_bg_mask]
|
| 515 |
+
if len(non_bg_values) > 0:
|
| 516 |
+
non_bg_min = float(non_bg_values.min())
|
| 517 |
+
non_bg_max = float(non_bg_values.max())
|
| 518 |
+
if non_bg_max - non_bg_min > 1e-8:
|
| 519 |
+
# Scale non-background to [0, 1] range
|
| 520 |
+
normalized_depth[non_bg_mask] = (
|
| 521 |
+
(non_bg_values - non_bg_min) / (non_bg_max - non_bg_min)
|
| 522 |
+
)
|
| 523 |
+
logger.info(
|
| 524 |
+
"Re-normalized content area: min=%.4f, max=%.4f",
|
| 525 |
+
float(normalized_depth[non_bg_mask].min()),
|
| 526 |
+
float(normalized_depth[non_bg_mask].max())
|
| 527 |
+
)
|
| 528 |
+
|
| 529 |
+
# Final statistics after background removal
|
| 530 |
+
final_min = float(normalized_depth.min())
|
| 531 |
+
final_max = float(normalized_depth.max())
|
| 532 |
+
final_range = final_max - final_min
|
| 533 |
+
final_std = float(np.std(normalized_depth))
|
| 534 |
+
|
| 535 |
+
logger.info(
|
| 536 |
+
"After background removal: min=%.4f, max=%.4f, range=%.4f, std=%.4f",
|
| 537 |
+
final_min, final_max, final_range, final_std
|
| 538 |
+
)
|
| 539 |
+
|
| 540 |
# Calculate expected z-range in the final mesh
|
| 541 |
+
# Background will be at base_height, content will be above
|
| 542 |
expected_z_min = final_min * height_scale_val + base_thickness_val
|
| 543 |
expected_z_max = final_max * height_scale_val + base_thickness_val
|
| 544 |
expected_z_range = expected_z_max - expected_z_min
|