Qt package Baidu face recognition + image recognition

In recent years, the development of AI technology has been in full swing. The wages and salaries are also rising, and the application prospects are also very broad. The face recognition that caught fire last year has blossomed all over the country this year. Before that, the face recognition interface of face + + was encapsulated. This year, I saw Baidu's AI for free, and the effect is getting better and better. The algorithm of living body detection is more like a hanging bomb It needs to pass a picture to judge whether the person in the picture is a remake of the picture or not). Niubi is in a mess. I kneel anyway. Specially spent half a day to encapsulate Baidu face recognition + image recognition for later use. By the way, it is predicted that Baidu AI will be the first or the second in the domestic AI market in the future, and will last for at least ten years.
In order to be compatible with qt4, qtscript is used to parse the received data.

* 1: ID card front information + back information
* 2: recognizable bank card information
* 3: identifiable driving license + driving license information
* 4: face recognition, face comparison and live detection
* 5: can set request address + user key + application key
* 6: just pass in the picture directly, the signal returns, and the millisecond level response is extremely fast
* 7: General Qt4-Qt5,windows linux Embedded linux

Executable Download: https://pan.baidu.com/s/1pzhQL_YMYZyn4hW94e0QsQ 

Core code:

QByteArray FaceBaiDu::getImageData(const QImage &img)
{
    QImage image = img;
    QByteArray imageData;
    QBuffer buffer(&imageData);
    image.save(&buffer, "jpg");
    return imageData.toBase64();
}

QString FaceBaiDu::getImageData2(const QImage &img)
{
    return QString(getImageData(img));
}

QHttpPart FaceBaiDu::dataToHttpPart(const QByteArray &body, const QString &name)
{
    QHttpPart httpPart;
    httpPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant(QString("form-data;name=\"%1\"").arg(name)));
    httpPart.setBody(body);
    return httpPart;
}

void FaceBaiDu::sendData(const QString &url, const QList<QHttpPart> &httpParts)
{
    //Initialize message body
    QHttpMultiPart *httpMultiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);

    //Add message content one by one
    foreach (QHttpPart httpPart, httpParts) {
        httpMultiPart->append(httpPart);
    }

    //Initialize request object
    QNetworkRequest request;
    request.setUrl(QUrl(url));

#ifdef ssl
    //Set the openssl signature configuration, otherwise an error will be reported on ARM
    QSslConfiguration conf = request.sslConfiguration();
    conf.setPeerVerifyMode(QSslSocket::VerifyNone);
#if (QT_VERSION > QT_VERSION_CHECK(5,0,0))
    conf.setProtocol(QSsl::TlsV1_0);
#else
    conf.setProtocol(QSsl::TlsV1);
#endif
    request.setSslConfiguration(conf);
#endif

    //Send request
    QNetworkReply *reply = manager->post(request, httpMultiPart);
    httpMultiPart->setParent(reply);
}

