C++ implements face recognition (Baidu cloud platform)

C++ implements face recognition (Baidu cloud platform)

Project resource download

Project idea: opencv collects face photos, sends the photos to Baidu Smart Cloud Platform, and Baidu Cloud Platform compares the data with the face database and returns the result.

1. Project environment


Ubuntu 64 20.0.4

opencv 4.2.0

2. Environment configuration

1. Install opencv

sudo apt-get install libopencv-dev

The installation needs to wait for a while, some systems may need to install dependencies, just follow the prompts to install.

After the installation is complete, you can use the command

dpkg -s libopencv-dev

The installation needs to wait for a while, some systems may need to install dependencies, just follow the prompts to install.

After the installation is complete, you can use the command

dpkg -s libopencv-dev
1

View the status and version of the installation. There are differences between different system versions. Here I am opencv4.2.0. The specific version is related to the software source used.

After the installation is complete, you can test it first.

/*
test.cpp
*/

#include<iostream>
#include "opencv4/opencv2/opencv.hpp"

using namespace std;
using namespace cv;

int main()
{
    // 打开默认摄像头0
    VideoCapture cap = 0;
    // 如果摄像头开启出现异常,则退出程序
    if(!cap.isOpened())
    {
        cout << "Camera open failed!" << endl;
        return -1;
    }
    cout << "Camera open success!" << endl;

    // 实例化人脸级联分类器
    cv::CascadeClassifier Classifier("/usr/share/opencv4/haarcascades/haarcascade_frontalface_alt2.xml");

    // 照片容器容器 Mat为 cv 内定义类型
    Mat ColorFace; // 用于展示,彩色展示效果较好
    Mat GrayFace;  // 用于查找人脸,灰色查找降低计算量
    
    // 人脸列表 Rect类型,为人脸的矩形框坐标
    vector<Rect> AllFace;

    // 存储从图片中截取的人脸
    Mat MatFace;

    // 存储截图
    vector<uchar> JpgFace;
    
    // 循环采集图片并展示,达到视频效果
    while(true)
    {
        // 拍照
        cap >> ColorFace;

        // 将彩色照片转为灰度,降低计算量
        cvtColor(ColorFace, GrayFace,cv::COLOR_BGR2GRAY);

        // 将灰度照片均衡化,便于从背景中分离人脸
        equalizeHist(GrayFace, GrayFace);

        // 从灰度照片中检测人脸,并将检测到的人脸存入人脸列表中
        Classifier.detectMultiScale(GrayFace, AllFace);
       
        // 判断是否检测到人脸,没有人脸则列表为空
        if(AllFace.size()==0){
            cout << "no face" << endl;
        }else{
            // 框出检测到的人脸,第一个参数为要框出的图片,第二个参数为人脸,最后一个是框的颜色
            rectangle(ColorFace, AllFace[0], Scalar(255, 255, 0));
        }
        
        // 显示图片,第一个参数为窗口名称,第二个为展示的图片
        imshow("test", ColorFace);
        
        // 等待40ms,相当于25帧(24帧是人眼连贯的最小帧率)
        waitKey(40);
    }

    return 0;
}

Execute the compile command

g++ test.cpp -o test

At this time, we will find that it has reported an error, and the error is as follows

This happened because the library opencv4/opencv2/opencv.hpp was introduced into our project, but it was not compiled.

The solution is as follows:

① Libraries used for dynamic linking at compile time

g++ test.cpp -o test -lopencv_highgui

Unfortunately, I did not succeed in using this method. After consulting a lot of information, I found that the .so file was not linked correctly, which is the problem of environment variables, so I have a second solution.

Secondly, the first method needs to add -l dynamic link compilation every time the function under the new opencv is used, which is too cumbersome and makes the compilation command too complicated. The second method also solves this problem.

②Environment variable configuration

Links: Reference Articles

Execute the following statement

pkg-config --cflags opencv

returns the following results

