Update main.py
Browse files
main.py
CHANGED
|
@@ -1,46 +1,46 @@
|
|
| 1 |
-
from fastapi import FastAPI, UploadFile, file_uploader
|
| 2 |
-
import json
|
| 3 |
-
from PIL import Image
|
| 4 |
-
from io import BytesIO
|
| 5 |
-
import numpy as np
|
| 6 |
-
from model import build_model
|
| 7 |
-
app = FastAPI()
|
| 8 |
-
|
| 9 |
-
#Load model
|
| 10 |
-
image_shape = (224,224,3)
|
| 11 |
-
num_classes = 6
|
| 12 |
-
model = build_model(image_shape, num_classes)
|
| 13 |
-
model.load_weights('./model_with_weights.h5')
|
| 14 |
-
classes = {
|
| 15 |
-
0: 'Ahegao',
|
| 16 |
-
1: 'Angry',
|
| 17 |
-
2: 'Happy',
|
| 18 |
-
3: 'Neutral'
|
| 19 |
-
4: 'Sad',
|
| 20 |
-
5: 'Surprise'
|
| 21 |
-
}
|
| 22 |
-
|
| 23 |
-
@app.get("/")
|
| 24 |
-
def first_api():
|
| 25 |
-
return {
|
| 26 |
-
"response": "Face Expression Prediction"
|
| 27 |
-
}
|
| 28 |
-
|
| 29 |
-
@app.post("/prediction")
|
| 30 |
-
async def prediction(image: UploadFile = File(...)):
|
| 31 |
-
image = await image.read()
|
| 32 |
-
|
| 33 |
-
# process image
|
| 34 |
-
image = Image.open(BytesIO(image))
|
| 35 |
-
image = image.resize((image_shape[0], image_shape[1]))
|
| 36 |
-
image = image.convert('L')
|
| 37 |
-
image = np.expand_dims(image, axis=2)
|
| 38 |
-
|
| 39 |
-
image = np.expand_dims(image, axis=0)
|
| 40 |
-
prediction = model.predict(image)[0]
|
| 41 |
-
label = np.argmax(prediction, axis=-1).tolist()
|
| 42 |
-
|
| 43 |
-
return {
|
| 44 |
-
"label": label,
|
| 45 |
-
"class": classes[label]
|
| 46 |
}
|
|
|
|
| 1 |
+
from fastapi import FastAPI, UploadFile, file_uploader
|
| 2 |
+
import json
|
| 3 |
+
from PIL import Image
|
| 4 |
+
from io import BytesIO
|
| 5 |
+
import numpy as np
|
| 6 |
+
from model import build_model
|
| 7 |
+
app = FastAPI()
|
| 8 |
+
|
| 9 |
+
#Load model
|
| 10 |
+
image_shape = (224,224,3)
|
| 11 |
+
num_classes = 6
|
| 12 |
+
model = build_model(image_shape, num_classes)
|
| 13 |
+
model.load_weights('./model_with_weights.h5')
|
| 14 |
+
classes = {
|
| 15 |
+
0: 'Ahegao',
|
| 16 |
+
1: 'Angry',
|
| 17 |
+
2: 'Happy',
|
| 18 |
+
3: 'Neutral',
|
| 19 |
+
4: 'Sad',
|
| 20 |
+
5: 'Surprise'
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
@app.get("/")
|
| 24 |
+
def first_api():
|
| 25 |
+
return {
|
| 26 |
+
"response": "Face Expression Prediction"
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
@app.post("/prediction")
|
| 30 |
+
async def prediction(image: UploadFile = File(...)):
|
| 31 |
+
image = await image.read()
|
| 32 |
+
|
| 33 |
+
# process image
|
| 34 |
+
image = Image.open(BytesIO(image))
|
| 35 |
+
image = image.resize((image_shape[0], image_shape[1]))
|
| 36 |
+
image = image.convert('L')
|
| 37 |
+
image = np.expand_dims(image, axis=2)
|
| 38 |
+
|
| 39 |
+
image = np.expand_dims(image, axis=0)
|
| 40 |
+
prediction = model.predict(image)[0]
|
| 41 |
+
label = np.argmax(prediction, axis=-1).tolist()
|
| 42 |
+
|
| 43 |
+
return {
|
| 44 |
+
"label": label,
|
| 45 |
+
"class": classes[label]
|
| 46 |
}
|