This commit is contained in:
2025-10-13 18:34:48 +08:00
commit 37865d041f
116 changed files with 31168 additions and 0 deletions

View File

@ -0,0 +1,35 @@
# 安装百度AI开放平台 C++ SDK
**C++ SDK目录结构**
├── base
│ ├── base.h // 授权相关类
│ ├── base64.h // base64加密类
│ ├── http.h // http请求类
│ └── utils.h // 工具类
├── face.h // 人脸识别交互类
├── image_censor.h // 图像审核交互类
├── image_classify.h // 图像识别交互类
├── image_search.h // 图像搜索交互类
├── kg.h // 人脸识别交互类
├── nlp.h // 人脸识别交互类
├── ocr.h // 人脸识别交互类
└── speech.h // 语音识别交互类
**支持 C++ 11+**
**直接使用开发包步骤如下:**
1.在[官方网站](http://ai.baidu.com/sdk)下载C++ SDK压缩包。
2.将下载的`aip-cpp-sdk-version.zip`解压, 其中文件为包含实现代码的头文件。
3.安装依赖库curl(需要支持ssl) openssl jsoncpp(>1.6.2版本0.x版本将不被支持)。
4.编译工程时添加 C++11 支持 (gcc/clang 添加编译参数 -std=c++11), 添加第三方库链接参数 lcurl, lcrypto, ljsoncpp。
5.在源码中include 您需要使用的交互类头文件face.h image_censor.h image_classify.h kg.h nlp.h ocr.h speech.h等引入压缩包中的头文即可使用aip命名空间下的类和方法。
# 详细使用文档
参考[百度AI开放平台官方文档](http://ai.baidu.com/docs)

View File

@ -0,0 +1,311 @@
/**
* Copyright (c) 2017 Baidu.com, Inc. All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* @author baidu aip
*/
#ifndef __AIP_BASE_H__
#define __AIP_BASE_H__
#include <memory>
#include <cstring>
#include "http.h"
#include "json/json.h"
#include "base64.h"
#include "curl/curl.h"
#include "utils.h"
namespace aip {
static const char* AIP_SDK_VERSION = "0.3.3";
static const char* CURL_ERROR_CODE = "curl_error_code";
static const std::string ACCESS_TOKEN_URL = "https://aip.baidubce.com/oauth/2.0/token";
static const std::map<std::string, std::string> null;
static const Json::Value json_null;
class AipBase
{
private:
std::string _app_id;
int _expired_time;
bool _is_bce;
bool _has_decide_type;
std::string _scope;
protected:
std::string getAccessToken()
{
time_t now = time(NULL);
if (!access_token.empty())
{
return this->access_token;
}
if (now < this->_expired_time - 60 * 60 * 24)
{
return this->access_token;
}
std::string response;
std::map<std::string, std::string> params;
params["grant_type"] = "client_credentials";
params["client_id"] = this->ak;
params["client_secret"] = this->sk;
int status_code = this->client.get(
ACCESS_TOKEN_URL,
&params,
nullptr,
&response
);
Json::Value obj;
if (status_code != CURLcode::CURLE_OK) {
obj[CURL_ERROR_CODE] = status_code;
return obj.toStyledString();
}
std::string error;
std::unique_ptr<Json::CharReader> reader(crbuilder.newCharReader());
reader->parse(response.data(), response.data() + response.size(), &obj, &error);
this->access_token = obj["access_token"].asString();
this->_expired_time = obj["expires_in"].asInt() + (int) now;
this->_scope = obj["scope"].asString();
return this->access_token;
}
void merge_json(Json::Value& data, const Json::Value& options) {
Json::Value::Members mem = options.getMemberNames();
for (auto & iter : mem) {
data[iter.c_str()] = options[iter];
}
}
public:
std::string ak;
std::string sk;
HttpClient client;
Json::CharReaderBuilder crbuilder;
std::string access_token;
AipBase(const std::string & app_id, const std::string & ak, const std::string & sk):
_app_id(app_id),
_is_bce(false),
_has_decide_type(false),
ak(ak),
sk(sk)
{
if (_app_id == "")
{
}
}
void setConnectionTimeoutInMillis(int connect_timeout)
{
this->client.setConnectTimeout(connect_timeout);
}
void setSocketTimeoutInMillis(int socket_timeout)
{
this->client.setSocketTimeout(socket_timeout);
}
void setDebug(bool debug)
{
this->client.setDebug(debug);
}
std::string getAk() {
return ak;
}
Json::Value request(
std::string url,
std::map<std::string, std::string> const & params,
std::string const & data,
std::map<std::string, std::string> const & headers)
{
std::string response;
Json::Value obj;
std::string body;
auto headers_for_sign = headers;
auto temp_params = params;
temp_params["charset"] = "UTF-8";
this->prepare_request(url, temp_params, headers_for_sign);
int status_code = this->client.post(url, &temp_params, data, &headers_for_sign, &response);
if (status_code != CURLcode::CURLE_OK) {
obj[CURL_ERROR_CODE] = status_code;
return obj;
}
std::string error;
std::unique_ptr<Json::CharReader> reader(crbuilder.newCharReader());
reader->parse(response.data(), response.data() + response.size(), &obj, &error);
return obj;
}
Json::Value request(
std::string url,
std::map<std::string, std::string> const & params,
std::map<std::string, std::string> const & data,
std::map<std::string, std::string> const & headers)
{
std::string response;
Json::Value obj;
auto headers_for_sign = headers;
auto temp_params = params;
this->prepare_request(url, temp_params, headers_for_sign);
int status_code = this->client.post(url, &temp_params, data, &headers_for_sign, &response);
if (status_code != CURLcode::CURLE_OK) {
obj[CURL_ERROR_CODE] = status_code;
return obj;
}
std::string error;
std::unique_ptr<Json::CharReader> reader(crbuilder.newCharReader());
reader->parse(response.data(), response.data() + response.size(), &obj, &error);
return obj;
}
void prepare_request(std::string url,
std::map<std::string, std::string> & params,
std::map<std::string, std::string> & headers)
{
params["aipSdk"] = "C";
params["aipSdkVersion"] = AIP_SDK_VERSION;
if (_has_decide_type) {
if (_is_bce) {
std::string method = "POST";
sign(method, url, params, headers, ak, sk);
} else {
params["access_token"] = this->getAccessToken();
}
return;
}
if (getAccessToken() == "") {
_is_bce = true;
} else {
const char * t = std::strstr(this->_scope.c_str(), "brain_all_scope");
if (t == NULL)
{
_is_bce = true;
}
}
_has_decide_type = true;
prepare_request(url, params, headers);
}
Json::Value requestjson(
std::string url,
Json::Value & data,
std::map<std::string, std::string> & params,
std::map<std::string, std::string> const & headers)
{
std::string response;
Json::Value obj;
auto headers_for_sign = headers;
auto temp_params = params;
this->prepare_request(url, temp_params, headers_for_sign);
int status_code = this->client.post(url, nullptr, data, nullptr, &response);
if (status_code != CURLcode::CURLE_OK) {
obj[aip::CURL_ERROR_CODE] = status_code;
return obj;
}
std::string error;
std::unique_ptr<Json::CharReader> reader(crbuilder.newCharReader());
reader->parse(response.data(), response.data() + response.size(), &obj, &error);
return obj;
}
// Json::Value request_com(
// std::string const & url,
// Json::Value & data)
// {
// std::string response;
// Json::Value obj;
// int status_code = this->client.post(url, nullptr, data, nullptr, &response);
//
// if (status_code != CURLcode::CURLE_OK) {
// obj[aip::CURL_ERROR_CODE] = status_code;
// return obj;
// }
// std::string error;
// std::unique_ptr<Json::CharReader> reader(crbuilder.newCharReader());
// reader->parse(response.data(), response.data() + response.size(), &obj, &error);
//
// return obj;
// }
Json::Value request_com(
std::string const & url,
Json::Value & data,
std::map<std::string, std::string>* headers = nullptr,
std::map<std::string, std::string>* params = nullptr)
{
std::string response;
Json::Value obj;
std::map<std::string, std::string> headers_for_sign;
if (headers != nullptr) {
headers_for_sign = *headers;
}
std::map<std::string, std::string> temp_params;
if (params != nullptr) {
temp_params = *params;
}
this->prepare_request(url, temp_params, headers_for_sign);
int status_code = CURLcode::CURLE_OK;
if (headers == nullptr || headers->find("Content-Type") == headers->end()
|| (*headers)["Content-Type"] == "application/json") {
status_code = this->client.post(url, &temp_params, data, &headers_for_sign, &response);
} else if ((*headers)["Content-Type"] == "application/x-www-form-urlencoded") {
status_code = this->client.post_form(url, &temp_params, data, &headers_for_sign, &response);
}
if (status_code != CURLcode::CURLE_OK) {
obj[aip::CURL_ERROR_CODE] = status_code;
return obj;
}
std::string error;
std::unique_ptr<Json::CharReader> reader(crbuilder.newCharReader());
reader->parse(response.data(), response.data() + response.size(), &obj, &error);
return obj;
}
};
}
#endif

View File

@ -0,0 +1,130 @@
/**
* Copyright (c) 2017 Baidu.com, Inc. All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* @author baidu aip
*/
#ifndef __AIP_BASE64_H__
#define __AIP_BASE64_H__
#include <iostream>
#include <string>
namespace aip {
static const std::string base64_chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";
static inline bool is_base64(const char c)
{
return (isalnum(c) || (c == '+') || (c == '/'));
}
std::string base64_encode(const char * bytes_to_encode, unsigned int in_len)
{
std::string ret;
int i = 0;
int j = 0;
unsigned char char_array_3[3];
unsigned char char_array_4[4];
while (in_len--)
{
char_array_3[i++] = *(bytes_to_encode++);
if(i == 3)
{
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
char_array_4[3] = char_array_3[2] & 0x3f;
for(i = 0; (i <4) ; i++)
{
ret += base64_chars[char_array_4[i]];
}
i = 0;
}
}
if(i)
{
for(j = i; j < 3; j++)
{
char_array_3[j] = '\0';
}
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
char_array_4[3] = char_array_3[2] & 0x3f;
for(j = 0; (j < i + 1); j++)
{
ret += base64_chars[char_array_4[j]];
}
while((i++ < 3))
{
ret += '=';
}
}
return ret;
}
std::string base64_decode(std::string const & encoded_string)
{
int in_len = (int) encoded_string.size();
int i = 0;
int j = 0;
int in_ = 0;
unsigned char char_array_4[4], char_array_3[3];
std::string ret;
while (in_len-- && ( encoded_string[in_] != '=') && is_base64(encoded_string[in_])) {
char_array_4[i++] = encoded_string[in_]; in_++;
if (i ==4) {
for (i = 0; i <4; i++)
char_array_4[i] = base64_chars.find(char_array_4[i]);
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
for (i = 0; (i < 3); i++)
ret += char_array_3[i];
i = 0;
}
}
if (i) {
for (j = i; j <4; j++)
char_array_4[j] = 0;
for (j = 0; j <4; j++)
char_array_4[j] = base64_chars.find(char_array_4[j]);
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
for (j = 0; (j < i - 1); j++) ret += char_array_3[j];
}
return ret;
}
}
#endif

View File

@ -0,0 +1,306 @@
/**
* Copyright (c) 2017 Baidu.com, Inc. All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* @author baidu aip
*/
#ifndef __AIP_HTTP_H__
#define __AIP_HTTP_H__
#include "curl/curl.h"
#include <sstream>
#include <string>
#include <map>
#include <json/json.h>
namespace aip {
inline size_t onWriteData(void * buffer, size_t size, size_t nmemb, void * userp)
{
std::string * str = dynamic_cast<std::string *>((std::string *)userp);
str->append((char *)buffer, size * nmemb);
return nmemb;
}
class HttpClient
{
private:
bool debug = false;
int connect_timeout = 10000;
int socket_timeout = 10000;
void makeUrlencodedForm(std::map<std::string, std::string> const & params, std::string * content) const
{
content->clear();
std::map<std::string, std::string>::const_iterator it;
for(it=params.begin(); it!=params.end(); it++)
{
char * key = curl_escape(it->first.c_str(), (int) it->first.size());
char * value = curl_escape(it->second.c_str(),(int) it->second.size());
*content += key;
*content += '=';
*content += value;
*content += '&';
curl_free(key);
curl_free(value);
}
}
void appendUrlParams(std::map<std::string, std::string> const & params, std::string* url) const
{
if(params.empty()) {
return;
}
std::string content;
this->makeUrlencodedForm(params, &content);
bool url_has_param = false;
for (const auto& ch : *url) {
if (ch == '?') {
url_has_param = true;
break;
}
}
if (url_has_param) {
url->append("&");
} else {
url->append("?");
}
url->append(content);
}
void appendHeaders(std::map<std::string, std::string> const & headers, curl_slist ** slist) const
{
std::ostringstream ostr;
std::map<std::string, std::string>::const_iterator it;
for(it=headers.begin(); it!=headers.end(); it++)
{
ostr << it->first << ":" << it->second;
*slist = curl_slist_append(*slist, ostr.str().c_str());
ostr.str("");
}
}
public:
HttpClient() = default;
HttpClient(const HttpClient &) = delete;
HttpClient & operator=(const HttpClient &) = delete;
void setConnectTimeout(int connect_timeout)
{
this->connect_timeout = connect_timeout;
}
void setSocketTimeout(int socket_timeout)
{
this->socket_timeout = socket_timeout;
}
void setDebug(bool debug)
{
this->debug = debug;
}
int get(
std::string url,
std::map<std::string, std::string> const * params,
std::map<std::string, std::string> const * headers,
std::string * response) const
{
CURL * curl = curl_easy_init();
struct curl_slist * slist = NULL;
if (headers) {
this->appendHeaders(*headers, &slist);
}
if (params) {
this->appendUrlParams(*params, &url);
}
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, onWriteData);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *) response);
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, true);
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT_MS, this->connect_timeout);
curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, this->socket_timeout);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);
curl_easy_setopt(curl, CURLOPT_VERBOSE, this->debug);
int status_code = curl_easy_perform(curl);
curl_easy_cleanup(curl);
curl_slist_free_all(slist);
return status_code;
}
int post(
std::string url,
std::map<std::string, std::string> const * params,
const std::string & body,
std::map<std::string, std::string> const * headers,
std::string * response) const
{
struct curl_slist * slist = NULL;
CURL * curl = curl_easy_init();
if (headers) {
this->appendHeaders(*headers, &slist);
}
if (params) {
this->appendUrlParams(*params, &url);
}
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist);
curl_easy_setopt(curl, CURLOPT_POST, true);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, body.c_str());
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, body.size());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, onWriteData);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *) response);
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, true);
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT_MS, this->connect_timeout);
curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, this->socket_timeout);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);
curl_easy_setopt(curl, CURLOPT_VERBOSE, this->debug);
int status_code = curl_easy_perform(curl);
curl_easy_cleanup(curl);
curl_slist_free_all(slist);
return status_code;
}
/**
* application/x-www-form-urlencoded
* @param url
* @param params
* @param data
* @param headers
* @param response
* @return
*/
int post(
std::string url,
std::map<std::string, std::string> const * params,
std::map<std::string, std::string> const & data,
std::map<std::string, std::string> const * headers,
std::string * response) const
{
std::string body;
this->makeUrlencodedForm(data, &body);
return this->post(std::move(url), params, body, headers, response);
}
/**
* application/json
* @param url
* @param params
* @param data
* @param headers
* @param response
* @return
*/
int post(
std::string url,
std::map<std::string, std::string> const * params,
Json::Value const & data,
std::map<std::string, std::string> const * headers,
std::string * response) const
{
std::string body;
Json::StreamWriterBuilder swb;
std::unique_ptr<Json::StreamWriter> writer(swb.newStreamWriter());
std::ostringstream os;
writer->write(data, &os);
body = os.str();
std::map<std::string, std::string> temp_headers;
if (headers != nullptr) {
for (const auto & iter : *headers) {
temp_headers[iter.first] = iter.second;
}
}
if (temp_headers.find("Content-Type") == temp_headers.end()) {
temp_headers["Content-Type"] = "application/json";
}
return this->post(url.c_str(), params, body, &temp_headers, response);
}
/**
* application/x-www-form-urlencoded
* all type data
* @param url
* @param params
* @param data
* @param headers
* @param response
* @return
*/
int post_form(
std::string url,
std::map<std::string, std::string> const * params,
Json::Value const & data,
std::map<std::string, std::string> const * headers,
std::string * response) const
{
std::string body;
body.clear();
Json::Value::Members mem = data.getMemberNames();
for (auto iter = mem.begin(); iter != mem.end(); iter++) {
std::string str = "";
char * curl_escape_value;
char * key = curl_escape((*iter).c_str(), (int)((*iter).size()));
body += key;
body += '=';
Json::Value jsonValue = data[*iter];
switch(jsonValue.type()) {
case Json::realValue:
body += std::to_string(data[*iter].asDouble());
break;
case Json::intValue:
body += std::to_string(data[*iter].asInt64());
break;
case Json::booleanValue:
body += std::to_string(data[*iter].asBool());
break;
case Json::stringValue:
str = data[*iter].asString();
curl_escape_value = curl_escape(str.c_str(), (int)(str.size()));
body += curl_escape_value;
curl_free(curl_escape_value);
break;
default:
break;
}
body += '&';
curl_free(key);
}
return this->post(std::move(url), params, body, headers, response);
}
int post(
std::string url,
std::map<std::string, std::string> const * params,
std::map<std::string, std::string> const * headers,
std::string * response) const
{
const static std::string EMPTY_STRING;
return this->post(std::move(url), params, EMPTY_STRING, headers, response);
}
};
}
#endif

