使用Qt请求百度ai人脸识别接口方式
177******01 发布于2019-05-16 浏览:2293 回复:1
0
收藏

1、首先获取token (token,30天有效期,到期需要重新申请,也可以每次使用时申请)
curl -i -k 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=PqiVM5lvB337w6CeBkua0B3dRw&client_secret=zidS239ieGM5vfsEk2OYulCkR43fh3nIt'
2、Qt中调用方法:

void CameraFilter::checkImage()
{
QHttpMultiPart *multiPart = newQHttpMultiPart(QHttpMultiPart::FormDataType);
QHttpPart max_face_num;
max_face_num.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"max_face_num\""));
max_face_num.setBody("1");

QHttpPart face_fields;
face_fields.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"face_fields\""));
face_fields.setBody("age,gender,qualities");

QHttpPart image_data;
image_data.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"image\""));
image_data.setBody("图像的base64编码数据");

multiPart->append(max_face_num);
multiPart->append(face_fields);
multiPart->append(image_data);

QUrl url("https://aip.baidubce.com/rest/2.0/face/v1/detect?access_token=24.cbc92b55c4e10c057065857526cd849a.2592000.1525230249.282335-11029510");
QNetworkRequest request(url);

networkManager->post(request, multiPart);

}

 

void CameraFilter::replyFinished(QNetworkReply *reply) //当回复结束后
{

QTextCodec *codec = QTextCodec::codecForName("utf8");

//使用utf8编码,这样才可以显示中文

QString all = codec->toUnicode(reply->readAll());
qDebug() << all << endl;

reply->deleteLater(); //最后要释放reply对象

}

收藏
点赞
0
个赞
共1条回复 最后由756665228回复于2019-05-16
#2756665228回复于2019-05-16

代码分享?

1
TOP
切换版块