Skip to main content

Emotion detection system

Emotion detection system using python 


An emotion detection system using Python and a pre-trained model like fer (Facial Emotion Recognition) or DeepFace. This code will use the DeepFace library, which supports multiple models for facial emotion recognition.

Step 1: Install Required Libraries

pip install deepface opencv-python

Step 2: Write the Emotion Detection Code

import cv2
from deepface import DeepFace # Load the pre-trained DeepFace model for emotion detection def detect_emotion(image_path): # Analyze the image to detect emotions analysis = DeepFace.analyze(img_path=image_path, actions=['emotion']) # Get the dominant emotion dominant_emotion = analysis['dominant_emotion'] return dominant_emotion # Capture video from the webcam def emotion_from_webcam(): cap = cv2.VideoCapture(0) while True: ret, frame = cap.read() # Save the frame as a temporary image cv2.imwrite("temp.jpg", frame) # Detect emotion in the current frame emotion = detect_emotion("temp.jpg") # Display the detected emotion on the frame cv2.putText(frame, emotion, (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2, cv2.LINE_AA) # Show the frame cv2.imshow('Emotion Detection', frame) # Break the loop if 'q' is pressed if cv2.waitKey(1) & 0xFF == ord('q'): break # Release the video capture object and close all OpenCV windows cap.release() cv2.destroyAllWindows() # Run the emotion detection from webcam emotion_from_webcam()

How the Code Works:

  1. DeepFace: The DeepFace.analyze function is used to analyze the image and detect emotions. It supports several models, but the default model is VGG-Face.
  2. Webcam Capture: The code captures video from the webcam and detects emotions in real-time.
  3. Display Emotion: The detected emotion is overlaid on the video frame.

Step 3: Run the Code

When you run the code, it will open a webcam feed, detect emotions in real-time, and display the dominant emotion on the video stream. Press q to exit the webcam feed.

Notes:

  • The emotion detection is based on facial expressions, so ensure your face is visible in the webcam for accurate results.
  • You can replace "temp.jpg" with frame directly if you prefer to analyze the frame without saving it as an image file. However, the current approach ensures that the frame is properly analyzed in case of any format issues.

Comments

Popular posts from this blog

Website hosting on EC2 instances AWS Terminal

Website hosting on EC2 instances  In the world of web development and server management, Apache HTTP Server, commonly known as Apache, stands as one of the most popular and powerful web servers. Often, developers and administrators require custom images with Apache server configurations for various purposes, such as deploying standardized environments or distributing applications. In this guide, we'll walk through the process of creating a custom image with Apache server (httpd) installed on an AWS terminal.   Setting Up AWS Environment: Firstly, ensure you have an AWS account and access to the AWS Management Console. Once logged in: 1. Launch an EC2 Instance: Navigate to EC2 service and launch a new instance. Choose an appropriate Amazon Machine Image (AMI) based on your requirements. It's recommended to select a base Linux distribution such as Amazon Linux. 2. Connect to the Instance: After launching the instance, connect to it using SSH or AWS Systems Manager Session Manage...

An Introduction to LangChain: Simplifying Language Model Applications

  An Introduction to LangChain: Simplifying Language Model Applications LangChain is a powerful framework designed to streamline the development and deployment of applications that leverage language models. As the capabilities of language models continue to expand, LangChain offers a unified interface and a set of tools that make it easier for developers to build complex applications, manage workflows, and integrate with various data sources. Let's explore what LangChain is, its key features, and how it can be used to create sophisticated language model-driven applications. What is LangChain? LangChain is an open-source framework that abstracts the complexities of working with large language models (LLMs) and provides a consistent, modular approach to application development. It is particularly well-suited for tasks that involve natural language processing (NLP), such as chatbots, data analysis, content generation, and more. By providing a cohesive set of tools and components, Lang...

"Mastering Computer Vision: An In-Depth Exploration of OpenCV"

                                     OPEN CV  What is OPEN CV?   OpenCV  is a huge open-source library for computer vision, machine learning, and image processing. OpenCV supports a wide variety of programming languages like Python, C++, Java, etc. It can process images and videos to identify objects, faces, or even the handwriting of a human. When it is integrated with various libraries, such as  Numpy   which is a highly optimized library for numerical operations, then the number of weapons increases in your Arsenal i.e. whatever operations one can do in Numpy can be combined with OpenCV. With its easy-to-use interface and robust features, OpenCV has become the favorite of data scientists and computer vision engineers. Whether you’re looking to track objects in a video stream, build a face recognition system, or edit images creatively, OpenCV Python implementation is...