• 沒有找到結果。

1. 导入依赖模块

<?php

namespace HuaweiCloud\SDK\Frs\V2\Model;

require_once "vendor/autoload.php";

use HuaweiCloud\SDK\Core\Auth\BasicCredentials;

use HuaweiCloud\SDK\Core\Http\HttpConfig;

use HuaweiCloud\SDK\Core\Exceptions\ConnectionException;

use HuaweiCloud\SDK\Core\Exceptions\RequestTimeoutException;

use HuaweiCloud\SDK\Core\Exceptions\ServiceResponseException;

use HuaweiCloud\SDK\Frs\V2\FrsClient;

2. 配置客户端连接参数 – 默认配置

// 使用默认配置

$config = HttpConfig::getDefaultConfig();

– 网络代理(可选)

// 使用代理服务器

$config->setProxyProtocol('http');

$config->setProxyHost('proxy.huawei.com');

$config->setProxyPort(8080);

$config->setProxyUser('username');

$config->setProxyPassword('password');

– 超时配置(可选)

// 默认连接超时时间为60秒,读取超时时间为120秒。可根据需要修改默认值。

$config->setTimeout(120);

$config->setConnectionTimeout(60);

– SSL配置(可选)

// 配置跳过服务端证书验证

$config->setIgnoreSslVerification(true);

// 配置服务器端CA证书,用于SDK验证服务端证书合法性

$config->setCertFile("{yourCertFile}");

3. 配置认证信息

配置AK、SK、projectId信息。华为云通过AK识别用户的身份,通过SK对请求数 据进行签名验证,用于确保请求的机密性、完整性和请求者身份的正确性。

// 终端节点以 FRS 服务北京四的 endpoint 为例

$ak = "<YOUR AK>";

$sk = "<YOUR SK>";

$endpoint = "https://face.cn-north-4.myhuaweicloud.com";

$projectId = "<YOUR projectId>";

$credentials = new BasicCredentials($ak,$sk,$projectId);

认证参数说明:

– ak、sk:访问秘钥信息,获取方法请参见准备工作。

– projectId:华为云项目ID,获取方法请参见准备工作。

– endpoint:华为云各服务应用区域和各服务的终端节点,详情请查看 地区和

终端节点 。

4. 初始化客户端

指定云服务endpoint方式

$client = FrsClient::newBuilder(new FrsClient) ->withHttpConfig($config)

->withEndpoint($endpoint) ->withCredentials($credentials) ->build();

5. 发送并查看响应

// 以调用人脸检测接口 DetectFaceByBase64 为例

$response = $client->DetectFaceByBase64($request);

} catch (ConnectionException $e) { $msg = $e->getMessage();

echo "\n". $msg ."\n";

} catch (RequestTimeoutException $e) { $msg = $e->getMessage();

echo "\n". $msg ."\n";

} catch (ServiceResponseException $e) { echo "\n";

echo $e->getHttpStatusCode(). "\n";

echo $e->getErrorCode() . "\n";

echo $e->getErrorMsg() . "\n";

}echo "\n";

$response = $client->DetectFaceByBase64($request);

} catch (ConnectionException $e) { $msg = $e->getMessage();

echo "\n". $msg ."\n";

} catch (RequestTimeoutException $e) { $msg = $e->getMessage();

echo "\n". $msg ."\n";

} catch (ServiceResponseException $e) {

echo "\n";

echo $e->getHttpStatusCode(). "\n";

echo $e->getErrorCode() . "\n";

echo $e->getErrorMsg() . "\n";

}echo "\n";

$response = $client->DetectFaceByBase64($request);

} catch (ConnectionException $e) { $msg = $e->getMessage();

echo "\n". $msg ."\n";

} catch (RequestTimeoutException $e) { $msg = $e->getMessage();

echo "\n". $msg ."\n";

} catch (ServiceResponseException $e) { echo "\n";

echo $e->getHttpStatusCode(). "\n";

echo $e->getErrorCode() . "\n";

echo $e->getErrorMsg() . "\n";

}echo "\n";

