# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python3.8(face_emotion_recog)
#     language: python
#     name: face_emotion_recog
# ---

#loading the required libraries
from fastai import *
from fastai.vision import *
from fastai.vision import image
#from fastai.vision.widgets import *

import os
import pandas as pd
import numpy as np
import cv2

# ### Loading the model for making the predictions

model4_test=load_learner(path=r"D:\Data science\Alma better\DL Facial emotion recognition\Images\images\train",file='fastai_emojis_model4.pkl')

model4_test.dl

os.chdir(r'D:\Data science\Alma better\DL Facial emotion recognition\Images\images\validation\surprise')

# ### Some test predictions on an image

test1=cv2.imread('./330.jpg')
t = pil2tensor(test1, dtype=np.float32) # converts to numpy tensor
#t = t.permute(2,0,1) # Move num_channels as first dimension
t = t.float()/255.0
im = Image(t) # Convert to fastAi Image - this class has "apply_tfms" 
model4_test.predict(im)

show_image(im)

model4_test.predict(im)[0]

type(model4_test.predict(im)[0])

model4_test

os.getcwd()

os.chdir('D:/Data science/Alma better/DL Facial emotion recognition/Images/images/validation/happy')

#path='./531.jpg'
img = cv2.imread('./531.jpg')
plt.imshow(img)

# #### Testing on different category images

os.chdir('D:/Data science/Alma better/DL Facial emotion recognition/Images/images/validation/surprise')

a3=cv2.imread('./10162.jpg')
t = pil2tensor(a3, dtype=np.float32) # converts to numpy tensor
t = t.float()/255.0
#t = t.permute(2,0,1) # Move num_channels as first dimension
im = Image(t) # Convert to fastAi Image - this class has "apply_tfms" 
pred0=model4_test.predict(im)
print(pred0)
print(str(pred0[0]))

a1=cv2.imread('./10097.jpg')
plt.imshow(a1)

#not used just for experimentation
Emojis_dict = {'Category tensor(0)':"Angry", 'Category tensor(1)':"Disgust", 'Category tensor(2)':"Fear", 'Category tensor(3)':"Happy",\
               'Category tensor(4)':"Neutral", 'Category tensor(5)':"Sad", 'Category tensor(6)':"Surprise"}


# ### Images emotion detection pipeline

def prediction(img1):
    predictions = []
    
    predictions = model4_test.predict(img1)
    predictions[0]
    #print(predictions)
    #type(predictions)
    prediction1=[]
    prediction1=str(predictions[0])
    #emotion = []
    #emotion = Emojis_dict[predictions1]
    if prediction1 == 'angry':
        print("The person here is angry")
    elif prediction1 == 'disgust':
        print("The person here is disgusted")
    elif prediction1 == 'fear':
        print("The person here is in fear")
    elif prediction1 == 'happy':
        print("The person here is happy")   
    elif prediction1 == 'neutral':
        print("The person here is neutral")
    elif prediction1 == 'sad':
        print("The person here is sad")
    elif prediction1 == 'surprise':
        print("The person here is surprised")
    else:
        print("Cannot detect")
    #cv2.destroyWindow("preview")


def return_prediction(path):
    #converting image to gray scale and save it
    img = cv2.imread(path)
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    cv2.imwrite(path, gray)
    
    #detect face in image, crop it then resize it then save it
    #face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') 
    face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
    eye_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_eye.xml')
    img = cv2.imread(path)
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)
    for (x,y,w,h) in faces:
        face_clip = img[y:y+h, x:x+w]
        cv2.imwrite(path, cv2.resize(face_clip, (350, 350)))
    
    #read the processed image then make prediction and display the result
    read_image = cv2.imread(path)
    t = pil2tensor(read_image, dtype=np.float32) # converts to numpy tensor
    t = t.float()/255.0
    #t = t.permute((2,0,1))
    #t=t.transpose((2,0,1))
    
    img1 = Image(t) # Convert to fastAi Image - this class has "apply_tfms" 

    model_pred1 = model4_test.predict(img1)[0]
    predicted=prediction(img1)  #uncomment when above type of display text is required for image outputs
    plt.imshow(img)  #uncomment if image has to be displayed
    return str(model_pred1)


t.shape

a5 = t.float()/255.0

a5.shape

a9=a5.permute(2,0,1).shape

a9

return_prediction('./10259.jpg')

os.getcwd()

return_prediction('./10306.jpg')

