Spaces:
Sleeping
Sleeping
danieaneta
commited on
Commit
·
fca87db
1
Parent(s):
bd0ef29
application
Browse files- app.py +27 -0
- depth_est.py +59 -0
- requirements.txt +8 -0
app.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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("/depth")
|
| 9 |
+
# async def depth(image: str):
|
| 10 |
+
|
| 11 |
+
# depth_base64 = DepthEstimation(image).run_depth()
|
| 12 |
+
# return {"output": depth_base64}
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
# @app.get("/depth")
|
| 16 |
+
# async def depth(file: UploadFile):
|
| 17 |
+
|
| 18 |
+
# depth_base64 = DepthEstimation(File).run_depth()
|
| 19 |
+
# return {"output": depth_base64}
|
| 20 |
+
|
| 21 |
+
@app.post("/depth")
|
| 22 |
+
async def depth(file: UploadFile):
|
| 23 |
+
file_data = await file.read()
|
| 24 |
+
encoded_file_data = base64.b64encode(file_data).decode('utf-8')
|
| 25 |
+
depth_base64 = DepthEstimation(encoded_file_data).run_depth()
|
| 26 |
+
return {"file_data": depth_base64}
|
| 27 |
+
|
depth_est.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from PIL import Image
|
| 2 |
+
import cv2
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
import base64
|
| 5 |
+
import requests
|
| 6 |
+
import io
|
| 7 |
+
import numpy as np
|
| 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="Intel/dpt-hybrid-midas")
|
| 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 |
+
opencv-python
|