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
#include
#include
#include
#include 
#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 vec_Img;

	vector 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;
}

c++实现Face++ API的调用_第1张图片



参考文献

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

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

你可能感兴趣的:(C/C++)