|
import io |
|
import numpy as np |
|
import cv2 |
|
from fastapi import FastAPI, File, UploadFile |
|
from PIL import Image |
|
import torch |
|
import torch.nn.functional as F |
|
|
|
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") |
|
depth_map = np.array(image) |
|
|
|
|
|
command = detect_path(depth_map) |
|
|
|
return {"command": command} |
|
|
|
def detect_path(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" |
|
|
|
|