morro777 commited on
Commit
5c48314
·
verified ·
1 Parent(s): b9bfdd4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -7
app.py CHANGED
@@ -3,21 +3,35 @@ import numpy as np
3
  import cv2
4
  from keras.models import load_model
5
 
 
6
  model = load_model("emotion_model.h5", compile=False)
7
 
8
  emotion_labels = ["Angry", "Happy", "Sad"]
9
 
10
  def predict_emotion(image):
11
- img = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
12
- face = cv2.resize(img, (64, 64))
13
- face = face / 255.0
14
- face = np.reshape(face, (1, 64, 64, 1))
 
 
 
 
 
 
 
 
15
 
16
- prediction = model.predict(face)
17
- emotion = emotion_labels[np.argmax(prediction)]
 
18
 
19
- return emotion
20
 
 
 
 
 
21
  demo = gr.Interface(
22
  fn=predict_emotion,
23
  inputs=gr.Image(type="numpy"),
 
3
  import cv2
4
  from keras.models import load_model
5
 
6
+ # Load model
7
  model = load_model("emotion_model.h5", compile=False)
8
 
9
  emotion_labels = ["Angry", "Happy", "Sad"]
10
 
11
  def predict_emotion(image):
12
+ try:
13
+ # Convert to grayscale
14
+ img = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
15
+
16
+ # Resize to model input size
17
+ face = cv2.resize(img, (64, 64))
18
+
19
+ # Normalize
20
+ face = face / 255.0
21
+
22
+ # Reshape for model
23
+ face = np.reshape(face, (1, 64, 64, 1))
24
 
25
+ # Predict
26
+ prediction = model.predict(face, verbose=0)
27
+ emotion = emotion_labels[np.argmax(prediction)]
28
 
29
+ return emotion
30
 
31
+ except Exception as e:
32
+ return "Error: " + str(e)
33
+
34
+ # Gradio UI
35
  demo = gr.Interface(
36
  fn=predict_emotion,
37
  inputs=gr.Image(type="numpy"),