sample-videoanalysiscar

Codes for video analysis

1 month after

EN|CN

Developers can deploy the application on the Atlas 200 DK AI acceleration cloud server to decode the local MP4 file or RTSP video streams, detect vehicles in video frames, predict their attributes, generate structured information, and send the structured information to the server for storage and display.

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-videoanalysiscar repository at https://github.com/Ascend/sample-videoanalysiscar to any directory on Ubuntu Server where MindSpore Studio is located as the MindSpore Studio installation user, for example, /home/ascend/sample-videoanalysiscar.

  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 vehicle detection application code is located as the MindSpore Studio installation user, for example, /home/ascend/sample-videoanalysiscar.

  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: For the Atlas 200 DK developer board, this parameter indicates the IP address of the developer board.For the AI acceleration cloud server, this parameter indicates the IP address of the host.

    • model_mode indicates the deployment mode of the model file. The value can be local or internet. 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, you need to have downloaded the model file and the dependent common code library to the sample-videoanalysiscar/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 video analysis data: is displayed, enter the absolute path for storing video analysis data in MindSpore Studio. The MindSpore Studio user must have the read and write permissions. If the path does not exit, the script is automatically created.

    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 video analysis 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 video analysis application in the background:

    python3 presenterserver/presenter_server.py --app video_analysis_car &

    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 7005. 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.
  4. The video structured application can parse local videos and RTSP video streams.

    • To parse a local video, upload the video file to the Host.

      For example, upload the video file car.mp4 to the /home/HwHiAiUser/sample directory on the host.

      NOTE:
      H.264 and H.265 MP4 files are supported. If an MP4 file needs to be edited, you are advised to use FFmpeg. If a video file is edited by other tools, FFmpeg may fail to parse the file.

    • If only RTSP video streams need to be parsed, skip this step.

Running

  1. Run the video analysis application.

    Run the following command in the /home/ascend/sample-videoanalysiscar directory to start the video analysis application:

    bash run_videoanalysiscarapp.sh host_ip presenter_view_appname channel1 [channel2] &

    • host_ip: For the Atlas 200 DK developer board, this parameter indicates the IP address of the developer board.For the AI acceleration cloud server, this parameter indicates the IP address of the host.
    • presenter_view_app_name: Indicates View Name displayed on the Presenter Server page, which is user-defined. The value of this parameter must be unique on the Presenter Server page.
    • channel1: absolute path of a video file on the host
    • channel2: URL of an RTSP video stream

    Example command:

    bash run_videoanalysiscarapp.sh 192.168.1.2 video /home/HwHiAiUser/sample/car.mp4 &

  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). For details, see 3.

    NOTE:
    The Presenter Server of the video analysis application supports the display of a maximum of two presenter_view_app_name at the same time.

    The navigation tree on the left displays the app name and channel name of the video. The large image of the extracted video frame and the detected target small image are displayed in the middle. After you click the small image, the detailed inference result and score are displayed on the right.

    Vehicle attribute detection supports the identification of vehicle brands, vehicle colors, and license plates.

    NOTE:
    In the network model of license plate recognition, the license plate images automatically generated by the program are trained as the training set, instead of using real license plate images. Therefore, this model has low accuracy in identifying real license plate numbers. If a high-accuracy model is required, collect real license plate images as the training set and train them.

Follow-up Operations

  • Stopping the Video Structured Analysis Application

    To stop the video analysis application, perform the following operations:

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

    bash stop_videoanalysiscarapp.sh host_ip

    host_ip: For the Atlas 200 DK developer board, this parameter indicates the IP address of the developer board.For the AI acceleration cloud server, this parameter indicates the IP address of the host.

    Example command:

    bash stop_videoanalysiscarapp.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 video structured analysis application, perform the following operations:

    Run the following command to check the process of the Presenter Server service corresponding to the video structured analysis application as the MindSpore Studio installation user:

    ps -ef | grep presenter | grep video_analysis_car

    [email protected]:~/sample-videoanalysiscar$ ps -ef | grep presenter | grep video_analysis_car
    ascend 3655 20313 0 15:10 pts/24?? 00:00:00 python3 presenterserver/presenter_server.py --app video_analysis_car

    In the preceding information, 3655 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 3655

Downloading Network Models and Dependency Code Library

  • Downloading network model

    The models used in the Ascend DK open source applications 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-videoanalysiscar/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 identifying the vehicle color

    (car_color.om)

    -

    Download the model from the computer_vision/classification/car_color 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/car_color directory in the https://github.com/Ascend/models/ repository.

    Precautions during model conversion:

    The car_color_inference node processes ten pictures at a time. Therefore, the value of N in Input Shaple needs to be changed to 10 during conversion.

    Network model for identifying the vehicle brand (car_type.om)

    It is a GoogLeNet model based on Caffe.

    Download the model from the computer_vision/classification/car_type 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/car_type directory in the https://github.com/Ascend/models/ repository.

    Precautions during model conversion:

    The car_type_inference node processes ten pictures at a time. Therefore, the value of N in Input Shaple needs to be changed to 10 during conversion.

    Network model for identifying the license plate (car_plate_detection.om)

    It is a MobileNet-SSD model based on Caffe.

    Download the model from the computer_vision/object_detect/car_plate_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/car_plate_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 identifying the license plate number (car_plate_recognition.om)

    It is a CNN model based on Caffe.

    Download the model from the computer_vision/classification/car_plate_recognition 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/car_plate_recognition directory in the https://github.com/Ascend/models/ repository.

    Network model for object detection

    (vgg_ssd.om)

    It is an SSD512 model based on Caffe.

    Download the model from the computer_vision/object_detect/vgg_ssd 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/vgg_ssd directory in the https://github.com/Ascend/models/ repository.

  • Download the dependent software libraries

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

    Table 2 Download the dependent software library

    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.

    FFmpeg code

    The current application uses FFmpeg to decapsulate video files.

    The URL for downloading the FFmpeg 4.0 code is https://github.com/FFmpeg/FFmpeg/tree/release/4.0.

    After the download, keep the folder name ffmpeg.

    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