23A475R commited on
Commit
136ac77
1 Parent(s): 31a0dc3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -54
app.py CHANGED
@@ -8,66 +8,66 @@ from keras.models import load_model
8
 
9
  # Load the pre-trained models and define parameters
10
  detection_model_path = 'haarcascade_files/haarcascade_frontalface_default.xml'
11
- emotion_model_path = 'model_2_aug_nocall_BEST/model_2_aug_nocall_entire_model.h5'
12
  face_detection = cv2.CascadeClassifier(detection_model_path)
13
  emotion_classifier = load_model(emotion_model_path, compile=False)
14
  EMOTIONS = ['neutral', 'happiness', 'surprise', 'sadness', 'anger', 'disgust', 'fear', 'contempt', 'unknown']
15
 
16
- # Function to predict emotions from a frame
17
- def predict(frame_or_path):
18
- if isinstance(frame_or_path, np.ndarray): # If input is a webcam frame
19
- frame = imutils.resize(frame_or_path, width=300)
20
- else: # If input is a file path
21
- frame = cv2.imread(frame_or_path)
22
- if frame is None:
23
- return None, "Error: Unable to read image or video."
24
- gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
25
- faces = face_detection.detectMultiScale(gray, scaleFactor=1.1,
26
- minNeighbors=5, minSize=(30, 30),
27
- flags=cv2.CASCADE_SCALE_IMAGE)
28
- if len(faces) == 0:
29
- return frame, "No face detected."
30
- (fX, fY, fW, fH) = faces[0]
31
- roi = gray[fY:fY + fH, fX:fX + fW]
32
- roi = cv2.resize(roi, (48, 48))
33
- roi = roi.astype("float") / 255.0
34
- roi = img_to_array(roi)
35
- roi = np.expand_dims(roi, axis=0)
36
- preds = emotion_classifier.predict(roi)[0]
37
- label = EMOTIONS[preds.argmax()]
38
- cv2.putText(frame, label, (fX, fY - 10),
39
- cv2.FONT_HERSHEY_DUPLEX, 1, (238, 164, 64), 1)
40
- cv2.rectangle(frame, (fX, fY), (fX + fW, fY + fH),
41
- (238, 164, 64), 2)
42
- return frame, {emotion: float(prob) for emotion, prob in zip(EMOTIONS, preds)}
43
 
44
- # Define input and output components for Gradio
 
45
 
46
- image_input = [
47
- gr.components.Image(sources="webcam", label="Your face"),
48
- gr.components.File(label="Upload Image or Video")
49
- ]
50
- output = [
51
- gr.components.Image(label="Predicted Emotion"),
52
- gr.components.Label(num_top_classes=2, label="Top 2 Probabilities")
53
- ]
54
 
55
- # Launch the Gradio interface
56
- title = "Facial Emotion Recognition"
57
- description = "How well can this model predict your emotions? Take a picture with your webcam, or upload an image, and it will guess if you are happy, sad, angry, disgusted, scared, surprised, or neutral."
58
- thumbnail = "https://raw.githubusercontent.com/gradio-app/hub-emotion-recognition/master/thumbnail.png"
59
 
60
- example_images = [
61
- [
62
- os.path.join(os.path.dirname(__file__), "images/chandler.jpeg"),
63
- os.path.join(os.path.dirname(__file__), "images/janice.jpeg"),
64
- os.path.join(os.path.dirname(__file__), "images/joey.jpeg"),
65
- os.path.join(os.path.dirname(__file__), "images/phoebe.jpeg"),
66
- os.path.join(os.path.dirname(__file__), "images/rachel_monica.jpeg"),
67
- os.path.join(os.path.dirname(__file__), "images/ross.jpeg"),
68
- os.path.join(os.path.dirname(__file__), "images/gunther.jpeg")
69
- ]
70
- ]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
 
72
- gr.Interface(fn=predict, inputs=image_input, outputs=output, examples=example_images,
73
- title=title, description=description, thumbnail=thumbnail).launch()
 
 
 
8
 
9
  # Load the pre-trained models and define parameters
10
  detection_model_path = 'haarcascade_files/haarcascade_frontalface_default.xml'
11
+ emotion_model_path = 'model4_0.83/model4_entire_model.h5'
12
  face_detection = cv2.CascadeClassifier(detection_model_path)
13
  emotion_classifier = load_model(emotion_model_path, compile=False)
14
  EMOTIONS = ['neutral', 'happiness', 'surprise', 'sadness', 'anger', 'disgust', 'fear', 'contempt', 'unknown']
15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
+ # face_detector_mtcnn = MTCNN()
18
+ classifier = load_model(emotion_model_path)
19
 
20
+ def predict_emotion(image):
 
 
 
 
 
 
 
21
 
22
+ faces = face_detection(image)
 
 
 
23
 
24
+ for face in faces:
25
+ x,y,w,h = face['box']
26
+
27
+ roi = image[y:y+h,x:x+w]
28
+
29
+ # Converting the region of interest to grayscale, and resize
30
+ roi_gray = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)
31
+ roi_gray = cv2.resize(roi_gray,(48,48),interpolation=cv2.INTER_AREA)
32
+
33
+ img = roi_gray.astype('float')/255.0
34
+ img = img_to_array(img)
35
+ img = np.expand_dims(img,axis=0)
36
+
37
+ prediction = classifier.predict(img)[0]
38
+ #top_indices = np.argsort(prediction)[-2:]
39
+ #top_emotion = top_indices[1]
40
+ #second_emotion = top_indices[0]
41
+ #label = emotions[top_emotion]
42
+ confidences = {emotions[i]: float(prediction[i]) for i in range(len(emotions))}
43
+
44
+ return confidences
45
+
46
+
47
+
48
+ demo = gr.Interface(
49
+ fn = predict_emotion,
50
+ inputs = gr.Image(type="numpy"),
51
+ outputs = gr.Label(num_top_classes=9),
52
+ #flagging_options=["blurry", "incorrect", "other"],
53
+ examples = [
54
+ os.path.join(os.path.dirname(__file__), "images/Image_1.jpg"),
55
+ os.path.join(os.path.dirname(__file__), "images/Image_2.jpg"),
56
+ os.path.join(os.path.dirname(__file__), "images/Image_3.jpg"),
57
+ os.path.join(os.path.dirname(__file__), "images/Image_4.jpg"),
58
+ os.path.join(os.path.dirname(__file__), "images/Image_5.jpg"),
59
+ os.path.join(os.path.dirname(__file__), "images/Image_6.jpg"),
60
+ os.path.join(os.path.dirname(__file__), "images/Image_7.jpg"),
61
+ os.path.join(os.path.dirname(__file__), "images/Image_8.jpg"),
62
+ os.path.join(os.path.dirname(__file__), "images/Image_9.jpg"),
63
+ os.path.join(os.path.dirname(__file__), "images/Image_10.jpg"),
64
+
65
+ ],
66
+ title = "Whatchu feeling?",
67
+ theme = "shivi/calm_seafoam"
68
+ )
69
 
70
+
71
+
72
+ if __name__ == "__main__":
73
+ demo.launch()