$response = $client->CompareFaceByBase64($request);

} catch (ConnectionException $e) { $msg = $e->getMessage();

echo "\n". $msg ."\n";

} catch (RequestTimeoutException $e) { $msg = $e->getMessage();

echo "\n". $msg ."\n";

} catch (ServiceResponseException $e) { echo "\n";

echo $e->getHttpStatusCode(). "\n";

echo $e->getErrorCode() . "\n";

echo $e->getErrorMsg() . "\n";

}echo "\n";

$response = $client->SearchFaceByBase64($request);

} catch (ConnectionException $e) { $msg = $e->getMessage();

echo "\n". $msg ."\n";

} catch (RequestTimeoutException $e) { $msg = $e->getMessage();

echo "\n". $msg ."\n";

} catch (ServiceResponseException $e) {

echo "\n";

echo $e->getHttpStatusCode(). "\n";

echo $e->getErrorCode() . "\n";

echo $e->getErrorMsg() . "\n";

}echo "\n";

$response = $client->CreateFaceSet($request);

} catch (ConnectionException $e) { $msg = $e->getMessage();

echo "\n". $msg ."\n";

} catch (RequestTimeoutException $e) { $msg = $e->getMessage();

echo "\n". $msg ."\n";

} catch (ServiceResponseException $e) { echo "\n";

echo $e->getHttpStatusCode(). "\n";

echo $e->getErrorCode() . "\n";

echo $e->getErrorMsg() . "\n";

}echo "\n";

$response = $client->ShowFaceSet($request);

} catch (ConnectionException $e) { $msg = $e->getMessage();

echo "\n". $msg ."\n";

} catch (RequestTimeoutException $e) { $msg = $e->getMessage();

echo "\n". $msg ."\n";

} catch (ServiceResponseException $e) { echo "\n";

echo $e->getHttpStatusCode(). "\n";

echo $e->getErrorCode() . "\n";

echo $e->getErrorMsg() . "\n";

}echo "\n";

echo $response;

● 查询所有人脸库

$request = newShowAllFaceSetsRequest();

try {

$response = $client->ShowAllFaceSets($request);

} catch (ConnectionException $e) { $msg = $e->getMessage();

echo "\n". $msg ."\n";

} catch (RequestTimeoutException $e) { $msg = $e->getMessage();

echo "\n". $msg ."\n";

} catch (ServiceResponseException $e) { echo "\n";

echo $e->getHttpStatusCode(). "\n";

echo $e->getErrorCode() . "\n";

echo $e->getErrorMsg() . "\n";

}echo "\n";

echo $response;

● 删除人脸库

$request = newDeleteFaceSetRequest();

$request->setFaceSetName("人脸库名称");

try {

$response = $client->DeleteFaceSet($request);

} catch (ConnectionException $e) { $msg = $e->getMessage();

echo "\n". $msg ."\n";

} catch (RequestTimeoutException $e) { $msg = $e->getMessage();

echo "\n". $msg ."\n";

} catch (ServiceResponseException $e) { echo "\n";

echo $e->getHttpStatusCode(). "\n";

echo $e->getErrorCode() . "\n";

echo $e->getErrorMsg() . "\n";

}echo "\n";

$response = $client->AddFacesByBase64($request);

} catch (ConnectionException $e) { $msg = $e->getMessage();

echo "\n". $msg ."\n";

} catch (RequestTimeoutException $e) { $msg = $e->getMessage();

echo "\n". $msg ."\n";

} catch (ServiceResponseException $e) { echo "\n";

echo $e->getHttpStatusCode(). "\n";

echo $e->getErrorCode() . "\n";

echo $e->getErrorMsg() . "\n";

}echo "\n";

$response = $client->DeleteFaceByFaceId($request);

} catch (ConnectionException $e) { $msg = $e->getMessage();

echo "\n". $msg ."\n";

} catch (RequestTimeoutException $e) { $msg = $e->getMessage();

echo "\n". $msg ."\n";

} catch (ServiceResponseException $e) { echo "\n";

echo $e->getHttpStatusCode(). "\n";

echo $e->getErrorCode() . "\n";

echo $e->getErrorMsg() . "\n";

}echo "\n";

