midas / app.py
adpro's picture
Update app.py
bef39ed verified
raw
history blame
1.23 kB
from fastapi import FastAPI, File, UploadFile
import io
import numpy as np
from PIL import Image
import uvicorn
app = FastAPI()
@app.post("/analyze_path/")
async def analyze_path(file: UploadFile = File(...)):
image_bytes = await file.read()
image = Image.open(io.BytesIO(image_bytes)).convert("L") # Chuyển ảnh sang grayscale
depth_map = np.array(image)
# Phân tích ảnh Depth Map
command = detect_path(depth_map)
return {"command": command}
def detect_path(depth_map):
"""Xác định đường có thể đi từ ảnh Depth Map"""
h, w = depth_map.shape
center_x = w // 2
scan_y = h - 20 # Quét dòng gần đáy ảnh
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"
# 🟢 Đảm bảo app khởi động đúng khi chạy trên Hugging Face
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=7860)