vs2015 c++ 調用百度雲API人像分割算法

目的:在本地輸入圖像,調用百度雲上的人像分割算法,實現人像分割。

網址:https://login.bce.baidu.com/?account=&redirect=http%3A%2F%2Fconsole.bce.baidu.com%2Fai%2F%3Ffromai%3D1#/ai/body/app/list

需要用百度賬號和密碼登錄。

1 、創建應用

1)點擊“創建應用”

 

2)填寫信息

 

3)記錄信息,vs代碼調用接口時需要。

 

2、 下載SDK

1)點擊“SDK下載”,下載api頭文件:

 

2)下載“C++SDK”,可以查看使用說明

 

3 、創建vs調用

參考說明:https://ai.baidu.com/ai-doc/BODY/ak3cpyv9i

1)安裝依賴庫libcurl

下載網址:https://curl.haxx.se/download.html

 

下載紅框部分,並解壓。

配置環境變量:D:\thirdLib\curl\curl-7.69.1-win64-mingw\bin

配置屬性列表:

附加包含目錄:D:\thirdLib\curl\curl-7.69.1-win64-mingw\include

附加庫目錄:D:\thirdLib\curl\curl-7.69.1-win64-mingw\lib

附加依賴項:libcurl.a、libcurl.dll.a

 

2)安裝依賴庫openssl

下載exe:http://slproweb.com/products/Win32OpenSSL.html

雙擊,一路”next”即可。

配置環境變量:D:\thirdLib\OpenSSL\OpenSSL-Win64\bin

配置屬性列表:

附加包含目錄:D:\thirdLib\OpenSSL\OpenSSL-Win64\include

附加庫目錄:D:\thirdLib\OpenSSL\OpenSSL-Win64\lib

附加依賴項:

4758cca.lib

aep.lib

atalla.lib

capi.lib

chil.lib

cswift.lib

gmp.lib

gost.lib

libeay32.lib

nuron.lib

padlock.lib

ssleay32.lib

sureware.lib

ubsec.lib

 

3)安裝依賴庫jsoncpp(>1.6.2版本,0.x版本將不被支持)

下載:https://github.com/open-source-parsers/jsoncpp

編譯:使用cmake進行編譯。

 

雙擊打開:JSONCPP.sln

進行編譯,生成jsoncpp.lib

 

附加包含目錄:D:\thirdLib\jsoncpp\jsoncpp-master\include

附加庫目錄:D:\thirdLib\jsoncpp\jsoncpp-master\build\src\lib_json\Release D:\thirdLib\jsoncpp\jsoncpp-master\build\src\lib_json\Release

附加依賴項:jsoncpp.lib

 

測試代碼:

#include "body_analysis.h"

#include <opencv2/opencv.hpp>

// 設置APPID/AK/SK

std::string app_id = "xxx";

std::string api_key = "xxxxx";

std::string secret_key = "xxxxx";

aip::Bodyanalysis client(app_id, api_key, secret_key);

void main()

{

             Json::Value result;

             std::string image;

              aip::get_file_content(“test.png”, &image);

              // 調用人像分割

              result = client.body_seg(image, aip::null);

              // 如果有可選參數

              std::map<std::string, std::string> options;

              options["type"] = "labelmap";//labelmap,scoremap,foreground

              // 帶參數調用人像分割

              result = client.body_seg(image, options);

              std::string res = result["labelmap"].asString();//轉爲string,asString(),

              std::string result_dec;//解碼結果

              result_dec = aip::base64_decode(res);

              std::vector<char> base64_img(result_dec.begin(), result_dec.end());

              cv::Mat img_decode = cv::imdecode(base64_img, CV_LOAD_IMAGE_COLOR);

              cv::Mat mask = img_decode * 255;

              cv::imwrite(“out.png”, mask);

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章