ravi86 commited on
Commit
5331715
·
verified ·
1 Parent(s): 961b8e6

Create handler.py

Browse files
Files changed (1) hide show
  1. handler.py +18 -0
handler.py ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from tensorflow.keras.models import load_model
2
+ from PIL import Image
3
+ import numpy as np
4
+
5
+ model = load_model("my_model.h5")
6
+ emotions = ["Angry", "Disgust", "Fear", "Happy", "Sad", "Surprise", "Neutral"]
7
+
8
+ def preprocess(image):
9
+ image = image.convert("L").resize((48, 48))
10
+ arr = np.array(image) / 255.0
11
+ arr = np.expand_dims(arr, axis=(0, -1)) # (1, 48, 48, 1)
12
+ return arr
13
+
14
+ def predict(image):
15
+ img = preprocess(image)
16
+ pred = model.predict(img)
17
+ label = emotions[np.argmax(pred)]
18
+ return {"label": label, "score": float(np.max(pred))}