$response = $client->BatchDeleteFaces($request);

} catch (ConnectionException $e) { $msg = $e->getMessage();

echo "\n". $msg ."\n";

} catch (RequestTimeoutException $e) { $msg = $e->getMessage();

echo "\n". $msg ."\n";

} catch (ServiceResponseException $e) { echo "\n";

echo $e->getHttpStatusCode(). "\n";

echo $e->getErrorCode() . "\n";

echo $e->getErrorMsg() . "\n";

}echo "\n";

$response = $client->UpdateFace($request);

} catch (ConnectionException $e) { $msg = $e->getMessage();

echo "\n". $msg ."\n";

} catch (RequestTimeoutException $e) { $msg = $e->getMessage();

echo "\n". $msg ."\n";

} catch (ServiceResponseException $e) { echo "\n";

echo $e->getHttpStatusCode(). "\n";

echo $e->getErrorCode() . "\n";

echo $e->getErrorMsg() . "\n";

}echo "\n";

$response = $client->ShowFacesByFaceId($request);

} catch (ConnectionException $e) { $msg = $e->getMessage();

echo "\n". $msg ."\n";

} catch (RequestTimeoutException $e) { $msg = $e->getMessage();

echo "\n". $msg ."\n";

} catch (ServiceResponseException $e) { echo "\n";

echo $e->getHttpStatusCode(). "\n";

echo $e->getErrorCode() . "\n";

echo $e->getErrorMsg() . "\n";

}echo "\n";

$response = $client->DetectLiveByBase64($request);

} catch (ConnectionException $e) { $msg = $e->getMessage();

echo "\n". $msg ."\n";

} catch (RequestTimeoutException $e) { $msg = $e->getMessage();

echo "\n". $msg ."\n";

} catch (ServiceResponseException $e) { echo "\n";

echo $e->getHttpStatusCode(). "\n";

echo $e->getErrorCode() . "\n";

echo $e->getErrorMsg() . "\n";

}echo "\n";

echo $response;

● 静默活体检测

$request = newDetectLiveFaceByBase64Request();

$body = newLiveDetectFaceBase64Req();

$body->setImageBase64("图片的base64编码");

$request->setBody($body);

try {

$response = $client->DetectLiveFaceByBase64($request);

} catch (ConnectionException $e) { $msg = $e->getMessage();

echo "\n". $msg ."\n";

} catch (RequestTimeoutException $e) { $msg = $e->getMessage();

echo "\n". $msg ."\n";

} catch (ServiceResponseException $e) { echo "\n";

echo $e->getHttpStatusCode(). "\n";

echo $e->getErrorCode() . "\n";

echo $e->getErrorMsg() . "\n";

}echo "\n";

echo $response;

SDK 代码自动生成

API Explorer提供API检索及平台调试,支持全量快速检索、可视化调试、帮助文档查

看、在线咨询。

您只需要在API Explorer中修改接口参数,即可自动生成对应的代码示例。

7-2 API Explorer

8 C++ SDK

本章节介绍新版C++ SDK,您可以参考本章节进行快速集成开发。

准备工作

● 已注册华为云帐号,并完成实名认证,帐号不能处于欠费或冻结状态。

● 已具备开发环境,支持 C++ 14 及以上版本,要求安装 CMake 3.10 及以上版 本。

● 登录“我的凭证 > 访问秘钥”页面,获取Access Key(AK)和Secret Access Key

(SK)。

● 登录“我的凭证”页面,获取“IAM用户名”“、帐号名”以及待使用区域的

“项目ID”。调用服务时会用到这些信息,请提前保存。

本样例以“华北-北京四”区域为例,获取对应的项目ID(project_id)。

8-1 我的凭证

安装 SDK

● 在Linux系统上安装SDK a. 获取依赖包

所需的这些第三方软件包在大部分系统的包管理工具中都有提供,例如基于 Debian/Ubuntu 的系统。

sudo apt-get install libcurl4-openssl-dev libboost-all-dev libssl-dev libcpprest-dev

