c++实现Face++ API的调用

1.Face++ API

Face++提供人脸相关技术的在线API服务给开发者使用,API 地址:http://www.faceplusplus.com.cn/。在使用Face++的在线API之前,肯定是要进行开发者注册申请的,申请完以后Face++会给开发者提供一个API Key和Secret Key,在后期的服务器通信时会用到两个Key。

2.调用说明

以Face++人脸检测为例,输入参数分别是:

       api_key(App的Face++ API Key);

       api_secret(APP的Face++ API Secret);

       img[POST](待检测图片的URL 或者 通过POST方法上传的二进制数据,原始图片大小需要小于1M);

返回数据为JSON格式的人脸的性别(gender), 年龄(age), 种族(race), 微笑程度(smiling), 眼镜(glass)和姿势(pose)等属性。调用之前需安装安装libcurl和jsoncpp下图为调用示例。

3.代码实现

#include<opencv2/opencv.hpp>
#include<iostream>
#include<vector>
#include<string>
#include <curl/curl.h>
#include "json.h"


using namespace std;
using namespace cv;

#define POSTURL  "http://apicn.faceplusplus.com/v2/detection/detect?api_key=06e4c8b1b3254eb2769d337457b8efac&api_secret=your=glass,pose,gender,age,race,smiling"

int writer(char *data, size_t size, size_t nmemb, string *writerData)
{
	if (writerData == NULL)
		return 0;
	int len = size*nmemb;
	writerData->append(data, len);
	return len;
}

int main()
{
	Mat img = imread("1.bmp");

	vector<uchar> vec_Img;

	vector<int> vecCompression_params;
	vecCompression_params.push_back(CV_IMWRITE_JPEG_QUALITY);
	vecCompression_params.push_back(90);
	imencode(".jpg", img, vec_Img, vecCompression_params);

	CURL *curl;
	CURLcode res;
	
	string buffer;

	struct curl_slist *http_header = NULL;

	curl = curl_easy_init();

	curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writer);
	curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);

	struct curl_httppost *formpost = 0;
	struct curl_httppost *lastptr = 0;
	curl_formadd(&formpost, &lastptr, CURLFORM_PTRNAME, "img", CURLFORM_BUFFER, "imgdata", CURLFORM_BUFFERPTR, vec_Img.data(), CURLFORM_BUFFERLENGTH, vec_Img.size(), CURLFORM_END);
	curl_easy_setopt(curl, CURLOPT_URL, POSTURL);
	curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);   

	res = curl_easy_perform(curl);

	curl_easy_cleanup(curl);


	Json::Value root;
	Json::Reader reader;
	bool parsingSuccessful = reader.parse(buffer, root);

	if (!parsingSuccessful)
	{
		cout << "Failed to parse the data!" << endl;
		exit(0);
	}

	cout << buffer << endl;

	waitKey(0);
	return 0;
}




参考文献

【1】http://www.2cto.com/kf/201401/274988.html

【2】http://blog.csdn.net/u011000290/article/details/51189528

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值