Skip to content

boramorka/Emotion_detection_v1

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation


Face Expression Recognition + Live Webcam Detection
😃 😐 😒 🎥

Built with

How To Run LocallyBuilt processFeedback

How To Run Locally

# Clone this repository
$ git clone https://github.com/boramorka/Emotion_detection_v1.git

# Go into the repository
$ cd Emotion_detection_v1

# Install dependencies
$ pip install requirements.txt

# Run app
$ python main.py

Make sure you have:

  • Nvidia videocard
  • Installed all requirement libraries
  • Installed Nvidia Cuda
  • Installed Nvidia drivers
  • Installed cuDNN

Usage

Built process

  • Dateset for training: https://www.kaggle.com/datasets/jonathanoheix/face-expression-recognition-dataset

  • Import libraries:

    import matplotlib.pyplot as plt
    import numpy as np
    import pandas as pd
    import seaborn as sns
    import os
    
    # Importing Deep Learning Libraries
    
    from keras.preprocessing.image import load_img, img_to_array
    from keras.preprocessing.image import ImageDataGenerator
    from keras.layers import Dense,Input,Dropout,GlobalAveragePooling2D,Flatten,Conv2D,BatchNormalization,Activation,MaxPooling2D
    from keras.models import Model,Sequential
    from keras.optimizers import Adam,SGD,RMSprop
  • Model architecture:

    model = Sequential()
    
    #1st CNN layer
    model.add(Conv2D(64,(3,3),padding = 'same',input_shape = (48,48,1)))
    model.add(BatchNormalization())
    model.add(Activation('relu'))
    model.add(MaxPooling2D(pool_size = (2,2)))
    model.add(Dropout(0.25))
    
    #2nd CNN layer
    model.add(Conv2D(128,(5,5),padding = 'same'))
    model.add(BatchNormalization())
    model.add(Activation('relu'))
    model.add(MaxPooling2D(pool_size = (2,2)))
    model.add(Dropout (0.25))
    
    #3rd CNN layer
    model.add(Conv2D(512,(3,3),padding = 'same'))
    model.add(BatchNormalization())
    model.add(Activation('relu'))
    model.add(MaxPooling2D(pool_size = (2,2)))
    model.add(Dropout (0.25))
    
    #4th CNN layer
    model.add(Conv2D(512,(3,3), padding='same'))
    model.add(BatchNormalization())
    model.add(Activation('relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(0.25))
    
    model.add(Flatten())
    
    #Fully connected 1st layer
    model.add(Dense(256))
    model.add(BatchNormalization())
    model.add(Activation('relu'))
    model.add(Dropout(0.25))
    
    # Fully connected layer 2nd layer
    model.add(Dense(512))
    model.add(BatchNormalization())
    model.add(Activation('relu'))
    model.add(Dropout(0.25))
    
    model.add(Dense(no_of_classes, activation='softmax'))
  • The model has a 4.5 million parameters

  • Now we can recognize 7 emotions

    • Angry
    • Disgust
    • Fear
    • Happy
    • Neutral
    • Sad
    • Surprise
  • Creating main.py for capturing:

    """Capturing block"""
    
    cap = cv2.VideoCapture(0)
    
    while True:
        _, frame = cap.read()
        labels = []
        gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
        faces = face_classifier.detectMultiScale(gray)
    
        main_model()
    
        cv2.imshow('Emotion Detector',frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
  • Main model for face detection and putting text:

      def main_model():
        for (x,y,w,h) in faces:
          cv2.rectangle(frame,(x,y),(x+w,y+h),(0,255,255),2)
          roi_gray = gray[y:y+h,x:x+w]
          roi_gray = cv2.resize(roi_gray,(48,48),interpolation=cv2.INTER_AREA)
    
    
    
          if np.sum([roi_gray])!=0:
              roi = roi_gray.astype('float')/255.0
              roi = img_to_array(roi)
              roi = np.expand_dims(roi,axis=0)
    
              prediction = classifier.predict(roi)[0]
              label=emotion_labels[prediction.argmax()]
              label_position = (x,y)
              cv2.putText(frame,label,label_position,cv2.FONT_HERSHEY_SIMPLEX,1,(0,255,0),2)
          else:
              cv2.putText(frame,'No Faces',(30,80),cv2.FONT_HERSHEY_SIMPLEX,1,(0,255,0),2)

Feedback

🤵 Feel free to send me feedback on Telegram. Feature requests are always welcome.

🧮 Check my other projects.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages