sample-facialrecognition

face recognition sample

1 month after

EN|CN

Developers can deploy the application on the Atlas 200 DK to register a face, predict the face information in the video by using the camera, and compare the predicted face with the registered face to predict the most possible user.

Prerequisites

Before using an open source application, ensure that:

  • MindSpore Studio has been installed.
  • The Atlas 200 DK developer board has been connected to MindSpore Studio, the cross compiler has been installed, the SD card has been prepared, and basic information has been configured.

Software Preparation

Before running the application, obtain the source code package and configure the environment as follows.

  1. Obtain the source code package.

    Download all the code in the sample-facialrecognition repository at https://github.com/Ascend/sample-facialrecognition to any directory on Ubuntu Server where MindSpore Studio is located as the MindSpore Studio installation user, for example, /home/ascend/sample-facialrecognition.

  2. Log in to Ubuntu Server where MindSpore Studio is located as the MindSpore Studio installation user and set the environment variable DDK_HOME.

    vim \~/.bashrc

    Run the following commands to add the environment variables DDK_HOME and LD_LIBRARY_PATH to the last line:

    export DDK_HOME=/home/XXX/tools/che/ddk/ddk

    export LD_LIBRARY_PATH=$DDK_HOME/uihost/lib

    NOTE:

    • XXX indicates the MindSpore Studio installation user, and /home/XXX/tools indicates the default installation path of the DDK.
    • If the environment variables have been added, skip this step.

    Enter :wq! to save and exit.

    Run the following command for the environment variable to take effect:

    source \~/.bashrc

Deployment

  1. Access the root directory where the facial recognition application code is located as the MindSpore Studio installation user, for example, /home/ascend/sample-facialrecognition.

  2. Run the deployment script to prepare the project environment, including compiling and deploying the ascenddk public library, downloading the network model, and configuring Presenter Server. The Presenter Server is used to receive the data sent by the application and display the result through the browser.

    bash deploy.sh host_ip model_mode

    • host_ip: this parameter indicates the IP address of the Atlas 200 DK developer board.

    • model_mode indicates the deployment mode of the model file. The default setting is internet.

      • local: If the Ubuntu system where MindSpore Studio is located is not connected to the network, use the local mode. In this case, download the network model file and the dependent common code library to the /sample-facialrecognition/script directory, by referring to Downloading Network Models and Dependency Code Library.
      • internet: If the Ubuntu system where MindSpore Studio is located is connected to the network, use the Internet mode. In this case, download the model file and dependency code library online.

    Example command:

    bash deploy.sh 192.168.1.2 internet

    • When the message Please choose one to show the presenter in browser(default: 127.0.0.1): is displayed, enter the IP address used for accessing the Presenter Server service in the browser. Generally, the IP address is the IP address for accessing the MindSpore Studio service.
    • When the message Please input a absolute path to storage facial recognition data: is displayed, enter the path for storing face registration data and parsing data in MindSpore Studio. The MindSpore Studio user must have the read and write permissions. If the path does not exist, the script will automatically create it.

    Select the IP address used by the browser to access the Presenter Server service in Current environment valid ip list and enter the path for storing facial recognition data, as shown in Figure 1.

    Figure 1 Project deployment

  3. Start Presenter Server.

    Run the following command to start the Presenter Server program of the facial recognition application in the background:

    python3 presenterserver/presenter_server.py --app facial_recognition &

    NOTE:
    presenter_server.py is located in the presenterserver directory. You can run the python3 presenter_server.py -h or python3 presenter_server.py --help command in this directory to view the usage method of presenter_server.py.

    Figure 2 shows that the presenter_server service is started successfully.

    Figure 2 Starting the Presenter Server process

    Use the URL shown in the preceding figure to log in to Presenter Server (only the Chrome browser is supported). The IP address is that entered in 2 and the default port number is 7009. The following figure indicates that Presenter Server is started successfully.

    Figure 3 Home page

    Figure 4 Example IP Address

    Among them:

    • The IP address of the Atlas 200 DK developer board is 192.168.1.2 (connected in USB mode).
    • The IP address used by the Presenter Server to communicate with the Atlas 200 DK is in the same network segment as the IP address of the Atlas 200 DK on the UI Host server. For example: 192.168.1.223.
    • The following is an example of accessing the IP address of the Presenter Server using a browser: 10.10.0.1, because the Presenter Server and MindSpore Studio are deployed on the same server, the IP address is also the IP address for accessing the MindSpre Studio through the browser.

Running

  1. Run the facial recognition application.

    Run the following command in the sample-facialrecognition directory to start the facial recognition application:

    bash run_facialrecognitionapp.sh host_ip presenter_view_app_name camera_channel_name &

    • host_ip: For the Atlas 200 DK developer board, this parameter indicates the IP address of the developer board.
    • presenter_view_app_name: Indicates App Name displayed on the Presenter Server page, which is user-defined. The value of this parameter must be unique on the Presenter Server page.
    • camera_channel_name: Indicates the channel to which a camera belongs. The value can be Channel-1 or Channel-2. For details, see View the Channel to Which a Camera Belongs of Atlas 200 DK User Guide.

    Example command:

    bash run_facialrecognitionapp.sh 192.168.1.2 video Channel-1 &

  2. Use the URL that is displayed when you start the Presenter Server service to log in to the Presenter Server website (only the Chrome browser is supported).

    Figure 4 shows the Presenter Server page.

    Figure 5 Presenter Server page

    NOTE:

    • The Presenter Server of the facial recognition application supports a maximum of two channels at the same time , each presenter_view_app_name corresponds to a channel.
    • Due to hardware limitations, the maximum frame rate supported by each channel is 20fps, a lower frame rate is automatically used when the network bandwidth is low.
  3. Register a face.

    1. Click the Face Library tab and enter a user name in the Username text box.

    2. Click Browse to upload a face image. Crop the face image based on the ratio of Example Photo.

    3. Click Submit. If the upload fails, you can change the cropping ratio.

  4. Perform facial recognition and comparison.

    On the App List tab page, click video for example in the App Name column. If a face is displayed in the camera and matches the registered face, the name and similarity information of the person are displayed.

Follow-up Operations

  • Stopping the Facial Recognition Application

    The facial recognition application is running continuously after being executed. To stop it, perform the following operation:

    Run the following command in the sample-facialrecognition directory as the MindSpore Studio installation user:

    bash stop_facialrecognitionapp.sh host_ip

    host_ip: For the Atlas 200 DK developer board, this parameter indicates the IP address of the developer board.

    Example command:

    bash stop_facialrecognitionapp.sh 192.168.1.2

  • Stopping the Presenter Server Service

    The Presenter Server service is always in the running state after being started. To stop the Presenter Server service of the facial recognition application, perform the following operations:

    Run the following command to check the process of the Presenter Server service corresponding to the facial recognition application as the MindSpore Studio installation user:

    ps -ef | grep presenter | grep facial_recognition

    [email protected]:~/sample-facialrecognition$ ps -ef | grep presenter | grep facial_recognition
    ascend 22294 20313 22 14:45 pts/24?? 00:00:01 python3 presenterserver/presenter_server.py --app facial_recognition

    In the preceding information, 22294 indicates the process ID of the Presenter Server service corresponding to the facial recognition application.

    To stop the service, run the following command:

    kill -9 22294

