Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -676,52 +676,56 @@ def convert_image_to_3d():
|
|
| 676 |
return
|
| 677 |
|
| 678 |
try:
|
| 679 |
-
|
| 680 |
-
|
| 681 |
-
|
| 682 |
-
|
| 683 |
-
|
| 684 |
-
|
| 685 |
-
|
| 686 |
-
|
| 687 |
-
|
| 688 |
-
|
| 689 |
-
|
| 690 |
-
|
| 691 |
-
|
| 692 |
-
|
| 693 |
-
|
| 694 |
-
|
| 695 |
-
|
| 696 |
-
|
| 697 |
-
|
| 698 |
-
|
| 699 |
-
|
| 700 |
-
|
| 701 |
-
|
| 702 |
-
|
| 703 |
-
|
| 704 |
-
|
| 705 |
-
|
|
|
|
|
|
|
|
|
|
| 706 |
|
| 707 |
-
|
| 708 |
-
|
| 709 |
-
|
| 710 |
-
|
| 711 |
-
|
| 712 |
-
|
| 713 |
-
|
| 714 |
-
|
| 715 |
-
|
| 716 |
-
|
| 717 |
-
|
| 718 |
-
|
| 719 |
-
|
| 720 |
-
|
| 721 |
-
|
| 722 |
-
|
| 723 |
-
|
| 724 |
-
|
|
|
|
| 725 |
|
| 726 |
fused_depth, error = process_with_timeout(estimate_depth, [], TIMEOUT_SECONDS)
|
| 727 |
|
|
|
|
| 676 |
return
|
| 677 |
|
| 678 |
try:
|
| 679 |
+
def estimate_depth():
|
| 680 |
+
with torch.no_grad():
|
| 681 |
+
# Make sure image is in RGB format for the models
|
| 682 |
+
rgb_image = image
|
| 683 |
+
if rgb_image.mode == 'RGBA':
|
| 684 |
+
# Convert RGBA to RGB for model processing
|
| 685 |
+
rgb_image = Image.new('RGB', image.size, (255, 255, 255))
|
| 686 |
+
rgb_image.paste(image, mask=image.split()[3]) # Use alpha channel as mask
|
| 687 |
+
|
| 688 |
+
# DPT-Large
|
| 689 |
+
dpt_result = dpt_model(rgb_image)
|
| 690 |
+
dpt_depth = dpt_result["depth"]
|
| 691 |
+
processing_jobs[job_id]['progress'] = 40
|
| 692 |
+
|
| 693 |
+
|
| 694 |
+
|
| 695 |
+
|
| 696 |
+
# Depth Anything (if loaded)
|
| 697 |
+
if da_model and da_processor:
|
| 698 |
+
inputs = da_processor(images=rgb_image, return_tensors="pt") # Use RGB image here
|
| 699 |
+
inputs = {k: v.to("cpu") for k, v in inputs.items()}
|
| 700 |
+
outputs = da_model(**inputs)
|
| 701 |
+
da_depth = outputs.predicted_depth.squeeze()
|
| 702 |
+
da_depth = torch.nn.functional.interpolate(
|
| 703 |
+
da_depth.unsqueeze(0).unsqueeze(0),
|
| 704 |
+
size=(image.height, image.width),
|
| 705 |
+
mode='bicubic',
|
| 706 |
+
align_corners=False
|
| 707 |
+
).squeeze()
|
| 708 |
+
processing_jobs[job_id]['progress'] = 50
|
| 709 |
|
| 710 |
+
# Improved fusion of depth maps
|
| 711 |
+
fused_depth = fuse_depth_maps(dpt_depth, da_depth, detail_level)
|
| 712 |
+
else:
|
| 713 |
+
# Just use DPT with enhanced processing if Depth Anything is not available
|
| 714 |
+
fused_depth = np.array(dpt_depth) if isinstance(dpt_depth, Image.Image) else dpt_depth
|
| 715 |
+
if len(fused_depth.shape) > 2:
|
| 716 |
+
fused_depth = np.mean(fused_depth, axis=2)
|
| 717 |
+
# Apply more conservative normalization
|
| 718 |
+
p_low, p_high = np.percentile(fused_depth, [2, 98])
|
| 719 |
+
fused_depth = np.clip((fused_depth - p_low) / (p_high - p_low), 0, 1) if p_high > p_low else fused_depth
|
| 720 |
+
# Apply compression to limit extreme depths
|
| 721 |
+
fused_depth = np.power(fused_depth, 0.85)
|
| 722 |
+
|
| 723 |
+
# Save depth map for debugging
|
| 724 |
+
depth_debug_path = os.path.join(output_dir, "depth_map.png")
|
| 725 |
+
cv2.imwrite(depth_debug_path, (fused_depth * 255).astype(np.uint8))
|
| 726 |
+
|
| 727 |
+
return fused_depth
|
| 728 |
+
|
| 729 |
|
| 730 |
fused_depth, error = process_with_timeout(estimate_depth, [], TIMEOUT_SECONDS)
|
| 731 |
|