| from fastapi import FastAPI, File, UploadFile |
| from run import Start_Program |
| import base64 |
| from depth_est import DepthEstimation |
| import traceback |
| from PIL import Image |
| import io |
| import numpy as np |
|
|
|
|
| app = FastAPI() |
|
|
|
|
| @app.get("/") |
| def greet_json(): |
| return {"Hello": "World!"} |
|
|
|
|
| @app.post("/segment") |
| async def segment(file: UploadFile): |
| file_data = await file.read() |
| try: |
| encoded_file_data = base64.b64encode(file_data).decode("utf-8") |
| depth_PIL = DepthEstimation(encoded_file_data).run_depth() |
|
|
| base_image_data = base64.b64decode(encoded_file_data) |
| base_image_pil = Image.open(io.BytesIO(base_image_data)) |
| base_image = np.array(base_image_pil)[:, :, ::-1].copy() |
|
|
| depth_image = np.array(depth_PIL).copy() |
|
|
| segment_images = Start_Program(depth_image, base_image).run() |
|
|
| return_statement = {"file_data:": segment_images} |
| except Exception as e: |
| return_statement = "HF-ERROR-TRY-AGAIN" |
| |
| |
| return return_statement |