|
from transformers import ( |
|
AutoModelForSequenceClassification, |
|
pipeline, |
|
PreTrainedTokenizerFast |
|
) |
|
from WebCam import capture_webcam |
|
from colorama import Fore, Style |
|
import pandas as pd |
|
import time |
|
from datetime import datetime |
|
import matplotlib.pyplot as plt |
|
import gradio as gr |
|
|
|
|
|
text_dataDict = {"Time": [], "Emotion": [], "Confidence Score": []} |
|
face_dataDict = {"Time": [], "Emotion": [], "Confidence Score": []} |
|
|
|
emotionDetectModel = AutoModelForSequenceClassification.from_pretrained("borisn70/bert-43-multilabel-emotion-detection") |
|
tokenizer = PreTrainedTokenizerFast(tokenizer_file="tokenizer.json") |
|
pipe = pipeline(task="text-classification", model=emotionDetectModel, tokenizer=tokenizer) |
|
|
|
localFormat = "%Y-%m-%d %H:%M:%S" |
|
|
|
|
|
|
|
|
|
def emotionAnalysis(message): |
|
""" |
|
Main function that processes both text and facial emotions |
|
Args: |
|
inp (str): User input text |
|
Returns: |
|
tuple: (str, plt) Contains the emotion results text and the updated plot |
|
""" |
|
if (message.lower() == "quit"): |
|
return "Quitting...", displayResults() |
|
|
|
|
|
result = pipe(message) |
|
text_emotion = result[0]["label"] |
|
text_score = result[0]["score"] |
|
words_timestamp = datetime.now().astimezone().strftime(localFormat) |
|
|
|
|
|
text_dataDict["Time"].append(words_timestamp) |
|
text_dataDict["Emotion"].append(text_emotion) |
|
text_dataDict["Confidence Score"].append(round(text_score, 2)) |
|
|
|
|
|
face_emotion, face_score = capture_webcam() |
|
face_timestamp = datetime.now().astimezone().strftime(localFormat) |
|
|
|
|
|
face_dataDict["Time"].append(face_timestamp) |
|
face_dataDict["Emotion"].append(face_emotion) |
|
face_dataDict["Confidence Score"].append(face_score) |
|
|
|
|
|
return f"Text: {text_emotion} | Face: {face_emotion}", displayResults() |
|
|
|
def displayResults(): |
|
""" |
|
Creates and returns a matplotlib plot showing emotion trends over time |
|
Returns: |
|
matplotlib.pyplot: Plot object showing emotion analysis results |
|
""" |
|
|
|
plt.figure(figsize=(10, 6)) |
|
|
|
|
|
plt.title("Emotions Detected Through Facial Expressions and Text Over Time") |
|
plt.xlabel("Time") |
|
plt.ylabel("Emotions") |
|
|
|
|
|
plt.plot(face_dataDict["Time"], face_dataDict["Emotion"], marker='o', linestyle='-', label="Facial Emotions") |
|
|
|
|
|
plt.plot(text_dataDict["Time"], text_dataDict["Emotion"], marker='o', linestyle='-', color='red', label="Text Emotions") |
|
|
|
|
|
plt.legend() |
|
plt.xticks(rotation=45) |
|
plt.tight_layout() |
|
|
|
return plt |
|
|
|
|
|
print(Fore.GREEN + "This program will analyze your text for emotions and use your webcame to detect your emotions from your face. Do you give consent? (yes/no): ") |
|
consent = input() |
|
|
|
if (consent.lower() == 'yes'): |
|
|
|
interface = gr.Interface( |
|
fn=emotionAnalysis, |
|
inputs=["text"], |
|
outputs=[ |
|
gr.Text(label="Emotion Results"), |
|
gr.Plot(label="Emotion Timeline") |
|
], |
|
title="Emotion Analysis from Text and Face", |
|
description="Enter text into the textbox. Then, press 'Submit' or 'Enter' to activate the webcam. Wait and see the results." |
|
) |
|
|
|
interface.launch() |