|
from fastapi import FastAPI, UploadFile, File |
|
from transformers import DPTImageProcessor, DPTForDepthEstimation |
|
import torch |
|
import numpy as np |
|
from PIL import Image |
|
import io |
|
from fastapi.responses import JSONResponse |
|
import matplotlib.pyplot as plt |
|
import uvicorn |
|
import matplotlib |
|
matplotlib.use('Agg') |
|
|
|
app = FastAPI() |
|
|
|
|
|
processor = DPTImageProcessor.from_pretrained("Intel/dpt-large") |
|
model = DPTForDepthEstimation.from_pretrained("model/") |
|
|
|
|
|
focal_length = 14.35 |
|
sensor_width = 4.88 |
|
image_width = 3072 |
|
|
|
focal_length_px = (image_width * focal_length) / sensor_width |
|
|
|
|
|
@app.post("/predict/") |
|
async def predict_depth(file: UploadFile = File(...)): |
|
|
|
image_bytes = await file.read() |
|
image = Image.open(io.BytesIO(image_bytes)) |
|
|
|
|
|
inputs = processor(images=image, return_tensors="pt") |
|
|
|
with torch.no_grad(): |
|
outputs = model(**inputs) |
|
predicted_depth = outputs.predicted_depth |
|
|
|
|
|
prediction = torch.nn.functional.interpolate( |
|
predicted_depth.unsqueeze(1), |
|
size=image.size[::-1], |
|
mode="bicubic", |
|
align_corners=False, |
|
) |
|
|
|
|
|
output = prediction.squeeze().cpu().numpy() |
|
|
|
|
|
formatted = (output * 255 / np.max(output)).astype("uint8") |
|
depth_map_image = Image.fromarray(formatted) |
|
|
|
|
|
cm = focal_length_px / (output + 1e-6) |
|
|
|
|
|
fig, ax = plt.subplots() |
|
heat = ax.imshow(cm, cmap="plasma") |
|
plt.colorbar(heat) |
|
buf = io.BytesIO() |
|
plt.savefig(buf, format="png") |
|
buf.seek(0) |
|
|
|
|
|
return JSONResponse({ |
|
"depth_map": f"data:image/png;base64,{base64.b64encode(buf.read()).decode()}" |
|
}) |
|
|
|
|
|
if __name__ == "__main__": |
|
uvicorn.run(app, host="0.0.0.0", port=8000) |
|
|