Package opencv was not found in the pkg-config search path.
Perhaps you should add the directory containing `opencv.pc'
to the PKG_CONFIG_PATH environment variable
No package 'opencv' found

The reason is the lack of opencv.pc file, we need to create this file and import it into the environment variable, the specific operation is as follows:

First create the opencv.pc file, pay attention to the directory information

cd /usr/local/lib
sudo mkdir pkgconfig
cd pkgconfig
sudo touch opencv.pc

Then add the following to the file

sudo nano opencv.pc
prefix=/usr/local
exec_prefix=${prefix}
includedir=${prefix}/include
libdir=${exec_prefix}/lib

Name: opencv
Description: The opencv library
Version:4.0.1
Cflags: -I${includedir}/opencv4
Libs: -L${libdir} -lopencv_shape -lopencv_stitching -lopencv_objdetect -lopencv_superres -lopencv_videostab -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_video -lopencv_photo -lopencv_ml -lopencv_imgproc -lopencv_flann  -lopencv_core
~                                               

This is mainly the configuration information of opencv. We can see that there are all the link libraries we will use in Libs.

Save and exit file editing: ctrl + o ctrl + x.

Import the file into environment variables

export  PKG_CONFIG_PATH=/usr/local/lib/pkgconfig

This configures the environment variables of opencv.

execute the command again

pkg-config --cflags --libs opencv

The returned result is as follows, that is, the configuration is successful

-I/usr/local/include/opencv4 -L/usr/local/lib \
-lopencv_shape -lopencv_stitching -lopencv_objdetect \
-lopencv_superres -lopencv_videostab -lopencv_calib3d \
-lopencv_features2d -lopencv_highgui -lopencv_videoio \
-lopencv_imgcodecs -lopencv_video -lopencv_photo -lopencv_ml \
-lopencv_imgproc -lopencv_flann -lopencv_core

Return to the project folder and execute the following command to compile

sudo g++ test.cpp -o test `pkg-config --cflags --libs opencv`

Execute ls-l to find that the executable file test has been generated

Run example:

./test

It was found that it was reported again! ! !

Don't worry, this is because our virtual machine is not connected to the camera, we just need to follow the steps below.

Click on the virtual machine --> removable device --> IMC Networks Integrated Camera --> connect (disconnect from the host).

Execute ./test again

You can see that the program runs successfully, and you can end the program by pressing ctrl+c in the terminal.

2. Baidu cloud platform configuration

First of all, you need to go to Baidu Smart Cloud Platform to create a face search application. For the specific usage method of Baidu Cloud Platform, you can go to Baidu's tutorial, which is very simple.

①Log in to Baidu Smart Cloud

Link: Baidu Smart Cloud Platform

②Find face recognition cloud service

Click to use immediately after entering

③Create application

After successful creation, enter the management application

Click to view the face database and create a new user group

Just create a new user and upload a photo. Note that the uploaded user photo must be a frontal photo with clear facial contours and facial features.

So far, our face database on Baidu Smart Cloud has been created successfully.

3. Configure the SDK

In order to be able to apply Baidu Smart Cloud, we also need to download the corresponding SDK to configure the local environment.

Link: C++ SDK Documentation

Link: C++ SDK download address

Because our project is running in Ubuntu, it is recommended to download directly in Ubuntu.

After the download is complete, unzip it into our project file

Enter the project directory and execute the following command

cd aip-cpp-sdk-0.8.1
sudomv * ../

The SDK has been imported into the project, and some dependent packages need to be installed, refer to the SDK documentation above.

The above is the dependency package we need to download and install, execute the following command

sudoapt-getinstall libcurl4-openssl-dev
sudoapt-getinstall openssl
sudoapt-getinstall libjsoncpp-dev

Because the project introduces these libraries, and these libraries are C++11 standard, the following link should be added at the end when compiling

-lcurl -lcrypto -ljsoncpp  -std=c++11

After everything is installed, our project environment is configured.

But there are still some things we need to change.

Enter the base folder extracted before, open http.h, and change #include <json/json.h> on line 23 to #include <jsoncpp/json/json.h>

Also line 21 of base.h should make the same change.

3. Coding

The following is the complete code, with detailed notes.

#include<iostream>
#include "opencv4/opencv2/opencv.hpp"
#include "face.h"

using namespace std;
using namespace cv;
using namespace aip;

int main()
{
    // Json容器,用于存储云端发送回来的数据
    Json::Value result;

    // 打开默认摄像头0
    VideoCapture cap = 0;
    // 如果摄像头开启出现异常,则退出程序
    if(!cap.isOpened())
    {
        cout << "Camera open failed!" << endl;
        return -1;
    }
    cout << "Camera open success!" << endl;

    // 实例化人脸级联分类器
    cv::CascadeClassifier Classifier("/usr/share/opencv4/haarcascades/haarcascade_frontalface_alt2.xml");

    // 照片容器容器 Mat为 cv 内定义类型
    Mat ColorFace;
    Mat GrayFace;
    
    // 人脸列表 Rect类型,为人脸的矩形框坐标
    vector<Rect> AllFace;

    // 存储从图片中截取的人脸
    Mat MatFace;

    // 存储截图
    vector<uchar> JpgFace;

    // 连接百度智能云
    std::string app_id = "更换为你的 app_id";
    std::string api_key = "更换为你的 api_key";
    std::string secret_key = "更换为你的 secret_key";
    // 创建人脸识别云服务客户端
    aip::Face client(app_id, api_key, secret_key);

    // 因为百度智能云要求发送的人脸为 Base64 格式,所以声明一个 string 类型的容器存储它
    string Base64Face;

    // 循环采集图片并展示,达到视频效果
    while(true)
    {
        // 拍照
        cap >> ColorFace;

        // 将照片转化为灰度格式,降低计算量
        cvtColor(ColorFace, GrayFace,cv::COLOR_BGR2GRAY);

        // 将灰度照片均衡化,便于从背景中分离人脸
        equalizeHist(GrayFace, GrayFace);

        // 从灰度照片中检测人脸,并将检测到的人脸存入人脸列表中
        Classifier.detectMultiScale(GrayFace, AllFace);
        
        // 判断是否检测到人脸
        if(AllFace.size()==0){
            cout << "no face" << endl;
        }else{
            // 截取检测到的第一张人脸
            MatFace = GrayFace(AllFace[0]);
            // 将截取的无格式人脸转为 .jpg 格式
            imencode(".jpg", MatFace, JpgFace);
            // 将图片转为 Base64 格式
            Base64Face = base64_encode((char *)JpgFace.data(), JpgFace.size());
            // 从人脸库中搜索人脸
            result = client.search(Base64Face, "BASE64", "test1", aip::null);
            // 解析返回结果
            if(!result["result"].isNull()){
                // 当返回结果也检测到人脸时,将彩色照片中的人脸框起来
                rectangle(ColorFace, AllFace[0], Scalar(255, 255, 0));
                // 判断人脸相似度,当相似度大于80%,判定为同一人,输出其编号(人脸库中的编号),否则输出 unknown people
                if(result["result"]["user_list"][0]["score"].asInt() >= 80){
                    cout << "find " << result["result"]["user_list"][0]["user_id"] << endl;
                }else{
                    cout<< "unkown people" << endl;
                }
            }else{
                cout << "no face" << endl;
            }
        }
        // 展示图片
        imshow("test", ColorFace);

        waitKey(40);
    }

    return 0;
}

Compile with the following command

sudo g++ test.cpp -o test`pkg-config --cflags --libs opencv` -lcurl -lcrypto -ljsonc

run

./test

Original link: https://blog.csdn.net/X_xs_mxt/article/details/127710672#comments_25689722

Thanks to the author for allowing it to be reposted

Guess you like

Origin blog.csdn.net/qiuweichen1215/article/details/129716082