Tutorial: Facial Emotion Recognition with Python

·

5 min read

To use the REST API, you need to install any of the frameworks for making HTTP requests to the Luxand.cloud API.

In this example, we will use the requests module because Python Requests is a library that is designed to make working with requests quick and easy. Standard Python HTTP libraries, such as Urllib3, often require significantly more code to perform the same action, which makes it difficult to work with.

To install the library from the command line, you need to enter:

pip install requests

The json module, which is included in the standard Python library, is also required by the API server to process responses.

Do not forget to include the specified libraries in your code.

import requests

import json

Authorization

You need to obtain authorization data for the Luxand.cloud API. To get access, you need a token, which is available in your Luxand.cloud dashboard. Each request to the Luxand.cloud API must be accompanied by an HTTP header with the "token" parameter set to the token value. The serialization of such a header in Python will look like this:

headers = {

   "token": token

}

Image Upload to Database

The Luxand.cloud API can process both web URLs of the analyzed image and work with images pre-uploaded to the Luxand.cloud service.

For full use of all Luxand.cloud functionality, it is more convenient to pre-upload your image database (dataset) that we will be working with. Uploading an image to the Luxand.cloud service is done by a POST request to the URL: api.luxand.cloud/subject/v2

This request must be accompanied by the "name" parameter in the request body with the name of the uploaded file, the file itself is attached to the request as a byte string (multipart/form-data body type). The implementation in Python will look like this:

headers = {

   "token": token

}

data = {

   "name": name

}

files = {

   "photo": open(image_path, "rb")

}

response = requests.post(url, headers=headers, data=data, files=files)

The request must be accompanied by the following parameters:

  • name - The name of the file.

  • image_path - The full path to the file on the system where the code is running.

  • token - The token for accessing the API.

The result of this request must be accepted in order to extract the 'id' field, which is the identifier of the uploaded file in the Luxand.cloud system.

In the event of an upload error (incorrect format or file size too large), the service will return an error with its definition in the 'error' field of the response.

In Python, the response can be processed as follows:

result = json.loads(response.text)

if response.ok:

    print(result['id'])

else:

    print(result['error'])

The Use of Emotion Recognition Functionality

Now everything is ready to use the Luxand.cloud API. Accessing the API is done via the POST method. The request body is formatted similarly to how you would upload an image to a database (discussed earlier). You can learn more about the specific method for emotion recognition here: Recognize Emotions in a Photo. The pipeline for using this method, and others, is standardized:

  1. Method POST

  2. Header

headers = {

   "token": token

}
  1. The request body (multipart/form-data) is the byte contents of the image file in the 'photo' field or a link to the image from a web resource.

The implementation of such a request in Python using the requests module will look like this:

headers = {

   "token": token

}

files = {

   "photo": open(image_path, "rb")

}

response = requests.post(url, headers=headers, files=files)

image_path - The full path to the file in the system where the code is running.

token - The token for accessing the API.

If you are working with a web URL of an image (without first uploading it to the Luxand.cloud database), the body of the request will look like this:

files = {

   "photo": “https://domen/image.jpg”

}

"domen/image.jpg" is the web address of an image. As you can see, in this case all the other instructions remain the same, only instead of the byte content of the image we specify its URL.

In response, the service returns a JSON object:

{

        "status": "success",

        "faces": [

                {

                        "rectangle": {

                                "top": 321,

                                "left": 329,

                                "bottom": 678,

                                "right": 686

                        },

                        "emotions": {

                                "happiness": 0.324,

                                "neutral": 0.676

                        }

                }

        ]

}

Field values are as follows:

  • 'status' - status or result of the operation, example - "success"

  • 'faces' - a list of detected faces in the image, each element of this list defines the following parameters:

    • 'rectangle' - coordinates of the detected face square in parameters - top, left, bottom, and right

    • 'emotions' - detected face emotion in terms of categorical parameters - happiness, neutral … with an indication of the degree of emotion correspondence of each of the found categories from 0 to 1.

In Python, you can process the response as follows:

result = json.loads(response.text)

if response.ok:

    print(result['faces'])

else:

    print(result['error'])

In addition to the basic functionality of the Computer Vision API class, Luxand.cloud provides auxiliary methods that simplify the use of the main functionality. For example, the Luxand.cloud API allows you to upload your own collection of photos of people and their faces. You can create a photo gallery from this database and manage it.

The full code you can find here: Tutorial: Facial Emotion Recognition with Python