# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + id="7MJ4VODzLnNK"
# Working on the same concept with videos as input
# Import the necessary FER() packages for Video Processing

# + colab={"base_uri": "https://localhost:8080/"} id="1UyhZo6hLumQ" outputId="a2196dc8-e1ae-4d86-80d5-c650e443b020"
pip install FER

# + id="0U4zwro2LvuG"
from fer import Video
from fer import FER
import os
import sys
import pandas as pd
# Put in the location of the video file that has to be processed
location_videofile = "/content/Video_One.mp4"

# + colab={"base_uri": "https://localhost:8080/"} id="nfb0Qa3tL80n" outputId="fbc4ab4d-fa51-43e8-e130-c3dfdf80e9cb"
# But the Face detection detector
face_detector = FER(mtcnn=True)
# Input the video for processing
input_video = Video(location_videofile)

# + colab={"base_uri": "https://localhost:8080/"} id="EU6brKYqL_mf" outputId="548d3f3e-2f87-499e-aa31-c571c5eb9d25"
# The Analyze() function will run analysis on every frame of the input video. 
# It will create a rectangular box around every image and show the emotion values next to that.
# Finally, the method will publish a new video that will have a box around the face of the human with live emotion values.
processing_data = input_video.analyze(face_detector, display=False)

# + colab={"base_uri": "https://localhost:8080/", "height": 399} id="G90Y_niyMFbm" outputId="c0dead17-29ba-444c-9e16-fe221861c766"
# We will now convert the analysed information into a dataframe.
# This will help us import the data as a .CSV file to perform analysis over it later
vid_df = input_video.to_pandas(processing_data)
vid_df = input_video.get_first_face(vid_df)
vid_df = input_video.get_emotions(vid_df)

# Plotting the emotions against time in the video
pltfig = vid_df.plot(figsize=(20, 8), fontsize=16).get_figure()

# + colab={"base_uri": "https://localhost:8080/", "height": 266} id="ytaOuk4sMLMQ" outputId="9a4a8448-cf66-4f6c-8731-4c15eb3911c0"
# We will now work on the dataframe to extract which emotion was prominent in the video
angry = sum(vid_df.angry)
disgust = sum(vid_df.disgust)
fear = sum(vid_df.fear)
happy = sum(vid_df.happy)
sad = sum(vid_df.sad)
surprise = sum(vid_df.surprise)
neutral = sum(vid_df.neutral)

emotions = ['Angry', 'Disgust', 'Fear', 'Happy', 'Sad', 'Surprise', 'Neutral']
emotions_values = [angry, disgust, fear, happy, sad, surprise, neutral]

score_comparisons = pd.DataFrame(emotions, columns = ['Human Emotions'])
score_comparisons['Emotion Value from the Video'] = emotions_values
score_comparisons

# + colab={"base_uri": "https://localhost:8080/", "height": 637} id="uN8ENS1AMTpY" outputId="9f603071-f3fd-46be-db90-fbb246d462f6"
# We will conclude the project by experimenting the code on another video
location_videofile_two = "/content/Video_Two.mp4"
input_video_two = Video(location_videofile_two)
processing_data_two = input_video_two.analyze(face_detector, display=False)

vid_df_2 = input_video_two.to_pandas(processing_data_two)
vid_df_2 = input_video_two.get_first_face(vid_df_2)
vid_df_2 = input_video_two.get_emotions(vid_df_2)

pltfig = vid_df_2.plot(figsize=(20, 8), fontsize=16).get_figure()

# + colab={"base_uri": "https://localhost:8080/", "height": 266} id="BI5iGrr_MZwV" outputId="06cef5ce-3993-4097-e026-1fd10b473f33"
angry_2 = sum(vid_df_2.angry)
disgust_2 = sum(vid_df_2.disgust)
fear_2 = sum(vid_df_2.fear)
happy_2 = sum(vid_df_2.happy)
sad_2 = sum(vid_df_2.sad)
surprise_2 = sum(vid_df_2.surprise)
neutral_2 = sum(vid_df_2.neutral)

emotions_values_2 = [angry_2, disgust_2, fear_2, happy_2, sad_2, surprise_2, neutral_2]
score_comparisons = pd.DataFrame(emotions, columns = ['Human Emotions'])
score_comparisons['Emotion Value from the Video'] = emotions_values_2
score_comparisons

# + id="swsVeS1OQ2sz"


StarcoderdataJupyterScriptsDedupFiltered