c=Emojis_dict['Category tensor(3)']

#experimentation
for i in range(len(Emojis_dict)):
    if c=='Happy':
        print('Yes Happy face')
    else:
        print('Cannot detect')
prediction1=str('Category tensor(3)')
type(prediction1)


# +
#btn_upload = widgets.FileUpload()
#out_pl = widgets.Output()
#lbl_pred = widgets.Label()
# -

# ### Emotion detection pipeline for detection on videos

def test_rerun(text, cap):
    while(True):
        ret, img = cap.read()
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        font = cv2.FONT_HERSHEY_SIMPLEX
        cv2.putText(img, "The last phase of the person's Emotion was recorded "+str(text), (95,30), font, 1.0, (255, 0, 0), 2, cv2.LINE_AA)
        
        cv2.putText(img, "Press SPACE: Detecting", (5,470), font, 0.7, (255, 0, 0), 2, cv2.LINE_AA)
    
        cv2.putText(img, "Hold Q: To Quit😎", (460,470), font, 0.7, (255, 0, 0), 2, cv2.LINE_AA)
    
        faces = face_cascade.detectMultiScale(gray, 1.3, 5)
        for x,y,w,h in faces:
            cv2.rectangle(img, (x,y), (x+w, y+h), (255, 0, 0), 2)
        
        cv2.imshow("Image", img)

        
        if cv2.waitKey(1) == ord(' '):
            cv2.imwrite("test6.jpg", img)
            text = return_prediction("test6.jpg")
            test_video_pred(text, cap)
            break
            
        if cv2.waitKey(1) == ord('q'):
            cap.release()
            cv2.destroyAllWindows()
            break

os.getcwd()

os.chdir(r'D:\Data science\Alma better\DL Facial emotion recognition\Images\images\Input and output')

# +
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')

#cap = cv2.VideoCapture('./pexels-tiger-lily-7149007.mp4')

def test_video_pred(text, cap):
    while(True):
        ret, img = cap.read()
        if not ret:
            print("Can't receive frame (stream end?). Exiting ...")
            break
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        font = cv2.FONT_HERSHEY_SIMPLEX
        cv2.putText(img, "The last phase of person's emotion was recorded: "+str(text), (95,30), font, 1.0, (255, 0, 0), 2, cv2.LINE_AA)
        
        cv2.putText(img, "Press SPACE: For detection", (5,470), font, 0.7, (255, 0, 0), 2, cv2.LINE_AA)
    
        cv2.putText(img, "Hold Q: To Quit😎", (460,470), font, 0.7, (255, 0, 0), 2, cv2.LINE_AA)
    
        faces = face_cascade.detectMultiScale(gray, 1.3, 5)
        for x,y,w,h in faces:
            cv2.rectangle(img, (x,y), (x+w, y+h), (255, 0, 0), 2)
        
        cv2.imshow("Image", img)
        
        if cv2.waitKey(1) == ord(' '):
            cv2.imwrite("test6.jpg", img)
            text = return_prediction("test6.jpg")
            test_rerun(text, cap)
            #plt.imshow(img)
            break
            
        if cv2.waitKey(1) == ord('q'):
            cap.release()
            cv2.destroyAllWindows()
            break


# -

# ### Examples

cap = cv2.VideoCapture('./pexels-tiger-lily-7149007.mp4')
test_video_pred('None',cap)

cap=cv2.VideoCapture('./pexels-yan-krukov-7693411.mp4')
test_video_pred('None',cap)

# ### 🤩😃
# - It says the people in the video are in neutral and happy emotions. Yes!!Yayy!!!
# - Its working well

cap=cv2.VideoCapture('./pexels-yan-krukov-7640073.mp4')
test_video_pred('None',cap)

test_video_pred('None',cap)

os.chdir(r'D:\Data science\Alma better\DL Facial emotion recognition\Images\images\validation\fear')

return_prediction('./10099.jpg')

# ### Live video emotion detection

cap=cv2.VideoCapture(0)
test_video_pred('None',cap)

cap=cv2.VideoCapture(0)
test_video_pred('None',cap)

# - Here I'm holding the mobile from which it is detecting the emotion of the people in it
# - It is able to detect everymoment from the live video as soon as the person comes into the picture
# - Here I have used Resnet34 transfer learning(using fastai) for detecting the emotion of the person or the people.



StarcoderdataJupyterScriptsDedupFiltered