face Recognition-Qt Calls face++ Interface by Initiating Http Request

Face++ platform can be used for face recognition, face detection, face comparison, face search and so on. The following are the various operations of face using the face++API.
This is the official document address of face++. https://console.faceplusplus.com.cn/documents/4887579
  1. The face++ interface can be invoked either through the network API or through the off-line SDK. This article first introduces how Qt invokes the face++ interface by initiating Http requests. To invoke the face++ interface, you first need to register your account on the official website and get api_key api_secret. Two parameters that must be used to invoke the interface at this time.
  2. Several Key Concepts of Face Recognition
    (1) Face
    Face is a face found in an image in face recognition technology. When a picture is detected, the detected face will be recorded, including the position of the face in the image. Face_token is represented by a system identifier. Note: Multiple face detection for the same image will result in different face_token for the same face.
    (2) Face Set
    FaceSet (FaceSet) is a storage object used to store the detected face. face_token in a FaceSet is not repeated.
    (3) Face Comparison/Face Search
    After a computer detects a face in a picture, the process of identifying a person's identity through a face is called face comparison/face search.
    Face matching refers to collecting new faces and comparing them with the faces of a user with known identity to determine whether the new face belongs to the user with known identity. Face matching requires calling the Compare API.
    Face search is to collect users'new faces, search in the face set of multiple users with known identities, and find out which user the new face belongs to. Search API is called for face search.
  3. Next, we show how to call the face++ interface by calling the interface of face recognition.

    The above parameters are necessary parameters for api calls, which can be referred to in official documents: https://console.faceplusplus.com.cn/documents/4888373

Qt can initiate network requests to call face++ interface through QNetwork Access Manager, QNetwork request, Qurl, QHttpPart, QHttpMultiPart, etc.

The code is as follows:

//Get parameters of type String
QHttpPart Face::setHttpPart(QByteArray body, QVariant value)
{
    QHttpPart part ;
    part.setHeader(QNetworkRequest::ContentDispositionHeader,value);
    part.setBody(body);
    return part;
}
//Get the parameters of the type of image, where the binary form of the image is returned
QHttpPart Face::imageHttpPart(const QString &filename, QVariant value)
{

    QHttpPart imagePart ;
    imagePart.setHeader(QNetworkRequest::ContentTypeHeader,QVariant("image/jpeg"));
    imagePart.setHeader(QNetworkRequest::ContentDispositionHeader,value);
    QImage image(filename);
    QByteArray ba;
    QByteArray hex;
    QBuffer buf(&ba);
    if(buf.open(QIODevice::WriteOnly)){
        image.save(&buf,"jpeg",20);
        hex = ba.toBase64();
        buf.close();
    }else{
        qDebug()<<"buf.error"<<buf.errorString();
    }

    imagePart.setBody(hex);
    return imagePart;

}
//Face detection, filename is the path of the transferred image
void Face::detectFace(const QString &filename)
{
    QTimer timer;//Check and confirm the networking status
    timer.setInterval(10000);//Setting single request time
    timer.setSingleShot(true);
    QNetworkAccessManager *manager = new QNetworkAccessManager;
    if(filename!=""){
        QUrl url("https://api-cn.faceplusplus.com/facepp/v3/detect");
        //Parameters must be passed using QHttpMultiPart
        QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
        QHttpPart apikey = setHttpPart(this->apiKey.toLatin1(),QVariant("form-data; name=\"api_key\""));
        QHttpPart apisecret = setHttpPart(this->apiSecret.toLatin1(),QVariant("form-data; name=\"api_secret\""));
        multiPart->append(apikey);
        multiPart->append(apisecret);
        //If the transferred image is url, the following name is "image_url";
        //If the transferred image is a binary file, then " image_base64" follows the name;
        //If the transferred image is a file, then the name is followed by " image_file".
        QString dataform = "form-data; name=\"image_base64\"";
        QHttpPart imagePart = imageHttpPart(filename.toLatin1(),dataform);
        multiPart->append(imagePart);
        //Initiation of requests
        QNetworkRequest request(url);
        //Requests must be made in the form of Posts
        QNetworkReply *reply = manager->post(request, multiPart);
        // Delete multiPart with reply
        multiPart->setParent(reply); 
        //Time Cycle Mechanism
        QEventLoop loop;
        connect(&timer, &QTimer::timeout, &loop, &QEventLoop::quit);
        connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
        timer.start();
        loop.exec();  //Loop loop
        if (timer.isActive()){
            //QNetworkReply::finished, response completed, not yet timed out
                timer.stop();
             int statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
              if (reply->error() != QNetworkReply::NoError) {
                  this->isError=true;
                  qDebug() << "In detectImage the error is "<<reply->error()<<reply->errorString();
                  return ;
              }else {
                 finishedDetect(reply);
                 this->isError = false;

              }
          }else { //More than 8 seconds
              disconnect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
              reply->abort();
              reply->deleteLater();
              qDebug() << "Networking timeout";

           }
    }
}

This is to invoke the face++ interface by using Qt to initiate network requests.

Welcome to comment!

Keywords: network Qt SDK Attribute

Added by kundan on Sat, 27 Jul 2019 10:37:25 +0300