adpro commited on
Commit
a310278
·
verified ·
1 Parent(s): 783b08c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -67
app.py CHANGED
@@ -1,76 +1,25 @@
1
- import io
2
- import time
3
- import numpy as np
4
- import cv2
5
  import torch
6
- from transformers import AutoImageProcessor, ZoeDepthForDepthEstimation
7
- from fastapi import FastAPI, File, UploadFile
8
  from PIL import Image
9
- import uvicorn
10
 
11
- app = FastAPI()
12
-
13
- # 🟢 Tải mô hình ZoeDepth từ Hugging Face
14
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
15
- image_processor = AutoImageProcessor.from_pretrained("isl-org/ZoeD_NK_T", use_fast=True)
16
- model = ZoeDepthForDepthEstimation.from_pretrained("isl-org/ZoeD_NK_T").to(device)
17
- model.eval()
18
-
19
- @app.post("/analyze_path/")
20
- async def analyze_path(file: UploadFile = File(...)):
21
- # 🟢 Đọc file ảnh từ ESP32
22
- image_bytes = await file.read()
23
- image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
24
-
25
- # 🟢 Chuẩn bị ảnh cho mô hình ZoeDepth
26
- inputs = image_processor(images=image, return_tensors="pt").to(device)
27
-
28
- # 🟢 Bắt đầu đo thời gian dự đoán Depth Map
29
- start_time = time.time()
30
-
31
- # 🟢 Dự đoán Depth Map với ZoeDepth
32
- with torch.no_grad():
33
- outputs = model(**inputs)
34
-
35
- # 🟢 Xử lý ảnh sau khi dự đoán
36
- post_processed_output = image_processor.post_process_depth_estimation(
37
- outputs,
38
- source_sizes=[(image.height, image.width)],
39
- )
40
- predicted_depth = post_processed_output[0]["predicted_depth"]
41
- depth_map = predicted_depth * 255 / predicted_depth.max()
42
- depth_map = depth_map.detach().cpu().numpy().astype("uint8")
43
-
44
- end_time = time.time()
45
- print(f"⏳ ZoeDepth xử lý trong {end_time - start_time:.4f} giây")
46
-
47
- # 🟢 Đo thời gian xử lý đường đi
48
- start_detect_time = time.time()
49
- command = detect_path(depth_map)
50
- end_detect_time = time.time()
51
- print(f"⏳ detect_path() xử lý trong {end_detect_time - start_detect_time:.4f} giây")
52
 
53
- return {"command": command}
 
 
54
 
55
- def detect_path(depth_map):
56
- """Phân tích đường đi từ ảnh Depth Map"""
57
- h, w = depth_map.shape
58
- center_x = w // 2
59
- scan_y = h - 20 # Quét dòng gần đáy ảnh
60
 
61
- left_region = np.mean(depth_map[scan_y, :center_x])
62
- right_region = np.mean(depth_map[scan_y, center_x:])
63
- center_region = np.mean(depth_map[scan_y, center_x - 20:center_x + 20])
64
 
65
- if center_region > 200:
66
- return "forward"
67
- elif left_region > right_region:
68
- return "left"
69
- elif right_region > left_region:
70
- return "right"
71
- else:
72
- return "backward"
73
 
74
- # 🟢 Chạy server FastAPI
75
- if __name__ == "__main__":
76
- uvicorn.run(app, host="0.0.0.0", port=7860)
 
 
 
 
 
1
  import torch
2
+ from transformers import DPTFeatureExtractor, DPTForDepthEstimation
 
3
  from PIL import Image
4
+ import requests
5
 
 
 
 
6
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
8
+ # Load model và chuyển sang GPU
9
+ feature_extractor = DPTFeatureExtractor.from_pretrained("Intel/dpt-large")
10
+ model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large").to(device)
11
 
12
+ # Tải ảnh
13
+ url = "http://images.cocodataset.org/val2017/000000039769.jpg"
14
+ image = Image.open(requests.get(url, stream=True).raw)
 
 
15
 
16
+ # Chuẩn bị input
17
+ inputs = feature_extractor(images=image, return_tensors="pt").to(device)
 
18
 
19
+ # Dự đoán depth map
20
+ with torch.no_grad():
21
+ outputs = model(**inputs)
 
 
 
 
 
22
 
23
+ # Lấy output chuyển về dạng ảnh
24
+ depth_map = outputs.predicted_depth
25
+ depth_map = depth_map.squeeze().cpu().numpy()