Home >Backend Development >Python Tutorial >Analyzing Emotion, Age, and Gender Using Serengil/DeepFace Library in Python

Analyzing Emotion, Age, and Gender Using Serengil/DeepFace Library in Python

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-12-31 14:32:11867browse

In this article, we will discuss how to use Serengil's DeepFace library to analyze emotion, age, and gender from facial images. This article will include four main sections: (1) discussion of the libraries used, (2) how to use the libraries, (3) code explanation, and (4) analysis results.

1. Discussion of the DeepFace Library
DeepFace is a Python-based open-source library that offers facial analysis capabilities. This library was developed by Serengil and has become a powerful tool for many facial recognition and facial attribute analysis applications. DeepFace is able to detect and recognize faces, as well as analyze attributes such as emotion, age and gender with high accuracy.

DeepFace uses a machine learning model that has been trained on a large dataset of facial images. This model utilizes deep learning to extract facial features and perform attribute classification with precision. Some of the deep learning models used by DeepFace include VGG-Face, Google FaceNet, OpenFace, and many more. The ability to select and combine these models provides flexibility and reliability in a variety of application scenarios.

2. How to Use the Library
To use DeepFace, we need to install some dependencies first. Here are the detailed steps:

  • Make sure you have Python and pip installed on your system. You can check the installation by running the following command in the terminal:
python --version
pip --version
  • Install the DeepFace library with the following command:
pip install deepface
  • Apart from DeepFace, we also need other libraries such as OpenCV for image processing and NumPy for array manipulation. Install the library with the following command:
pip install opencv-python numpy

Once all the dependencies are installed, we are ready to start writing code to analyze faces.

3. Code Explanation
Here is the code to analyze emotion, age, and gender from facial images. This code consists of several main functions which will be explained in detail.

python
import json
import numpy as np
from deepface import DeepFace
import cv2

# Fungsi untuk menampilkan gambar
def show_image(img_path):
    img = cv2.imread(img_path)
    cv2.imshow("Image", img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

# Fungsi untuk konversi data agar bisa di-serialisasi
def convert_to_serializable(obj):
    if isinstance(obj, np.float32):
        return float(obj)
    raise TypeError(f"Object of type {type(obj)} is not JSON serializable")

# Fungsi untuk analisis wajah
def analyze_face(img_path):
    result = DeepFace.analyze(img_path)
    print("Hasil Analisis:", result)
    return result

# Fungsi utama
def main():
    # Path gambar
    img_path = "images/happy.jpg"

    # Analisis wajah
    analysis_result = analyze_face(img_path)

    # Simpan hasil analisis ke file JSON
    with open('result_analysis.json', 'w') as json_file:
        json.dump(analysis_result, json_file, default=convert_to_serializable)

if __name__ == "__main__":
    main()

Code Explanation
show_image(img_path): This function is used to display images using OpenCV. The image will be displayed in a separate window and wait for input from the user before closing the window.

convert_to_serializable(obj): This function converts a float32 numpy object to float so that it can be serialized to JSON format. This is necessary because numpy data types are not directly compatible with JSON.

analyze_face(img_path): Main function for analyzing faces. This function uses DeepFace to analyze the given face image and returns the analysis results.

main(): This function is the main entry point of the script. This function determines the image path, calls the face analysis function, and saves the analysis results to a JSON file.

img_path: Contains the image you want to analyze, an example of the image I used to analyze

Menganalisis Emosi, Umur, dan Gender Menggunakan Library Serengil/DeepFace di Python

4. Analysis Results
After running the above code using the image, you will get the facial analysis results saved in the result_analysis.json file. These results include information about the emotions, age, and gender of the analyzed faces. Here is an example of the result:

python --version
pip --version

With this information, you can understand more about the facial attributes analyzed using DeepFace. This library is very useful in various applications such as security, marketing, and research. For example, in the marketing field, emotional analysis can help in understanding consumer responses to advertising or products.

In addition, the ability to detect age and gender can be used in personalizing services, such as providing recommendations that match the user's profile. This article shows how powerful and flexible the DeepFace library is for facial analysis purposes.

The above is the detailed content of Analyzing Emotion, Age, and Gender Using Serengil/DeepFace Library in Python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn