Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,41 +4,46 @@ import torch
|
|
4 |
import numpy as np
|
5 |
from PIL import Image
|
6 |
|
7 |
-
#
|
8 |
-
|
9 |
feature_extractor = DPTFeatureExtractor.from_pretrained("Intel/dpt-large")
|
10 |
model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large")
|
|
|
11 |
|
12 |
def process_image(image):
|
13 |
-
#
|
14 |
encoding = feature_extractor(image, return_tensors="pt")
|
15 |
|
16 |
-
#
|
17 |
with torch.no_grad():
|
18 |
-
|
19 |
-
|
20 |
|
21 |
-
#
|
22 |
prediction = torch.nn.functional.interpolate(
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
|
|
|
|
28 |
output = prediction.cpu().numpy()
|
29 |
formatted = (output * 255 / np.max(output)).astype('uint8')
|
30 |
-
|
31 |
img = Image.fromarray(formatted)
|
32 |
-
return img
|
33 |
|
34 |
-
|
35 |
-
|
|
|
|
|
|
|
36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
-
iface
|
39 |
-
inputs=gr.inputs.Image(type="pil"),
|
40 |
-
outputs=gr.outputs.Image(type="pil", label="predicted depth"),
|
41 |
-
title=title,
|
42 |
-
description=description,
|
43 |
-
enable_queue=True)
|
44 |
-
iface.launch(debug=True)
|
|
|
4 |
import numpy as np
|
5 |
from PIL import Image
|
6 |
|
7 |
+
# Load model and feature extractor
|
|
|
8 |
feature_extractor = DPTFeatureExtractor.from_pretrained("Intel/dpt-large")
|
9 |
model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large")
|
10 |
+
model.eval()
|
11 |
|
12 |
def process_image(image):
|
13 |
+
# Chuẩn hóa ảnh đầu vào
|
14 |
encoding = feature_extractor(image, return_tensors="pt")
|
15 |
|
16 |
+
# Forward qua model
|
17 |
with torch.no_grad():
|
18 |
+
outputs = model(**encoding)
|
19 |
+
predicted_depth = outputs.predicted_depth
|
20 |
|
21 |
+
# Resize output về đúng kích thước ảnh gốc
|
22 |
prediction = torch.nn.functional.interpolate(
|
23 |
+
predicted_depth.unsqueeze(1),
|
24 |
+
size=image.size[::-1], # (H, W)
|
25 |
+
mode="bicubic",
|
26 |
+
align_corners=False
|
27 |
+
).squeeze()
|
28 |
+
|
29 |
+
# Chuyển thành ảnh uint8
|
30 |
output = prediction.cpu().numpy()
|
31 |
formatted = (output * 255 / np.max(output)).astype('uint8')
|
|
|
32 |
img = Image.fromarray(formatted)
|
|
|
33 |
|
34 |
+
return img
|
35 |
+
|
36 |
+
# Interface
|
37 |
+
title = "Demo: Zero-shot Depth Estimation with DPT"
|
38 |
+
description = "Intel's DPT: Dense Prediction Transformer for depth estimation from a single image."
|
39 |
|
40 |
+
iface = gr.Interface(
|
41 |
+
fn=process_image,
|
42 |
+
inputs=gr.Image(type="pil", label="Input Image"),
|
43 |
+
outputs=gr.Image(type="pil", label="Predicted Depth"),
|
44 |
+
title=title,
|
45 |
+
description=description,
|
46 |
+
allow_flagging="never"
|
47 |
+
)
|
48 |
|
49 |
+
iface.launch(debug=True)
|
|
|
|
|
|
|
|
|
|
|
|