Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,58 +1,52 @@
|
|
1 |
-
from doctest import Example
|
2 |
import gradio as gr
|
3 |
from transformers import DPTFeatureExtractor, DPTForDepthEstimation
|
4 |
import torch
|
5 |
import numpy as np
|
6 |
from PIL import Image
|
7 |
-
import open3d as o3d
|
8 |
-
from pathlib import Path
|
9 |
-
import os
|
10 |
|
|
|
11 |
feature_extractor = DPTFeatureExtractor.from_pretrained("Intel/dpt-large")
|
12 |
model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large")
|
|
|
13 |
|
|
|
|
|
|
|
|
|
14 |
|
15 |
-
|
16 |
-
image_path = Path(image_path)
|
17 |
-
image_raw = Image.open(image_path)
|
18 |
-
image = image_raw.resize(
|
19 |
-
(1920, int(1920 * image_raw.size[1] / image_raw.size[0])),
|
20 |
-
Image.Resampling.LANCZOS,
|
21 |
-
)
|
22 |
-
# prepare image for the model
|
23 |
encoding = feature_extractor(image, return_tensors="pt")
|
24 |
-
|
25 |
-
#
|
26 |
with torch.no_grad():
|
27 |
outputs = model(**encoding)
|
28 |
predicted_depth = outputs.predicted_depth
|
29 |
-
|
30 |
-
#
|
31 |
prediction = torch.nn.functional.interpolate(
|
32 |
predicted_depth.unsqueeze(1),
|
33 |
size=image.size[::-1],
|
34 |
mode="bicubic",
|
35 |
-
align_corners=False
|
36 |
).squeeze()
|
|
|
|
|
37 |
output = prediction.cpu().numpy()
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
|
|
|
42 |
|
43 |
-
|
44 |
-
|
45 |
-
description = "This demo is a variation from the original <a href='https://huggingface.co/spaces/nielsr/dpt-depth-estimation' target='_blank'>DPT Demo</a>. It uses the DPT model to predict the depth of an image and then uses 3D Point Cloud to create a 3D object."
|
46 |
-
examples = [["examples/" + img] for img in os.listdir("examples/")]
|
47 |
|
48 |
iface = gr.Interface(
|
49 |
fn=process_image,
|
50 |
-
inputs=
|
51 |
-
outputs=
|
52 |
title=title,
|
53 |
description=description,
|
54 |
-
examples=examples,
|
55 |
-
allow_flagging="never",
|
56 |
-
#cache_examples=False,
|
57 |
)
|
58 |
-
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import DPTFeatureExtractor, DPTForDepthEstimation
|
3 |
import torch
|
4 |
import numpy as np
|
5 |
from PIL import Image
|
|
|
|
|
|
|
6 |
|
7 |
+
# Load model và 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 |
+
# Đảm bảo ảnh là RGB
|
14 |
+
if image.mode != "RGB":
|
15 |
+
image = image.convert("RGB")
|
16 |
|
17 |
+
# Encode
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
encoding = feature_extractor(image, return_tensors="pt")
|
19 |
+
|
20 |
+
# Dự đoán depth
|
21 |
with torch.no_grad():
|
22 |
outputs = model(**encoding)
|
23 |
predicted_depth = outputs.predicted_depth
|
24 |
+
|
25 |
+
# Resize về kích thước ảnh gốc
|
26 |
prediction = torch.nn.functional.interpolate(
|
27 |
predicted_depth.unsqueeze(1),
|
28 |
size=image.size[::-1],
|
29 |
mode="bicubic",
|
30 |
+
align_corners=False
|
31 |
).squeeze()
|
32 |
+
|
33 |
+
# Chuẩn hóa và chuyển về ảnh uint8
|
34 |
output = prediction.cpu().numpy()
|
35 |
+
output = (output - np.min(output)) / (np.max(output) - np.min(output)) # normalize
|
36 |
+
formatted = (output * 255).astype("uint8")
|
37 |
+
depth_img = Image.fromarray(formatted)
|
38 |
|
39 |
+
return depth_img
|
40 |
|
41 |
+
title = "Demo: zero-shot depth estimation with DPT"
|
42 |
+
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."
|
|
|
|
|
43 |
|
44 |
iface = gr.Interface(
|
45 |
fn=process_image,
|
46 |
+
inputs=gr.inputs.Image(type="pil", label="Input Image"),
|
47 |
+
outputs=gr.outputs.Image(type="pil", label="Predicted Depth"),
|
48 |
title=title,
|
49 |
description=description,
|
|
|
|
|
|
|
50 |
)
|
51 |
+
|
52 |
+
iface.launch(debug=True)
|