View File

@ -0,0 +1,283 @@
/**
* Copyright (c) 2017 Baidu.com, Inc. All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* @author baidu aip
*/
#ifndef __AIP_UTILS_H__
#define __AIP_UTILS_H__
#include <string>
#include <fstream>
#include <ctype.h>
#include <openssl/evp.h>
#include <openssl/hmac.h>
#include <algorithm>
#include <openssl/md5.h>
const int __BCE_VERSION__ = 1;
const int __BCE_EXPIRE__ = 1800;
namespace aip {
template<class CharT, class Traits, class Allocator>
std::basic_istream<CharT, Traits>& getall(std::basic_istream<CharT, Traits>& input,
std::basic_string<CharT, Traits, Allocator>& str) {
std::ostringstream oss;
oss << input.rdbuf();
str.assign(oss.str());
return input;
}
inline int get_file_content(const char *filename, std::string* out) {
std::ifstream in(filename, std::ios::in | std::ios::binary);
if (in) {
getall(in, *out);
return 0;
} else {
return -1;
}
}
inline std::string to_upper(std::string src)
{
std::transform(src.begin(), src.end(), src.begin(), [](unsigned char c) { return std::toupper(c); });
return src;
}
inline std::string to_lower(std::string src)
{
std::transform(src.begin(), src.end(), src.begin(), [](unsigned char c) { return std::tolower(c); });
return src;
}
inline std::string to_hex(unsigned char c, bool lower = false)
{
const std::string hex = "0123456789ABCDEF";
std::stringstream ss;
ss << hex[c >> 4] << hex[c & 0xf];
return lower ? to_lower(ss.str()) : ss.str();
}
inline time_t now()
{
return time(NULL);
}
std::string utc_time(time_t timestamp)
{
struct tm result_tm;
char buffer[32];
#ifdef _WIN32
gmtime_s(&result_tm, &timestamp);
#else
gmtime_r(&timestamp, &result_tm);
#endif
size_t size = strftime(buffer, 32, "%Y-%m-%dT%H:%M:%SZ", &result_tm);
return std::string(buffer, size);
}
void url_parse(
const std::string & url,
std::map<std::string, std::string> & params)
{
int pos = (int)url.find("?");
if (pos != -1)
{
int key_start = pos + 1,
key_len = 0,
val_start = 0;
for (int i = key_start; i <= (int)url.size(); ++i)
{
switch (url[i])
{
case '=':
key_len = i - key_start;
val_start = i + 1;
break;
case '\0':
case '&':
if (key_len != 0)
{
params[url.substr(key_start, key_len)] = url.substr(val_start, i - val_start);
key_start = i + 1;
key_len = 0;
}
break;
default:
break;
}
}
}
}
std::string url_encode(const std::string & input, bool encode_slash=true)
{
std::stringstream ss;
const char *str = input.c_str();
for (uint32_t i = 0; i < input.size(); i++)
{
unsigned char c = str[i];
if (isalnum(c) || c == '_' || c == '-' || c == '~' || c == '.' || (!encode_slash && c == '/'))
{
ss << c;
}
else
{
ss << "%" << to_hex(c);
}
}
return ss.str();
}
std::string canonicalize_params(std::map<std::string, std::string> & params)
{
std::vector<std::string> v;
v.reserve(params.size());
for (auto & it : params) {
v.push_back(url_encode(it.first) + "=" + url_encode(it.second));
}
std::sort(v.begin(), v.end());
std::string result;
for (auto & it : v)
{
result.append((result.empty() ? "" : "&") + it);
}
return result;
}
std::string canonicalize_headers(std::map<std::string, std::string> & headers)
{
std::vector<std::string> v;
v.reserve(headers.size());
for (auto & it : headers) {
v.push_back(url_encode(to_lower(it.first)) + ":" + url_encode(it.second));
}
std::sort(v.begin(), v.end());
std::string result;
for (auto & it : v)
{
result.append((result.empty() ? "" : "\n") + it);
}
return result;
}
std::string get_headers_keys(std::map<std::string, std::string> & headers)
{
std::vector<std::string> v;
v.reserve(headers.size());
for (auto & it : headers) {
v.push_back(to_lower(it.first));
}
std::string result;
for (auto & it : v)
{
result.append((result.empty() ? "" : ";") + it);
}
return result;
}
std::string get_host(const std::string & url)
{
int pos = (int)url.find("://") + 3;
return url.substr(
pos,
url.find('/', pos) - pos
);
}
std::string get_path(const std::string & url)
{
int path_start = (int)url.find('/', url.find("://") + 3);
int path_end = (int)url.find('?');
path_end = path_end == -1 ? (int)url.size() : path_end;
return url.substr(path_start, path_end - path_start);
}
std::string hmac_sha256(
const std::string & src,
const std::string & sk)
{
const EVP_MD *evp_md = EVP_sha256();
unsigned char md[EVP_MAX_MD_SIZE];
unsigned int md_len = 0;
if (HMAC(evp_md,
reinterpret_cast<const unsigned char *>(sk.data()), (int)sk.size(),
reinterpret_cast<const unsigned char *>(src.data()), src.size(),
md, &md_len) == NULL)
{
return "";
}
std::stringstream ss;
for (int i = 0; i < (int)md_len; ++i)
{
ss << to_hex(md[i], true);
}
return ss.str();
}
void sign(
std::string method,
std::string & url,
std::map<std::string, std::string> & params,
std::map<std::string, std::string> & headers,
std::string & ak,
std::string & sk)
{
url_parse(url, params);
headers["Host"] = get_host(url);
std::string timestamp = utc_time(now());
headers["x-bce-date"] = timestamp;
std::stringstream ss;
ss << "bce-auth-v" << __BCE_VERSION__ << "/" << ak << "/"
<< timestamp << "/" << __BCE_EXPIRE__;
std::string val = ss.str();
std::string sign_key = hmac_sha256(val, sk);
ss.str("");
ss << to_upper(method) << '\n' << url_encode(get_path(url), false)
<< '\n' << canonicalize_params(params)
<< '\n' << canonicalize_headers(headers);
std::string signature = hmac_sha256(ss.str(), sign_key);
ss.str("");
ss << "bce-auth-v" << __BCE_VERSION__ << "/" << ak << "/"
<< timestamp << "/" << __BCE_EXPIRE__ << "/"
<< get_headers_keys(headers) << "/" << signature;
headers["authorization"] = ss.str();
}
}
#endif