Downloading Network Models and Dependency Code Library

  • Downloading network models

    The models used in the Facial Recognition application are converted models that adapt to the Ascend 310 chipset. For details about how to download this kind of models and the original network models, see Table 1. If you have a better model solution, you are welcome to share it at https://github.com/Ascend/models.

    Download the network models files (.om files) to the sample-facialrecognition/script directory.

    Table 1 Models used in Atlas DK open source applications

    Model Name

    Description

    Model Download Path

    Original Network Download Address

    Network model for face detection

    (face_detection.om)

    This model is used in the facial recognition applications.

    It is a network model converted from ResNet0-SSD300 model based on Caffe.

    Download the model from the computer_vision/object_detect/face_detection directory in the https://github.com/Ascend/models/ repository.

    For the version description, see the README.md file in the current directory.

    For details, see the README.md file of the computer_vision/object_detect/face_detection directory in the https://github.com/Ascend/models/ repository.

    Precautions during model conversion:

    During the conversion, a message is displayed indicating that the conversion fails. You only need to select SSDDetectionOutput from the drop-down list box for the last layer and click Retry.

    Network model for marking facial feature points

    (vanillacnn.om)

    This model is used in the facial recognition application.

    It is a network model converted from the VanillaCNN model based on Caffe.

    Download the model from the computer_vision/classification/vanillacnn directory in the https://github.com/Ascend/models/ repository.

    For the version description, see the README.md file in the current directory.

    For details, see the README.md file of the computer_vision/classification/vanillacnn directory in the https://github.com/Ascend/models/ repository.

    Precautions during model conversion:

    For details about the configuration during conversion, see Figure 5.

    Network model for obtaining feature vectors

    (sphereface.om)

    This model is used in the facial recognition application.

    It is a network model converted from the SphereFace model based on Caffe.

    Download the model from the computer_vision/classification/sphereface directory in the https://github.com/Ascend/models/ repository.

    For the version description, see the README.md file in the current directory.

    For details, see the README.md file of the computer_vision/classification/sphereface directory in the https://github.com/Ascend/models/ repository.

    Precautions during model conversion:

    For details about the configuration during conversion, see Figure 6.

    Figure 5 Configuration for the VanillaCNN model during conversion

    • Input Shape: The value of N is 4, indicating that four images are processed each time. The value of this parameter must be the same as the value of batch_size in the corresponding model in the graph.config file.
    • Input Image Preprocess: Set this parameter to Off.

    Figure 6 Configuration for the Sphereface model during conversion

    • Input Shape: The value of N is 8, indicating that eight images are processed each time. The value of this parameter must be the same as the value of batch_size in the corresponding model in the graph.config file.
    • Input Image Format: Enter the image format. In this example, this parameter is set to RGB888_U8.
    • Input Image Size[W|H]: Since the format of the input image is RGB8888_U8, 128 x 16 alignment is not required. Use the width and height required by the model, that is, 96 and 112, respectively.
    • Mean Less[B|G|R]: Indicates the mean value of the image used in the model training. The value can be obtained from the sphereface_model.prototxt file of the model.
    • Multiplying Factor[B|G|R]: Indicates the multiply coefficient of the image used in this model training. The value can be obtained from the sphereface_model.prototxt file of the model (that is, the value of scale).
  • Download the dependent software libraries

    Download the dependent software libraries to the sample-facialrecognition/script directory.

    Table 2 Download the dependent software libraries

    Module Name

    Module Description

    Download Address

    EZDVPP

    Encapsulates the dvpp interface and provides image and video processing capabilities, such as color gamut conversion and image / video conversion

    https://github.com/Ascend/sdk-ezdvpp

    After the download, keep the folder name ezdvpp

    Presenter Agent

    API for interacting with the Presenter Server.

    https://github.com/Ascend/sdk-presenter/tree/master

    >Obtain the presenteragent folder in this path, after the download, keep the folder name presenteragent.

    tornado (5.1.0)

    protobuf (3.5.1)

    numpy (1.14.2)

    Python libraries that Presenter Server depends on..

    You can search for related packages on the Python official website https://pypi.org/ for installation. If you run the pip3 install command to download the file online, you can run the following command to specify the version to be downloaded:

    pip3 install tornado==5.1.0 -i Installation source of the specified library --trusted-host Host name of the installation source