File size: 1,272 Bytes
071870f
 
f33a809
 
 
 
 
 
071870f
 
 
 
 
 
f33a809
071870f
 
 
 
 
 
 
 
f33a809
 
 
071870f
 
f33a809
 
071870f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f33a809
 
 
071870f
 
 
f33a809
 
 
071870f
 
 
 
 
 
 
 
 
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'

from fastapi import FastAPI, UploadFile, File
import json
from PIL import Image
from io import BytesIO

from model import get_model

app = FastAPI()

IMAGE_WIDTH = 224
IMAGE_HEIGHT = 224

MODEL_WEIGHT_PATH = 'vgg_face_weights2.h5'
model = get_model(
		image_shape = (IMAGE_WIDTH, IMAGE_HEIGHT, 3),
		num_classes = 6,
		model_weights = MODEL_WEIGHT_PATH
	)
print(model.summary())
print("Model Loaded Successfully")

######### Utilities #########
def load_image(image_data):
	image = Image.open(BytesIO(image_data))
	return image

def preprocess(image):
	image = image.resize((IMAGE_WIDTH, IMAGE_HEIGHT))

	image = np.array(image)
	image = np.expand_dims(image, axis=0)

	return image

def get_prediction(image):
	probs = model.predict(image)[0]
	label = np.argmax(probs)

	return {
		'pred_probs': pred_probs.tolist(),
		'label': int(label)
	}

@app.get("/")
def foo():
	return {
		"status": "Face Expression Classifier"
	}


@app.post("/get_prediction")
async def predict(face_image: UploadFile = File(...)):
	image = load_image(await face_image.read())

	image = preprocess(image)
	result = get_prediction(image)

	return {
		"result": json.dumps(result)
	}