c++实现Face++ API的调用
2016-06-12 21:06
337 查看
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下图为调用示例。
![](http://img.blog.csdn.net/20160612212425747?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
3.代码实现
![](http://img.blog.csdn.net/20160613214036997?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
参考文献
【1】http://www.2cto.com/kf/201401/274988.html
【2】http://blog.csdn.net/u011000290/article/details/51189528
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
相关文章推荐
- [C++ 面试基础知识总结] 类
- Emacs中实现C/C++函数列表显示
- 步步为营<三> C语言 语句和表达式
- c++11多线程简介
- C/C++,运算符重载
- GeekBand c++學習筆記之五(對象模型與動態綁定的討論)
- C++在单片机上面的啪啪啪
- 循环队列 作为消息队列
- const 在基本语言中的总结
- 【C++】第1章 在VS2015中用C++编写控制台应用程序
- C++STL 之map
- C语言链接mysql常用函数
- c++ new string数组问题
- 模拟 - 约瑟夫环问题及其变式
- 关于约瑟夫问题的那点事
- C 指向指针的指针
- C++中virtual与inline Debug与Release选项 const 与 #define
- C和C++中struct的区别 以及C++中struct和class 以及c和c++区别 引用和指针
- 贪吃蛇(C++ OpenGL)
- C++公有继承、私有继承以及友元