View File

@ -0,0 +1,220 @@
/**
* Copyright (c) 2017 Baidu.com, Inc. All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* @author baidu aip
*/
#ifndef __AIP_BODY_ANALYSIS_H__
#define __AIP_BODY_ANALYSIS_H__
#include "base/base.h"
namespace aip {
class Bodyanalysis : public AipBase
{
public:
std::string _body_analysis_v1 =
"https://aip.baidubce.com/rest/2.0/image-classify/v1/body_analysis";
std::string _body_attr_v1 =
"https://aip.baidubce.com/rest/2.0/image-classify/v1/body_attr";
std::string _body_num_v1 =
"https://aip.baidubce.com/rest/2.0/image-classify/v1/body_num";
std::string _driver_behavior_v1 =
"https://aip.baidubce.com/rest/2.0/image-classify/v1/driver_behavior";
std::string _body_seg_v1 =
"https://aip.baidubce.com/rest/2.0/image-classify/v1/body_seg";
std::string _gesture_v1 =
"https://aip.baidubce.com/rest/2.0/image-classify/v1/gesture";
std::string _body_tracking_v1 =
"https://aip.baidubce.com/rest/2.0/image-classify/v1/body_tracking";
std::string _hand_analysis_v1 =
"https://aip.baidubce.com/rest/2.0/image-classify/v1/hand_analysis";
std::string _body_danger_v1 =
"https://aip.baidubce.com/rest/2.0/video-classify/v1/body_danger";
std::string _fingertip_v1 =
"https://aip.baidubce.com/rest/2.0/image-classify/v1/fingertip";
Bodyanalysis(const std::string & app_id, const std::string & ak, const std::string & sk)
: AipBase(app_id, ak, sk)
{
}
/**
* 人体关键点识别
* 接口使用文档链接: https://ai.baidu.com/ai-doc/BODY/0k3cpyxme
*/
Json::Value body_analysis_v1(
std::string const &image)
{
std::map<std::string, std::string> data;
data["image"] = base64_encode(image.c_str(), (int) image.size());
Json::Value result =
this->request(_body_analysis_v1, null, data, null);
return result;
}
/**
* 人体检测与属性识别
* 接口使用文档链接: https://ai.baidu.com/ai-doc/BODY/Ak3cpyx6v
*/
Json::Value body_attr_v1(
std::string const &image,
const std::map<std::string, std::string> &options)
{
std::map<std::string, std::string> data;
data["image"] = base64_encode(image.c_str(), (int) image.size());
std::copy(options.begin(), options.end(), std::inserter(data, data.end()));
Json::Value result =
this->request(_body_attr_v1, null, data, null);
return result;
}
/**
* 人流量统计
* 接口使用文档链接: https://ai.baidu.com/ai-doc/BODY/7k3cpyy1t
*/
Json::Value body_num_v1(
std::string const &image,
const std::map<std::string, std::string> &options)
{
std::map<std::string, std::string> data;
data["image"] = base64_encode(image.c_str(), (int) image.size());
std::copy(options.begin(), options.end(), std::inserter(data, data.end()));
Json::Value result =
this->request(_body_num_v1, null, data, null);
return result;
}
/**
* 驾驶行为分析
* 接口使用文档链接: https://ai.baidu.com/ai-doc/BODY/Nk3cpywct
*/
Json::Value driver_behavior_v1(
std::string const &image,
const std::map<std::string, std::string> &options)
{
std::map<std::string, std::string> data;
data["image"] = base64_encode(image.c_str(), (int) image.size());
std::copy(options.begin(), options.end(), std::inserter(data, data.end()));
Json::Value result =
this->request(_driver_behavior_v1, null, data, null);
return result;
}
/**
* 人像分割
* 接口使用文档链接: https://ai.baidu.com/ai-doc/BODY/Fk3cpyxua
*/
Json::Value body_seg_v1(
std::string const &image,
const std::map<std::string, std::string> &options)
{
std::map<std::string, std::string> data;
data["image"] = base64_encode(image.c_str(), (int) image.size());
std::copy(options.begin(), options.end(), std::inserter(data, data.end()));
Json::Value result =
this->request(_body_seg_v1, null, data, null);
return result;
}
/**
* 手势识别
* 接口使用文档链接: https://ai.baidu.com/ai-doc/BODY/4k3cpywrv
*/
Json::Value gesture_v1(
std::string const &image)
{
std::map<std::string, std::string> data;
data["image"] = base64_encode(image.c_str(), (int) image.size());
Json::Value result =
this->request(_gesture_v1, null, data, null);
return result;
}
/**
* 人流量统计(动态版)
* 接口使用文档链接: https://ai.baidu.com/ai-doc/BODY/wk3cpyyog
*/
Json::Value body_tracking_v1(
std::string const &dynamic,
std::string const &image,
Json::Value & options)
{
Json::Value data;
data["image"] = base64_encode(image.c_str(), (int) image.size());
data["dynamic"] = dynamic;
merge_json(data, options);
std::map<std::string, std::string> headers;
headers["Content-Type"] = "application/x-www-form-urlencoded";
Json::Value result = this->request_com(_body_tracking_v1, data, &headers);
return result;
}
/**
* 手部关键点识别
* 接口使用文档链接: https://ai.baidu.com/ai-doc/BODY/Kk3cpyxeu
*/
Json::Value hand_analysis_v1(
std::string const &image)
{
std::map<std::string, std::string> data;
data["image"] = base64_encode(image.c_str(), (int) image.size());
Json::Value result =
this->request(_hand_analysis_v1, null, data, null);
return result;
}
/**
* 危险行为识别
* 接口使用文档链接: https://ai.baidu.com/ai-doc/BODY/uk3cpywke
*/
Json::Value body_danger_v1(
std::string const &video_data)
{
std::map<std::string, std::string> data;
data["data"] = base64_encode(video_data.c_str(), (int) video_data.size());
Json::Value result =
this->request(_body_danger_v1, null, data, null);
return result;
}
/**
* 指尖检测
* 接口使用文档链接: https://ai.baidu.com/ai-doc/BODY/Jk7ir38ut
*/
Json::Value fingertip_v1(
std::string const &image)
{
std::map<std::string, std::string> data;
data["image"] = base64_encode(image.c_str(), (int) image.size());
Json::Value result =
this->request(_fingertip_v1, null, data, null);
return result;
}
};
}
#endif