void FaceBaiDu::finished(QNetworkReply *reply)
{
    QString error = reply->errorString();
    if (!error.isEmpty() && error != "Unknown error") {
        emit receiveError(error);
    }

    if (reply->bytesAvailable() > 0 && reply->error() == QNetworkReply::NoError) {
        QString data = reply->readAll();
        reply->deleteLater();

        //Send and receive data signal
        emit receiveData(data);

        //Initialize script engine
        QScriptEngine engine;
        //Building analytic objects
        QScriptValue script = engine.evaluate("value=" + data);

        //Get authentication identifier
        QString token = script.property("access_token").toString();
        if (!token.isEmpty()) {
            tokenFace = token;
            tokenOcr = token;
        }

        //General return result field
        int code = script.property("error_code").toInt32();
        QString msg = script.property("error_msg").toString();
        emit receiveResult(code, msg);

        //Face recognition
        QScriptValue result = script.property("result");
        if (!result.isNull()) {
            //Face recognition
            QScriptValue face_list = result.property("face_list");
            if (face_list.isObject()) {
                checkFaceList(face_list);
            }

            //Face alignment
            QScriptValue score = result.property("score");
            if (!score.isNull()) {
                double value = score.toString().toDouble();
                if (value > 0) {
                    emit receiveFaceCompare(QRect(), QRect(), value);
                } else {
                    emit receiveFaceCompareFail();
                }
            }

            //In vivo detection
            QScriptValue face_liveness = result.property("face_liveness");
            if (!face_liveness.isNull()) {
                double liveness = face_liveness.toString().toDouble();
                if (liveness > 0) {
                    emit receiveLive(liveness);
                }
            }

            //Bank card
            QScriptValue bank_card_number = result.property("bank_card_number");
            if (!bank_card_number.isNull()) {
                QString card_number = bank_card_number.toString();
                QString bank_name = result.property("bank_name").toString();
                if (!card_number.isEmpty()) {
                    emit receiveBankCardInfo(card_number, bank_name);
                }
            }
        }

        //Character recognition
        QScriptValue words_result = script.property("words_result");
        if (!words_result.isNull()) {
            //Front of ID card
            QScriptValue nation = words_result.property("Nation");
            if (nation.isObject()) {
                checkCardFront(words_result);
            }

            //Reverse of ID card
            QScriptValue issuedby = words_result.property("signing and issuing organization");
            if (issuedby.isObject()) {
                checkCardBack(words_result);
            }

            //Driver's license
            QScriptValue license_number = words_result.property("Certificate number");
            if (license_number.isObject()) {
                checkDriverLicense(words_result);
            }

            //Driving license
            QScriptValue model = words_result.property("Brand model");
            if (model.isObject()) {
                checkRVehicleLicense(words_result);
            }
        }
    }
}

void FaceBaiDu::checkFaceList(const QScriptValue &face_list)
{
    QRect face_rectangle;

    //Create iterators to parse specific values one by one
    QScriptValueIterator it(face_list);
    while (it.hasNext()) {
        it.next();

        QString face_token = it.value().property("face_token").toString();
        if (!face_token.isEmpty()) {
            QScriptValue location = it.value().property("location");
            if (location.isObject()) {
                face_rectangle.setX(location.property("left").toInt32());
                face_rectangle.setY(location.property("top").toInt32());
                face_rectangle.setWidth(location.property("width").toInt32());
                face_rectangle.setHeight(location.property("height").toInt32());
            }
        }

        it.next();
        if (face_rectangle.width() > 0) {
            emit receiveFaceRect(face_rectangle);
        } else {
            break;
        }
    }
}

void FaceBaiDu::checkCardFront(const QScriptValue &scriptValue)
{
    QScriptValue name = scriptValue.property("Full name");
    QScriptValue address = scriptValue.property("address");
    QScriptValue birthday = scriptValue.property("Birth");
    QScriptValue number = scriptValue.property("Citizenship number");
    QScriptValue sex = scriptValue.property("Gender");
    QScriptValue nation = scriptValue.property("Nation");

    QString strName = name.property("words").toString();
    QString strAddress = address.property("words").toString();
    QString strBirthday = birthday.property("words").toString();
    QString strNumber = number.property("words").toString();
    QString strSex = sex.property("words").toString();
    QString strNation = nation.property("words").toString();

    emit receiveIDCardInfoFront(strName, strSex, strNumber, strBirthday, strNation, strAddress);
}

void FaceBaiDu::checkCardBack(const QScriptValue &scriptValue)
{
    QScriptValue issuedby = scriptValue.property("signing and issuing organization");
    QScriptValue dateStart = scriptValue.property("Date of issue");
    QScriptValue dateEnd = scriptValue.property("Expiration date");

    QString strIssuedby = issuedby.property("words").toString();
    QString strDataStart = dateStart.property("words").toString();
    QString strDateEnd = dateEnd.property("words").toString();

    QString strDate = QString("%1.%2.%3-%4.%5.%6")
                      .arg(strDataStart.mid(0, 4)).arg(strDataStart.mid(4, 2)).arg(strDataStart.mid(6, 2))
                      .arg(strDateEnd.mid(0, 4)).arg(strDateEnd.mid(4, 2)).arg(strDateEnd.mid(6, 2));
    emit receiveIDCardInfoBack(strDate, strIssuedby);
}

