39.1 目標(biāo)
- 通過百度智能云的API調(diào)度寫一首指定主題的詩
39.2 網(wǎng)址
-
https://ai.baidu.com/sdk#asr
下載SDK并查看接口相關(guān)說明
39.3 開發(fā)步驟(Ubuntu開發(fā))
-
下載對(duì)應(yīng)SDK
├── base │ ├── base.h // 請(qǐng)求客戶端基類 │ ├── base64.h // base64加密相關(guān)類 │ ├── http.h // http請(qǐng)求封裝類 │ └── utils.h // 工具類 └── nlp.h // 自然語言處理 交互類
-
安裝依賴庫libcurl(需要支持https) openssl jsoncpp(>1.6.2版本)
-
編譯工程時(shí)添加 C++11 支持 (gcc/clang 添加編譯參數(shù) -std=c++11), 添加第三方庫鏈接參數(shù) 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
-
執(zhí)行編譯指令
gcc main.cpp -o main -std=c++11
-
提示
././base/http.h:18:23: fatal error: curl/curl.h: 沒有那個(gè)文件或目錄
-
找到安裝的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: 沒有那個(gè)文件或目錄
- 查看是否安裝josncpp,
dpkg -l | grep jsoncpp-dev
- 沒有安裝則執(zhí)行安裝
sudo apt-get install libjsoncpp-dev
- 沒有安裝則執(zhí)行安裝
- 查看安裝位置
dpkg -L libjsoncpp-dev
- 添加安裝包含的頭文件路徑
- 提示
-
再次編譯
gcc main.cpp -o main -std=c++11 -I /usr/local/curl/include -I/usr/include/jsoncpp
- 無頭文件路徑錯(cuò)誤
-
加入例程
#include <iostream> #include "../nlp.h" #include "../base/base.h" #include "/usr/include/jsoncpp/json/json.h" // 設(shè)置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 = "百度是一家高科技公司"; // 調(diào)用詞法分析 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
-
編譯成功
-
加入設(shè)置的設(shè)備相關(guān)參數(shù),并加入打印信息
#include <iostream> #include "../nlp.h" #include "../base/base.h" #include "/usr/include/jsoncpp/json/json.h" // 設(shè)置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 = "百度是一家高科技公司"; // 調(diào)用詞法分析 //result = client.lexer(text, aip::null); //調(diào)用語言生成——寫詩 result = client.poem("離愁",aip::null); #else std::string text = "上海市浦東新區(qū)納賢路701號(hào)百度上海研發(fā)中心 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":"一曲離愁對(duì)月彈\t千般別緒向誰言\t相思?jí)衾镫S風(fēng)去\t欲語還休淚已干","title":"離愁"}]}
39.4 移植到開發(fā)板
-
交叉編譯curl,參考99.3.2
-
交叉編譯openssl,參考99.5.1
-
交叉編譯jsoncpp,參考99.7.1
-
創(chuàng)建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:對(duì)‘SSL_CTX_set_srp_password@OPENSSL_1_1_0’未定義的引用 /home/lux/Downloads/Curl/Arm/curl-8.1.2/_install_Forarm/lib//libcurl.so:對(duì)‘SSL_CTX_use_certificate_chain_file@OPENSSL_1_1_0’未定義的引用 /home/lux/Downloads/Curl/Arm/curl-8.1.2/_install_Forarm/lib//libcurl.so:對(duì)‘OPENSSL_init_ssl@OPENSSL_1_1_0’未定義的引用 /home/lux/Downloads/Curl/Arm/curl-8.1.2/_install_Forarm/lib//libcurl.so:對(duì)‘SSL_set0_wbio@OPENSSL_1_1_0’未定義的引用 /home/lux/Downloads/Curl/Arm/curl-8.1.2/_install_Forarm/lib//libcurl.so:對(duì)‘SSL_CTX_use_PrivateKey@OPENSSL_1_1_0’未定義的引用 ... /home/lux/Downloads/Curl/Arm/curl-8.1.2/_install_Forarm/lib//libcurl.so:對(duì)‘SSL_CTX_set_ciphersuites@OPENSSL_1_1_1’未定義的引用 /home/lux/Downloads/Curl/Arm/curl-8.1.2/_install_Forarm/lib//libcurl.so:對(duì)‘SSL_CTX_set_cipher_list@OPENSSL_1_1_0’未定義的引用 /home/lux/Downloads/Curl/Arm/curl-8.1.2/_install_Forarm/lib//libcurl.so:對(duì)‘SSL_get_peer_certificate@OPENSSL_1_1_0’未定義的引用 /home/lux/Downloads/Curl/Arm/curl-8.1.2/_install_Forarm/lib//libcurl.so:對(duì)‘SSL_CTX_set_verify@OPENSSL_1_1_0’未定義的引用 /home/lux/Downloads/Curl/Arm/curl-8.1.2/_install_Forarm/lib//libcurl.so:對(duì)‘SSL_shutdown@OPENSSL_1_1_0’未定義的引用 /home/lux/Downloads/Curl/Arm/curl-8.1.2/_install_Forarm/lib//libcurl.so:對(duì)‘SSL_get_privatekey@OPENSSL_1_1_0’未定義的引用 /home/lux/Downloads/Curl/Arm/curl-8.1.2/_install_Forarm/lib//libcurl.so:對(duì)‘SSL_get0_alpn_selected@OPENSSL_1_1_0’未定義的引用
-
說是ssl和curl的版本不匹配
-
降低curl為7.38.0版本
-
重新交叉編譯一個(gè)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放到根文件系統(tǒng)中,在開發(fā)板上執(zhí)行
./main
- 提示
./main: error while loading shared libraries: libcurl.so.4: cannot open shared object file: No such file or directory
- 把交叉編譯的curl的文件放到根文件系統(tǒng)的中
├── 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
- 提示
-
在開發(fā)板上再次執(zhí)行
./main
- 提示
./main: error while loading shared libraries: libjsoncpp.so.25: cannot open shared object file: No such file or directory
- 把交叉編譯的jsoncpp的文件放到根文件系統(tǒng)的中
sudo cp lib/* ~/Linux/nfs/buildrootfs2019/usr/lib/ -rfa
- 提示
-
在開發(fā)板上再次執(zhí)行
./main
- 提示
{"curl_error_code":1}
- 看來是能成功運(yùn)行了
- 提示
-
提示的錯(cuò)誤信息意思是指curl不支持協(xié)議,從百度智能云的訪問情況來看應(yīng)該是https協(xié)議,通過在開發(fā)板上執(zhí)行
curl -V
查看,確實(shí)不支持 -
curl-7.38.0移植配置的時(shí)候就能看到不支持https協(xié)議,沒想到在這里出問題了,搞了一天半,各種版本配合測試,都不行,curl-8.1.2支持https協(xié)議,但是程序檢查編譯的時(shí)候不行,于是就想了一個(gè)不是辦法的辦法。編譯的時(shí)候,用指定的7.38.0的路徑,但是,開發(fā)板的文件系統(tǒng)的庫用curl-8.1.2的文件。這樣就可以交叉編譯了,同時(shí)開發(fā)板也支持https協(xié)議了。我試過是可以的。一個(gè)人獨(dú)自研究是太慢了,搜了好多文章還是沒解決,看來Linux學(xué)習(xí)是應(yīng)該找同伴一起。
-
執(zhí)行交叉編譯
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 \
-
在開發(fā)板上運(yùn)行
{"log_id":1665275110318083458,"poem":[{"content":"一曲離愁對(duì)月彈\t千般別緒向誰言\t相思?jí)衾镫S風(fēng)去\t欲語還休淚已干","title":"離愁"}]}
成功
39.5 移植Qt上(Ubuntu上)
-
創(chuàng)建工程文檔
-
工程添加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界面工程中添加現(xiàn)有文件
nlp.h
以及base文件夾下的所有文件 -
Ui界面搞一個(gè)按鈕觸發(fā)
-
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" // 設(shè)置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 = "百度是一家高科技公司"; // 調(diào)用詞法分析 //result = client.lexer(text, aip::null); //調(diào)用語言生成——寫詩 result = client.poem("離愁",aip::null); #else std::string text = "上海市浦東新區(qū)納賢路701號(hào)百度上海研發(fā)中心 F4A000 張三"; result = client.address_v1(text, aip::null); #endif jsonData = write.write(result); qDebug() << jsonData.c_str(); return 0; }
-
NLP.pro中添加頭文件路徑以及庫文件路徑(根據(jù)自己的實(shí)際情況修改)
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 \
39.6 移植Qt開發(fā)板上
-
其他步驟不變,將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 \
-
編譯,我的編譯是在工程目錄下執(zhí)行
qmake
-
然后執(zhí)行
make
,即可生成目標(biāo)文件 -
移植到根文件系統(tǒng)中去即可
-
執(zhí)行,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\"}]}";
文章來源:http://www.zghlxwxcb.cn/news/detail-514980.html -
有顯示,但是時(shí)Unicode編碼格式的,后續(xù)需要轉(zhuǎn)換格式文章來源地址http://www.zghlxwxcb.cn/news/detail-514980.html
39.7 后續(xù)優(yōu)化()
- 現(xiàn)在難點(diǎn)基本實(shí)現(xiàn)完成了,剩余的優(yōu)化工作包括:
- 根文件系統(tǒng)的中文顯示
- 界面的優(yōu)化
39.8中文顯示(參考)
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <qdebug.h>
#include "nlp.h"
#include "base/base.h"
#include "/usr/include/jsoncpp/json/json.h"
// 設(shè)置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編碼字符串轉(zhuǎn)換成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
// 調(diào)用詞法分析
//std::string text = "百度是一家高科技公司";
//result = client.lexer(text, aip::null);
//調(diào)用語言生成——寫詩
result = client.poem("離愁",aip::null);
#else
std::string text = "上海市浦東新區(qū)納賢路701號(hào)百度上海研發(fā)中心 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字符串?dāng)?shù)據(jù)
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()方法將轉(zhuǎn)換后的UTF-8字符串轉(zhuǎn)換為QString對(duì)象
QString content = QString::fromUtf8(chinese.c_str());
QString title = QString::fromUtf8(tit.c_str());
//ui->plainTextEdit->setWordWrapMode(QTextOption::ManualWrap); //設(shè)置手動(dòng)換行
ui->plainTextEdit->appendPlainText(title); //標(biāo)題
ui->plainTextEdit->insertPlainText("\n"); //插入換行符
ui->plainTextEdit->appendPlainText(content); //內(nèi)容
return 0;
}
void MainWindow::DisplayChniese()
{
}
到了這里,關(guān)于百度智能云——自然語言處理的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!