nielsr HF staff commited on
Commit
202510d
1 Parent(s): 4e0bbd0

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -0
app.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import DPTFeatureExtractor, DPTForDepthEstimation
3
+ import torch
4
+ import numpy as np
5
+ import cv2
6
+
7
+ torch.hub.download_url_to_file('http://images.cocodataset.org/val2017/000000039769.jpg', 'cats.jpg')
8
+
9
+ feature_extractor = DPTFeatureExtractor.from_pretrained("Intel/dpt-large")
10
+ model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large")
11
+
12
+ def write_depth(depth, bits):
13
+ depth_min = depth.min()
14
+ depth_max = depth.max()
15
+
16
+ max_val = (2 ** (8 * bits)) - 1
17
+
18
+ if depth_max - depth_min > np.finfo("float").eps:
19
+ out = max_val * (depth - depth_min) / (depth_max - depth_min)
20
+ else:
21
+ out = np.zeros(depth.shape, dtype=depth.dtype)
22
+
23
+ cv2.imwrite("result.png", out.astype("uint16"), [cv2.IMWRITE_PNG_COMPRESSION, 0])
24
+
25
+ return
26
+
27
+ def process_image(image):
28
+ # prepare image for the model
29
+ encoding = feature_extractor(image, return_tensors="pt")
30
+
31
+ # forward pass
32
+ with torch.no_grad():
33
+ outputs = model(**encoding)
34
+
35
+ predicted_depth = outputs.predicted_depth
36
+
37
+ # interpolate to original size
38
+ predicted_depth = torch.nn.functional.interpolate(
39
+ predicted_depth.unsqueeze(1),
40
+ size=image.size[::-1],
41
+ mode="bicubic",
42
+ align_corners=False,
43
+ )
44
+ prediction = prediction.squeeze().cpu().numpy()
45
+
46
+ # write predicted depth to file
47
+ write_depth(prediction, bits=2)
48
+
49
+ result = Image.open("result.png")
50
+
51
+ return result
52
+
53
+ title = "Interactive demo: DPT"
54
+ description = "Demo for Intel's DPT, a Dense Prediction Transformer for state-of-the-art dense prediction tasks such as semantic segmentation and depth estimation."
55
+ examples =[['cats.jpg']]
56
+
57
+ css = ".output-image, .input-image {height: 40rem !important; width: 100% !important;}"
58
+ #css = "@media screen and (max-width: 600px) { .output_image, .input_image {height:20rem !important; width: 100% !important;} }"
59
+ # css = ".output_image, .input_image {height: 600px !important}"
60
+
61
+ iface = gr.Interface(fn=process_image,
62
+ inputs=gr.inputs.Image(type="pil"),
63
+ outputs=gr.outputs.Image(type="pil", label="predicted depth"),
64
+ title=title,
65
+ description=description,
66
+ examples=examples,
67
+ css=css,
68
+ enable_queue=True)
69
+ iface.launch(debug=True)