adpro commited on
Commit
38420c4
·
verified ·
1 Parent(s): 040e0cc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +4 -7
app.py CHANGED
@@ -1,5 +1,4 @@
1
  from fastapi import FastAPI, UploadFile, File
2
- import uvicorn
3
  import cv2
4
  import numpy as np
5
  import torch
@@ -9,7 +8,6 @@ import io
9
 
10
  app = FastAPI()
11
 
12
- # Load AI model MiDaS
13
  midas = torch.hub.load("intel-isl/MiDaS", "MiDaS_small")
14
  midas.eval()
15
  transform = T.Compose([T.Resize((256, 256)), T.ToTensor(), T.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])])
@@ -18,18 +16,17 @@ transform = T.Compose([T.Resize((256, 256)), T.ToTensor(), T.Normalize(mean=[0.4
18
  async def upload_image(file: UploadFile = File(...)):
19
  image_bytes = await file.read()
20
  image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
21
-
22
- # Convert to tensor & run AI model
23
  img_tensor = transform(image).unsqueeze(0)
24
  with torch.no_grad():
25
  depth_map = midas(img_tensor).squeeze().cpu().numpy()
26
 
27
- # Normalize depth map
28
  depth_map = cv2.normalize(depth_map, None, 0, 255, cv2.NORM_MINMAX).astype(np.uint8)
29
- depth_img = cv2.applyColorMap(depth_map, cv2.COLORMAP_JET)
30
-
31
  _, buffer = cv2.imencode(".jpg", depth_img)
32
  return {"depth_map": buffer.tobytes()}
33
 
34
  if __name__ == "__main__":
 
35
  uvicorn.run(app, host="0.0.0.0", port=7860)
 
1
  from fastapi import FastAPI, UploadFile, File
 
2
  import cv2
3
  import numpy as np
4
  import torch
 
8
 
9
  app = FastAPI()
10
 
 
11
  midas = torch.hub.load("intel-isl/MiDaS", "MiDaS_small")
12
  midas.eval()
13
  transform = T.Compose([T.Resize((256, 256)), T.ToTensor(), T.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])])
 
16
  async def upload_image(file: UploadFile = File(...)):
17
  image_bytes = await file.read()
18
  image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
19
+
 
20
  img_tensor = transform(image).unsqueeze(0)
21
  with torch.no_grad():
22
  depth_map = midas(img_tensor).squeeze().cpu().numpy()
23
 
 
24
  depth_map = cv2.normalize(depth_map, None, 0, 255, cv2.NORM_MINMAX).astype(np.uint8)
25
+ depth_img = cv2.resize(depth_map, (128, 64))
26
+
27
  _, buffer = cv2.imencode(".jpg", depth_img)
28
  return {"depth_map": buffer.tobytes()}
29
 
30
  if __name__ == "__main__":
31
+ import uvicorn
32
  uvicorn.run(app, host="0.0.0.0", port=7860)