23A475R commited on
Commit
5971329
1 Parent(s): a13d47c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -0
app.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+ from keras.preprocessing.image import img_to_array
4
+ import imutils
5
+ import cv2
6
+ from keras.models import load_model
7
+ import numpy as np
8
+
9
+ # parameters for loading data and images
10
+ detection_model_path = 'haarcascade_files/haarcascade_frontalface_default.xml'
11
+ emotion_model_path = 'models/model2_entire_model.h5'
12
+
13
+ # hyper-parameters for bounding boxes shape
14
+ # loading models
15
+ face_detection = cv2.CascadeClassifier(detection_model_path)
16
+ emotion_classifier = load_model(emotion_model_path, compile=False)
17
+ EMOTIONS = ['neutral','happiness','surprise','sadness','anger','disgust','fear','contempt','unknown']
18
+
19
+
20
+ def predict(frame):
21
+
22
+ frame = imutils.resize(frame, width=300)
23
+ gray = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
24
+ faces = face_detection.detectMultiScale(gray, scaleFactor=1.1,
25
+ minNeighbors=5, minSize=(30, 30),
26
+ flags=cv2.CASCADE_SCALE_IMAGE)
27
+
28
+ frameClone = frame.copy()
29
+ if len(faces) > 0:
30
+ faces = sorted(faces, reverse=True,
31
+ key=lambda x: (x[2] - x[0]) * (x[3] - x[1]))[0]
32
+ (fX, fY, fW, fH) = faces
33
+ # Extract the ROI of the face from the grayscale image, resize it to a fixed 28x28 pixels, and then prepare
34
+ # the ROI for classification via the CNN
35
+ roi = gray[fY:fY + fH, fX:fX + fW]
36
+ roi = cv2.resize(roi, (48, 48))
37
+ roi = roi.astype("float") / 255.0
38
+ roi = img_to_array(roi)
39
+ roi = np.expand_dims(roi, axis=0)
40
+
41
+ preds = emotion_classifier.predict(roi)[0]
42
+ label = EMOTIONS[preds.argmax()]
43
+ else:
44
+ return frameClone, "Can't find your face"
45
+
46
+ probs = {}
47
+ cv2.putText(frameClone, label, (fX, fY - 10),
48
+ cv2.FONT_HERSHEY_DUPLEX, 1, (238, 164, 64), 1)
49
+ cv2.rectangle(frameClone, (fX, fY), (fX + fW, fY + fH),
50
+ (238, 164, 64), 2)
51
+
52
+ for (i, (emotion, prob)) in enumerate(zip(EMOTIONS, preds)):
53
+ probs[emotion] = float(prob)
54
+
55
+ return frameClone, probs
56
+
57
+
58
+ inp = gr.inputs.Image(source="webcam", label="Your face")
59
+ out = [
60
+ gr.outputs.Image(label="Predicted Emotion"),
61
+ gr.outputs.Label(num_top_classes=2, label="Top 2 Probabilities")
62
+ ]
63
+ title = "Facial Emotion Recognition"
64
+ description = "How well can this model predict your emotions? Take a picture with your webcam, and it will guess if" \
65
+ " you are: happy, sad, angry, disgusted, scared, surprised, or neutral."
66
+ # thumbnail = "https://raw.githubusercontent.com/gradio-app/hub-emotion-recognition/master/thumbnail.png"
67
+
68
+ gr.Interface(predict, inp, out, capture_session=True, title=title, thumbnail=thumbnail,
69
+ description=description).launch(inbrowser=True)