-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
44 lines (37 loc) · 1.01 KB
/
main.py
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
from fastapi import FastAPI, UploadFile, File
import json
from PIL import Image
from io import BytesIO
import numpy as np
from model import build_model
app = FastAPI()
# Load model
image_shape = (256, 256, 1)
num_classes = 3
model = build_model(image_shape, num_classes)
model.load_weights('./model_with_weights.h5')
classes = {
0: 'COVID-19',
1: 'Non-COVID',
2: 'Normal'
}
@app.get("/")
def first_api():
return {
"response": "COVID Prediction"
}
@app.post("/prediction")
async def prediction(image: UploadFile = File(...)):
image = await image.read()
# process image
image = Image.open(BytesIO(image))
image = image.resize((image_shape[0], image_shape[1]))
image = image.convert('L')
image = np.expand_dims(image, axis=2)
image = np.expand_dims(image, axis=0)
prediction = model.predict(image)[0]
label = np.argmax(prediction, axis=-1).tolist()
return {
"label": label,
"class": classes[label]
}