View File

@ -0,0 +1,327 @@
/**
* Copyright (c) 2017 Baidu.com, Inc. All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* @author baidu aip
*/
#ifndef __AIP_CONTENTCENSOR_H__
#define __AIP_CONTENTCENSOR_H__
#include "base/base.h"
namespace aip {
class Contentcensor: public AipBase
{
public:
std::string _img_censor_user_defined_v2 =
"https://aip.baidubce.com/rest/2.0/solution/v1/img_censor/v2/user_defined";
std::string _text_censor_user_defined_v2 =
"https://aip.baidubce.com/rest/2.0/solution/v1/text_censor/v2/user_defined";
std::string _live_save_v1 = "https://aip.baidubce.com/rest/2.0/solution/v1/live/v1/config/save";
std::string _live_stop_v1 = "https://aip.baidubce.com/rest/2.0/solution/v1/live/v1/config/stop";
std::string _live_view_v1 = "https://aip.baidubce.com/rest/2.0/solution/v1/live/v1/config/view";
std::string _live_pull_v1 = "https://aip.baidubce.com/rest/2.0/solution/v1/live/v1/audit/pull";
std::string _video_censor_submit_v1 = "https://aip.baidubce.com/rest/2.0/solution/v1/video_censor/v1/video/submit";
std::string _video_censor_pull_v1 = "https://aip.baidubce.com/rest/2.0/solution/v1/video_censor/v1/video/pull";
std::string _async_voice_submit_v1 = "https://aip.baidubce.com/rest/2.0/solution/v1/async_voice/submit";
std::string _async_voice_pull_v1 = "https://aip.baidubce.com/rest/2.0/solution/v1/async_voice/pull";
std::string _document_censor_submit_url = "https://aip.baidubce.com/rest/2.0/solution/v1/solution/document/v1/submit";
std::string _document_censor_pull_url = "https://aip.baidubce.com/rest/2.0/solution/v1/solution/document/v1/pull";
Contentcensor(const std::string & app_id, const std::string & ak, const std::string & sk)
: AipBase(app_id, ak, sk)
{
}
/**
* 内容审核平台-图像
* 接口使用文档链接: https://ai.baidu.com/ai-doc/ANTIPORN/jk42xep4e
*/
Json::Value img_censor_user_defined_v2_img(std::string const &image, const Json::Value & options)
{
Json::Value data;
data["image"] = base64_encode(image.c_str(), (int) image.size());
merge_json(data, options);
std::map<std::string, std::string> headers;
headers["Content-Type"] = "application/x-www-form-urlencoded";
Json::Value result = this->request_com(_img_censor_user_defined_v2, data, &headers);
return result;
}
/**
* 内容审核平台-图像
* 接口使用文档链接: https://ai.baidu.com/ai-doc/ANTIPORN/jk42xep4e
*/
Json::Value img_censor_user_defined_v2_url(std::string const &imgUrl, const Json::Value & options)
{
Json::Value data;
data["imgUrl"] = imgUrl;
merge_json(data, options);
std::map<std::string, std::string> headers;
headers["Content-Type"] = "application/x-www-form-urlencoded";
Json::Value result = this->request_com(_img_censor_user_defined_v2, data, &headers);
return result;
}
/**
* 内容审核平台-文本
* 接口使用文档链接: https://ai.baidu.com/ai-doc/ANTIPORN/Rk3h6xb3i
*/
Json::Value text_censor_user_defined_v2(std::string const &text)
{
std::map<std::string, std::string> data;
data["text"] = text;
Json::Value result =
this->request(_text_censor_user_defined_v2, null, data, null);
return result;
}
/**
* 内容审核平台-直播流(新增任务)
* 接口使用文档链接: https://ai.baidu.com/ai-doc/ANTIPORN/mkxlraoz5
*/
Json::Value live_save_v1(std::string const &streamUrl, std::string const &streamType,
std::string const &extId, long long const &startTime,
long long const &endTime, std::string const &streamName, const Json::Value & options)
{
Json::Value data;
data["streamUrl"] = streamUrl;
data["streamType"] = streamType;
data["extId"] = extId;
data["startTime"] = startTime;
data["endTime"] = endTime;
data["streamName"] = streamName;
merge_json(data, options);
std::map<std::string, std::string> headers;
headers["Content-Type"] = "application/x-www-form-urlencoded";
Json::Value result = this->request_com(_live_save_v1, data, &headers);
return result;
}
/**
* 内容审核平台-直播流(删除任务)
* 接口使用文档链接: https://ai.baidu.com/ai-doc/ANTIPORN/Ckxls2owb
*/
Json::Value live_stop_v1(
std::string const &taskId,
const Json::Value & options)
{
Json::Value data;
data["taskId"] = taskId;
merge_json(data, options);
std::map<std::string, std::string> headers;
headers["Content-Type"] = "application/x-www-form-urlencoded";
Json::Value result = this->request_com(_live_stop_v1, data, &headers);
return result;
}
/**
* 内容审核平台-直播流(查看配置)
* 接口使用文档链接: https://ai.baidu.com/ai-doc/ANTIPORN/ckxls6tl1
*/
Json::Value live_view_v1(
std::string const &taskId,
const Json::Value & options)
{
Json::Value data;
data["taskId"] = taskId;
merge_json(data, options);
std::map<std::string, std::string> headers;
headers["Content-Type"] = "application/x-www-form-urlencoded";
Json::Value result = this->request_com(_live_view_v1, data, &headers);
return result;
}
/**
* 内容审核平台-直播流(获取结果)
* 接口使用文档链接: https://ai.baidu.com/ai-doc/ANTIPORN/Pkxlshd1s
*/
Json::Value live_pull_v1(
std::string const &taskId,
const Json::Value & options)
{
Json::Value data;
data["taskId"] = taskId;
merge_json(data, options);
std::map<std::string, std::string> headers;
headers["Content-Type"] = "application/x-www-form-urlencoded";
Json::Value result = this->request_com(_live_view_v1, data, &headers);
return result;
}
/**
* 内容审核平台-长视频(提交任务)
* 接口使用文档链接: https://ai.baidu.com/ai-doc/ANTIPORN/bksy7ak30
*/
Json::Value video_censor_submit_v1(
std::string const &url,
std::string const &extId,
const Json::Value & options)
{
Json::Value data;
data["url"] = url;
data["extId"] = extId;
merge_json(data, options);
std::map<std::string, std::string> headers;
headers["Content-Type"] = "application/x-www-form-urlencoded";
Json::Value result = this->request_com(_video_censor_submit_v1, data, &headers);
return result;
}
/**
* 内容审核平台-长视频(获取结果)
* 接口使用文档链接: https://ai.baidu.com/ai-doc/ANTIPORN/jksy7j3jv
*/
Json::Value video_censor_pull_v1(
std::string const &taskId,
const Json::Value & options)
{
Json::Value data;
data["taskId"] = taskId;
merge_json(data, options);
std::map<std::string, std::string> headers;
headers["Content-Type"] = "application/x-www-form-urlencoded";
Json::Value result = this->request_com(_video_censor_pull_v1, data, &headers);
return result;
}
/**
* 音频文件异步审核
* 接口使用文档链接: https://ai.baidu.com/ai-doc/ANTIPORN/akxlple3t
*/
Json::Value async_voice_submit_v1(
std::string const &url, std::string const &fmt, int rate,
const Json::Value & options)
{
Json::Value data;
data["url"] = url;
data["fmt"] = fmt;
data["rate"] = rate;
merge_json(data, options);
std::map<std::string, std::string> headers;
headers["Content-Type"] = "application/x-www-form-urlencoded";
Json::Value result = this->request_com(_async_voice_submit_v1, data, &headers);
return result;
}
/**
* 音频文件异步审核-查询
* 接口使用文档链接: https://ai.baidu.com/ai-doc/ANTIPORN/jkxlpxllo
*/
Json::Value async_voice_pull_v1_taskid(
std::string const &taskId)
{
std::map<std::string, std::string> data;
data["taskId"] = taskId;
Json::Value result =
this->request(_async_voice_pull_v1, null, data, null);
return result;
}
/**
* 音频文件异步审核-查询
* 接口使用文档链接: https://ai.baidu.com/ai-doc/ANTIPORN/jkxlpxllo
*/
Json::Value async_voice_pull_v1_audioid(
std::string const &audioId)
{
std::map<std::string, std::string> data;
data["audioId"] = audioId;
Json::Value result =
this->request(_async_voice_pull_v1, null, data, null);
return result;
}
/**
* 文档审核-提交任务
* https://ai.baidu.com/ai-doc/ANTIPORN/2l8484xvl
*/
Json::Value document_censor_file_submit(
std::string const & file_name,
std::string const & document,
const std::map<std::string, std::string> & options)
{
std::map<std::string, std::string> data;
data["fileBase64"] = base64_encode(document.c_str(), (int) document.size());
data["fileName"] = file_name;
std::copy(options.begin(), options.end(), std::inserter(data, data.end()));
Json::Value result =
this->request(_document_censor_submit_url, null, data, null);
return result;
}
/**
* 文档审核-提交任务
* https://ai.baidu.com/ai-doc/ANTIPORN/2l8484xvl
*/
Json::Value document_censor_url_submit(
std::string const & file_name,
std::string const & url,
const std::map<std::string, std::string> & options)
{
std::map<std::string, std::string> data;
data["url"] = url;
data["fileName"] = file_name;
std::copy(options.begin(), options.end(), std::inserter(data, data.end()));
Json::Value result =
this->request(_document_censor_submit_url, null, data, null);
return result;
}
/**
* 文档审核-拉取结果
* https://ai.baidu.com/ai-doc/ANTIPORN/4l848df5n
*/
Json::Value document_censor_pull(
std::string const & task_id,
const std::map<std::string, std::string> & options)
{
std::map<std::string, std::string> data;
data["taskId"] = task_id;
std::copy(options.begin(), options.end(), std::inserter(data, data.end()));
Json::Value result =
this->request(_document_censor_pull_url, null, data, null);
return result;
}
};
}
#endif

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,111 @@
/**
* Copyright (c) 2017 Baidu.com, Inc. All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* @author baidu aip
*/
#ifndef __AIP_IMAGECENSOR_H__
#define __AIP_IMAGECENSOR_H__
#include "base/base.h"
namespace aip {
class Imagecensor: public AipBase
{
public:
std::string _anti_porn =
"https://aip.baidubce.com/rest/2.0/antiporn/v1/detect";
std::string _anti_porn_gif =
"https://aip.baidubce.com/rest/2.0/antiporn/v1/detect_gif";
std::string _anti_terror =
"https://aip.baidubce.com/rest/2.0/antiterror/v1/detect";
Imagecensor(const std::string & app_id, const std::string & ak, const std::string & sk): AipBase(app_id, ak, sk)
{
}
/**
* anti_porn
* 该请求用于鉴定图片的色情度。即对于输入的一张图片(可正常解码,且长宽比适宜),输出图片的色情度。目前支持三个维度:色情、性感、正常。
* @param image 图像文件二进制内容可以使用aip::get_file_content函数获取
* options 可选参数:
*/
Json::Value anti_porn(
std::string const & image,
const std::map<std::string, std::string> & options)
{
std::map<std::string, std::string> data;
data["image"] = base64_encode(image.c_str(), (int) image.size());
std::copy(options.begin(), options.end(), std::inserter(data, data.end()));
Json::Value result =
this->request(_anti_porn, null, data, null);
return result;
}
/**
* anti_porn_gif
* 该请求用于鉴定GIF图片的色情度对于非gif接口请使用色情识别接口。接口会对图片中每一帧进行识别并返回所有检测结果中色情值最大的为结果。目前支持三个维度色情、性感、正常。
* @param image 图像文件二进制内容可以使用aip::get_file_content函数获取
* options 可选参数:
*/
Json::Value anti_porn_gif(
std::string const & image,
const std::map<std::string, std::string> & options)
{
std::map<std::string, std::string> data;
data["image"] = base64_encode(image.c_str(), (int) image.size());
std::copy(options.begin(), options.end(), std::inserter(data, data.end()));
Json::Value result =
this->request(_anti_porn_gif, null, data, null);
return result;
}
/**
* anti_terror
* 该请求用于鉴定图片是否涉暴涉恐。即对于输入的一张图片(可正常解码,且长宽比适宜),输出图片的涉暴涉恐程度。
* @param image 图像文件二进制内容可以使用aip::get_file_content函数获取
* options 可选参数:
*/
Json::Value anti_terror(
std::string const & image,
const std::map<std::string, std::string> & options)
{
std::map<std::string, std::string> data;
data["image"] = base64_encode(image.c_str(), (int) image.size());
std::copy(options.begin(), options.end(), std::inserter(data, data.end()));
Json::Value result =
this->request(_anti_terror, null, data, null);
return result;
}
};
}
#endif

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,693 @@
/**
* Copyright (c) 2017 Baidu.com, Inc. All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* @author baidu aip
*/
#ifndef __AIP_IMAGEPROCESS_H__
#define __AIP_IMAGEPROCESS_H__
#include "base/base.h"
namespace aip {
class Imageprocess: public AipBase
{
public:
std::string _image_definition_enhance =
"https://aip.baidubce.com/rest/2.0/image-process/v1/image_definition_enhance";
std::string _sky_seg =
"https://aip.baidubce.com/rest/2.0/image-process/v1/sky_seg";
std::string _image_tyle_trans =
"https://aip.baidubce.com/rest/2.0/image-process/v1/style_trans";
std::string _selfie_anime =
"https://aip.baidubce.com/rest/2.0/image-process/v1/selfie_anime";
std::string _color_enhance =
"https://aip.baidubce.com/rest/2.0/image-process/v1/color_enhance";
std::string _image_inpainting =
"https://aip.baidubce.com/rest/2.0/image-process/v1/inpainting";
std::string _image_quality_enhance_v1 =
"https://aip.baidubce.com/rest/2.0/image-process/v1/image_quality_enhance";
std::string _contrast_enhance_v1 =
"https://aip.baidubce.com/rest/2.0/image-process/v1/contrast_enhance";
std::string _dehaze_v1 =
"https://aip.baidubce.com/rest/2.0/image-process/v1/dehaze";
std::string _colourize_v1 =
"https://aip.baidubce.com/rest/2.0/image-process/v1/colourize";
std::string _stretch_restore_v1 =
"https://aip.baidubce.com/rest/2.0/image-process/v1/stretch_restore";
std::string _remove_moire_v1 = "https://aip.baidubce.com/rest/2.0/image-process/v1/remove_moire";
std::string _customize_stylization_v1 =
"https://aip.baidubce.com/rest/2.0/image-process/v1/customize_stylization";
std::string _doc_repair_v1 = "https://aip.baidubce.com/rest/2.0/image-process/v1/doc_repair";
std::string _denoise_v1 = "https://aip.baidubce.com/rest/2.0/image-process/v1/denoise";
Imageprocess(const std::string & app_id, const std::string & ak, const std::string & sk):
AipBase(app_id, ak, sk)
{
}
/**
* 图像修复
* 去除图片中不需要的遮挡物,并用背景内容填充,提高图像质量。
* @param image 二进制图像数据
* @param rectangle 要去除的位置为规则矩形时,给出坐标信息.每个元素包含left, top, width, heightint 类型
* options 可选参数:
*/
Json::Value imageinpainting(
std::string const & image,
Json::Value & rectangle,
std::map<std::string, std::string> options)
{
Json::Value data;
std::string access_token = this->getAccessToken();
data["image"] = base64_encode(image.c_str(), (int) image.size());
data["rectangle"] = rectangle;
std::map< std::string,std::string >::iterator it ;
for(it = options.begin(); it != options.end(); it++)
{
data[it->first] = it->second;
}
std::string mid = "?access_token=";
std::string url = _image_inpainting+mid+access_token;
Json::Value result =
this->request_com(url, data);
return result;
}
/**
* 图像修复
* 去除图片中不需要的遮挡物,并用背景内容填充,提高图像质量。
* @param image 二进制图像数据
* @param rectangle 要去除的位置为规则矩形时,给出坐标信息.每个元素包含left, top, width, heightint 类型
* options 可选参数:
*/
Json::Value imageinpainting_url(
std::string const & url,
Json::Value & rectangle,
std::map<std::string, std::string> options)
{
Json::Value data;
data["url"] = url;
data["rectangle"] = rectangle;
std::map< std::string,std::string >::iterator it ;
for(it = options.begin(); it != options.end(); it++)
{
data[it->first] = it->second;
}
Json::Value result =
this->request_com(_image_inpainting, data);
return result;
}
/**
* 图像色彩增强
* 可智能调节图片的色彩饱和度、亮度、对比度,使得图片内容细节、色彩更加逼真,可用于提升网站图片、手机相册图片、视频封面图片的质量
* @param image 二进制图像数据
* options 可选参数:
*/
Json::Value colorenhance(
std::string const & image,
const std::map<std::string, std::string> & options)
{
std::map<std::string, std::string> data;
data["image"] = base64_encode(image.c_str(), (int) image.size());
std::copy(options.begin(), options.end(), std::inserter(data, data.end()));
Json::Value result =
this->request(_color_enhance, null, data, null);
return result;
}
/**
* 人像动漫化接口
* 运用世界领先的对抗生成网络,结合人脸检测、头发分割、人像分割等技术,为用户量身定制千人千面的二次元动漫形象,并且可通过参数设置,生成戴口罩的二次元动漫人像
* @param image 二进制图像数据
* options 可选参数:
* type anime或者anime_mask。前者生成二次元动漫图后者生成戴口罩的二次元动漫人像
* mask_id 在type参数填入anime_mask时生效18之间的整数用于指定所使用的口罩的编码。type参数没有填入anime_mask或mask_id 为空时,生成不戴口罩的二次元动漫图。
*/
Json::Value selfieanime(
std::string const & image,
const std::map<std::string, std::string> & options)
{
std::map<std::string, std::string> data;
data["image"] = base64_encode(image.c_str(), (int) image.size());
std::copy(options.begin(), options.end(), std::inserter(data, data.end()));
Json::Value result =
this->request(_selfie_anime, null, data, null);
return result;
}
/**
* 图像风格转换
* 可将图像转化成卡通画、铅笔画、彩色铅笔画或者哥特油画、彩色糖块油画、呐喊油画、神奈川冲浪里油画、奇异油画、薰衣草油画等共计9种风格可用于开展趣味活动或集成到美图应用中对图像进行风格转换
* @param image 二进制图像数据
* @param option 转换的风格
* options 可选参数:
*/
Json::Value imagestyletrans(
std::string const & image,
std::string const & option,
const std::map<std::string, std::string> & options)
{
std::map<std::string, std::string> data;
data["image"] = base64_encode(image.c_str(), (int) image.size());
data["option"] = option;
std::copy(options.begin(), options.end(), std::inserter(data, data.end()));
Json::Value result =
this->request(_image_tyle_trans, null, data, null);
return result;
}
/**
* 天空分割
* 可智能分割出天空边界位置,输出天空和其余背景的灰度图和二值图,可用于图像二次处理,进行天空替换、抠图等图片编辑场景。
* @param image 二进制图像数据
* options 可选参数:
*/
Json::Value skyseg(
std::string const & image,
const std::map<std::string, std::string> & options)
{
std::map<std::string, std::string> data;
data["image"] = base64_encode(image.c_str(), (int) image.size());
std::copy(options.begin(), options.end(), std::inserter(data, data.end()));
Json::Value result =
this->request(_sky_seg, null, data, null);
return result;
}
/**
* 图像清晰增强
* 对压缩后的模糊图像实现智能快速去噪,优化图像纹理细节,使画面更加自然清晰
* @param image 二进制图像数据
* options 可选参数:
*/
Json::Value imagedefinitionenhance(
std::string const & image,
const std::map<std::string, std::string> & options)
{
std::map<std::string, std::string> data;
data["image"] = base64_encode(image.c_str(), (int) image.size());
std::copy(options.begin(), options.end(), std::inserter(data, data.end()));
Json::Value result =
this->request(_image_definition_enhance, null, data, null);
return result;
}
/**
* 图像风格转换
* 图像风格转换
* @param url 图片完整url
* @param option 转换的风格
* options 可选参数:
*/
Json::Value imagestyletransurl(
std::string const & url,
std::string const & option,
const std::map<std::string, std::string> & options)
{
std::map<std::string, std::string> data;
data["url"] = url;
data["option"] = option;
std::copy(options.begin(), options.end(), std::inserter(data, data.end()));
Json::Value result =
this->request(_image_tyle_trans, null, data, null);
return result;
}
/**
* 图像色彩增强
* @param url 图片完整url
* options 可选参数:
*/
Json::Value colorenhanceurl(
std::string const & url,
const std::map<std::string, std::string> & options)
{
std::map<std::string, std::string> data;
data["url"] = url;
std::copy(options.begin(), options.end(), std::inserter(data, data.end()));
Json::Value result =
this->request(_color_enhance, null, data, null);
return result;
}
/**
* 人像动漫化接口
* 人像动漫化接口
* @param url 图片完整url
* options 可选参数:
* type anime或者anime_mask。前者生成二次元动漫图后者生成戴口罩的二次元动漫人像
* mask_id 在type参数填入anime_mask时生效18之间的整数用于指定所使用的口罩的编码。type参数没有填入anime_mask或mask_id 为空时,生成不戴口罩的二次元动漫图。
*/
Json::Value selfieanimeurl(
std::string const & url,
const std::map<std::string, std::string> & options)
{
std::map<std::string, std::string> data;
data["url"] = url;
std::copy(options.begin(), options.end(), std::inserter(data, data.end()));
Json::Value result =
this->request(_selfie_anime, null, data, null);
return result;
}
/**
* 天空分割
* @param url 图片完整url
* options 可选参数:
*/
Json::Value skysegurl(
std::string const & url,
const std::map<std::string, std::string> & options)
{
std::map<std::string, std::string> data;
data["url"] = url;
std::copy(options.begin(), options.end(), std::inserter(data, data.end()));
Json::Value result =
this->request(_sky_seg, null, data, null);
return result;
}
/**
* 图像清晰增强
* @param url 图片完整url
* options 可选参数:
*/
Json::Value imagedefinitionenhanceurl(
std::string const & url,
const std::map<std::string, std::string> & options)
{
std::map<std::string, std::string> data;
data["url"] = url;
std::copy(options.begin(), options.end(), std::inserter(data, data.end()));
Json::Value result =
this->request(_image_definition_enhance, null, data, null);
return result;
}
/**
* 图像无损放大
* @param image 二进制图像数据
* options 可选参数:
*/
Json::Value image_quality_enhance_v1(
std::string const & image,
const std::map<std::string, std::string> & options)
{
std::map<std::string, std::string> data;
data["image"] = base64_encode(image.c_str(), (int) image.size());
std::copy(options.begin(), options.end(), std::inserter(data, data.end()));
Json::Value result =
this->request(_image_quality_enhance_v1, null, data, null);
return result;
}
/**
* 图像无损放大
* @param url 图片完整url
* options 可选参数:
*/
Json::Value image_quality_enhance_v1_url(
std::string const & url,
const std::map<std::string, std::string> & options)
{
std::map<std::string, std::string> data;
data["url"] = url;
std::copy(options.begin(), options.end(), std::inserter(data, data.end()));
Json::Value result =
this->request(_image_quality_enhance_v1, null, data, null);
return result;
}
/**
* 图像对比度增强
* @param image 二进制图像数据
* options 可选参数:
*/
Json::Value contrast_enhance_v1(
std::string const & image,
const std::map<std::string, std::string> & options)
{
std::map<std::string, std::string> data;
data["image"] = base64_encode(image.c_str(), (int) image.size());
std::copy(options.begin(), options.end(), std::inserter(data, data.end()));
Json::Value result =
this->request(_contrast_enhance_v1, null, data, null);
return result;
}
/**
* 图像对比度增强
* @param url 图片完整url
* options 可选参数:
*/
Json::Value contrast_enhance_v1_url(
std::string const & url,
const std::map<std::string, std::string> & options)
{
std::map<std::string, std::string> data;
data["url"] = url;
std::copy(options.begin(), options.end(), std::inserter(data, data.end()));
Json::Value result =
this->request(_contrast_enhance_v1, null, data, null);
return result;
}
/**
* 图像去雾
* @param image 二进制图像数据
* options 可选参数:
*/
Json::Value dehaze_v1(
std::string const & image,
const std::map<std::string, std::string> & options)
{
std::map<std::string, std::string> data;
data["image"] = base64_encode(image.c_str(), (int) image.size());
std::copy(options.begin(), options.end(), std::inserter(data, data.end()));
Json::Value result =
this->request(_dehaze_v1, null, data, null);
return result;
}
/**
* 图像去雾
* @param url 图片完整url
* options 可选参数:
*/
Json::Value dehaze_v1_url(
std::string const & url,
const std::map<std::string, std::string> & options)
{
std::map<std::string, std::string> data;
data["url"] = url;
std::copy(options.begin(), options.end(), std::inserter(data, data.end()));
Json::Value result =
this->request(_dehaze_v1, null, data, null);
return result;
}
/**
* 黑白图像上色
* @param image 二进制图像数据
* options 可选参数:
*/
Json::Value colourize_v1(
std::string const & image,
const std::map<std::string, std::string> & options)
{
std::map<std::string, std::string> data;
data["image"] = base64_encode(image.c_str(), (int) image.size());
std::copy(options.begin(), options.end(), std::inserter(data, data.end()));
Json::Value result =
this->request(_colourize_v1, null, data, null);
return result;
}
/**
* 黑白图像上色
* @param url 图片完整url
* options 可选参数:
*/
Json::Value colourize_v1_url(
std::string const & url,
const std::map<std::string, std::string> & options)
{
std::map<std::string, std::string> data;
data["url"] = url;
std::copy(options.begin(), options.end(), std::inserter(data, data.end()));
Json::Value result =
this->request(_colourize_v1, null, data, null);
return result;
}
/**
* 拉伸图像恢复
* @param image 二进制图像数据
* options 可选参数:
*/
Json::Value stretch_restore_v1(
std::string const & image,
const std::map<std::string, std::string> & options)
{
std::map<std::string, std::string> data;
data["image"] = base64_encode(image.c_str(), (int) image.size());
std::copy(options.begin(), options.end(), std::inserter(data, data.end()));
Json::Value result =
this->request(_stretch_restore_v1, null, data, null);
return result;
}
/**
* 拉伸图像恢复
* @param url 图片完整url
* options 可选参数:
*/
Json::Value stretch_restore_v1_url(
std::string const & url,
const std::map<std::string, std::string> & options)
{
std::map<std::string, std::string> data;
data["url"] = url;
std::copy(options.begin(), options.end(), std::inserter(data, data.end()));
Json::Value result =
this->request(_stretch_restore_v1, null, data, null);
return result;
}
/**
* 图片去摩尔纹
* 接口使用文档链接: https://ai.baidu.com/ai-doc/IMAGEPROCESS/ql4wdlnc0
*/
Json::Value remove_moire_v1(
std::string const & image,
const std::map<std::string, std::string> &options)
{
std::map<std::string, std::string> data;
data["image"] = base64_encode(image.c_str(), (int) image.size());
std::copy(options.begin(), options.end(), std::inserter(data, data.end()));
Json::Value result =
this->request(_remove_moire_v1, null, data, null);
return result;
}
/**
* 图片去摩尔纹 - url
* 接口使用文档链接: https://ai.baidu.com/ai-doc/IMAGEPROCESS/ql4wdlnc0
*/
Json::Value remove_moire_v1_url(
std::string const & url,
const std::map<std::string, std::string> &options)
{
std::map<std::string, std::string> data;
data["url"] = url;
std::copy(options.begin(), options.end(), std::inserter(data, data.end()));
Json::Value result =
this->request(_remove_moire_v1, null, data, null);
return result;
}
/**
* 图片去摩尔纹 - pdf
* 接口使用文档链接: https://ai.baidu.com/ai-doc/IMAGEPROCESS/ql4wdlnc0
*/
Json::Value remove_moire_v1_pdf(
std::string const & pdf,
const std::map<std::string, std::string> &options)
{
std::map<std::string, std::string> data;
data["pdf_file"] = base64_encode(pdf.c_str(), (int) pdf.size());
std::copy(options.begin(), options.end(), std::inserter(data, data.end()));
Json::Value result =
this->request(_remove_moire_v1, null, data, null);
return result;
}
/**
* 图像风格自定义
* 接口使用文档链接: https://ai.baidu.com/ai-doc/IMAGEPROCESS/al50vf6bq
*/
Json::Value customize_stylization_v1(std::string const & image, Json::Value & options)
{
Json::Value data;
data["image"] = base64_encode(image.c_str(), (int) image.size());
merge_json(data, options);
std::map<std::string, std::string> headers;
headers["Content-Type"] = "application/x-www-form-urlencoded";
Json::Value result = this->request_com(_customize_stylization_v1, data, &headers);
return result;
}
/**
* 图像风格自定义 - url
* 接口使用文档链接: https://ai.baidu.com/ai-doc/IMAGEPROCESS/al50vf6bq
*/
Json::Value customize_stylization_v1_url(std::string const & url, Json::Value & options)
{
Json::Value data;
data["url"] = url;
merge_json(data, options);
std::map<std::string, std::string> headers;
headers["Content-Type"] = "application/x-www-form-urlencoded";
Json::Value result = this->request_com(_customize_stylization_v1, data, &headers);
return result;
}
/**
* 文档图片去底纹
* 接口使用文档链接: https://ai.baidu.com/ai-doc/IMAGEPROCESS/Nl6os53ab
*/
Json::Value doc_repair_v1(
std::string const & image,
const std::map<std::string, std::string> &options)
{
std::map<std::string, std::string> data;
data["image"] = base64_encode(image.c_str(), (int) image.size());
std::copy(options.begin(), options.end(), std::inserter(data, data.end()));
Json::Value result =
this->request(_doc_repair_v1, null, data, null);
return result;
}
/**
* 文档图片去底纹 - url
* 接口使用文档链接: https://ai.baidu.com/ai-doc/IMAGEPROCESS/Nl6os53ab
*/
Json::Value doc_repair_v1_url(
std::string const &url,
const std::map<std::string, std::string> &options)
{
std::map<std::string, std::string> data;
data["url"] = url;
std::copy(options.begin(), options.end(), std::inserter(data, data.end()));
Json::Value result =
this->request(_doc_repair_v1, null, data, null);
return result;
}
/**
* 图像去噪
* 接口使用文档链接: https://ai.baidu.com/ai-doc/IMAGEPROCESS/Tl78sby7g
*/
Json::Value denoise_v1(
std::string const & image, int option)
{
Json::Value data;
data["image"] = base64_encode(image.c_str(), (int) image.size());
data["option"] = option;
std::map<std::string, std::string> headers;
headers["Content-Type"] = "application/x-www-form-urlencoded";
Json::Value result = this->request_com(_denoise_v1, data, &headers);
return result;
}
/**
* 图像去噪 - url
* 接口使用文档链接: https://ai.baidu.com/ai-doc/IMAGEPROCESS/Tl78sby7g
*/
Json::Value denoise_v1_url(
std::string const &url, int option)
{
Json::Value data;
data["url"] = url;
data["option"] = option;
std::map<std::string, std::string> headers;
headers["Content-Type"] = "application/x-www-form-urlencoded";
Json::Value result = this->request_com(_denoise_v1, data, &headers);
return result;
}
};
}
#endif

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,206 @@
/**
* Copyright (c) 2017 Baidu.com, Inc. All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* @author baidu aip
*/
#ifndef __AIP_KG_H__
#define __AIP_KG_H__
#include "base/base.h"
namespace aip {
class Kg: public AipBase
{
public:
std::string _create_task =
"https://aip.baidubce.com/rest/2.0/kg/v1/pie/task_create";
std::string _update_task =
"https://aip.baidubce.com/rest/2.0/kg/v1/pie/task_update";
std::string _task_info =
"https://aip.baidubce.com/rest/2.0/kg/v1/pie/task_info";
std::string _task_query =
"https://aip.baidubce.com/rest/2.0/kg/v1/pie/task_query";
std::string _task_start =
"https://aip.baidubce.com/rest/2.0/kg/v1/pie/task_start";
std::string _task_status =
"https://aip.baidubce.com/rest/2.0/kg/v1/pie/task_status";
Kg(const std::string & app_id, const std::string & ak, const std::string & sk): AipBase(app_id, ak, sk)
{
}
/**
* create_task
* 创建一个新的信息抽取任务
* @param name 任务名字
* @param template_content json string 解析模板内容
* @param input_mapping_file 抓取结果映射文件的路径
* @param url_pattern url pattern
* @param output_file 输出文件名字
* options 可选参数:
* limit_count 限制解析数量limit_count为0时进行全量任务limit_count>0时只解析limit_count数量的页面
*/
Json::Value create_task(
std::string const & name,
std::string const & template_content,
std::string const & input_mapping_file,
std::string const & url_pattern,
std::string const & output_file,
const std::map<std::string, std::string> & options)
{
std::map<std::string, std::string> data;
data["name"] = name;
data["template_content"] = template_content;
data["input_mapping_file"] = input_mapping_file;
data["url_pattern"] = url_pattern;
data["output_file"] = output_file;
std::copy(options.begin(), options.end(), std::inserter(data, data.end()));
Json::Value result =
this->request(_create_task, null, data, null);
return result;
}
/**
* update_task
* 更新任务配置,在任务重新启动后生效
* @param id 任务ID
* options 可选参数:
* name 任务名字
* template_content json string 解析模板内容
* input_mapping_file 抓取结果映射文件的路径
* url_pattern url pattern
* output_file 输出文件名字
*/
Json::Value update_task(
const int & id,
const std::map<std::string, std::string> & options)
{
std::map<std::string, std::string> data;
data["id"] = std::to_string(id);
std::copy(options.begin(), options.end(), std::inserter(data, data.end()));
Json::Value result =
this->request(_update_task, null, data, null);
return result;
}
/**
* task_info
* 根据任务id获取单个任务的详细信息
* @param id 任务ID
* options 可选参数:
*/
Json::Value task_info(
const int & id,
const std::map<std::string, std::string> & options)
{
std::map<std::string, std::string> data;
data["id"] = std::to_string(id);
std::copy(options.begin(), options.end(), std::inserter(data, data.end()));
Json::Value result =
this->request(_task_info, null, data, null);
return result;
}
/**
* task_query
* 该请求用于菜品识别。即对于输入的一张图片(可正常解码,且长宽比适宜),输出图片的菜品名称、卡路里信息、置信度。
* options 可选参数:
* id 任务ID精确匹配
* name 中缀模糊匹配,abc可以匹配abc,aaabc,abcde等
* status 要筛选的任务状态
* page 页码
* per_page 页码
*/
Json::Value task_query(
const std::map<std::string, std::string> & options)
{
std::map<std::string, std::string> data;
std::copy(options.begin(), options.end(), std::inserter(data, data.end()));
Json::Value result =
this->request(_task_query, null, data, null);
return result;
}
/**
* task_start
* 启动一个已经创建的信息抽取任务
* @param id 任务ID
* options 可选参数:
*/
Json::Value task_start(
const int & id,
const std::map<std::string, std::string> & options)
{
std::map<std::string, std::string> data;
data["id"] = std::to_string(id);
std::copy(options.begin(), options.end(), std::inserter(data, data.end()));
Json::Value result =
this->request(_task_start, null, data, null);
return result;
}
/**
* task_status
* 查询指定的任务的最新执行状态
* @param id 任务ID
* options 可选参数:
*/
Json::Value task_status(
const int & id,
const std::map<std::string, std::string> & options)
{
std::map<std::string, std::string> data;
data["id"] = std::to_string(id);
std::copy(options.begin(), options.end(), std::inserter(data, data.end()));
Json::Value result =
this->request(_task_status, null, data, null);
return result;
}
};
}
#endif

