File size: 2,668 Bytes
8d36ed5 fac5d14 8d36ed5 02cc722 8d36ed5 fac5d14 425f8a0 81def50 fac5d14 8d36ed5 fac5d14 81def50 b3652fb 425f8a0 fac5d14 8d36ed5 fac5d14 8d36ed5 fac5d14 8d36ed5 fac5d14 8d36ed5 fac5d14 8d36ed5 fac5d14 8d36ed5 b3652fb 8d36ed5 b3652fb 8d36ed5 81def50 8d36ed5 81def50 8d36ed5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
import io
import time
import torch
import numpy as np
import cv2
from fastapi import FastAPI, File, UploadFile
from PIL import Image
import uvicorn
from torchvision import transforms
from midas.model_loader import load_model # Thư viện MiDaS
# 🟢 Tạo FastAPI
app = FastAPI()
# 🟢 Kiểm tra GPU
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# 🟢 Tải model MiDaS (DPT-Swin2 Large 384)
model_path = "dpt_swin2_large_384.pt" # Đảm bảo đã tải file này từ GitHub
midas = load_model(model_path, device)
midas.eval()
# 🟢 Chuẩn bị bộ tiền xử lý ảnh
transform = transforms.Compose([
transforms.Resize((384, 384)),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])
@app.post("/analyze_path/")
async def analyze_path(file: UploadFile = File(...)):
# 🟢 Đọc file ảnh từ ESP32
image_bytes = await file.read()
image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
# 🔵 Resize và chuẩn hóa ảnh
input_tensor = transform(image).unsqueeze(0).to(device)
# 🟢 Dự đoán Depth Map với MiDaS
start_time = time.time()
with torch.no_grad():
depth_map = midas(input_tensor)
end_time = time.time()
print(f"⏳ MiDaS xử lý trong {end_time - start_time:.4f} giây")
# 🟢 Chuẩn hóa ảnh Depth Map
depth_map = depth_map.squeeze().cpu().numpy()
depth_map = (depth_map - depth_map.min()) / (depth_map.max() - depth_map.min()) * 255
depth_map = depth_map.astype("uint8")
# 🟢 Xử lý phát hiện đường đi
start_detect_time = time.time()
command = detect_path(depth_map)
end_detect_time = time.time()
print(f"⏳ detect_path() xử lý trong {end_detect_time - start_detect_time:.4f} giây")
return {"command": command}
def detect_path(depth_map):
"""Phân tích đường đi từ ảnh Depth Map"""
h, w = depth_map.shape
center_x = w // 2
scan_y = int(h * 0.8) # Quét dòng 80% từ trên xuống
left_region = np.mean(depth_map[scan_y, :center_x])
right_region = np.mean(depth_map[scan_y, center_x:])
center_region = np.mean(depth_map[scan_y, center_x - 40:center_x + 40])
# 🟢 Cải thiện logic xử lý
threshold = 100 # Ngưỡng phân biệt vật cản
if center_region > threshold:
return "forward"
elif left_region > right_region:
return "left"
elif right_region > left_region:
return "right"
else:
return "backward"
# 🟢 Chạy server FastAPI
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=7860)
|