spdlog 需要从源码进行安装。

git clone https://github.com/gabime/spdlog.git cd spdlog

mkdir build cd build

cmake -DCMAKE_POSITION_INDEPENDENT_CODE=ON .. // 用以生成动态库 makesudo make install

b. 编译安装

git clone https://github.com/huaweicloud/huaweicloud-sdk-cpp-v3.git cd huaweicloud-sdk-cpp-v3

mkdir build cd build cmake ..

makesudo make install

完成上述操作后,C++ SDK 安装目录为 /usr/local。

● 在Windows系统上安装SDK

a. 安装 vcpkg 并使用 vcpkg 安装所需软件包

vcpkg install curl cpprestsdk boost openssl spdlog

b. 使用CLion进行编译

i. 使用CLion打开huaweicloud-sdk-cpp-v3 目录。

ii. 选择“File > Settings”。

iii. 选择“Build, Execution, Deployment > > CMake”。

iv. 在CMake options中加入:

-DCMAKE_TOOLCHAIN_FILE={your vcpkg install dir}/scripts/buildsystems/vcpkg.cmake

v. 右键 CMakeLists.txt 选择 Load CMake Project。

vi. 选择Build开始编译。

c. 安装C++ SDK

编译完成后选择“Build > Install”。

完成上述操作后,C++ SDK 安装目录为 C:\Program File (x86)\huaweicloud-sdk-cpp-v3。

开始使用

详细的SDK介绍,使用异步客户端,配置日志请参见SDK中心、C++ SDK使用指导。

1. 导入依赖模块

#include <cstdlib>

#include <iostream>

#include <string>

#include <memory>

#include <huaweicloud/core/exception/Exceptions.h>

#include <huaweicloud/core/Client.h>

#include <huaweicloud/frs/v2/FrsClient.h>

using namespace HuaweiCloud::Sdk::Frs::V2;

using namespace HuaweiCloud::Sdk::Frs::V2::Model;

using namespace HuaweiCloud::Sdk::Core;

using namespace HuaweiCloud::Sdk::Core::Exception;

using namespace std;

2. 配置客户端连接参数 – 默认配置

// 使用默认配置

HttpConfig httpConfig = HttpConfig();

– 网络代理(可选)

// 根据需要配置网络代理

httpConfig.setProxyProtocol("http");

httpConfig.setProxyHost("proxy.huawei.com");

httpConfig.setProxyPort("8080");

httpConfig.setProxyUser("username");

httpConfig.setProxyPassword("password");

– 超时配置(可选)

// 默认连接超时为60秒,默认读取超时为120秒。可根据需求修改该默认值 httpConfig.setConnectTimeout(60);

httpConfig.setReadTimeout(120);

– SSL配置(可选)

// 配置跳过服务端证书验证

httpConfig.setIgnoreSslVerification(true);

3. 配置认证信息

配置AK、SK、projectId信息。华为云通过AK识别用户的身份,通过SK对请求数 据进行签名验证,用于确保请求的机密性、完整性和请求者身份的正确性。

string ak = "<YOUR AK>";

string sk = "<YOUR SK>";

string projectId = "<YOUR projectId>";

auto auth = std::make_unique<BasicCredentials>();

auth->withAk(ak) .withSk(sk)

.withProjectId(projectId);

认证参数说明:

– ak、sk:访问秘钥信息,获取方法请参见准备工作。

– projectId:华为云项目ID,获取方法请参见准备工作。

4. 初始化客户端

指定云服务endpoint方式

string endpoint = "https://face.cn-north-4.myhuaweicloud.com";

auto client = FrsClient::newBuilder()

.withCredentials(std::unique_ptr<Credentials>(auth.release())) .withHttpConfig(httpConfig)

.withEndPoint(endpoint) .build();

endpoint:华为云各服务应用区域和各服务的终端节点,详情请查看 地区和终端

节点 。

5. 发送请求并查看响应

// 以调用人脸检测接口 RecognizeGeneralTable 为例 DetectFaceByBase64Request request;

FaceDetectBase64Req body;

body.setImageBase64("图片的base64编码");

