-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackend.py
More file actions
29 lines (21 loc) · 751 Bytes
/
backend.py
File metadata and controls
29 lines (21 loc) · 751 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import uvicorn
from fastapi import FastAPI, UploadFile, File
from tensorflow import keras
import cv2
import numpy as np
app = FastAPI()
model_new = keras.models.load_model('mnist.hdf5')
# define a root `/` endpoint
@app.get("/")
def index():
return {"ok": True}
@app.post("/predict")
async def predict(img: UploadFile = File(...)):
contents = await img.read()
nparr = np.fromstring(contents, np.uint8)
img_color = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
img_grey = cv2.cvtColor(img_color, cv2.COLOR_BGR2GRAY)
pred = model_new.predict(img_grey.reshape(1, 28, 28, 1))
return {"result": float(np.argmax(pred[0])), "percent": pred[0].tolist()}
if __name__ == "__main__":
uvicorn.run(app, host="127.0.0.1", port=8001)