Update app.py
Browse files
app.py
CHANGED
|
@@ -28,7 +28,7 @@ from fastdepth.models import MobileNetSkipAdd
|
|
| 28 |
|
| 29 |
# 🟢 Load mô hình FastDepth
|
| 30 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 31 |
-
model = MobileNetSkipAdd(output_size=(224, 224))
|
| 32 |
model.load_state_dict(torch.load(weights_path, map_location=device))
|
| 33 |
model.eval().to(device)
|
| 34 |
|
|
@@ -37,23 +37,29 @@ async def analyze_path(file: UploadFile = File(...)):
|
|
| 37 |
# 🟢 Đọc file ảnh từ ESP32
|
| 38 |
image_bytes = await file.read()
|
| 39 |
image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
# 🟢 Chuyển đổi ảnh thành tensor (chuẩn hóa cho FastDepth)
|
| 42 |
transform = torchvision.transforms.Compose([
|
| 43 |
torchvision.transforms.Resize((224, 224)),
|
| 44 |
torchvision.transforms.ToTensor(),
|
| 45 |
])
|
| 46 |
-
img_tensor = transform(
|
| 47 |
|
| 48 |
# 🟢 Dự đoán Depth Map với FastDepth
|
| 49 |
with torch.no_grad():
|
| 50 |
depth_map = model(img_tensor).squeeze().cpu().numpy()
|
| 51 |
|
| 52 |
-
# 🟢 Lật ngược ảnh (nếu cần)
|
| 53 |
-
flipped_depth_map = cv2.flip(depth_map, -1)
|
| 54 |
-
|
| 55 |
# 🟢 Phân tích đường đi
|
| 56 |
-
command = detect_path(
|
| 57 |
|
| 58 |
return {"command": command}
|
| 59 |
|
|
|
|
| 28 |
|
| 29 |
# 🟢 Load mô hình FastDepth
|
| 30 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 31 |
+
model = MobileNetSkipAdd(output_size=(224, 224))
|
| 32 |
model.load_state_dict(torch.load(weights_path, map_location=device))
|
| 33 |
model.eval().to(device)
|
| 34 |
|
|
|
|
| 37 |
# 🟢 Đọc file ảnh từ ESP32
|
| 38 |
image_bytes = await file.read()
|
| 39 |
image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
|
| 40 |
+
|
| 41 |
+
# 🟢 Chuyển ảnh sang NumPy để lật đúng chiều
|
| 42 |
+
image_np = np.array(image)
|
| 43 |
+
|
| 44 |
+
# 🟢 Lật ảnh trước khi tính toán Depth Map
|
| 45 |
+
flipped_image = cv2.flip(image_np, -1) # Lật trái ↔ phải, trên ↔ xuống
|
| 46 |
+
|
| 47 |
+
# 🟢 Chuyển đổi lại thành ảnh PIL để đưa vào FastDepth
|
| 48 |
+
flipped_image_pil = Image.fromarray(flipped_image)
|
| 49 |
|
| 50 |
# 🟢 Chuyển đổi ảnh thành tensor (chuẩn hóa cho FastDepth)
|
| 51 |
transform = torchvision.transforms.Compose([
|
| 52 |
torchvision.transforms.Resize((224, 224)),
|
| 53 |
torchvision.transforms.ToTensor(),
|
| 54 |
])
|
| 55 |
+
img_tensor = transform(flipped_image_pil).unsqueeze(0).to(device)
|
| 56 |
|
| 57 |
# 🟢 Dự đoán Depth Map với FastDepth
|
| 58 |
with torch.no_grad():
|
| 59 |
depth_map = model(img_tensor).squeeze().cpu().numpy()
|
| 60 |
|
|
|
|
|
|
|
|
|
|
| 61 |
# 🟢 Phân tích đường đi
|
| 62 |
+
command = detect_path(depth_map)
|
| 63 |
|
| 64 |
return {"command": command}
|
| 65 |
|