|
import io |
|
import os |
|
import sys |
|
import time |
|
import numpy as np |
|
import cv2 |
|
import torch |
|
import torchvision |
|
from fastapi import FastAPI, File, UploadFile |
|
from PIL import Image |
|
import uvicorn |
|
|
|
app = FastAPI() |
|
|
|
|
|
if not os.path.exists("fastdepth"): |
|
os.system("git clone https://github.com/dwofk/fast-depth.git fastdepth") |
|
|
|
|
|
sys.path.append(os.path.abspath("fastdepth")) |
|
|
|
|
|
weights_path = "fastdepth/models/fastdepth_nyu.pt" |
|
if not os.path.exists(weights_path): |
|
print("🔻 Trọng số chưa có, đang tải từ GitHub...") |
|
os.system(f"wget -O {weights_path} https://github.com/dwofk/fast-depth/raw/master/models/fastdepth_nyu.pt") |
|
else: |
|
print("✅ Trọng số đã có sẵn.") |
|
|
|
|
|
from fastdepth.models import MobileNetSkipAdd |
|
|
|
|
|
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") |
|
model = MobileNetSkipAdd(output_size=(224, 224)) |
|
try: |
|
model.load_state_dict(torch.load(weights_path, map_location=device)) |
|
print("✅ Mô hình FastDepth đã được load thành công!") |
|
except FileNotFoundError: |
|
print("❌ Không tìm thấy file trọng số! Kiểm tra lại đường dẫn.") |
|
|
|
model.eval().to(device) |
|
|
|
@app.post("/analyze_path/") |
|
async def analyze_path(file: UploadFile = File(...)): |
|
|
|
image_bytes = await file.read() |
|
image = Image.open(io.BytesIO(image_bytes)).convert("RGB") |
|
|
|
|
|
image_np = np.array(image) |
|
|
|
|
|
flipped_image = cv2.flip(image_np, -1) |
|
|
|
|
|
flipped_image_pil = Image.fromarray(flipped_image) |
|
|
|
|
|
transform = torchvision.transforms.Compose([ |
|
torchvision.transforms.Resize((224, 224)), |
|
torchvision.transforms.ToTensor(), |
|
]) |
|
img_tensor = transform(flipped_image_pil).unsqueeze(0).to(device) |
|
|
|
|
|
start_time = time.time() |
|
|
|
|
|
with torch.no_grad(): |
|
depth_map = model(img_tensor).squeeze().cpu().numpy() |
|
|
|
end_time = time.time() |
|
print(f"⏳ FastDepth xử lý trong {end_time - start_time:.4f} giây") |
|
|
|
|
|
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 = h - 20 |
|
|
|
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 - 20:center_x + 20]) |
|
|
|
if center_region > 200: |
|
return "forward" |
|
elif left_region > right_region: |
|
return "left" |
|
elif right_region > left_region: |
|
return "right" |
|
else: |
|
return "backward" |
|
|
|
|
|
if __name__ == "__main__": |
|
uvicorn.run(app, host="0.0.0.0", port=7860) |
|
|