One, foreword

Recently, I need to develop an application related to face recognition under Linux platform, and use the face recognition SDK of Hongsoft. I have used it on Windows platform before, and I feel good. There is a Demo in the SDK to see the effect quickly. No Demo was found in the Linux SDK, so I thought of porting the Windows Demo to Linux. This article documents the porting process and Ubuntu 20.04 is used for Linux (using the VMware Workstation 15 Player virtual machine).

2. Configure dependencies

2.1 ArcFace SDK

Download the face recognition SDK 3.1 Linux value-added version from the official website of Hongsoft, decompress it to the appropriate directory, and obtain the APP_ID, SDK_KEY and ACTIVE_KEY from the official website, and write them into the configuration file to activate the SDK.

2.2 OpenCV

Go to OpenCV official website to download the source code, I use version 3.4.9. Installation in Linux GCC 9.3.0 (Ubuntu 20.04)

Sudo apt update sudo apt install build-essential sudo apt install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-devcd<OpenCV source directory > mkdir buildcdBuild cmake-dcmake_build_type = release-dcmake_install_prefix =< custom directory >.. make -j3You can compile with a core number of 1 threads
sudo make install
Copy the code

2.3 Qt

Qt uses version 5.14.2.

Iii. Project documents

3.1. Pro file

The original Windows Demo uses Visual Studio 2015. Under Linux, I use Qt Creator for development, so I need to write a.pro file, including the following aspects:

  • Qt module used
  • The name of the compiled program
  • Header files, source files, and resource files used
  • The header file and name of the dependent library

The path of the dependent library in the. Pro file varies from person to person. Therefore, you need to configure the path based on your actual situation.

For more details, see the source code provided at the end of the article.

3.2 File Coding

The original source file uses GBK encoding, which needs to be converted to UTF-8 encoding. Save the following command to the convert.sh file and grant executable permissions to chmod u+x convert.sh.

#! /bin/bash
for i in "$@"; do
    desc=$(file "$i")
    if $(echo $desc | grep -i "UTF-8 Unicode" > /dev/null); then
        if $(echo $desc | grep -i "(with BOM)" > /dev/null); then
            echo "Remove UTF-8 BOM: " $i
            sed -i "1s/^\xef\xbb\xbf//" "$i"
        fi
    elif $(echo $desc | grep -i "ISO-8859" > /dev/null); then
        echo     "GBK --> UTF-8 : " $i
        temp=temp.txt
        iconv -f gbk -t utf-8 -o "$temp" "$i"
        mv "$temp" "$i"
    fi
done
Copy the code

Run find in the source root directory. -type f \ (-name “* h” – o -name “*. CPP \”) | xargs – I {}. / the convert. Sh “{}” will all the files from the GBK encoding to utf-8, And remove the EXISTING UTF-8 file BOM header.

4. Code modification

At this point, you can open the project using Qt Creator, but there are some issues in the code, both because the original code uses some Windows platform-specific apis and because some of the code is incompatible with Linux. Remove windows-specific dependencies until they are compiled, then add necessary dependencies, and finally solve compatibility problems.

4.1 Errors are reported until compilation passes

You can use the following methods to solve compilation errors:

  • Delete the headers in utils. CPP, the GUID macro, the body of the listDevices function, and the UTF8_To_string and string_To_UTF8 functions.
  • Will contain allqDebugInstead ofQDebug.
  • willSleep(milli)Instead ofstd::this_thread::sleep_for(std::chrono::milliseconds(milli)).
  • Delete MSVC link library compiler instruction#pragma comment ....
  • Migrate the original OpenCV 2 interface to the current OpenCV 3.
    • IplImage is converted to CV ::Mat bycv::Mat mat(ipl, false)tocv::Mat mat = cv::cvarrToMat(ipl).
    • CV ::Mat to IplImage byIplImage(mat)tocvIplImage(mat)In the original code, CV ::Mat has an address after converting to IplImage. It is not safe to take the address of an rvalue. It is necessary to use a variable to store the converted value and then take the address of this variable.
    • When cvRectangle is calledCV_RGBtocvScalar.
    • usecv::cvtColorYou need to include additional header filesopencv2/imgproc.hpp.
    • usecv::VideoCaptureYou need to include additional header filesopencv2/videoio.hpp
  • willstrcpy_stostrncpy, only changes in the position of parameters.
  • willTRUEInstead oftrueThat will beFALSEInstead offalse.

After fixing the compile error and ignoring the warning, it’s time to add some necessary dependencies that you just removed and resolve compatibility issues.

Because of environmental differences, errors can occur in inconsistent order, but basically one of the errors mentioned above.

4.2 Re-implement the function of obtaining the camera list

The original Windows Demo used windows-specific dshow to find the camera, here we try to open it directly with CV ::VideoCapture to get the index of the camera:

auto list = std: :vector<int> ();for (auto i = 0; i ! =10; ++i)
{
    auto cap = cv::VideoCapture(i);
    if (cap.isOpened()) { list.emplace_back(i); }
    cap.release();
}
Copy the code

Demo can have only one RGB camera on or one RGB camera and one IR camera on at the same time. The original code saves the name of the camera, which is only used to count the number of cameras. The specific camera is configured through the settings.ini file. After changing the way of detecting the number of cameras present, incidentally changing the logic of opening the camera, only one camera is considered to be open normal camera only. In the settings.ini file, configure the index of the two types of cameras. If the index is -1, the smaller index is regarded as a common camera, and the larger index is regarded as an infrared camera. If the index is different from the actual situation, you can manually specify the camera index.

The settings.ini file will be explained more later when you run the Demo.

4.3 Fix the compatibility problem when the file selection box fails to be displayed

Under the Ubuntu 20.04 Qt QFileDialog: : getOpenFileName and QFileDialog: : getExistingDirectory has some problems, when open the interface card’s death, By the last parameter is set to QFileDialog: : DontUseNativeDialog can solve this problem.

5. Run Demo

5.1 Preview the Interface

5.2 configuration

  1. Configuration files are already packaged with the source code and need to be moved to the same directory as the executable at runtime.
  2. Enter the APP_ID, SDK_KEY, and ACTIVE_KEY obtained from the official website in the configuration file.
  3. Compile and run.

Download the source code

Based on the official Windows Qt Demo modified source code