Update app.py
Browse files
app.py
CHANGED
@@ -3,9 +3,9 @@ import time
|
|
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 |
|
@@ -14,24 +14,29 @@ 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 |
-
|
23 |
-
|
|
|
24 |
start_time = time.time()
|
25 |
|
26 |
-
#
|
27 |
-
|
|
|
|
|
|
|
|
|
28 |
image_np = np.array(image)
|
29 |
-
|
30 |
|
31 |
# 🟢 Chuẩn bị ảnh cho mô hình
|
32 |
-
inputs = feature_extractor(images=
|
33 |
|
34 |
-
# 🟢 Dự đoán Depth Map với DPT-
|
35 |
with torch.no_grad():
|
36 |
outputs = model(**inputs)
|
37 |
|
@@ -43,19 +48,19 @@ def process_depth_map(image_pil):
|
|
43 |
depth_colored = cv2.applyColorMap(depth_map, cv2.COLORMAP_INFERNO)
|
44 |
depth_pil = Image.fromarray(depth_colored)
|
45 |
|
|
|
|
|
|
|
46 |
end_time = time.time()
|
47 |
print(f"⏳ DPT xử lý trong {end_time - start_time:.4f} giây")
|
48 |
|
49 |
-
return
|
50 |
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
outputs=gr.Image(type="pil"),
|
56 |
-
title="🔍 Depth Map Estimation",
|
57 |
-
description="Tải ảnh lên để xem Depth Map",
|
58 |
-
).launch(share=True) # `share=True` để tạo link truy cập từ bên ngoài
|
59 |
|
|
|
60 |
if __name__ == "__main__":
|
61 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
3 |
import numpy as np
|
4 |
import cv2
|
5 |
import torch
|
|
|
6 |
from transformers import DPTFeatureExtractor, DPTForDepthEstimation
|
7 |
+
from fastapi import FastAPI, File, UploadFile, Response
|
8 |
+
from fastapi.responses import FileResponse
|
9 |
from PIL import Image
|
10 |
import uvicorn
|
11 |
|
|
|
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 |
+
@app.post("/analyze_path/")
|
23 |
+
async def analyze_path(file: UploadFile = File(...)):
|
24 |
+
"""Xử lý ảnh Depth Map và lưu ảnh để hiển thị"""
|
25 |
start_time = time.time()
|
26 |
|
27 |
+
# 🟢 Đọc file ảnh từ ESP32
|
28 |
+
image_bytes = await file.read()
|
29 |
+
image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
|
30 |
+
|
31 |
+
# 🔵 Resize ảnh để xử lý nhanh hơn
|
32 |
+
image = image.resize((256, 256))
|
33 |
image_np = np.array(image)
|
34 |
+
flipped_image = cv2.flip(image_np, -1)
|
35 |
|
36 |
# 🟢 Chuẩn bị ảnh cho mô hình
|
37 |
+
inputs = feature_extractor(images=flipped_image, return_tensors="pt").to(device)
|
38 |
|
39 |
+
# 🟢 Dự đoán Depth Map với DPT-Hybrid
|
40 |
with torch.no_grad():
|
41 |
outputs = model(**inputs)
|
42 |
|
|
|
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 trình duyệt
|
52 |
+
depth_pil.save("depth_map.png")
|
53 |
+
|
54 |
end_time = time.time()
|
55 |
print(f"⏳ DPT xử lý trong {end_time - start_time:.4f} giây")
|
56 |
|
57 |
+
return {"message": "Depth Map processed successfully. View at /depth_map/"}
|
58 |
|
59 |
+
@app.get("/depth_map/")
|
60 |
+
async def get_depth_map():
|
61 |
+
"""Trả về ảnh Depth Map để hiển thị trên trình duyệt"""
|
62 |
+
return FileResponse("depth_map.png", media_type="image/png")
|
|
|
|
|
|
|
|
|
63 |
|
64 |
+
# 🟢 Chạy server FastAPI trên Hugging Face Spaces
|
65 |
if __name__ == "__main__":
|
66 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|