Qt封裝百度人臉識別+圖像識別

AI技術的發展在最近幾年如火如荼,工資待遇也是水漲船高,應用的前景也是很是廣闊,去年火起來的人臉識別,今年全國遍地開花,以前封裝了下face++的人臉識別等接口,今年看了下百度的AI,還免費了,效果也是愈來愈好,活體檢測這個算法更是作的吊炸天(只須要傳一張圖片就能判斷圖片中的人是翻拍的照片非活體),牛逼的一塌糊塗,我反正是跪了。特地花了半天時間將百度人臉識別+圖像識別封裝了下,以便後期使用。順便預測下,百度AI在將來的國內AI市場中,不是第一就是第二,並且會持續保持至少十年。
爲了兼容qt4,特地採用了qtscript解析收到的數據。linux

 * 1:可識別身份證正面信息+背面信息
 * 2:可識別銀行卡信息
 * 3:可識別駕駛證+行駛證信息
 * 4:可進行人臉識別,人臉比對,活體檢測
 * 5:可設置請求地址+用戶密鑰+應用密鑰
 * 6:直接傳入圖片便可,信號返回,毫秒級極速響應
 * 7:通用Qt4-Qt5,windows linux 嵌入式linux算法

可執行文件下載:https://pan.baidu.com/s/1pzhQL_YMYZyn4hW94e0QsQ windows

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)
{
    //初始化消息體
    QHttpMultiPart *httpMultiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);

    //逐個添加消息內容
    foreach (QHttpPart httpPart, httpParts) {
        httpMultiPart->append(httpPart);
    }

    //初始化請求對象
    QNetworkRequest request;
    request.setUrl(QUrl(url));

#ifdef ssl
    //設置openssl簽名配置,不然在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

    //發送請求
    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();

        //發送接收數據信號
        emit receiveData(data);

        //初始化腳本引擎
        QScriptEngine engine;
        //構建解析對象
        QScriptValue script = engine.evaluate("value=" + data);

        //獲取鑑權標識符
        QString token = script.property("access_token").toString();
        if (!token.isEmpty()) {
            tokenFace = token;
            tokenOcr = token;
        }

        //通用返回結果字段
        int code = script.property("error_code").toInt32();
        QString msg = script.property("error_msg").toString();
        emit receiveResult(code, msg);

        //人臉識別部分
        QScriptValue result = script.property("result");
        if (!result.isNull()) {
            //人臉識別
            QScriptValue face_list = result.property("face_list");
            if (face_list.isObject()) {
                checkFaceList(face_list);
            }

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

            //活體檢測
            QScriptValue face_liveness = result.property("face_liveness");
            if (!face_liveness.isNull()) {
                double liveness = face_liveness.toString().toDouble();
                if (liveness > 0) {
                    emit receiveLive(liveness);
                }
            }

            //銀行卡
            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);
                }
            }
        }

        //文字識別部分
        QScriptValue words_result = script.property("words_result");
        if (!words_result.isNull()) {
            //身份證正面
            QScriptValue nation = words_result.property("民族");
            if (nation.isObject()) {
                checkCardFront(words_result);
            }

            //身份證反面
            QScriptValue issuedby = words_result.property("簽發機關");
            if (issuedby.isObject()) {
                checkCardBack(words_result);
            }

            //駕駛證
            QScriptValue license_number = words_result.property("證號");
            if (license_number.isObject()) {
                checkDriverLicense(words_result);
            }

            //行駛證
            QScriptValue model = words_result.property("品牌型號");
            if (model.isObject()) {
                checkRVehicleLicense(words_result);
            }
        }
    }
}

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

    //建立迭代器逐個解析具體值
    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("姓名");
    QScriptValue address = scriptValue.property("住址");
    QScriptValue birthday = scriptValue.property("出生");
    QScriptValue number = scriptValue.property("公民身份號碼");
    QScriptValue sex = scriptValue.property("性別");
    QScriptValue nation = scriptValue.property("民族");

    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("簽發機關");
    QScriptValue dateStart = scriptValue.property("簽發日期");
    QScriptValue dateEnd = scriptValue.property("失效日期");

    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("證號");
    QScriptValue name = scriptValue.property("姓名");
    QScriptValue gender = scriptValue.property("性別");
    QScriptValue nationality = scriptValue.property("國籍");
    QScriptValue address = scriptValue.property("住址");
    QScriptValue birthday = scriptValue.property("出生日期");
    QScriptValue issueDate = scriptValue.property("初次領證日期");
    QScriptValue classType = scriptValue.property("準駕車型");
    QScriptValue validFrom = scriptValue.property("有效起始日期");
    QScriptValue validFor = scriptValue.property("有效期限");

    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("號牌號碼");
    QScriptValue vehicleType = scriptValue.property("車輛類型");
    QScriptValue owner = scriptValue.property("全部人");
    QScriptValue address = scriptValue.property("住址");
    QScriptValue useCharacter = scriptValue.property("使用性質");
    QScriptValue model = scriptValue.property("品牌型號");
    QScriptValue vin = scriptValue.property("車輛識別代號");
    QScriptValue engineNo = scriptValue.property("發動機號碼");
    QScriptValue registerDate = scriptValue.property("註冊日期");
    QScriptValue issueDate = scriptValue.property("發證日期");

    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
    //設置openssl簽名配置,不然在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);

    //若是須要活體檢測則NONE改成LOW NORMAL HIGH
    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)
{
    //記住最後一次處理的時間,限制頻繁的調用
    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);
}
相關文章
相關標籤/搜索