Update app.py
Browse files
app.py
CHANGED
@@ -1,54 +1,52 @@
|
|
1 |
import io
|
2 |
import time
|
3 |
-
import torch
|
4 |
import numpy as np
|
5 |
import cv2
|
|
|
|
|
6 |
from fastapi import FastAPI, File, UploadFile
|
7 |
from PIL import Image
|
8 |
import uvicorn
|
9 |
-
from torchvision import transforms
|
10 |
|
11 |
-
# 🟢 Tạo FastAPI
|
12 |
app = FastAPI()
|
13 |
|
14 |
-
# 🟢
|
15 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
16 |
|
17 |
-
# 🟢 Tải model
|
18 |
-
|
19 |
-
midas.to(device)
|
20 |
-
|
21 |
-
|
22 |
-
# 🟢 Chuẩn bị bộ tiền xử lý ảnh
|
23 |
-
transform = transforms.Compose([
|
24 |
-
transforms.Resize((384, 384)),
|
25 |
-
transforms.ToTensor(),
|
26 |
-
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
|
27 |
-
])
|
28 |
|
29 |
@app.post("/analyze_path/")
|
30 |
async def analyze_path(file: UploadFile = File(...)):
|
|
|
|
|
31 |
# 🟢 Đọc file ảnh từ ESP32
|
32 |
image_bytes = await file.read()
|
33 |
image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
|
|
|
|
|
|
|
34 |
image_np = np.array(image)
|
35 |
flipped_image = cv2.flip(image_np, -1)
|
36 |
-
#
|
37 |
-
|
38 |
|
39 |
-
|
40 |
-
|
|
|
41 |
with torch.no_grad():
|
42 |
-
|
43 |
-
end_time = time.time()
|
44 |
-
print(f"⏳ MiDaS xử lý trong {end_time - start_time:.4f} giây")
|
45 |
|
46 |
-
# 🟢
|
47 |
-
|
48 |
-
depth_map = (
|
49 |
-
|
|
|
|
|
50 |
|
51 |
-
# 🟢
|
52 |
start_detect_time = time.time()
|
53 |
command = detect_path(depth_map)
|
54 |
end_detect_time = time.time()
|
|
|
1 |
import io
|
2 |
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 |
app = FastAPI()
|
12 |
|
13 |
+
# 🟢 Chọn thiết bị xử lý (GPU nếu có)
|
14 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
15 |
|
16 |
+
# 🟢 Tải model DPT-Hybrid thay cho ZoeDepth để tăng tốc
|
17 |
+
feature_extractor = DPTFeatureExtractor.from_pretrained("Intel/dpt-hybrid-midas")
|
18 |
+
model = DPTForDepthEstimation.from_pretrained("Intel/dpt-hybrid-midas").to(device)
|
19 |
+
model.eval()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
@app.post("/analyze_path/")
|
22 |
async def analyze_path(file: UploadFile = File(...)):
|
23 |
+
# 🟢 Bắt đầu đo thời gian dự đoán Depth Map
|
24 |
+
start_time = time.time()
|
25 |
# 🟢 Đọc file ảnh từ ESP32
|
26 |
image_bytes = await file.read()
|
27 |
image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
|
28 |
+
|
29 |
+
# 🔵 Resize ảnh để xử lý nhanh hơn
|
30 |
+
image = image.resize((256, 256)) # Giảm kích thước giúp tăng tốc độ xử lý
|
31 |
image_np = np.array(image)
|
32 |
flipped_image = cv2.flip(image_np, -1)
|
33 |
+
# 🟢 Chuẩn bị ảnh cho mô hình
|
34 |
+
inputs = feature_extractor(images=flipped_image, return_tensors="pt").to(device)
|
35 |
|
36 |
+
|
37 |
+
|
38 |
+
# 🟢 Dự đoán Depth Map với DPT-Hybrid
|
39 |
with torch.no_grad():
|
40 |
+
outputs = model(**inputs)
|
|
|
|
|
41 |
|
42 |
+
# 🟢 Xử lý ảnh sau khi dự đoán
|
43 |
+
predicted_depth = outputs.predicted_depth.squeeze().cpu().numpy()
|
44 |
+
depth_map = (predicted_depth * 255 / predicted_depth.max()).astype("uint8")
|
45 |
+
|
46 |
+
end_time = time.time()
|
47 |
+
print(f"⏳ DPT xử lý trong {end_time - start_time:.4f} giây")
|
48 |
|
49 |
+
# 🟢 Đo thời gian xử lý đường đi
|
50 |
start_detect_time = time.time()
|
51 |
command = detect_path(depth_map)
|
52 |
end_detect_time = time.time()
|