File size: 968 Bytes
e4054b9
ca98f25
 
 
 
 
 
 
 
 
 
 
f5aa73d
ca98f25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
746211f
ca98f25
 
 
 
 
 
 
 
fa819f2
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
45
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 = (224,224,3)
num_classes = 6
model = build_model(image_shape, num_classes)
model.load_weights('./new_model_weights.h5')
classes = {
    0: 'Ahegao',
    1: 'Angry',
    2: 'Happy',
    3: 'Neutral',
    4: 'Sad',
    5: 'Surprise'
}

@app.get("/")
def first_api():
    return {
        "response": "Face Expression 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 = np.expand_dims(image, axis=0)
    prediction = model.predict(image)[0]
    label = np.argmax(prediction, axis=-1).tolist()

    return {
        "label": label,
        "class": classes[label]
    }