request.setBody(body);

std::cout << "---begin execute request---" << std::endl;

说明

使用人脸比对SDK时,image1、image2参数需为相同类型,即同为url、base64或file。

6. 异常处理

8-1 异常处理

一级分类 一级分类说明 二级分类 二级分类说明

ConnectionExcepti

on 连接类异常 HostUnreachableE

xception 网络不可达、被拒 绝。

SslHandShakeExce

ption SSL认证异常。

RequestTimeoutEx

ception 响应超时异常 CallTimeoutExcept

ion 单次请求,服务器

处理超时未返回。

一级分类 一级分类说明 二级分类 二级分类说明

auto reponse = client->detectFaceByBase64(request);

std::cout << reponse->getHttpBody() << std::endl;

} catch (HostUnreachableException& e) {

std::cout << "host unreachable:" << e.what() << std::endl;

} catch (SslHandShakeException& e) {

std::cout << "ssl handshake error:" << e.what() << std::endl;

} catch (RetryOutageException& e) {

std::cout << "retryoutage error:" << e.what() << std::endl;

} catch (CallTimeoutException& e) {

std::cout << "call timeout:" << e.what() << std::endl;

} catch (ServiceResponseException& e) {

std::cout << "http status code:" << e.getStatusCode() << std::endl;

std::cout << "error code:" << e.getErrorCode() << std::endl;

std::cout << "error msg:" << e.getErrorMsg() << std::endl;

std::cout << "RequestId:" << e.getRequestId() << std::endl;

} catch (exception& e) {

std:cout << "unknown exception:" << e.what() << std::endl;

}

std::cout << "---request finished---" << std::endl;

return0;

SDK demo 代码解析

● 人脸检测

DetectFaceByBase64Request request;

FaceDetectBase64Req body;

body.setImageBase64("图片的base64编码");

request.setBody(body);

std::cout << "---begin execute request---" << std::endl;

try {

auto reponse = client->detectFaceByBase64(request);

std::cout << reponse->getHttpBody() << std::endl;

} catch (HostUnreachableException& e) {

std::cout << "host unreachable:" << e.what() << std::endl;

} catch (SslHandShakeException& e) {

std::cout << "ssl handshake error:" << e.what() << std::endl;

} catch (RetryOutageException& e) {

std::cout << "retryoutage error:" << e.what() << std::endl;

} catch (CallTimeoutException& e) {

std::cout << "call timeout:" << e.what() << std::endl;

} catch (ServiceResponseException& e) {

std::cout << "http status code:" << e.getStatusCode() << std::endl;

std::cout << "error code:" << e.getErrorCode() << std::endl;

std::cout << "error msg:" << e.getErrorMsg() << std::endl;

std::cout << "RequestId:" << e.getRequestId() << std::endl;

} catch (exception& e) {

std:cout << "unknown exception:" << e.what() << std::endl;

}

std::cout << "---request finished---" << std::endl;

return0;

● 人脸比对

CompareFaceByBase64Request request;

FaceCompareBase64Req body;

body.setImage1Base64("图片1的base64编码");

body.setImage2Base64("图片2的base64编码");

request.setBody(body);

std::cout << "---begin execute request---" << std::endl;

try {

auto reponse = client->compareFaceByBase64(request);

std::cout << reponse->getHttpBody() << std::endl;

} catch (HostUnreachableException& e) {

std::cout << "host unreachable:" << e.what() << std::endl;

} catch (SslHandShakeException& e) {

std::cout << "ssl handshake error:" << e.what() << std::endl;

} catch (RetryOutageException& e) {

std::cout << "retryoutage error:" << e.what() << std::endl;

} catch (CallTimeoutException& e) {

std::cout << "call timeout:" << e.what() << std::endl;

} catch (ServiceResponseException& e) {

std::cout << "http status code:" << e.getStatusCode() << std::endl;

std::cout << "error code:" << e.getErrorCode() << std::endl;

std::cout << "error msg:" << e.getErrorMsg() << std::endl;

std::cout << "RequestId:" << e.getRequestId() << std::endl;

} catch (exception& e) {

std:cout << "unknown exception:" << e.what() << std::endl;

}

