adpro commited on
Commit
f6e7520
·
verified ·
1 Parent(s): bef39ed

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -19
app.py CHANGED
@@ -3,6 +3,7 @@ import io
3
  import numpy as np
4
  from PIL import Image
5
  import uvicorn
 
6
 
7
  app = FastAPI()
8
 
@@ -11,31 +12,23 @@ 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 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)
 
3
  import numpy as np
4
  from PIL import Image
5
  import uvicorn
6
+ import cv2
7
 
8
  app = FastAPI()
9
 
 
12
  image_bytes = await file.read()
13
  image = Image.open(io.BytesIO(image_bytes)).convert("L") # Chuyển ảnh sang grayscale
14
  depth_map = np.array(image)
15
+ flipped_depth_map = cv2.flip(depth_map, -1)
16
  # Phân tích ảnh Depth Map
17
+ command = detect_path(flipped_depth_map)
18
 
19
  return {"command": command}
20
 
21
  def detect_path(depth_map):
22
+ _, thresh = cv2.threshold(depth_map, 200, 255, cv2.THRESH_BINARY)
23
+ contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
 
 
 
 
 
 
24
 
25
+ if len(contours) == 0:
26
  return "forward"
27
+
28
+ left_region = np.mean(depth_map[:, :depth_map.shape[1]//3])
29
+ right_region = np.mean(depth_map[:, 2*depth_map.shape[1]//3:])
30
+
31
+ if left_region > right_region:
32
  return "left"
 
 
33
  else:
34
+ return "right"