void FaceBaiDu::checkDriverLicense(const QScriptValue &scriptValue)
{
    QScriptValue licenseNumber = scriptValue.property("Certificate number");
    QScriptValue name = scriptValue.property("Full name");
    QScriptValue gender = scriptValue.property("Gender");
    QScriptValue nationality = scriptValue.property("nationality");
    QScriptValue address = scriptValue.property("address");
    QScriptValue birthday = scriptValue.property("Date of birth");
    QScriptValue issueDate = scriptValue.property("Issue Date ");
    QScriptValue classType = scriptValue.property("Quasi driving vehicle");
    QScriptValue validFrom = scriptValue.property("Effective from");
    QScriptValue validFor = scriptValue.property("Validity period");

    QString strLicenseNumber = licenseNumber.property("words").toString();
    QString strName = name.property("words").toString();
    QString strGender = gender.property("words").toString();
    QString strNationality = nationality.property("words").toString();
    QString strAddress = address.property("words").toString();
    QString strBirthday = birthday.property("words").toString();
    QString strIssueDate = issueDate.property("words").toString();
    QString strClassType = classType.property("words").toString();
    QString strValidFrom = validFrom.property("words").toString();
    QString strValidFor = validFor.property("words").toString();

    emit receiveDriverInfo(strValidFrom, strGender, "", strIssueDate, strClassType, strLicenseNumber,
                           strValidFor, strBirthday, "1", strAddress, strNationality, strName);
}

void FaceBaiDu::checkRVehicleLicense(const QScriptValue &scriptValue)
{
    QScriptValue plateNo = scriptValue.property("License plate number");
    QScriptValue vehicleType = scriptValue.property("Vehicle type");
    QScriptValue owner = scriptValue.property("All");
    QScriptValue address = scriptValue.property("address");
    QScriptValue useCharacter = scriptValue.property("Nature of usage");
    QScriptValue model = scriptValue.property("Brand model");
    QScriptValue vin = scriptValue.property("Vehicle identification number");
    QScriptValue engineNo = scriptValue.property("Engine number");
    QScriptValue registerDate = scriptValue.property("Registration date");
    QScriptValue issueDate = scriptValue.property("Date of issue");

    QString strPlateNo = plateNo.property("words").toString();
    QString strCehicleType = vehicleType.property("words").toString();
    QString strOwner = owner.property("words").toString();
    QString strAddress = address.property("words").toString();
    QString strUseCharacter = useCharacter.property("words").toString();
    QString strModel = model.property("words").toString();
    QString strVin = vin.property("words").toString();
    QString strEngineNo = engineNo.property("words").toString();
    QString strRegisterDate = registerDate.property("words").toString();
    QString strIssueDate = issueDate.property("words").toString();

    emit receiveRvehicleInfo(strIssueDate, strCehicleType, "", strVin, strPlateNo, strUseCharacter,
                             strAddress, strOwner, strModel, strRegisterDate, strEngineNo);
}

void FaceBaiDu::sendData(const QString &url, const QString &data, const QString &header)
{
    QNetworkRequest request;
    request.setHeader(QNetworkRequest::ContentTypeHeader, header);
    request.setUrl(QUrl(url));

#ifdef ssl
    //Set the openssl signature configuration, otherwise an error will be reported on ARM
    QSslConfiguration conf = request.sslConfiguration();
    conf.setPeerVerifyMode(QSslSocket::VerifyNone);
#if (QT_VERSION > QT_VERSION_CHECK(5,0,0))
    conf.setProtocol(QSsl::TlsV1_0);
#else
    conf.setProtocol(QSsl::TlsV1);
#endif
    request.setSslConfiguration(conf);
#endif

    manager->post(request, data.toUtf8());
}

void FaceBaiDu::getToken(const QString &client_id, const QString &client_secret)
{
    QStringList list;
    list.append(QString("grant_type=%1").arg("client_credentials"));
    list.append(QString("client_id=%1").arg(client_id));
    list.append(QString("client_secret=%1").arg(client_secret));
    QString data = list.join("&");

    QString url = "https://aip.baidubce.com/oauth/2.0/token";
    sendData(url, data);
}

