File size: 2,442 Bytes
5e803d9
 
 
2c21767
 
3a66e6c
 
 
2c21767
3a66e6c
 
 
 
 
4296598
3a66e6c
 
4296598
 
3a66e6c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2c21767
 
 
5e803d9
2c21767
5e803d9
 
2c21767
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# code adapted from Sefik Ilkin Serengil's Facial Expression Recognition with Keras tutorial
# https://raw.githubusercontent.com/serengil/tensorflow-101/master/python/emotion-analysis-from-video.py

import gradio as gr
import cv2
import numpy as np
from keras.preprocessing.image import img_to_array
from keras.models import model_from_json

# Facial expression recognizer initialization
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
model = model_from_json(open('facial_expression_model_structure.json', 'r').read())
model.load_weights('facial_expression_model_weights.h5')

# Define the emotions
emotions = ('angry', 'disgust', 'fear', 'happy', 'sad', 'surprise', 'neutral')

# Define the frame scaling factor
scaling_factor = 1.0
  
def process_image(img):
  # Resize the frame
  frame = cv2.resize(img, None, fx=scaling_factor, fy=scaling_factor, interpolation=cv2.INTER_AREA)
  
  # Convert to grayscale
  gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

  # Run the face detector on the grayscale image
  face_rects = face_cascade.detectMultiScale(gray, 1.3, 5)
  
  # Draw a rectangle around the face
  for (x,y,w,h) in face_rects:
    #cv2.rectangle(frame, (x,y), (x+w,y+h), (0,255,0), 3)
    cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),2) #draw rectangle to main image

    detected_face = frame[int(y):int(y+h), int(x):int(x+w)] #crop detected face
    detected_face = cv2.cvtColor(detected_face, cv2.COLOR_BGR2GRAY) #transform to gray scale
    detected_face = cv2.resize(detected_face, (48, 48)) #resize to 48x48

    img_pixels = img_to_array(detected_face)
    img_pixels = np.expand_dims(img_pixels, axis = 0)

    img_pixels /= 255 #pixels are in scale of [0, 255]. normalize all pixels in scale of [0, 1]

    predictions = model.predict(img_pixels) #store probabilities of 7 expressions

    #find max indexed array 0: angry, 1:disgust, 2:fear, 3:happy, 4:sad, 5:surprise, 6:neutral
    max_index = np.argmax(predictions[0])
    emotion = emotions[max_index]

    #write emotion text above rectangle
    cv2.putText(frame, emotion, (int(x), int(y)), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,255,255), 2)
    
  return frame


interface = gr.Interface(
  fn = process_image,
  inputs='webcam',
  outputs='image',
  title='Facial Expression Detection',
  description='Simple facial expression detection example with OpenCV, using a CNN model pre-trained on the FER 2013 dataset.')
  
interface.launch()