adpro commited on
Commit
bd6b543
·
verified ·
1 Parent(s): 81f06ad

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -8
app.py CHANGED
@@ -14,8 +14,8 @@ app = FastAPI()
14
  # 🟢 Chọn thiết bị xử lý (GPU nếu có)
15
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
16
 
17
- # 🟢 Tải model DPT-Hybrid để tăng tốc
18
- feature_extractor = AutoImageProcessor.from_pretrained("Intel/dpt-swinv2-tiny-256")
19
  model = AutoModelForDepthEstimation.from_pretrained("Intel/dpt-swinv2-tiny-256").to(device)
20
  model.eval()
21
 
@@ -29,14 +29,14 @@ async def analyze_path(file: UploadFile = File(...)):
29
  image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
30
 
31
  # 🔵 Resize ảnh để xử lý nhanh hơn
32
- image = image.resize((512, 512))
33
  image_np = np.array(image)
34
- flipped_image = cv2.flip(image_np, -1)
35
 
36
  # 🟢 Chuẩn bị ảnh cho mô hình
37
- inputs = feature_extractor(images=flipped_image, return_tensors="pt").to(device)
38
 
39
- # 🟢 Dự đoán Depth Map với DPT-Hybrid
40
  with torch.no_grad():
41
  outputs = model(**inputs)
42
 
@@ -52,7 +52,7 @@ async def analyze_path(file: UploadFile = File(...)):
52
  depth_pil.save("depth_map.png")
53
 
54
  end_time = time.time()
55
- print(f"⏳ DPT xử lý trong {end_time - start_time:.4f} giây")
56
 
57
  return {"message": "Depth Map processed successfully. View at /view/"}
58
 
@@ -68,7 +68,7 @@ async def view_depth_map():
68
  <html>
69
  <head>
70
  <title>Depth Map Viewer</title>
71
- <meta http-equiv="refresh" content="1">
72
  </head>
73
  <body>
74
  <h2>Depth Map (Tự động cập nhật mỗi 5 giây)</h2>
 
14
  # 🟢 Chọn thiết bị xử lý (GPU nếu có)
15
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
16
 
17
+ # 🟢 Tải model DPT-Swinv2 Tiny để tăng tốc
18
+ processor = AutoImageProcessor.from_pretrained("Intel/dpt-swinv2-tiny-256")
19
  model = AutoModelForDepthEstimation.from_pretrained("Intel/dpt-swinv2-tiny-256").to(device)
20
  model.eval()
21
 
 
29
  image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
30
 
31
  # 🔵 Resize ảnh để xử lý nhanh hơn
32
+ image = image.resize((256, 256))
33
  image_np = np.array(image)
34
+ flipped_image = cv2.flip(image_np, -1) # Lật ảnh
35
 
36
  # 🟢 Chuẩn bị ảnh cho mô hình
37
+ inputs = processor(images=flipped_image, return_tensors="pt").to(device)
38
 
39
+ # 🟢 Dự đoán Depth Map với model mới
40
  with torch.no_grad():
41
  outputs = model(**inputs)
42
 
 
52
  depth_pil.save("depth_map.png")
53
 
54
  end_time = time.time()
55
+ print(f"⏳ Model xử lý trong {end_time - start_time:.4f} giây")
56
 
57
  return {"message": "Depth Map processed successfully. View at /view/"}
58
 
 
68
  <html>
69
  <head>
70
  <title>Depth Map Viewer</title>
71
+ <meta http-equiv="refresh" content="5">
72
  </head>
73
  <body>
74
  <h2>Depth Map (Tự động cập nhật mỗi 5 giây)</h2>