AI技术的发展在最近几年如火如荼,工资待遇也是水涨船高,应用的前景也是非常广阔,去年火起来的人脸识别,今年全国遍地开花,之前封装了下face++的人脸识别等接口,今年看了下百度的AI,还免费了,效果也是越来越好,活体检测这个算法更是做的吊炸天(只需要传一张图片就能判断图片中的人是翻拍的照片非活体),牛逼的一塌糊涂,我反正是跪了。特意花了半天时间将百度人脸识别+图像识别封装了下,以便后期使用。顺便预测下,百度AI在未来的国内AI市场中,不是第一就是第二,而且会持续保持至少十年。
为了兼容qt4,特意采用了qtscript解析收到的数据。

* 1:可识别身份证正面信息+背面信息
 * 2:可识别银行卡信息
 * 3:可识别驾驶证+行驶证信息
 * 4:可进行人脸识别,人脸比对,活体检测
 * 5:可设置请求地址+用户密钥+应用密钥
 * 6:直接传入图片即可,信号返回,毫秒级极速响应
 * 7:通用Qt4-Qt5,windows linux 嵌入式linux

可执行文件下载:https://pan.baidu.com/s/1pzhQL_YMYZyn4hW94e0QsQ

核心代码:

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);
#elseconf.setProtocol(QSsl::TlsV1);
#endifrequest.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);
#elseconf.setProtocol(QSsl::TlsV1);
#endifrequest.setSslConfiguration(conf);
#endifmanager->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 HIGHQStringList 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);
}

Qt封装百度人脸识别+图像识别相关推荐

  1. 基于Emgu CV+百度人脸识别,实现视频动态 人脸抓取与识别

    背景 目前AI 处于风口浪尖,作为 公司的CTO,也作为自己的技术专研,开始了AI之旅,在朋友圈中也咨询 一些大牛对于AI 机器学习框架的看法,目前自己的研究方向主要开源的 AI 库,如:Emgu C ...

  2. 【探花交友】阿里云OSS、百度人脸识别

    文章目录 1.完善用户信息 1.1.阿里云OSS 1.2.百度人脸识别 1.完善用户信息 用户在首次登录时需要完善个人信息,包括性别.昵称.生日.城市.头像等.其中,头像数据需要做图片上传,这里采用阿 ...

  3. 【探花交友DAY 03】个人信息完善 阿里云OSS百度人脸识别引入 统一Token和异常处理

    1. 阿里云OSS和百度人脸识别 1.1 需求分析 在登录判断的时候,我们需要根据用户的手机号查询用户是否为新用户,如果为新用户,那么在登录完成后需要跳转到完善用户信息界面,用户需要设置性别,头像和昵 ...

  4. 利用百度人脸识别API和pyqt5实现基于人脸识别的可视化课堂签到管理系统

    利用百度人脸识别API和pyqt5实现基于人脸识别的可视化课堂签到管理系统 一.项目介绍 基于人脸识别的课堂签到管理系统 二.概要设计 工程项目:基于人脸识别的课堂签到管理系统 分为:三个阶段 1.定 ...

  5. android 百度人脸识别,百度人脸识别模块使用分享

    [本文出自APICloud官方论坛,感谢鲍永道的分享.] 首先介绍下百度人脸识别模块(baiduFaceRec): baiduFaceRec模块封装了百度AI人脸识别功能,使用此模块可实现百度人脸检测 ...

  6. flutter 刷脸_GitHub - nnnggel/baidu_face_plugin: 百度人脸识别和活体检测 Flutter 插件(目前版本仅支持 Android)...

    baidu_face_plugin 百度人脸识别和活体检测 Flutter 插件(目前版本仅支持 Android) 使用方式 注册百度开发者账号 前往 百度开发者账号 进行注册. 申请并配置licen ...

  7. php百度人脸识别做登陆,php调用百度人脸识别接口查询数据库人脸信息实现验证登录功能...

    博主在进行鼎食城毕业设计时,需要实现一个人脸识别登录功能,想到可以利用百度的人脸识别接口来完成,于是便去下载了百度的识别SDK,我用的是PHP,需要的的可以去下载其他版本,以下是识别效果: 用户在开始 ...

  8. 调用百度人脸识别API进行人脸对比 C语言

    百度人脸识别api使用是免费的,有人脸对比.人脸搜索.人脸检测与属性分析三个功能,本文写的是人脸对比.这里给出百度人脸对比api的技术文档,请点击网址https://cloud.baidu.com/d ...

  9. 百度人脸识别 人脸识别模型_当我说人脸识别很容易时,他们笑了。 但是可以。...

    百度人脸识别 人脸识别模型 by Tirmidzi Faizal Aflahi 通过提尔米兹·法扎尔·阿弗拉希 当我说人脸识别很容易时,他们笑了. 但是可以. (They laughed when I ...

最新文章

  1. ftp 信息服务器日常维护,Web Ftp Mail服务器的日常管理与维护
  2. 华为「硬」生生把AI搞出暴力美学
  3. linux怎么挂载第二块硬盘分区,linux下挂载第二块已有linux分区的硬盘,要读取从硬盘的文件,应该怎么打开第二块硬盘?...
  4. mysql外键约束查询语句_MySQL数据库 : 查询语句,连接查询及外键约束
  5. 《CMake实践》笔记二:INSTALL/CMAKE_INSTALL_PREFIX
  6. mixin机制 vue_vue mixins组件复用的几种方式(小结)
  7. JAVA面试题之经典题型
  8. Vmware Workstation虚拟机繁忙导致虚拟机系统死机
  9. java计算机毕业设计小区宠物管理系统源码+系统+数据库+lw文档
  10. ios重签工具避免双重认证
  11. sqlserver transact-sql UPDATE tran 用事务处理更新语句
  12. 一般信道容量迭代算法c语言,(信息论编码)信道容量迭代算法
  13. python字典修改键所对应值_详解如何修改python中字典的键和值
  14. BTree与B+Tree图文详解
  15. 库存转换是什么意思_什么是库存?
  16. 每日一课 | 机器学习入门—如何学习机器学习
  17. UML正日薄西山的13个理由
  18. IOT专用IOP平台
  19. Python UnicodeDecodeError: ‘utf-8‘ codec can‘t decode byte 0xbb in position 0: invalid start byte
  20. easyexcel 列宽、行高、样式

热门文章

  1. std::vector实现邻接链表
  2. 共集电极放大电路的分析
  3. ELF文件格式-笔记
  4. 导入/导入 MySQL数据库
  5. Ubuntu 镜像在虚拟机上的安装方法
  6. 千万不要错过,新媒体运营15个宝藏公众号分享
  7. “面对面”app及员工管理系统---第一个java项目总结
  8. 节点nodeType、parentNode、children。下拉菜单。
  9. 行式存储和列式存储的区别
  10. [转]MP4文件格式的解析及分割算法