Update app.py
Browse files
app.py
CHANGED
@@ -1,51 +1,40 @@
|
|
1 |
-
|
2 |
-
import cv2
|
3 |
import numpy as np
|
4 |
-
import
|
5 |
-
import
|
6 |
from PIL import Image
|
7 |
-
import
|
|
|
8 |
|
9 |
app = FastAPI()
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
#
|
40 |
-
|
41 |
-
print("✅ Depth Map đã được tạo!")
|
42 |
-
|
43 |
-
return Response(content=buffer.tobytes(), media_type="image/jpeg") # 🟢 Trả ảnh trực tiếp
|
44 |
-
|
45 |
-
except Exception as e:
|
46 |
-
print("❌ Lỗi xử lý ảnh:", str(e))
|
47 |
-
return {"error": str(e)}
|
48 |
-
|
49 |
-
if __name__ == "__main__":
|
50 |
-
import uvicorn
|
51 |
-
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
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 torch
|
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") # Convert to grayscale
|
15 |
+
depth_map = np.array(image)
|
16 |
+
|
17 |
+
# 🟢 Tìm đường đi bằng phẳng (vùng có độ sâu ổn định)
|
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 # Điểm giữa ảnh
|
25 |
+
scan_y = h - 20 # Quét dòng gần cuối ảnh
|
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 |
+
# 🟢 Logic điều hướng dựa vào độ sâu
|
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: # Phía trái trống hơn
|
35 |
+
return "left"
|
36 |
+
elif right_region > left_region: # Phía phải trống hơn
|
37 |
+
return "right"
|
38 |
+
else:
|
39 |
+
return "backward" # Nếu tất cả đều bị cản, lùi lại
|
40 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|