赞
踩
https://ai.baidu.com/sdk#asr
下载SDK并查看接口相关说明下载对应SDK
├── base
│ ├── base.h // 请求客户端基类
│ ├── base64.h // base64加密相关类
│ ├── http.h // http请求封装类
│ └── utils.h // 工具类
└── nlp.h // 自然语言处理 交互类
安装依赖库libcurl(需要支持https) openssl jsoncpp(>1.6.2版本)
编译工程时添加 C++11 支持 (gcc/clang 添加编译参数 -std=c++11), 添加第三方库链接参数 lcurl, lcrypto, ljsoncpp。
在源码中include"nlp.h"
,引入压缩包中的头文件以使用aip命名空间下的类和方法。
程序框架搭建
nlp/
├── base
│ ├── base64.h
│ ├── base.h
│ ├── http.h
│ └── utils.h
├── body_analysis.h
├── content_censor.h
├── face.h
├── image_censor.h
├── image_classify.h
├── image_process.h
├── image_search.h
├── kg.h
├── machine_translation.h
├── nlp.h
├── ocr.h
├── README.md
├── speech.h
├── src
│ └── main.cpp
├── video_censor.h
└── voice_censor.h
2 directories, 21 files
执行编译指令gcc main.cpp -o main -std=c++11
提示././base/http.h:18:23: fatal error: curl/curl.h: 没有那个文件或目录
找到安装的curl的位置,找到include目录位置
再次编译gcc main.cpp -o main -std=c++11 -I /usr/local/curl/include
.././base/http.h:23:23: fatal error: json/json.h: 没有那个文件或目录
dpkg -l | grep jsoncpp-dev
sudo apt-get install libjsoncpp-dev
dpkg -L libjsoncpp-dev
再次编译gcc main.cpp -o main -std=c++11 -I /usr/local/curl/include -I/usr/include/jsoncpp
加入例程
#include <iostream>
#include "../nlp.h"
#include "../base/base.h"
#include "/usr/include/jsoncpp/json/json.h"
// 设置APPID/AK/SK
std::string app_id = "你的 App ID";
std::string api_key = "你的 Api key";
std::string secret_key = "你的 Secret Key";
aip::Nlp client(app_id, api_key, secret_key);
int main(int argc, char const *argv[])
{
Json::Value result;
std::string text = "百度是一家高科技公司";
// 调用词法分析
result = client.lexer(text, aip::null);
return 0;
}
加入库文件,再次编译gcc main.cpp -o main -std=c++11 -I /usr/local/curl/include -I/usr/include/jsoncpp -lcurl -lcrypto -ljsoncpp
/usr/bin/ld: 找不到 -lcurl
加入头文件路径,再次编译gcc main.cpp -o main -std=c++11 -I /usr/local/curl/include -I/usr/include/jsoncpp -L /usr/lib/x86_64-linux-gnu -lcurl -lcrypto -ljsoncpp -L /usr/local/curl/lib
/usr/bin/ld: /tmp/cctz4Ibz.o: undefined reference to symbol '_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEaSEPKc@@GLIBCXX_3.4.21' //usr/lib/x86_64-linux-gnu/libstdc++.so.6: error adding symbols: DSO missing from command line
用g++,再次编译g++ main.cpp -o main -std=c++11 -I /usr/local/curl/include -I/usr/include/jsoncpp -L /usr/lib/x86_64-linux-gnu -lcurl -lcrypto -ljsoncpp -L /usr/local/curl/lib
编译成功
加入设置的设备相关参数,并加入打印信息
#include <iostream>
#include "../nlp.h"
#include "../base/base.h"
#include "/usr/include/jsoncpp/json/json.h"
// 设置APPID/AK/SK
std::string app_id = "34215401";
std::string api_key = "fjNddzHij8swD23qDtgRrmv5";
std::string secret_key = "2R02CforwL4qtD4rGGlbegzaooTG9HS0";
aip::Nlp client(app_id, api_key, secret_key);
int main(int argc, char const *argv[])
{
Json::Value result;
std::string jsonData;
Json::FastWriter write;
#if 1
std::string text = "百度是一家高科技公司";
// 调用词法分析
//result = client.lexer(text, aip::null);
//调用语言生成——写诗
result = client.poem("离愁",aip::null);
#else
std::string text = "上海市浦东新区纳贤路701号百度上海研发中心 F4A000 张三";
result = client.address_v1(text, aip::null);
#endif
jsonData = write.write(result);
printf("%s\r\n",jsonData.c_str());
return 0;
}
{"log_id":1664820259893206066,"poem":[{"content":"一曲离愁对月弹\t千般别绪向谁言\t相思梦里随风去\t欲语还休泪已干","title":"离愁"}]}
交叉编译curl,参考99.3.2
交叉编译openssl,参考99.5.1
交叉编译jsoncpp,参考99.7.1
创建arm编译脚本
arm-linux-gnueabihf-g++ main.cpp -o main \
-std=c++11 \
-I /home/lux/Downloads/Curl/Arm/curl-8.1.2/include/ \
-I /home/lux/Downloads/Jsoncpp/jsoncpp_Arm/include/ \
-I /home/lux/Downloads/OpenSSL/ForArm/openssl-1.1.1d/include/ \
-L /home/lux/Downloads/Curl/Arm/curl-8.1.2/_install_Forarm/lib/ \
-L /home/lux/Downloads/Jsoncpp/jsoncpp_Arm/_install_forArm/lib/ \
-L /home/lux/Downloads/OpenSSL/ForArm/openssl-1.1.1d/_install_forArm/lib/ \
-lcurl -lcrypto -ljsoncpp \
提示
/home/lux/Downloads/Curl/Arm/curl-8.1.2/_install_Forarm/lib//libcurl.so:对‘SSL_CTX_set_srp_password@OPENSSL_1_1_0’未定义的引用
/home/lux/Downloads/Curl/Arm/curl-8.1.2/_install_Forarm/lib//libcurl.so:对‘SSL_CTX_use_certificate_chain_file@OPENSSL_1_1_0’未定义的引用
/home/lux/Downloads/Curl/Arm/curl-8.1.2/_install_Forarm/lib//libcurl.so:对‘OPENSSL_init_ssl@OPENSSL_1_1_0’未定义的引用
/home/lux/Downloads/Curl/Arm/curl-8.1.2/_install_Forarm/lib//libcurl.so:对‘SSL_set0_wbio@OPENSSL_1_1_0’未定义的引用
/home/lux/Downloads/Curl/Arm/curl-8.1.2/_install_Forarm/lib//libcurl.so:对‘SSL_CTX_use_PrivateKey@OPENSSL_1_1_0’未定义的引用
...
/home/lux/Downloads/Curl/Arm/curl-8.1.2/_install_Forarm/lib//libcurl.so:对‘SSL_CTX_set_ciphersuites@OPENSSL_1_1_1’未定义的引用
/home/lux/Downloads/Curl/Arm/curl-8.1.2/_install_Forarm/lib//libcurl.so:对‘SSL_CTX_set_cipher_list@OPENSSL_1_1_0’未定义的引用
/home/lux/Downloads/Curl/Arm/curl-8.1.2/_install_Forarm/lib//libcurl.so:对‘SSL_get_peer_certificate@OPENSSL_1_1_0’未定义的引用
/home/lux/Downloads/Curl/Arm/curl-8.1.2/_install_Forarm/lib//libcurl.so:对‘SSL_CTX_set_verify@OPENSSL_1_1_0’未定义的引用
/home/lux/Downloads/Curl/Arm/curl-8.1.2/_install_Forarm/lib//libcurl.so:对‘SSL_shutdown@OPENSSL_1_1_0’未定义的引用
/home/lux/Downloads/Curl/Arm/curl-8.1.2/_install_Forarm/lib//libcurl.so:对‘SSL_get_privatekey@OPENSSL_1_1_0’未定义的引用
/home/lux/Downloads/Curl/Arm/curl-8.1.2/_install_Forarm/lib//libcurl.so:对‘SSL_get0_alpn_selected@OPENSSL_1_1_0’未定义的引用
说是ssl和curl的版本不匹配
降低curl为7.38.0版本
重新交叉编译一个curl版本
修改arm的编译脚本
arm-linux-gnueabihf-g++ main.cpp -o main \
-std=c++11 \
-I /home/lux/Downloads/Curl/Arm/curl-7.38.0/include/ \
-I /home/lux/Downloads/Jsoncpp/jsoncpp_Arm/include/ \
-I /home/lux/Downloads/OpenSSL/ForArm/openssl-1.1.1d/include/ \
-L /home/lux/Downloads/Curl/Arm/curl-7.38.0/_install_Forarm/lib/ \
-L /home/lux/Downloads/Jsoncpp/jsoncpp_Arm/_install_forArm/lib/ \
-L /home/lux/Downloads/OpenSSL/ForArm/openssl-1.1.1d/_install_forArm/lib/ \
-lcurl -lcrypto -ljsoncpp \
编译成功
将main放到根文件系统中,在开发板上执行./main
./main: error while loading shared libraries: libcurl.so.4: cannot open shared object file: No such file or directory
├── bin
│ ├── curl
│ └── curl-config
├── include
│ └── curl
│ ├── curlbuild.h
│ ├── curl.h
│ ├── curlrules.h
│ ├── curlver.h
│ ├── easy.h
│ ├── mprintf.h
│ ├── multi.h
│ ├── stdcheaders.h
│ └── typecheck-gcc.h
├── lib
│ ├── libcurl.a
│ ├── libcurl.la
│ ├── libcurl.so -> libcurl.so.4.3.0
│ ├── libcurl.so.4 -> libcurl.so.4.3.0
│ ├── libcurl.so.4.3.0
│ └── pkgconfig
│ └── libcurl.pc
└── share
├── aclocal
│ └── libcurl.m4
└── man
├── man1
└── man3
sudo cp bin/* ~/Linux/nfs/buildrootfs2019/usr/bin/ -rfa
sudo cp include/* ~/Linux/nfs/buildrootfs2019/usr/include/ -rfa
sudo cp lib/* ~/Linux/nfs/buildrootfs2019/usr/lib/ -rfa
sudo cp share/* ~/Linux/nfs/buildrootfs2019/usr/share/ -rfa
在开发板上再次执行./main
./main: error while loading shared libraries: libjsoncpp.so.25: cannot open shared object file: No such file or directory
sudo cp lib/* ~/Linux/nfs/buildrootfs2019/usr/lib/ -rfa
在开发板上再次执行./main
{"curl_error_code":1}
提示的错误信息意思是指curl不支持协议,从百度智能云的访问情况来看应该是https协议,通过在开发板上执行curl -V
查看,确实不支持
curl-7.38.0移植配置的时候就能看到不支持https协议,没想到在这里出问题了,搞了一天半,各种版本配合测试,都不行,curl-8.1.2支持https协议,但是程序检查编译的时候不行,于是就想了一个不是办法的办法。编译的时候,用指定的7.38.0的路径,但是,开发板的文件系统的库用curl-8.1.2的文件。这样就可以交叉编译了,同时开发板也支持https协议了。我试过是可以的。一个人独自研究是太慢了,搜了好多文章还是没解决,看来Linux学习是应该找同伴一起。
执行交叉编译
arm-linux-gnueabihf-g++ main.cpp -o main \
-std=c++11 \
-I /home/lux/Downloads/Curl/Arm/curl-7.38.0/include/ \
-I /home/lux/Downloads/Jsoncpp/jsoncpp_Arm/include/ \
-I /home/lux/Downloads/OpenSSL/ForArm/openssl-1.1.1d/include/ \
-L /home/lux/Downloads/Curl/Arm/curl-7.38.0/_install_Forarm/lib \
-L /home/lux/Downloads/Jsoncpp/jsoncpp_Arm/_install_forArm/lib/ \
-L /home/lux/Downloads/OpenSSL/ForArm/openssl-1.1.1d/_install_forArm/lib/ \
-lcurl -lcrypto -ljsoncpp \
在开发板上运行
{"log_id":1665275110318083458,"poem":[{"content":"一曲离愁对月弹\t千般别绪向谁言\t相思梦里随风去\t欲语还休泪已干","title":"离愁"}]}
成功
创建工程文档
工程添加SDK文档
NLP
├── base
│ ├── base64.h
│ ├── base.h
│ ├── http.h
│ └── utils.h
├── body_analysis.h
├── content_censor.h
├── face.h
├── image_censor.h
├── image_classify.h
├── image_process.h
├── image_search.h
├── kg.h
├── machine_translation.h
├── main.cpp
├── mainwindow.cpp
├── mainwindow.h
├── mainwindow.ui
├── nlp.h
├── NLP.pro
├── NLP.pro.user
├── ocr.h
├── speech.h
├── video_censor.h
└── voice_censor.h
Qt界面工程中添加现有文件nlp.h
以及base文件夹下的所有文件
Ui界面搞一个按钮触发
mianwindow.cpp
写程序
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <qdebug.h>
#include "nlp.h"
#include "base/base.h"
#include "/usr/include/jsoncpp/json/json.h"
// 设置APPID/AK/SK
std::string app_id = "34215401";
std::string api_key = "fjNddzHij8swD23qDtgRrmv5";
std::string secret_key = "2R02CforwL4qtD4rGGlbegzaooTG9HS0";
aip::Nlp client(app_id, api_key, secret_key);
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
//connect(ui->pushButton,&QPushButton::clicked,&MainWindow::GreatPoem);
connect(ui->pushButton,&QPushButton::clicked,this,&MainWindow::GreatPoem);
}
MainWindow::~MainWindow()
{
delete ui;
}
int MainWindow::GreatPoem()
{
Json::Value result;
std::string jsonData;
Json::FastWriter write;
#if 1
std::string text = "百度是一家高科技公司";
// 调用词法分析
//result = client.lexer(text, aip::null);
//调用语言生成——写诗
result = client.poem("离愁",aip::null);
#else
std::string text = "上海市浦东新区纳贤路701号百度上海研发中心 F4A000 张三";
result = client.address_v1(text, aip::null);
#endif
jsonData = write.write(result);
qDebug() << jsonData.c_str();
return 0;
}
NLP.pro中添加头文件路径以及库文件路径(根据自己的实际情况修改)
INCLUDEPATH += /usr/local/curl/include \
/usr/include/jsoncpp \
LIBS += /usr/lib/x86_64-linux-gnu/libjsoncpp.so \
/usr/lib/x86_64-linux-gnu/libcrypto.so \
/usr/local/curl/lib/libcurl.so \
其他步骤不变,将pro文件中的头文件路径修改下,编程自己交叉编译的路径即可
INCLUDEPATH += /home/lux/Downloads/Curl/Arm/curl-7.38.0/include/ \
/home/lux/Downloads/Jsoncpp/jsoncpp_Arm/include/ \
/home/lux/Downloads/OpenSSL/ForArm/openssl-1.1.1d/include/ \
LIBS += /home/lux/Downloads/Jsoncpp/jsoncpp_Arm/_install_forArm/lib/libjsoncpp.so \
/home/lux/Downloads/OpenSSL/ForArm/openssl-1.1.1d/_install_forArm/lib/libcrypto.so \
/home/lux/Downloads/Curl/Arm/curl-7.38.0/_install_Forarm/lib/libcurl.so \
编译,我的编译是在工程目录下执行qmake
然后执行make
,即可生成目标文件
移植到根文件系统中去即可
执行,LCD屏幕显示 {\"log_id\":1669504523329624528,\"poem\":[{\"content\":\"\\u4e00\\u66f2\\u79bb\\u6101\\u5bf9\\u6708\\u5f39\t\\u5343\\u822c\\u522b\\u7eea\\u5411\\u8c01\\u8a00\t\\u76f8\\u601d\\u68a6\\u91cc\\u968f\\u98ce\\u53bb\t\\u6b32\\u8bed\\u8fd8\\u4f11\\u6cea\\u5df2\\u5e72\",\"title\":\"\\u79bb\\u6101\"}]}";
有显示,但是时Unicode编码格式的,后续需要转换格式
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <qdebug.h>
#include "nlp.h"
#include "base/base.h"
#include "/usr/include/jsoncpp/json/json.h"
// 设置APPID/AK/SK
std::string app_id = "34215401";
std::string api_key = "fjNddzHij8swD23qDtgRrmv4";
std::string secret_key = "2R02CforwL4qtD4rGGlbegzaooTG9HS5";
aip::Nlp client(app_id, api_key, secret_key);
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->pushButton,&QPushButton::clicked,this,&MainWindow::GreatPoem);
connect(ui->pushButton1,&QPushButton::clicked,this,&MainWindow::DisplayChniese);
}
MainWindow::~MainWindow()
{
delete ui;
}
/*******************************************************************
*@function : unicodeToUTF8
*@description: 将unicode编码字符串转换成utf8格式
*@input para :
*@returnValue:
*@auther :
*@time :
*@note :
********************************************************************/
std::string unicodeToUTF8(const std::string& str)
{
std::string result;
for (size_t i = 0; i < str.length(); i++) {
if (str[i] == '\\') {
if (i + 5 < str.length() && str[i+1] == 'u') {
unsigned int codepoint = 0;
for (size_t j = 0; j < 4; j++) {
char c = str[i+2+j];
codepoint <<= 4;
if (c >= '0' && c <= '9')
codepoint += c - '0';
else if (c >= 'a' && c <= 'f')
codepoint += c - 'a' + 10;
else if (c >= 'A' && c <= 'F')
codepoint += c - 'A' + 10;
else
return ""; // invalid Unicode escape sequence
}
i += 5; // skip over the Unicode escape sequence
if (codepoint <= 0x7F) {
result += (char)(codepoint & 0xFF);
} else if (codepoint <= 0x7FF) {
result += (char)(((codepoint >> 6) & 0x1F) | 0xC0);
result += (char)((codepoint & 0x3F) | 0x80);
} else if (codepoint <= 0xFFFF) {
result += (char)(((codepoint >> 12) & 0x0F) | 0xE0);
result += (char)(((codepoint >> 6) & 0x3F) | 0x80);
result += (char)((codepoint & 0x3F) | 0x80);
} else if (codepoint <= 0x10FFFF) {
result += (char)(((codepoint >> 18) & 0x07) | 0xF0);
result += (char)(((codepoint >> 12) & 0x3F) | 0x80);
result += (char)(((codepoint >> 6) & 0x3F) | 0x80);
result += (char)((codepoint & 0x3F) | 0x80);
} else
return ""; // invalid Unicode codepoint
} else
return ""; // invalid escape sequence
} else
result += str[i];
}
return result;
}
/*******************************************************************
*@function : GreatPoem
*@description: 生成写诗
*@input para :
*@returnValue:
*@auther :
*@time :
*@note :
********************************************************************/
int MainWindow::GreatPoem()
{
Json::Value result;
std::string jsonData;
Json::FastWriter write;
#if 0
#if 1
// 调用词法分析
//std::string text = "百度是一家高科技公司";
//result = client.lexer(text, aip::null);
//调用语言生成——写诗
result = client.poem("离愁",aip::null);
#else
std::string text = "上海市浦东新区纳贤路701号百度上海研发中心 F4A000 张三";
result = client.address_v1(text, aip::null);
#endif
jsonData = write.write(result);
qDebug() << jsonData.c_str();
#endif
std::string json_str_src = "{\"log_id\":1669504523329624528,\"poem\":[{\"content\":\"\\u4e00\\u66f2\\u79bb\\u6101\\u5bf9\\u6708\\u5f39\t\\u5343\\u822c\\u522b\\u7eea\\u5411\\u8c01\\u8a00\t\\u76f8\\u601d\\u68a6\\u91cc\\u968f\\u98ce\\u53bb\t\\u6b32\\u8bed\\u8fd8\\u4f11\\u6cea\\u5df2\\u5e72\",\"title\":\"\\u79bb\\u6101\"}]}";
//std::string str = json_str.replace("\t","\n");
QString temp_str = QString::fromStdString(json_str_src);
temp_str = temp_str.replace("\t","\n");
std::string json_str = temp_str.toStdString();
Json::Reader reader;
Json::Value root;
//解析json字符串数据
if (!reader.parse(json_str, root)) {
std::cerr << "failed to parse JSON" << std::endl;
return -1;
}
std::string chinese = root["poem"][0]["content"].asString();
std::string tit = root["poem"][0]["title"].asString();
//使用了QString类的fromUtf8()方法将转换后的UTF-8字符串转换为QString对象
QString content = QString::fromUtf8(chinese.c_str());
QString title = QString::fromUtf8(tit.c_str());
//ui->plainTextEdit->setWordWrapMode(QTextOption::ManualWrap); //设置手动换行
ui->plainTextEdit->appendPlainText(title); //标题
ui->plainTextEdit->insertPlainText("\n"); //插入换行符
ui->plainTextEdit->appendPlainText(content); //内容
return 0;
}
void MainWindow::DisplayChniese()
{
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。