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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -45
app.py CHANGED
@@ -676,52 +676,56 @@ 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
  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