Update app.py
Browse files
app.py
CHANGED
@@ -3,32 +3,30 @@ import time
|
|
3 |
import numpy as np
|
4 |
import cv2
|
5 |
import torch
|
|
|
6 |
from transformers import DPTFeatureExtractor, DPTForDepthEstimation
|
7 |
from fastapi import FastAPI, File, UploadFile
|
8 |
from PIL import Image
|
9 |
import uvicorn
|
10 |
|
11 |
-
# 🟢 Tạo FastAPI
|
12 |
app = FastAPI()
|
13 |
|
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-Hybrid để 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 |
-
|
23 |
-
|
24 |
-
"""Xử lý ảnh Depth Map và lưu ảnh để hiển thị trên Hugging Face"""
|
25 |
start_time = time.time()
|
26 |
|
27 |
-
# 🟢 Đọc file ảnh
|
28 |
-
image_bytes = await file.read()
|
29 |
image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
|
30 |
|
31 |
-
# 🔵 Resize ảnh để xử lý
|
32 |
image = image.resize((256, 256))
|
33 |
image_np = np.array(image)
|
34 |
flipped_image = cv2.flip(image_np, -1)
|
@@ -48,41 +46,19 @@ async def analyze_path(file: UploadFile = File(...)):
|
|
48 |
depth_colored = cv2.applyColorMap(depth_map, cv2.COLORMAP_INFERNO)
|
49 |
depth_pil = Image.fromarray(depth_colored)
|
50 |
|
51 |
-
# 🟢 Lưu ảnh Depth Map để hiển thị trên Gradio
|
52 |
-
depth_pil.save("depth_map.png") # 🎯 **Gradio sẽ truy cập ảnh này**
|
53 |
-
|
54 |
end_time = time.time()
|
55 |
print(f"⏳ DPT xử lý trong {end_time - start_time:.4f} giây")
|
56 |
|
57 |
-
#
|
58 |
-
start_detect_time = time.time()
|
59 |
-
command = detect_path(depth_map)
|
60 |
-
end_detect_time = time.time()
|
61 |
-
print(f"⏳ detect_path() xử lý trong {end_detect_time - start_detect_time:.4f} giây")
|
62 |
-
|
63 |
-
return command # Trả về lệnh điều hướng (không kèm ảnh)
|
64 |
-
|
65 |
-
def detect_path(depth_map):
|
66 |
-
"""Phân tích đường đi từ ảnh Depth Map"""
|
67 |
-
h, w = depth_map.shape
|
68 |
-
center_x = w // 2
|
69 |
-
scan_y = int(h * 0.8) # Quét dòng 80% từ trên xuống
|
70 |
-
|
71 |
-
left_region = np.mean(depth_map[scan_y, :center_x])
|
72 |
-
right_region = np.mean(depth_map[scan_y, center_x:])
|
73 |
-
center_region = np.mean(depth_map[scan_y, center_x - 40:center_x + 40])
|
74 |
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
else:
|
84 |
-
return "backward"
|
85 |
|
86 |
-
# 🟢 Chạy server FastAPI trên Hugging Face Spaces
|
87 |
if __name__ == "__main__":
|
88 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
3 |
import numpy as np
|
4 |
import cv2
|
5 |
import torch
|
6 |
+
import gradio as gr
|
7 |
from transformers import DPTFeatureExtractor, DPTForDepthEstimation
|
8 |
from fastapi import FastAPI, File, UploadFile
|
9 |
from PIL import Image
|
10 |
import uvicorn
|
11 |
|
|
|
12 |
app = FastAPI()
|
13 |
|
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-Hybrid thay cho ZoeDepth để 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_bytes):
|
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 = image.resize((256, 256))
|
31 |
image_np = np.array(image)
|
32 |
flipped_image = cv2.flip(image_np, -1)
|
|
|
46 |
depth_colored = cv2.applyColorMap(depth_map, cv2.COLORMAP_INFERNO)
|
47 |
depth_pil = Image.fromarray(depth_colored)
|
48 |
|
|
|
|
|
|
|
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 Hugging Face
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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="bytes"),
|
58 |
+
outputs=gr.Image(type="pil"),
|
59 |
+
title="🔍 Depth Map Estimation",
|
60 |
+
description="Tải ảnh lên để xem Depth Map",
|
61 |
+
).launch(share=True) # `share=True` để tạo link truy cập từ bên ngoài
|
|
|
|
|
62 |
|
|
|
63 |
if __name__ == "__main__":
|
64 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|