gaur3009 commited on
Commit
7ba5ba4
·
verified ·
1 Parent(s): cc5f70a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -11
app.py CHANGED
@@ -4,21 +4,20 @@ import cv2
4
  import numpy as np
5
  from torchvision import transforms
6
  from PIL import Image
7
- from transformers import DPTForDepthEstimation, DPTFeatureExtractor, MidasForDepthEstimation, MidasImageProcessor
8
 
9
- # Load depth estimation model (MiDaS v3 for better accuracy)
10
- model_name = "Intel/midas-v3" # Upgraded model
11
- processor = MidasImageProcessor.from_pretrained(model_name)
12
- depth_model = MidasForDepthEstimation.from_pretrained(model_name)
13
- depth_model.eval()
 
14
 
15
  def estimate_depth(image):
16
- """Estimate depth map from image using MiDaS v3."""
17
  image = image.convert("RGB")
18
- inputs = processor(images=image, return_tensors="pt")
19
  with torch.no_grad():
20
- outputs = depth_model(**inputs)
21
- depth = outputs.predicted_depth.squeeze().cpu().numpy()
22
  depth = cv2.resize(depth, (image.width, image.height))
23
  depth = (depth - depth.min()) / (depth.max() - depth.min()) * 255
24
  return depth.astype(np.uint8)
@@ -82,4 +81,4 @@ iface = gr.Interface(
82
  )
83
 
84
  if __name__ == "__main__":
85
- iface.launch(share=True)
 
4
  import numpy as np
5
  from torchvision import transforms
6
  from PIL import Image
 
7
 
8
+ # Load MiDaS depth estimation model from torch.hub
9
+ midas_model = torch.hub.load("intel-isl/MiDaS", "MiDaS_small")
10
+ midas_model.eval()
11
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
12
+ midas_model.to(device)
13
+ midas_transform = torch.hub.load("intel-isl/MiDaS", "transforms").default_transform
14
 
15
  def estimate_depth(image):
16
+ """Estimate depth map using MiDaS v3."""
17
  image = image.convert("RGB")
18
+ input_tensor = midas_transform(image).unsqueeze(0).to(device)
19
  with torch.no_grad():
20
+ depth = midas_model(input_tensor).squeeze().cpu().numpy()
 
21
  depth = cv2.resize(depth, (image.width, image.height))
22
  depth = (depth - depth.min()) / (depth.max() - depth.min()) * 255
23
  return depth.astype(np.uint8)
 
81
  )
82
 
83
  if __name__ == "__main__":
84
+ iface.launch(share=True)