View File

@ -0,0 +1,144 @@
/**
* Copyright (c) 2017 Baidu.com, Inc. All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* @author baidu aip
*/
#ifndef __AIP_MACHINE_TRANSLATION_H__
#define __AIP_MACHINE_TRANSLATION_H__
#include "base/base.h"
namespace aip {
class Machinetranslation : public AipBase
{
public:
std::string _pictrans_v1 =
"https://aip.baidubce.com/file/2.0/mt/pictrans/v1";
std::string _texttrans_v1 =
"https://aip.baidubce.com/rpc/2.0/mt/texttrans/v1";
std::string _texttrans_with_dict_v1 =
"https://aip.baidubce.com/rpc/2.0/mt/texttrans-with-dict/v1";
std::string _doc_translation_create_v2 =
"https://aip.baidubce.com/rpc/2.0/mt/v2/doc-translation/create";
std::string _doc_translation_query_v2 =
"https://aip.baidubce.com/rpc/2.0/mt/v2/doc-translation/query";
std::string _speech_translation_v2 =
"https://aip.baidubce.com/rpc/2.0/mt/v2/speech-translation";
Machinetranslation(const std::string & app_id, const std::string & ak, const std::string & sk)
: AipBase(app_id, ak, sk)
{
}
/**
* 文本翻译-通用版
* 接口使用文档链接: https://ai.baidu.com/ai-doc/MT/4kqryjku9
*/
Json::Value texttrans_v1(
std::string const &from,
std::string const &to,
std::string const &q,
const Json::Value & options)
{
Json::Value data;
data["from"] = from;
data["to"] = to;
data["q"] = q;
merge_json(data, options);
Json::Value result = this->request_com(_texttrans_v1, data);
return result;
}
/**
* 文本翻译-词典版
* 接口使用文档链接: https://ai.baidu.com/ai-doc/MT/nkqrzmbpc
*/
Json::Value texttrans_with_dict_v1(
std::string const &from,
std::string const &to,
std::string const &q,
const Json::Value & options)
{
Json::Value data;
data["from"] = from;
data["to"] = to;
data["q"] = q;
merge_json(data, options);
Json::Value result = this->request_com(_texttrans_with_dict_v1, data);
return result;
}
/**
* 文档翻译
* 接口使用文档链接: https://ai.baidu.com/ai-doc/MT/Xky9x5xub
*/
Json::Value doc_translation_create_v2(
std::string const &from,
std::string const &to,
Json::Value & options)
{
Json::Value data;
data["from"] = from;
data["to"] = to;
merge_json(data, options);
Json::Value result =
this->request_com(_doc_translation_create_v2, data);
return result;
}
/**
* 文档翻译-文档状态查询
* 接口使用文档链接: https://ai.baidu.com/ai-doc/MT/Xky9x5xub
*/
Json::Value doc_translation_query_v2(
std::string const &id)
{
Json::Value data;
data["id"] = id;
Json::Value result = this->request_com(_doc_translation_query_v2, data);
return result;
}
/**
* 语音翻译
* 接口使用文档链接: https://ai.baidu.com/ai-doc/MT/el4cmi76f
*/
Json::Value speech_translation_v2(
std::string const &from,
std::string const &to,
std::string const &voice,
std::string const &format)
{
Json::Value data;
data["from"] = from;
data["to"] = to;
data["voice"] = base64_encode(voice.c_str(), (int) voice.size());
data["format"] = format;
Json::Value result =
this->request_com(_speech_translation_v2, data);
return result;
}
};
}
#endif

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,135 @@
#ifndef __AIP_SPEECH_H__
#define __AIP_SPEECH_H__
#include "base/base.h"
#include <json/json.h>
namespace aip {
class Speech : public AipBase {
public:
std::string _asr = "https://vop.baidu.com/server_api";
std::string _tts = "http://tsn.baidu.com/text2audio";
Speech(const std::string app_id, const std::string &ak, const std::string &sk) : AipBase(app_id, ak, sk) {
}
Json::Value request_asr(
std::string const &url,
Json::Value &data) {
std::string response;
Json::Value obj;
int status_code = this->client.post(url, nullptr, data, nullptr, &response);
if (status_code != CURLcode::CURLE_OK) {
obj[aip::CURL_ERROR_CODE] = status_code;
return obj;
}
std::string error;
std::unique_ptr<Json::CharReader> reader(crbuilder.newCharReader());
reader->parse(response.data(), response.data() + response.size(), &obj, &error);
return obj;
}
Json::Value request_tts(
const std::string url,
std::map<std::string, std::string> &data,
std::string &file_content) {
std::string response;
Json::Value obj;
Json::Value file_json;
int status_code = this->client.post(url, nullptr, data, nullptr, &response);
if (status_code != CURLcode::CURLE_OK) {
obj[aip::CURL_ERROR_CODE] = status_code;
return obj;
}
file_content = response;
return obj;
}
Json::Value recognize(const std::string voice_binary, const std::string &format, const int &rate,
std::map<std::string, std::string> const &options) {
Json::Value data;
std::map<std::string, std::string>::const_iterator it;
for (it = options.begin(); it != options.end(); it++) {
data[it->first] = it->second;
if (it->first == "dev_pid") {
data[it->first] = atoi(it->second.c_str());
}
}
std::string token = this->getAccessToken();
data["speech"] = base64_encode(voice_binary.c_str(), (int) voice_binary.size());
data["format"] = format;
data["rate"] = std::to_string(rate);
data["channel"] = "1";
data["token"] = token;
data["cuid"] = this->getAk();
data["len"] = (int) voice_binary.size();
Json::Value result = this->request_asr(_asr, data);
return result;
}
Json::Value recognize_url(const std::string &url,
const std::string &callback, const std::string &format,
const int &rate,
std::map<std::string, std::string> options) {
Json::Value data;
std::map<std::string, std::string>::iterator it;
for (it = options.begin(); it != options.end(); it++) {
data[it->first] = it->second;
if (it->first == "dev_pid") {
data[it->first] = atoi(it->second.c_str());
}
}
std::string token = this->getAccessToken();
data["url"] = url;
data["callback"] = callback;
data["format"] = format;
data["rate"] = std::to_string(rate);
data["channel"] = 1;
data["token"] = token;
data["cuid"] = this->getAk();
Json::Value result = this->request_asr(_asr, data);
return result;
}
Json::Value text2audio(const std::string &text, std::map<std::string, std::string> const &options,
std::string &file_content) {
std::map<std::string, std::string> data;
std::map<std::string, std::string>::const_iterator it;
for (it = options.begin(); it != options.end(); it++) {
data[it->first] = it->second;
}
std::string token = this->getAccessToken();
data["tex"] = text;
data["lan"] = "zh";
data["ctp"] = "1";
data["tok"] = token;
data["cuid"] = this->getAk();
Json::Value result = this->request_tts(_tts, data, file_content);
return result;
}
};
}
#endif

