adpro commited on
Commit
e8e141f
·
verified ·
1 Parent(s): b098c95

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -7
app.py CHANGED
@@ -60,20 +60,30 @@ def detect_path(depth_map):
60
  center_x = w // 2
61
  scan_y = int(h * 0.8) # Quét dòng 80% từ trên xuống
62
 
63
- left_region = np.mean(depth_map[scan_y, :center_x])
64
- right_region = np.mean(depth_map[scan_y, center_x:])
 
65
  center_region = np.mean(depth_map[scan_y, center_x - 40:center_x + 40])
66
 
67
- # 🟢 Cải thiện logic xử
68
- threshold = 100 # Ngưỡng phân biệt vật cản
 
 
 
 
 
 
69
  if center_region > threshold:
70
  return "forward"
71
- elif left_region > right_region:
 
 
72
  return "left"
73
  elif right_region > left_region:
74
  return "right"
75
- else:
76
- return "backward"
 
77
 
78
  # 🟢 Chạy server FastAPI
79
  if __name__ == "__main__":
 
60
  center_x = w // 2
61
  scan_y = int(h * 0.8) # Quét dòng 80% từ trên xuống
62
 
63
+ # 🟢 Chia ảnh thành 3 vùng: trái, giữa, phải
64
+ left_region = np.mean(depth_map[scan_y, :center_x - 40])
65
+ right_region = np.mean(depth_map[scan_y, center_x + 40:])
66
  center_region = np.mean(depth_map[scan_y, center_x - 40:center_x + 40])
67
 
68
+ # 🟢 Ngưỡng phát hiện vật cản (càng thấp, càng nhạy)
69
+ threshold = 80
70
+
71
+ # 🟢 Không có vật cản ở cả 3 vùng → đi thẳng
72
+ if left_region > threshold and center_region > threshold and right_region > threshold:
73
+ return "forward"
74
+
75
+ # 🟢 Nếu chỉ có giữa trống → đi thẳng
76
  if center_region > threshold:
77
  return "forward"
78
+
79
+ # 🟢 Nếu chỉ có trái hoặc phải trống → chọn hướng có vùng trống lớn nhất
80
+ if left_region > right_region:
81
  return "left"
82
  elif right_region > left_region:
83
  return "right"
84
+
85
+ # 🟢 Nếu tất cả đều có vật cản → lùi lại
86
+ return "backward"
87
 
88
  # 🟢 Chạy server FastAPI
89
  if __name__ == "__main__":