gruhit-patel
commited on
Commit
•
12da60d
1
Parent(s):
c4684e7
Image rescaling bug fix
Browse files- backend.py +63 -63
backend.py
CHANGED
@@ -1,64 +1,64 @@
|
|
1 |
-
import os
|
2 |
-
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
|
3 |
-
|
4 |
-
from fastapi import FastAPI, UploadFile, File
|
5 |
-
import json
|
6 |
-
from PIL import Image
|
7 |
-
from io import BytesIO
|
8 |
-
import numpy as np
|
9 |
-
|
10 |
-
from model import get_model
|
11 |
-
|
12 |
-
app = FastAPI()
|
13 |
-
|
14 |
-
IMAGE_WIDTH = 224
|
15 |
-
IMAGE_HEIGHT = 224
|
16 |
-
|
17 |
-
MODEL_WEIGHT_PATH = 'vgg_face_weights2.h5'
|
18 |
-
model = get_model(
|
19 |
-
image_shape = (IMAGE_WIDTH, IMAGE_HEIGHT, 3),
|
20 |
-
num_classes = 6,
|
21 |
-
model_weights = MODEL_WEIGHT_PATH
|
22 |
-
)
|
23 |
-
print(model.summary())
|
24 |
-
print("Model Loaded Successfully")
|
25 |
-
|
26 |
-
######### Utilities #########
|
27 |
-
def load_image(image_data):
|
28 |
-
image = Image.open(BytesIO(image_data))
|
29 |
-
return image
|
30 |
-
|
31 |
-
def preprocess(image):
|
32 |
-
image = image.resize((IMAGE_WIDTH, IMAGE_HEIGHT))
|
33 |
-
|
34 |
-
image = np.array(image)
|
35 |
-
image = np.expand_dims(image, axis=0)
|
36 |
-
|
37 |
-
return image
|
38 |
-
|
39 |
-
def get_prediction(image):
|
40 |
-
probs = model.predict(image)[0]
|
41 |
-
label = np.argmax(probs)
|
42 |
-
|
43 |
-
return {
|
44 |
-
'pred_probs': probs.tolist(),
|
45 |
-
'label': int(label)
|
46 |
-
}
|
47 |
-
|
48 |
-
@app.get("/")
|
49 |
-
def foo():
|
50 |
-
return {
|
51 |
-
"status": "Face Expression Classifier"
|
52 |
-
}
|
53 |
-
|
54 |
-
|
55 |
-
@app.post("/get_prediction")
|
56 |
-
async def predict(face_img: UploadFile = File(...)):
|
57 |
-
image = load_image(await face_img.read())
|
58 |
-
|
59 |
-
image = preprocess(image)
|
60 |
-
result = get_prediction(image)
|
61 |
-
|
62 |
-
return {
|
63 |
-
"result": json.dumps(result)
|
64 |
}
|
|
|
1 |
+
import os
|
2 |
+
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
|
3 |
+
|
4 |
+
from fastapi import FastAPI, UploadFile, File
|
5 |
+
import json
|
6 |
+
from PIL import Image
|
7 |
+
from io import BytesIO
|
8 |
+
import numpy as np
|
9 |
+
|
10 |
+
from model import get_model
|
11 |
+
|
12 |
+
app = FastAPI()
|
13 |
+
|
14 |
+
IMAGE_WIDTH = 224
|
15 |
+
IMAGE_HEIGHT = 224
|
16 |
+
|
17 |
+
MODEL_WEIGHT_PATH = 'vgg_face_weights2.h5'
|
18 |
+
model = get_model(
|
19 |
+
image_shape = (IMAGE_WIDTH, IMAGE_HEIGHT, 3),
|
20 |
+
num_classes = 6,
|
21 |
+
model_weights = MODEL_WEIGHT_PATH
|
22 |
+
)
|
23 |
+
print(model.summary())
|
24 |
+
print("Model Loaded Successfully")
|
25 |
+
|
26 |
+
######### Utilities #########
|
27 |
+
def load_image(image_data):
|
28 |
+
image = Image.open(BytesIO(image_data))
|
29 |
+
return image
|
30 |
+
|
31 |
+
def preprocess(image):
|
32 |
+
image = image.resize((IMAGE_WIDTH, IMAGE_HEIGHT))
|
33 |
+
|
34 |
+
image = np.array(image)
|
35 |
+
image = np.expand_dims(image, axis=0) / 255.0
|
36 |
+
|
37 |
+
return image
|
38 |
+
|
39 |
+
def get_prediction(image):
|
40 |
+
probs = model.predict(image)[0]
|
41 |
+
label = np.argmax(probs)
|
42 |
+
|
43 |
+
return {
|
44 |
+
'pred_probs': probs.tolist(),
|
45 |
+
'label': int(label)
|
46 |
+
}
|
47 |
+
|
48 |
+
@app.get("/")
|
49 |
+
def foo():
|
50 |
+
return {
|
51 |
+
"status": "Face Expression Classifier"
|
52 |
+
}
|
53 |
+
|
54 |
+
|
55 |
+
@app.post("/get_prediction")
|
56 |
+
async def predict(face_img: UploadFile = File(...)):
|
57 |
+
image = load_image(await face_img.read())
|
58 |
+
|
59 |
+
image = preprocess(image)
|
60 |
+
result = get_prediction(image)
|
61 |
+
|
62 |
+
return {
|
63 |
+
"result": json.dumps(result)
|
64 |
}
|