bharathj16 commited on
Commit
0530e27
1 Parent(s): 764d3dc

Create ImageDepth1.py

Browse files
Files changed (1) hide show
  1. ImageDepth1.py +31 -0
ImageDepth1.py ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import DPTImageProcessor, DPTForDepthEstimation
2
+ import torch
3
+ import numpy as np
4
+ from PIL import Image
5
+ import requests
6
+
7
+ url = "http://images.cocodataset.org/val2017/000000039769.jpg"
8
+ image = Image.open(requests.get(url, stream=True).raw)
9
+
10
+ processor = DPTImageProcessor.from_pretrained("Intel/dpt-large")
11
+ model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large")
12
+
13
+ # prepare image for the model
14
+ inputs = processor(images=image, return_tensors="pt")
15
+
16
+ with torch.no_grad():
17
+ outputs = model(**inputs)
18
+ predicted_depth = outputs.predicted_depth
19
+
20
+ # interpolate to original size
21
+ prediction = torch.nn.functional.interpolate(
22
+ predicted_depth.unsqueeze(1),
23
+ size=image.size[::-1],
24
+ mode="bicubic",
25
+ align_corners=False,
26
+ )
27
+
28
+ # visualize the prediction
29
+ output = prediction.squeeze().cpu().numpy()
30
+ formatted = (output * 255 / np.max(output)).astype("uint8")
31
+ depth = Image.fromarray(formatted)