File size: 988 Bytes
2ab4a36
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5b3feb2
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
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'

import tensorflow as tf
from fastapi import FastAPI, UploadFile, File
from utils import load_image, preprocess_image, predict
from model import get_model
import json

app = FastAPI()

MODEL_WEIGHT_PATH = './finetune_v1_weights.keras'
model = get_model(MODEL_WEIGHT_PATH)
print("Model Loaded Successfully")

@app.get("/")
def tester():
    return {
        "status": "Hello World"
    }

@app.post("/get_prediction")
async def get_prediction(x_ray_image: UploadFile = File(...)):

    # Load the image i.e. convert from bytes -> Image (unit8)
    image = load_image(await x_ray_image.read())

    # Preprocess image to make it compatible for model
    image = preprocess_image(image)

    # Retrive model prediction
    prediction = predict(image, model)

    print("Model Predicted: \n", prediction)

    return {
        'prediction': json.dumps(prediction)
    }

@app.post("/test")
def test():
    return {
        "status": 10
    }