|
import io |
|
import time |
|
import numpy as np |
|
import cv2 |
|
import torch |
|
from transformers import AutoImageProcessor, ZoeDepthForDepthEstimation |
|
from fastapi import FastAPI, File, UploadFile |
|
from PIL import Image |
|
import uvicorn |
|
|
|
app = FastAPI() |
|
|
|
|
|
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") |
|
image_processor = AutoImageProcessor.from_pretrained("Intel/zoedepth-nyu-kitti") |
|
model = ZoeDepthForDepthEstimation.from_pretrained("Intel/zoedepth-nyu-kitti").to(device) |
|
model.eval() |
|
|
|
@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") |
|
|
|
|
|
inputs = image_processor(images=image, return_tensors="pt").to(device) |
|
|
|
|
|
start_time = time.time() |
|
|
|
|
|
with torch.no_grad(): |
|
outputs = model(**inputs) |
|
|
|
|
|
post_processed_output = image_processor.post_process_depth_estimation( |
|
outputs, |
|
source_sizes=[(image.height, image.width)], |
|
) |
|
predicted_depth = post_processed_output[0]["predicted_depth"] |
|
depth_map = predicted_depth * 255 / predicted_depth.max() |
|
depth_map = depth_map.detach().cpu().numpy().astype("uint8") |
|
|
|
end_time = time.time() |
|
print(f"⏳ ZoeDepth 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) |
|
|