api_server.py: moved from frontend/main.py, made roughly functional

main
Nils Koch 3 years ago
parent 3f48276aed
commit 8f0c3a32b8
  1. 34
      api_server.py

@ -1,6 +1,7 @@
from io import BytesIO
from typing import Optional from typing import Optional
from fastapi import FastAPI from fastapi import FastAPI, File, UploadFile
from pydantic import BaseModel from pydantic import BaseModel
import numpy as np import numpy as np
@ -8,7 +9,9 @@ from cv2 import cv2
import torch import torch
import torch.nn as nn import torch.nn as nn
import torch.nn.functional as F import torch.nn.functional as F
from ..nets import Model from nets import Model
from PIL import Image
app = FastAPI() app = FastAPI()
@ -23,11 +26,6 @@ app = FastAPI()
# kommt ctd überhaupt mit was anderem klar? # kommt ctd überhaupt mit was anderem klar?
class Item(BaseModel):
name: str
price: float
is_offer: Optional[bool] = None
class IrImage(BaseModel): class IrImage(BaseModel):
image: np.array image: np.array
@ -96,16 +94,18 @@ def inference_ctd(left, right, model, n_iter=20):
@app.put("/ir") @app.put("/ir")
def read_ir_input(ir_image: IrImage): async def read_ir_input(file: UploadFile = File(...)):
pred_disp = inference_ctd(ir_image.image, reference_pattern) try:
img = np.array(Image.open(BytesIO(await file.read())))
except Exception as e:
return {'error': 'couldn\'t read file', 'exception': e}
print(img.shape)
if len(img.shape) == 2:
img = np.stack((img for _ in range(3)))
pred_disp = inference_ctd(np.array(img), reference_pattern, None)
return {"pred_disp": pred_disp} return {"pred_disp": pred_disp}
@app.get("/items/{item_id}") @app.get('/')
def read_item(item_id: int, q: Optional[str] = None): def main():
return {"item_id": item_id, "q": q} return {'test': 'abc'}
@app.put("/items/{item_id}")
def update_item(item_id: int, item: Item):
return {"item_price": item.price, "item_id": item_id}
Loading…
Cancel
Save