Real-time Emotion Recognition Using GPT with Python and OpenCV šŸ“·

parmarjatin4911@gmail.com - Jan 11 - - Dev Community

xciting News!
šŸš€ Just wrapped up an incredible project - an Emotion Recognition System that's like nothing you've seen before! šŸ¤–āœØ Combining the power of GPT-4 (or the speedy GPT-3.5-turbo) with a deep learning model trained on FER2013, this system detects your facial emotions in real-time from your webcam feed! šŸ˜²šŸ‘€ What's more? It doesn't stop there! šŸŒŸ Based on your emotions, our system generates personalized AI responses, making every interaction uniquely tailored to you. šŸ—£ļøšŸ’¬ Tech stack alert! šŸ› ļø Implemented using TensorFlow, OpenCV, and OpenAI's API, we've harnessed the latest in AI to bring you an immersive and interactive experience. šŸ¤ÆšŸ’» Ready to have your emotions reflected in the digital realm? Dive into the future with our Emotion Recognition System! šŸš€šŸ”®

Donwload Dataset:

Kaggle
Place in the same folder as the existing code.

Implementation code

Now, let's dive into the implementation. Real-time Emotion Recognition Using GPT with Python and OpenCV implementation using Python and the numpy pandas tensorflow opencv-python openai scikit-learn tqdm library. This assumes that you have prior knowledge of setting up Python and installing the necessary libraries.

CODE

import os
import time
import numpy as np
import pandas as pd
import tensorflow as tf
from tqdm.keras import TqdmCallback
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout
from sklearn.model_selection import train_test_split
import cv2
import openai
import time

MODEL_FILENAME = 'emotion_recognition_model.h5'
openai.api_key = ""

def load_data():
data = pd.read_csv('fer2013.csv')
pixels = data['pixels'].apply(lambda x: np.fromstring(x, sep=' '))
x = np.vstack(pixels.values)
x = x.reshape(-1, 48, 48, 1)
x = x.astype('float32') / 255.0
y = pd.get_dummies(data['emotion']).values
return x, y

def create_model():
model = Sequential([
Conv2D(32, (3, 3), activation='relu', input_shape=(48, 48, 1)),
MaxPooling2D((2, 2)),
Conv2D(64, (3, 3), activation='relu'),
MaxPooling2D((2, 2)),
Flatten(),
Dense(128, activation='relu'),
Dropout(0.5),
Dense(7, activation='softmax')
])
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
return model

def train_model(model, x, y):
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2)
start_time = time.time()
model.fit(
x_train, y_train, epochs=20, batch_size=64,
validation_data=(x_test, y_test),
callbacks=[TqdmCallback(verbose=1)],
verbose=0
)
end_time = time.time()
print(f'Training time: {end_time - start_time:.2f} seconds')
model.save(MODEL_FILENAME)

def emotion_recognition():
model = tf.keras.models.load_model(MODEL_FILENAME)
emotion_labels = ['Angry', 'Disgust', 'Fear', 'Happy', 'Sad', 'Surprise', 'Neutral']
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FPS, 30) # Set the frame rate to 30 FPS

cooldown_time = 5 # Time (in seconds) between GPT-4/GPT-3.5-turbo API calls
display_duration = 20 # Time (in seconds) to display the AI's response
last_call_time = 0 # Last time the GPT-4/GPT-3.5-turbo API was called
response_display_start_time = 0 # Time the current response started being displayed
chatbot_response = ""

while True:
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5)

emotion_label = None
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
face = gray[y:y+h, x:x+w]
face = cv2.resize(face, (48, 48))
face = face.astype('float32') / 255.0
face = np.expand_dims(face, 0)
face = np.expand_dims(face, -1)
emotion = model.predict(face)
emotion_label = emotion_labels[np.argmax(emotion)]

current_time = time.time()
if current_time - last_call_time > cooldown_time and current_time - response_display_start_time > display_duration:

Get chatbot response

Get chatbot response

Get chatbot response

chatbot_response = generate_chatbot_response(f"what you see my face {emotion_label}.", "You know my emotion just by seeing my face, say something about my expression.")
chatbot_response = f'AI: {chatbot_response}'

Update last_call_time and response_display_start_time

last_call_time = current_time
response_display_start_time = current_time

Display chatbot response

cv2.putText(frame, chatbot_response, (10, 30), cv2.FONT_HERSHEY_SIMPLEX,1, (255, 0, 0), 4)

if emotion_label:
cv2.putText(frame, emotion_label, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 0, 255), 2)

cv2.imshow('Emotion Recognition', frame)

if cv2.waitKey(1) & 0xFF == ord('q'):
break

cap.release()
cv2.destroyAllWindows()

def main():
if not os.path.exists(MODEL_FILENAME):
print("Loading dataset...")
x, y = load_data()

print("Creating model...")
model = create_model()

print("Training model...")
train_model(model, x, y)
print("Model trained and saved.")

print("Running emotion recognition...")
emotion_recognition()

def generate_chatbot_response(prompt, system_content, retries=30, backoff_factor=2):
for retry_count in range(retries):
try:
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[{"role": "system", "content": system_content}, {"role": "user", "content": prompt}],
max_tokens=1200,
n=1,
stop=None,
temperature=1,
)
return response.choices[0].message['content'].strip()
except openai.error.OpenAIError as e:
if 'Model overloaded' in str(e) and retry_count < retries - 1:
sleep_time = backoff_factor ** (retry_count + 1)
time.sleep(sleep_time)
else:
raise

if name == 'main':
main()

Save the above script as a Python file and key api key from openai (e.g., main.py ) and run it on your os system. The script upload iamge from the machine, performs analyze images , and displays the live feedback.

Conclusion

Exciting breakthrough! Our Emotion Recognition System blends GPT-4, FER2013-trained deep learning, and real-time webcam feeds to generate personalized AI responses. Implemented with TensorFlow, OpenCV, and OpenAI's API, it's a leap forward in interactive AI. šŸš€šŸ’¬

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player