Share emotion detection using opencv python

phammaipainter

New member
### Phát hiện cảm xúc bằng OpenCV Python

Phát hiện cảm xúc là quá trình xác định trạng thái cảm xúc của một người từ biểu hiện trên khuôn mặt của họ.Đây là một nhiệm vụ đầy thách thức, nhưng nó có một loạt các ứng dụng, chẳng hạn như dịch vụ khách hàng, bảo mật và chăm sóc sức khỏe.

Trong hướng dẫn này, chúng tôi sẽ chỉ cho bạn cách thực hiện phát hiện cảm xúc bằng OpenCV và Python.Chúng tôi sẽ sử dụng [bộ dữ liệu FER2013] (https://www.kaggle.com/c/challenges-in-representation-dearning-facial-pression-recignitnhãn.

Chúng tôi sẽ bắt đầu bằng cách tải bộ dữ liệu và xử lý trước các hình ảnh.Sau đó, chúng tôi sẽ đào tạo một mạng lưới thần kinh tích chập (CNN) để phân loại các hình ảnh thành sáu loại cảm xúc: tức giận, ghê tởm, sợ hãi, hạnh phúc, buồn bã và bất ngờ.Cuối cùng, chúng tôi sẽ đánh giá hiệu suất của mô hình của chúng tôi và sử dụng nó để phát hiện cảm xúc trong thời gian thực.

** 1.Tải dữ liệu **

Bộ dữ liệu FER2013 có sẵn trên [KAGGLE] (https://www.kaggle.com/c/challenges-in-representation-dearning-facial-pression-recignition-challenge/data).Để tải xuống bộ dữ liệu, nhấp vào nút ** Tải xuống ** và chọn ** Zip **.Khi bộ dữ liệu đã được tải xuống, giải nén nó và lưu các tệp vào một thư mục trên máy tính của bạn.

Bộ dữ liệu chứa hai thư mục: `Train` và` test`.Thư mục `Train` chứa 28.709 hình ảnh của các khuôn mặt với các nhãn cảm xúc tương ứng.Thư mục `test` chứa 3.589 hình ảnh của khuôn mặt không có nhãn cảm xúc.

Chúng tôi sẽ sử dụng thư mục `Train` để đào tạo mô hình của chúng tôi và thư mục` test` để đánh giá hiệu suất của nó.

** 2.Tiền xử lý hình ảnh **

Trước khi chúng tôi có thể đào tạo mô hình của mình, chúng tôi cần xử lý trước các hình ảnh.Điều này liên quan đến việc thay đổi kích thước hình ảnh thành một kích thước cố định và chuyển đổi chúng thành thang độ xám.

Chúng ta có thể sử dụng mã sau để xử lý trước các hình ảnh:

`` `Python
Nhập CV2
nhập khẩu NUMPY dưới dạng NP

def tiền xử lý_image (hình ảnh):
"" "Tiền xử lý một hình ảnh để phát hiện cảm xúc.

Args:
Hình ảnh: Một mảng numpy đại diện cho một hình ảnh.

Trả lại:
Một mảng numpy đại diện cho hình ảnh được xử lý trước.
"" "

# Thay đổi kích thước hình ảnh thành 224x224 pixel.
Hình ảnh = CV2.Resize (Hình ảnh, (224, 224))

# Chuyển đổi hình ảnh thành thang độ xám.
Hình ảnh = CV2.cvtcolor (hình ảnh, CV2.color_bgr2gray)

# Bình thường hóa các giá trị hình ảnh thành [0, 1].
hình ảnh = hình ảnh / 255.0

trở lại hình ảnh

`` `

Chúng ta có thể áp dụng chức năng này cho tất cả các hình ảnh trong các thư mục `Train` và` test`.

** 3.Đào tạo mô hình **

Bây giờ chúng tôi sẽ đào tạo CNN để phân loại hình ảnh thành sáu loại cảm xúc.Chúng tôi sẽ sử dụng [VGG16] ([1409.1556] Very Deep Convolutional Networks for Large-Scale Image Recognition), đây là một CNN được đào tạo trước đã được hiển thị để thực hiện tốt trên nhiều nhiệm vụ phân loại hình ảnh.

Chúng tôi có thể sử dụng mã sau để đào tạo mô hình của chúng tôi:

`` `Python
Nhập keras
từ keras.models nhập tuần tự
từ keras.layers nhập khẩu dày đặc, bỏ học, dẹt
từ keras.applications.vgg16 Nhập VGG16

Def Train_Model (Train_Data, Train_labels):
"" "Huấn luyện một CNN để phân loại hình ảnh thành sáu loại cảm xúc.

Args:
Train_data: Một mảng hình dạng không có hình (N_Samples, 224, 224, 1) chứa các hình ảnh đào tạo được xử lý trước.
Train_labels: Một mảng hình dạng (n_samples,) chứa các nhãn cảm xúc cho các hình ảnh đào tạo.

Trả lại:
Mô hình CNN được đào tạo.
"" "
=======================================
### Emotion Detection Using OpenCV Python

Emotion detection is the process of identifying a person's emotional state from their facial expression. It is a challenging task, but it has a wide range of applications, such as customer service, security, and healthcare.

In this tutorial, we will show you how to perform emotion detection using OpenCV and Python. We will use the [FER2013 dataset](https://www.kaggle.com/c/challenges-in-representation-learning-facial-expression-recognition-challenge/data), which contains over 35,000 images of faces with corresponding emotion labels.

We will start by loading the dataset and preprocessing the images. Then, we will train a convolutional neural network (CNN) to classify the images into six emotion categories: anger, disgust, fear, happiness, sadness, and surprise. Finally, we will evaluate the performance of our model and use it to detect emotions in real-time.

**1. Loading the dataset**

The FER2013 dataset is available on [Kaggle](https://www.kaggle.com/c/challenges-in-representation-learning-facial-expression-recognition-challenge/data). To download the dataset, click on the **Download** button and select **Zip**. Once the dataset has been downloaded, unzip it and save the files to a folder on your computer.

The dataset contains two folders: `train` and `test`. The `train` folder contains 28,709 images of faces with corresponding emotion labels. The `test` folder contains 3,589 images of faces without emotion labels.

We will use the `train` folder to train our model and the `test` folder to evaluate its performance.

**2. Preprocessing the images**

Before we can train our model, we need to preprocess the images. This involves resizing the images to a fixed size and converting them to grayscale.

We can use the following code to preprocess the images:

```python
import cv2
import numpy as np

def preprocess_image(image):
"""Preprocesses an image for emotion detection.

Args:
image: A numpy array representing an image.

Returns:
A numpy array representing the preprocessed image.
"""

# Resize the image to 224x224 pixels.
image = cv2.resize(image, (224, 224))

# Convert the image to grayscale.
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Normalize the image values to [0, 1].
image = image / 255.0

return image

```

We can apply this function to all of the images in the `train` and `test` folders.

**3. Training the model**

We will now train a CNN to classify the images into six emotion categories. We will use the [VGG16](https://arxiv.org/abs/1409.1556) architecture, which is a pre-trained CNN that has been shown to perform well on a variety of image classification tasks.

We can use the following code to train our model:

```python
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.applications.vgg16 import VGG16

def train_model(train_data, train_labels):
"""Trains a CNN to classify images into six emotion categories.

Args:
train_data: A numpy array of shape (n_samples, 224, 224, 1) containing the preprocessed training images.
train_labels: A numpy array of shape (n_samples,) containing the emotion labels for the training images.

Returns:
The trained CNN model.
"""
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top