adpro commited on
Commit
fac5d14
·
verified ·
1 Parent(s): 6eb70f3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -23
app.py CHANGED
@@ -1,22 +1,30 @@
1
  import io
2
  import time
 
3
  import numpy as np
4
  import cv2
5
- import torch
6
- from transformers import DPTFeatureExtractor, DPTForDepthEstimation
7
  from fastapi import FastAPI, File, UploadFile
8
  from PIL import Image
9
  import uvicorn
 
10
 
 
11
  app = FastAPI()
12
 
13
- # 🟢 Chọn thiết bị xử lý (GPU nếu có)
14
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
15
 
16
- # 🟢 Tải model DPT-Hybrid thay cho ZoeDepth để tăng tốc
17
- feature_extractor = DPTFeatureExtractor.from_pretrained("Intel/dpt-hybrid-midas")
18
- model = DPTForDepthEstimation.from_pretrained("Intel/dpt-hybrid-midas").to(device)
19
- model.eval()
 
 
 
 
 
 
 
20
 
21
  @app.post("/analyze_path/")
22
  async def analyze_path(file: UploadFile = File(...)):
@@ -24,27 +32,22 @@ async def analyze_path(file: UploadFile = File(...)):
24
  image_bytes = await file.read()
25
  image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
26
 
27
- # 🔵 Resize ảnh để xử lý nhanh hơn
28
- image = image.resize((384, 384)) # Giảm kích thước giúp tăng tốc độ xử lý
29
-
30
- # 🟢 Chuẩn bị ảnh cho mô hình
31
- inputs = feature_extractor(images=image, return_tensors="pt").to(device)
32
 
33
- # 🟢 Bắt đầu đo thời gian dự đoán Depth Map
34
  start_time = time.time()
35
-
36
- # 🟢 Dự đoán Depth Map với DPT-Hybrid
37
  with torch.no_grad():
38
- outputs = model(**inputs)
39
-
40
- # 🟢 Xử lý ảnh sau khi dự đoán
41
- predicted_depth = outputs.predicted_depth.squeeze().cpu().numpy()
42
- depth_map = (predicted_depth * 255 / predicted_depth.max()).astype("uint8")
43
-
44
  end_time = time.time()
45
- print(f"⏳ DPT xử lý trong {end_time - start_time:.4f} giây")
 
 
 
 
 
46
 
47
- # 🟢 Đo thời gian xử đường đi
48
  start_detect_time = time.time()
49
  command = detect_path(depth_map)
50
  end_detect_time = time.time()
 
1
  import io
2
  import time
3
+ import torch
4
  import numpy as np
5
  import cv2
 
 
6
  from fastapi import FastAPI, File, UploadFile
7
  from PIL import Image
8
  import uvicorn
9
+ from torchvision import transforms
10
 
11
+ # 🟢 Tạo FastAPI
12
  app = FastAPI()
13
 
14
+ # 🟢 Kiểm tra GPU
15
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
16
 
17
+ # 🟢 Tải model MiDaS
18
+ midas = torch.hub.load("isl-org/MiDaS", "DPT_Swin2_L_384")
19
+ midas.to(device)
20
+ midas.eval()
21
+
22
+ # 🟢 Chuẩn bị bộ tiền xử lý ảnh
23
+ transform = transforms.Compose([
24
+ transforms.Resize((384, 384)),
25
+ transforms.ToTensor(),
26
+ transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
27
+ ])
28
 
29
  @app.post("/analyze_path/")
30
  async def analyze_path(file: UploadFile = File(...)):
 
32
  image_bytes = await file.read()
33
  image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
34
 
35
+ # 🔵 Resize chuẩn hóa ảnh
36
+ input_tensor = transform(image).unsqueeze(0).to(device)
 
 
 
37
 
38
+ # 🟢 Dự đoán Depth Map với MiDaS
39
  start_time = time.time()
 
 
40
  with torch.no_grad():
41
+ depth_map = midas(input_tensor)
 
 
 
 
 
42
  end_time = time.time()
43
+ print(f"⏳ MiDaS xử lý trong {end_time - start_time:.4f} giây")
44
+
45
+ # 🟢 Chuẩn hóa ảnh Depth Map
46
+ depth_map = depth_map.squeeze().cpu().numpy()
47
+ depth_map = (depth_map - depth_map.min()) / (depth_map.max() - depth_map.min()) * 255
48
+ depth_map = depth_map.astype("uint8")
49
 
50
+ # 🟢 Xử phát hiện đường đi
51
  start_detect_time = time.time()
52
  command = detect_path(depth_map)
53
  end_detect_time = time.time()