Update app.py
Browse files
app.py
CHANGED
@@ -14,27 +14,24 @@ app = FastAPI()
|
|
14 |
# 🟢 Chọn thiết bị xử lý (GPU nếu có)
|
15 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
16 |
|
17 |
-
# 🟢 Tải model DPT-
|
18 |
feature_extractor = DPTFeatureExtractor.from_pretrained("Intel/dpt-swinv2-tiny-256")
|
19 |
model = DPTForDepthEstimation.from_pretrained("Intel/dpt-swinv2-tiny-256").to(device)
|
20 |
model.eval()
|
21 |
|
22 |
-
def process_depth_map(
|
23 |
"""Xử lý Depth Map và hiển thị trên Hugging Face"""
|
24 |
start_time = time.time()
|
25 |
|
26 |
-
# 🟢 Đọc file ảnh
|
27 |
-
image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
|
28 |
-
|
29 |
# 🔵 Resize ảnh để tăng tốc độ xử lý
|
30 |
-
image =
|
31 |
image_np = np.array(image)
|
32 |
flipped_image = cv2.flip(image_np, -1)
|
33 |
|
34 |
# 🟢 Chuẩn bị ảnh cho mô hình
|
35 |
inputs = feature_extractor(images=flipped_image, return_tensors="pt").to(device)
|
36 |
|
37 |
-
# 🟢 Dự đoán Depth Map với DPT-
|
38 |
with torch.no_grad():
|
39 |
outputs = model(**inputs)
|
40 |
|
@@ -49,12 +46,12 @@ def process_depth_map(image_bytes):
|
|
49 |
end_time = time.time()
|
50 |
print(f"⏳ DPT xử lý trong {end_time - start_time:.4f} giây")
|
51 |
|
52 |
-
return depth_pil # Trả về ảnh để hiển thị trên
|
53 |
|
54 |
# 🟢 Tạo UI trên Hugging Face Spaces với Gradio
|
55 |
gr.Interface(
|
56 |
fn=process_depth_map,
|
57 |
-
inputs=gr.Image(type="
|
58 |
outputs=gr.Image(type="pil"),
|
59 |
title="🔍 Depth Map Estimation",
|
60 |
description="Tải ảnh lên để xem Depth Map",
|
|
|
14 |
# 🟢 Chọn thiết bị xử lý (GPU nếu có)
|
15 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
16 |
|
17 |
+
# 🟢 Tải model DPT-Swinv2 Tiny để tăng tốc
|
18 |
feature_extractor = DPTFeatureExtractor.from_pretrained("Intel/dpt-swinv2-tiny-256")
|
19 |
model = DPTForDepthEstimation.from_pretrained("Intel/dpt-swinv2-tiny-256").to(device)
|
20 |
model.eval()
|
21 |
|
22 |
+
def process_depth_map(image_pil):
|
23 |
"""Xử lý Depth Map và hiển thị trên Hugging Face"""
|
24 |
start_time = time.time()
|
25 |
|
|
|
|
|
|
|
26 |
# 🔵 Resize ảnh để tăng tốc độ xử lý
|
27 |
+
image = image_pil.resize((256, 256))
|
28 |
image_np = np.array(image)
|
29 |
flipped_image = cv2.flip(image_np, -1)
|
30 |
|
31 |
# 🟢 Chuẩn bị ảnh cho mô hình
|
32 |
inputs = feature_extractor(images=flipped_image, return_tensors="pt").to(device)
|
33 |
|
34 |
+
# 🟢 Dự đoán Depth Map với DPT-Swinv2 Tiny
|
35 |
with torch.no_grad():
|
36 |
outputs = model(**inputs)
|
37 |
|
|
|
46 |
end_time = time.time()
|
47 |
print(f"⏳ DPT xử lý trong {end_time - start_time:.4f} giây")
|
48 |
|
49 |
+
return depth_pil # Trả về ảnh để hiển thị trên Gradio
|
50 |
|
51 |
# 🟢 Tạo UI trên Hugging Face Spaces với Gradio
|
52 |
gr.Interface(
|
53 |
fn=process_depth_map,
|
54 |
+
inputs=gr.Image(type="pil"), # 🔥 Sửa lỗi: dùng "pil" thay vì "bytes"
|
55 |
outputs=gr.Image(type="pil"),
|
56 |
title="🔍 Depth Map Estimation",
|
57 |
description="Tải ảnh lên để xem Depth Map",
|