void FaceBaiDu::detect(const QImage &img)
{
    QStringList list;
    list.append(QString("{\"image\":\"%1\",\"image_type\":\"BASE64\"}").arg(getImageData2(img)));
    QString data = list.join("");

    QString url = QString("https://aip.baidubce.com/rest/2.0/face/v3/detect?access_token=%1").arg(tokenFace);
    sendData(url, data);
}

void FaceBaiDu::compare(const QImage &img1, const QImage &img2)
{
    QString imgData1 = getImageData2(img1);
    QString imgData2 = getImageData2(img2);

    //Change NONE to LOW NORMAL HIGH if live detection is required
    QStringList list;
    list.append("[");
    list.append(QString("{\"image\":\"%1\",\"image_type\":\"BASE64\",\"liveness_control\":\"NONE\"}").arg(imgData1));
    list.append(",");
    list.append(QString("{\"image\":\"%1\",\"image_type\":\"BASE64\",\"liveness_control\":\"NONE\"}").arg(imgData2));
    list.append("]");
    QString data = list.join("");

    QString url = QString("https://aip.baidubce.com/rest/2.0/face/v3/match?access_token=%1").arg(tokenFace);
    sendData(url, data);
}

void FaceBaiDu::live(const QImage &img)
{
    QList<QImage> imgs;
    if (!img.isNull()) {
        imgs << img;
    }

    live(imgs);
}

void FaceBaiDu::live(const QList<QImage> &imgs)
{
    //Remember the last processing time and limit frequent calls
    QDateTime now = QDateTime::currentDateTime();
    if (lastTime.msecsTo(now) < 500) {
        return;
    }

    lastTime = now;

    QStringList list;
    list.append("[");

    int count = imgs.count();
    for (int i = 0; i < count; i++) {
        QString imgData = getImageData2(imgs.at(i));
        list.append(QString("{\"image\":\"%1\",\"image_type\":\"BASE64\"}").arg(imgData));
        if (i < count - 1) {
            list.append(",");
        }
    }

    list.append("]");
    QString data = list.join("");

    QString url = QString("https://aip.baidubce.com/rest/2.0/face/v3/faceverify?access_token=%1").arg(tokenFace);
    sendData(url, data);
}

void FaceBaiDu::idmatch(const QString &idcard, const QString &name)
{
    QStringList list;
    list.append(QString("{\"id_card_num\":\"%1\",\"name\":\"%2\"}").arg(idcard).arg(name));
    QString data = list.join("");

    QString url = QString("https://aip.baidubce.com/rest/2.0/face/v3/person/idmatch?access_token=%1").arg(tokenFace);
    sendData(url, data);
}

void FaceBaiDu::idcard(const QImage &img, bool front)
{
    QList<QHttpPart> httpParts;
    httpParts << dataToHttpPart(front ? "front" : "back", "id_card_side");
    httpParts << dataToHttpPart(getImageData(img), "image");

    QString url = QString("https://aip.baidubce.com/rest/2.0/ocr/v1/idcard?access_token=%1").arg(tokenOcr);
    sendData(url, httpParts);
}

void FaceBaiDu::bankcard(const QImage &img)
{
    QList<QHttpPart> httpParts;
    httpParts << dataToHttpPart(getImageData(img), "image");

    QString url = QString("https://aip.baidubce.com/rest/2.0/ocr/v1/bankcard?access_token=%1").arg(tokenOcr);
    sendData(url, httpParts);
}

void FaceBaiDu::driverLicense(const QImage &img)
{
    QList<QHttpPart> httpParts;
    httpParts << dataToHttpPart(getImageData(img), "image");

    QString url = QString("https://aip.baidubce.com/rest/2.0/ocr/v1/driving_license?access_token=%1").arg(tokenOcr);
    sendData(url, httpParts);
}

void FaceBaiDu::vehicleLicense(const QImage &img)
{
    QList<QHttpPart> httpParts;
    httpParts << dataToHttpPart(getImageData(img), "image");

    QString url = QString("https://aip.baidubce.com/rest/2.0/ocr/v1/vehicle_license?access_token=%1").arg(tokenOcr);
    sendData(url, httpParts);
}

 

Keywords: REST Linux SSL OpenSSL

Added by ludachris on Sat, 04 Jan 2020 21:25:58 +0200