Update app.py
Browse files
app.py
CHANGED
|
@@ -1,40 +1,41 @@
|
|
|
|
|
| 1 |
import io
|
| 2 |
import numpy as np
|
| 3 |
-
import cv2
|
| 4 |
-
from fastapi import FastAPI, File, UploadFile
|
| 5 |
from PIL import Image
|
| 6 |
-
import
|
| 7 |
-
import torch.nn.functional as F
|
| 8 |
|
| 9 |
app = FastAPI()
|
| 10 |
|
| 11 |
@app.post("/analyze_path/")
|
| 12 |
async def analyze_path(file: UploadFile = File(...)):
|
| 13 |
image_bytes = await file.read()
|
| 14 |
-
image = Image.open(io.BytesIO(image_bytes)).convert("L") #
|
| 15 |
depth_map = np.array(image)
|
| 16 |
|
| 17 |
-
#
|
| 18 |
command = detect_path(depth_map)
|
| 19 |
|
| 20 |
return {"command": command}
|
| 21 |
|
| 22 |
def detect_path(depth_map):
|
|
|
|
| 23 |
h, w = depth_map.shape
|
| 24 |
-
center_x = w // 2
|
| 25 |
-
scan_y = h - 20 # Quét dòng gần
|
| 26 |
|
| 27 |
left_region = np.mean(depth_map[scan_y, :center_x])
|
| 28 |
right_region = np.mean(depth_map[scan_y, center_x:])
|
| 29 |
center_region = np.mean(depth_map[scan_y, center_x - 20:center_x + 20])
|
| 30 |
|
| 31 |
-
|
| 32 |
-
if center_region > 200: # Đường trước mặt rộng và không có vật cản
|
| 33 |
return "forward"
|
| 34 |
-
elif left_region > right_region:
|
| 35 |
return "left"
|
| 36 |
-
elif right_region > left_region:
|
| 37 |
return "right"
|
| 38 |
else:
|
| 39 |
-
return "backward"
|
| 40 |
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, File, UploadFile
|
| 2 |
import io
|
| 3 |
import numpy as np
|
|
|
|
|
|
|
| 4 |
from PIL import Image
|
| 5 |
+
import uvicorn
|
|
|
|
| 6 |
|
| 7 |
app = FastAPI()
|
| 8 |
|
| 9 |
@app.post("/analyze_path/")
|
| 10 |
async def analyze_path(file: UploadFile = File(...)):
|
| 11 |
image_bytes = await file.read()
|
| 12 |
+
image = Image.open(io.BytesIO(image_bytes)).convert("L") # Chuyển ảnh sang grayscale
|
| 13 |
depth_map = np.array(image)
|
| 14 |
|
| 15 |
+
# Phân tích ảnh Depth Map
|
| 16 |
command = detect_path(depth_map)
|
| 17 |
|
| 18 |
return {"command": command}
|
| 19 |
|
| 20 |
def detect_path(depth_map):
|
| 21 |
+
"""Xác định đường có thể đi từ ảnh Depth Map"""
|
| 22 |
h, w = depth_map.shape
|
| 23 |
+
center_x = w // 2
|
| 24 |
+
scan_y = h - 20 # Quét dòng gần đáy ảnh
|
| 25 |
|
| 26 |
left_region = np.mean(depth_map[scan_y, :center_x])
|
| 27 |
right_region = np.mean(depth_map[scan_y, center_x:])
|
| 28 |
center_region = np.mean(depth_map[scan_y, center_x - 20:center_x + 20])
|
| 29 |
|
| 30 |
+
if center_region > 200:
|
|
|
|
| 31 |
return "forward"
|
| 32 |
+
elif left_region > right_region:
|
| 33 |
return "left"
|
| 34 |
+
elif right_region > left_region:
|
| 35 |
return "right"
|
| 36 |
else:
|
| 37 |
+
return "backward"
|
| 38 |
|
| 39 |
+
# 🟢 Đảm bảo app khởi động đúng khi chạy trên Hugging Face
|
| 40 |
+
if __name__ == "__main__":
|
| 41 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|