std::cout << "---request finished---" << std::endl;

return0;

● 人脸搜索

SearchFaceByBase64Request request;

request.setFaceSetName("人脸库名称");

FaceSearchBase64Req body;

body.setImageBase64("图片的base64编码");

request.setBody(body);

std::cout << "---begin execute request---" << std::endl;

try {

auto reponse = client->searchFaceByBase64(request);

std::cout << reponse->getHttpBody() << std::endl;

} catch (HostUnreachableException& e) {

std::cout << "host unreachable:" << e.what() << std::endl;

} catch (SslHandShakeException& e) {

std::cout << "ssl handshake error:" << e.what() << std::endl;

} catch (RetryOutageException& e) {

std::cout << "retryoutage error:" << e.what() << std::endl;

} catch (CallTimeoutException& e) {

std::cout << "call timeout:" << e.what() << std::endl;

} catch (ServiceResponseException& e) {

std::cout << "http status code:" << e.getStatusCode() << std::endl;

std::cout << "error code:" << e.getErrorCode() << std::endl;

std::cout << "error msg:" << e.getErrorMsg() << std::endl;

std::cout << "RequestId:" << e.getRequestId() << std::endl;

} catch (exception& e) {

std:cout << "unknown exception:" << e.what() << std::endl;

}

std::cout << "---request finished---" << std::endl;

return0;

● 创建人脸库

CreateFaceSetRequest request;

CreateFaceSetReq body;

body.setFaceSetName("人脸库名称");

request.setBody(body);

std::cout << "---begin execute request---" << std::endl;

try {

auto reponse = client->createFaceSet(request);

std::cout << reponse->getHttpBody() << std::endl;

} catch (HostUnreachableException& e) {

std::cout << "host unreachable:" << e.what() << std::endl;

} catch (SslHandShakeException& e) {

std::cout << "ssl handshake error:" << e.what() << std::endl;

} catch (RetryOutageException& e) {

std::cout << "retryoutage error:" << e.what() << std::endl;

} catch (CallTimeoutException& e) {

std::cout << "call timeout:" << e.what() << std::endl;

} catch (ServiceResponseException& e) {

std::cout << "http status code:" << e.getStatusCode() << std::endl;

std::cout << "error code:" << e.getErrorCode() << std::endl;

std::cout << "error msg:" << e.getErrorMsg() << std::endl;

std::cout << "RequestId:" << e.getRequestId() << std::endl;

} catch (exception& e) {

std:cout << "unknown exception:" << e.what() << std::endl;

}

std::cout << "---request finished---" << std::endl;

return0;

● 查询人脸库

ShowFaceSetRequest request;

request.setFaceSetName("人脸库名称");

std::cout << "---begin execute request---" << std::endl;

try {

auto reponse = client->showFaceSet(request);

std::cout << reponse->getHttpBody() << std::endl;

} catch (HostUnreachableException& e) {

std::cout << "host unreachable:" << e.what() << std::endl;

} catch (SslHandShakeException& e) {

std::cout << "ssl handshake error:" << e.what() << std::endl;

} catch (RetryOutageException& e) {

std::cout << "retryoutage error:" << e.what() << std::endl;

} catch (CallTimeoutException& e) {

std::cout << "call timeout:" << e.what() << std::endl;

} catch (ServiceResponseException& e) {

std::cout << "http status code:" << e.getStatusCode() << std::endl;

std::cout << "error code:" << e.getErrorCode() << std::endl;

std::cout << "error msg:" << e.getErrorMsg() << std::endl;

std::cout << "RequestId:" << e.getRequestId() << std::endl;

} catch (exception& e) {

std:cout << "unknown exception:" << e.what() << std::endl;

}

std::cout << "---request finished---" << std::endl;

return0;

● 查询所有人脸库

ShowAllFaceSetsRequest request;

std::cout << "---begin execute request---" << std::endl;

try {

auto reponse = client->showAllFaceSets(request);

auto reponse = client->showAllFaceSets(request);

相關文件