Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
import io
|
| 2 |
import os
|
| 3 |
import sys
|
|
|
|
| 4 |
import numpy as np
|
| 5 |
import cv2
|
| 6 |
import torch
|
|
@@ -18,18 +19,26 @@ if not os.path.exists("fastdepth"):
|
|
| 18 |
# 🟢 Thêm `fastdepth` vào `sys.path`
|
| 19 |
sys.path.append(os.path.abspath("fastdepth"))
|
| 20 |
|
| 21 |
-
# 🟢
|
| 22 |
weights_path = "fastdepth/models/fastdepth_nyu.pt"
|
| 23 |
if not os.path.exists(weights_path):
|
|
|
|
| 24 |
os.system(f"wget -O {weights_path} https://github.com/dwofk/fast-depth/raw/master/models/fastdepth_nyu.pt")
|
|
|
|
|
|
|
| 25 |
|
| 26 |
# 🟢 Import FastDepth
|
| 27 |
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
model.eval().to(device)
|
| 34 |
|
| 35 |
@app.post("/analyze_path/")
|
|
@@ -42,7 +51,7 @@ async def analyze_path(file: UploadFile = File(...)):
|
|
| 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)
|
| 46 |
|
| 47 |
# 🟢 Chuyển đổi lại thành ảnh PIL để đưa vào FastDepth
|
| 48 |
flipped_image_pil = Image.fromarray(flipped_image)
|
|
@@ -54,12 +63,21 @@ async def analyze_path(file: UploadFile = File(...)):
|
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
command = detect_path(depth_map)
|
|
|
|
|
|
|
| 63 |
|
| 64 |
return {"command": command}
|
| 65 |
|
|
|
|
| 1 |
import io
|
| 2 |
import os
|
| 3 |
import sys
|
| 4 |
+
import time
|
| 5 |
import numpy as np
|
| 6 |
import cv2
|
| 7 |
import torch
|
|
|
|
| 19 |
# 🟢 Thêm `fastdepth` vào `sys.path`
|
| 20 |
sys.path.append(os.path.abspath("fastdepth"))
|
| 21 |
|
| 22 |
+
# 🟢 Tải đúng file trọng số nếu chưa có
|
| 23 |
weights_path = "fastdepth/models/fastdepth_nyu.pt"
|
| 24 |
if not os.path.exists(weights_path):
|
| 25 |
+
print("🔻 Trọng số chưa có, đang tải từ GitHub...")
|
| 26 |
os.system(f"wget -O {weights_path} https://github.com/dwofk/fast-depth/raw/master/models/fastdepth_nyu.pt")
|
| 27 |
+
else:
|
| 28 |
+
print("✅ Trọng số đã có sẵn.")
|
| 29 |
|
| 30 |
# 🟢 Import FastDepth
|
| 31 |
from fastdepth.models import MobileNetSkipAdd
|
| 32 |
|
| 33 |
+
# 🟢 Load mô hình FastDepth đúng trọng số
|
| 34 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 35 |
model = MobileNetSkipAdd(output_size=(224, 224))
|
| 36 |
+
try:
|
| 37 |
+
model.load_state_dict(torch.load(weights_path, map_location=device))
|
| 38 |
+
print("✅ Mô hình FastDepth đã được load thành công!")
|
| 39 |
+
except FileNotFoundError:
|
| 40 |
+
print("❌ Không tìm thấy file trọng số! Kiểm tra lại đường dẫn.")
|
| 41 |
+
|
| 42 |
model.eval().to(device)
|
| 43 |
|
| 44 |
@app.post("/analyze_path/")
|
|
|
|
| 51 |
image_np = np.array(image)
|
| 52 |
|
| 53 |
# 🟢 Lật ảnh trước khi tính toán Depth Map
|
| 54 |
+
flipped_image = cv2.flip(image_np, -1)
|
| 55 |
|
| 56 |
# 🟢 Chuyển đổi lại thành ảnh PIL để đưa vào FastDepth
|
| 57 |
flipped_image_pil = Image.fromarray(flipped_image)
|
|
|
|
| 63 |
])
|
| 64 |
img_tensor = transform(flipped_image_pil).unsqueeze(0).to(device)
|
| 65 |
|
| 66 |
+
# 🟢 Bắt đầu đo thời gian dự đoán Depth Map
|
| 67 |
+
start_time = time.time()
|
| 68 |
+
|
| 69 |
# 🟢 Dự đoán Depth Map với FastDepth
|
| 70 |
with torch.no_grad():
|
| 71 |
depth_map = model(img_tensor).squeeze().cpu().numpy()
|
| 72 |
|
| 73 |
+
end_time = time.time()
|
| 74 |
+
print(f"⏳ FastDepth xử lý trong {end_time - start_time:.4f} giây")
|
| 75 |
+
|
| 76 |
+
# 🟢 Đo thời gian xử lý đường đi
|
| 77 |
+
start_detect_time = time.time()
|
| 78 |
command = detect_path(depth_map)
|
| 79 |
+
end_detect_time = time.time()
|
| 80 |
+
print(f"⏳ detect_path() xử lý trong {end_detect_time - start_detect_time:.4f} giây")
|
| 81 |
|
| 82 |
return {"command": command}
|
| 83 |
|