View File

@ -0,0 +1,63 @@
/**
* Copyright (c) 2017 Baidu.com, Inc. All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* @author baidu aip
*/
#ifndef __AIP_VIDEOCENSOR_H__
#define __AIP_VIDEOCENSOR_H__
#include "base/base.h"
namespace aip {
class Videocensor: public AipBase
{
public:
std::string _video_url =
"https://aip.baidubce.com/rest/2.0/solution/v1/video_censor/v2/user_defined";
Videocensor(const std::string & app_id, const std::string & ak, const std::string & sk): AipBase(app_id, ak, sk)
{
}
/**
* voice_censor
* 本接口除了支持自定义配置外,还对返回结果进行了总体的包装,按照用户在控制台中配置的规则直接返回是否合规,如果不合规则指出具体不合规的内容。
* @param voice 语音文件二进制内容可以使用aip::get_file_content函数获取
* options 可选参数:
*/
Json::Value video_censor(
std::string const & name,
std::string const & url,
std::string const & extId,
const std::map<std::string, std::string> & options)
{
std::map<std::string, std::string> data;
data["videoUrl"] = url;
data["name"] = name;
data["extId"] = extId;
std::copy(options.begin(), options.end(), std::inserter(data, data.end()));
Json::Value result =
this->request(_video_url, null, data, null);
return result;
}
};
}
#endif

