danieaneta commited on
Commit
9266e32
·
1 Parent(s): 1b98f3b
Files changed (4) hide show
  1. Dockerfile +21 -0
  2. app.py +17 -0
  3. depth_est.py +59 -0
  4. requirements.txt +8 -0
Dockerfile ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Use an official Python runtime as a parent image
2
+ FROM python:3.9
3
+
4
+ WORKDIR /code
5
+
6
+ COPY ./requirements.txt /code/requirements.txt
7
+
8
+ RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
9
+
10
+ RUN useradd user
11
+
12
+ USER user
13
+
14
+ ENV HOME=/home/user \
15
+ PATH=/home/user/.local/bin:$PATH
16
+
17
+ WORKDIR $HOME/app
18
+
19
+ COPY --chown=user . $HOME/app
20
+
21
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
app.py ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, File, UploadFile
2
+ # from depth_est_deprecated import DepthEstimation
3
+ from depth_est import DepthEstimation
4
+ import base64
5
+
6
+ app = FastAPI()
7
+
8
+ @app.get("/")
9
+ def home():
10
+ return {"message":"Hello World"}
11
+
12
+ @app.post("/depth")
13
+ async def depth(file: UploadFile):
14
+ file_data = await file.read()
15
+ encoded_file_data = base64.b64encode(file_data).decode('utf-8')
16
+ depth_base64 = DepthEstimation(encoded_file_data).run_depth()
17
+ return {"file_data": depth_base64}
depth_est.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from PIL import Image
2
+ from transformers import pipeline
3
+ import base64
4
+ import requests
5
+ import io
6
+ import numpy as np
7
+ import os
8
+
9
+ def convert_img_base64(img):
10
+ with open(img, 'rb') as file:
11
+ base64_image = base64.b64encode(file.read())
12
+ return base64_image
13
+
14
+
15
+
16
+ class DepthEstimation():
17
+ def __init__(self, img_base64):
18
+ self.img_base64 = img_base64
19
+
20
+ def convert_from_base64(self):
21
+ image_data = base64.b64decode(self.img_base64)
22
+ image = Image.open(io.BytesIO(image_data))
23
+ return image
24
+
25
+ def run_depth(self):
26
+ image_data = self.convert_from_base64()
27
+
28
+ pipe = pipeline("depth-estimation", model="depth-anything/Depth-Anything-V2-Base-hf")
29
+ depth = pipe(image_data)
30
+ depth_map = depth['predicted_depth']
31
+
32
+ # Convert the tensor to a NumPy array and normalize
33
+ depth_map = depth_map.squeeze().cpu().numpy() # Remove batch dimension
34
+ depth_map = np.uint8(depth_map / depth_map.max() * 255) # Normalize to 0-255
35
+
36
+ # Convert depth map to PIL Image
37
+ depth_image = Image.fromarray(depth_map)
38
+
39
+ # Save the depth image to a bytes buffer
40
+ buffer = io.BytesIO()
41
+ depth_image.save(buffer, format='PNG')
42
+ image_bytes = buffer.getvalue()
43
+
44
+ # Encode the bytes to base64
45
+ base64_string = base64.b64encode(image_bytes).decode()
46
+
47
+ # Decode and display for verification
48
+ # image_data = base64.b64decode(base64_string)
49
+ # image = Image.open(io.BytesIO(image_data))
50
+ # image.show()
51
+ # image.save('output_image.png')
52
+
53
+ return base64_string
54
+
55
+
56
+ # if __name__ == "__main__":
57
+ # img_path = 'img3.jpg'
58
+ # base64_image = convert_img_base64(img_path)
59
+ # DepthEstimation(base64_image).run_depth()
requirements.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ fastapi
2
+ requests
3
+ numpy
4
+ transformers
5
+ uvicorn
6
+ python-multipart
7
+ pillow
8
+ torch