mac9087 commited on
Commit
91fe13d
·
verified ·
1 Parent(s): a92b130

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -48
app.py CHANGED
@@ -676,55 +676,52 @@ def convert_image_to_3d():
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
- # Depth Anything (if loaded)
694
- if da_model and da_processor:
695
- inputs = da_processor(images=rgb_image, return_tensors="pt") # Use RGB image here
696
- inputs = {k: v.to("cpu") for k, v in inputs.items()}
697
- outputs = da_model(**inputs)
698
- da_depth = outputs.predicted_depth.squeeze()
699
- da_depth = torch.nn.functional.interpolate(
700
- da_depth.unsqueeze(0).unsqueeze(0),
701
- size=(image.height, image.width),
702
- mode='bicubic',
703
- align_corners=False
704
- ).squeeze()
705
- processing_jobs[job_id]['progress'] = 50
706
 
707
- # Improved fusion of depth maps
708
- fused_depth = fuse_depth_maps(dpt_depth, da_depth, detail_level)
709
- else:
710
- # Just use DPT with enhanced processing if Depth Anything is not available
711
- fused_depth = np.array(dpt_depth) if isinstance(dpt_depth, Image.Image) else dpt_depth
712
- if len(fused_depth.shape) > 2:
713
- fused_depth = np.mean(fused_depth, axis=2)
714
- # Apply more conservative normalization
715
- p_low, p_high = np.percentile(fused_depth, [2, 98])
716
- fused_depth = np.clip((fused_depth - p_low) / (p_high - p_low), 0, 1) if p_high > p_low else fused_depth
717
- # Apply compression to limit extreme depths
718
- fused_depth = np.power(fused_depth, 0.85)
719
-
720
- # Save depth map for debugging
721
- depth_debug_path = os.path.join(output_dir, "depth_map.png")
722
- cv2.imwrite(depth_debug_path, (fused_depth * 255).astype(np.uint8))
723
-
724
- return fused_depth
725
-
726
-
727
-
728
 
729
  fused_depth, error = process_with_timeout(estimate_depth, [], TIMEOUT_SECONDS)
730
 
 
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
+ # Depth Anything (if loaded)
694
+ if da_model and da_processor:
695
+ inputs = da_processor(images=rgb_image, return_tensors="pt") # Use RGB image here
696
+ inputs = {k: v.to("cpu") for k, v in inputs.items()}
697
+ outputs = da_model(**inputs)
698
+ da_depth = outputs.predicted_depth.squeeze()
699
+ da_depth = torch.nn.functional.interpolate(
700
+ da_depth.unsqueeze(0).unsqueeze(0),
701
+ size=(image.height, image.width),
702
+ mode='bicubic',
703
+ align_corners=False
704
+ ).squeeze()
705
+ processing_jobs[job_id]['progress'] = 50
706
 
707
+ # Improved fusion of depth maps
708
+ fused_depth = fuse_depth_maps(dpt_depth, da_depth, detail_level)
709
+ else:
710
+ # Just use DPT with enhanced processing if Depth Anything is not available
711
+ fused_depth = np.array(dpt_depth) if isinstance(dpt_depth, Image.Image) else dpt_depth
712
+ if len(fused_depth.shape) > 2:
713
+ fused_depth = np.mean(fused_depth, axis=2)
714
+ # Apply more conservative normalization
715
+ p_low, p_high = np.percentile(fused_depth, [2, 98])
716
+ fused_depth = np.clip((fused_depth - p_low) / (p_high - p_low), 0, 1) if p_high > p_low else fused_depth
717
+ # Apply compression to limit extreme depths
718
+ fused_depth = np.power(fused_depth, 0.85)
719
+
720
+ # Save depth map for debugging
721
+ depth_debug_path = os.path.join(output_dir, "depth_map.png")
722
+ cv2.imwrite(depth_debug_path, (fused_depth * 255).astype(np.uint8))
723
+
724
+ return fused_depth
 
 
 
725
 
726
  fused_depth, error = process_with_timeout(estimate_depth, [], TIMEOUT_SECONDS)
727