View File

@ -0,0 +1,87 @@
/**
* Copyright (c) 2017 Baidu.com, Inc. All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* @author baidu aip
*/
#ifndef __AIP_IMAGECENSOR_H__
#define __AIP_IMAGECENSOR_H__
#include "base/base.h"
namespace aip {
class Voicecensor: public AipBase
{
public:
std::string _voice_url =
"https://aip.baidubce.com/rest/2.0/solution/v1/voice_censor/v3/user_defined";
Voicecensor(const std::string & app_id, const std::string & ak, const std::string & sk): AipBase(app_id, ak, sk)
{
}
/**
* voice_censor
* 本接口除了支持自定义配置外,还对返回结果进行了总体的包装,按照用户在控制台中配置的规则直接返回是否合规,如果不合规则指出具体不合规的内容。
* @param voice 语音文件二进制内容可以使用aip::get_file_content函数获取
* options 可选参数:
*/
Json::Value voice_censor(
std::string const & voice,
std::int32_t const & rate,
std::string const & fmt,
const std::map<std::string, std::string> & options)
{
std::map<std::string, std::string> data;
data["base64"] = base64_encode(voice.c_str(), (int) voice.size());
data["fmt"] = fmt;
data["rate"] = rate;
std::copy(options.begin(), options.end(), std::inserter(data, data.end()));
Json::Value result =
this->request(_voice_url, null, data, null);
return result;
}
/**
* voice_censor
* 本接口除了支持自定义配置外,还对返回结果进行了总体的包装,按照用户在控制台中配置的规则直接返回是否合规,如果不合规则指出具体不合规的内容。
* @param voice 语音文件二进制内容可以使用aip::get_file_content函数获取
* options 可选参数:
*/
Json::Value voice_censorUrl(
std::string const & url,
std::int32_t const & rate,
std::string const & fmt,
const std::map<std::string, std::string> & options)
{
std::map<std::string, std::string> data;
data["url"] = url;
data["fmt"] = fmt;
data["rate"] = rate;
std::copy(options.begin(), options.end(), std::inserter(data, data.end()));
Json::Value result =
this->request(_voice_url, null, data, null);
return result;
}
};
}
#endif