Go SDK
本章节介绍人脸识别服务Go SDK,您可以参考本章节进行快速集成开发。
准备工作
- 注册华为账号并开通华为云,并完成实名认证,账号不能处于欠费或冻结状态。
- 已开通人脸识别服务。如未开通,请登录人脸识别管理控制台人脸识别管理控制台开通所需服务。
- 已具备开发环境,Go SDK 支持 go 1.14 及以上版本,可执行 go version 检查当前 Go 的版本信息。
- 登录“我的凭证 > 访问秘钥”页面,获取Access Key(AK)和Secret Access Key(SK)。
图1 获取AK、SK
- 登录“我的凭证”页面,获取“IAM用户名”、“账号名”以及待使用区域的“项目ID”。调用服务时会用到这些信息,请提前保存。
本样例以“华北-北京四”区域为例,获取对应的项目ID(project_id)。
图2 我的凭证
安装SDK
使用SDK前需要安装华为云Go SDK 库。
# 安装华为云Go库 go get -u github.com/huaweicloud/huaweicloud-sdk-go-v3 # 安装依赖 go get github.com/json-iterator/go
开始使用
在开始使用之前,请确保您安装的是最新版本的SDK。使用过时的版本可能会导致兼容性问题或无法使用最新功能。您可以通过运行以下命令来检查并更新SDK至最新版本。
go list -m all | grep huaweicloud-sdk-go-v3 go get -u github.com/huaweicloud/huaweicloud-sdk-go-v3
详细的SDK介绍请参见SDK中心、Go SDK使用指导、Go SDK使用视频。
- 导入依赖模块
import ( "fmt" "os" "github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic" // 导入v2版本sdk frs "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/frs/v2" "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/frs/v2/model" region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/frs/v2/region" )
- 配置认证信息
配置AK、SK信息。华为云通过AK识别用户的身份,通过SK对请求数据进行签名验证,用于确保请求的机密性、完整性和请求者身份的正确性。AK、SK获取方法请参见准备工作。
func GetCredential(ak, sk string) basic.Credentials { // Init Auth Info return basic.NewCredentialsBuilder(). WithAk(ak). WithSk(sk). Build() }
初始化认证信息:
ak := os.Getenv("HUAWEICLOUD_SDK_AK") sk := os.Getenv("HUAWEICLOUD_SDK_SK") client := GetCredential(ak, sk)
- 认证用的 ak 和sk 硬编码到代码中或者明文存储都有很大的安全风险,建议在配置文件或者环境变量中密文存放,使用时解密,确保安全。
- 本示例以 ak 和 sk 保存在环境变量中来实现身份验证为例,运行本示例前请先在本地环境中设置环境变量HUAWEICLOUD_SDK_AK和HUAWEICLOUD_SDK_SK。
图3 Windows环境新建环境变量
- 初始化客户端
指定region方式
// # 初始化人脸识别服务的客户端,并选择服务部署区域 func GetClient(auth basic.Credentials) *frs.FrsClient { client := frs.NewFrsClient( frs.FrsClientBuilder(). WithRegion(region.ValueOf('cn-north-4')). WithCredential(auth). Build()) return client }
服务部署区域请参见终端节点。
- 发送请求并查看下响应
request := &model.DetectFaceByBase64Request{} request.Body = &model.FaceDetectBase64Req{ ImageBase64: "/9j/4AAQSkZJRgABAQAAAQABAAD...", } response, err := client.DetectFaceByBase64(request) if err == nil { fmt.Printf("%+v\n", response) } else { fmt.Println(err) }
使用人脸比对SDK时,image1、image2参数需为相同类型,即同为url、base64或file。
- 异常处理
表1 异常处理 一级分类
一级分类说明
ServiceResponseError
服务响应异常
url.Error
url异常
// 捕获和处理不同类型的异常 response, err := client.DetectFaceByBase64(request) if err == nil { fmt.Printf("%+v\n", response) } else { fmt.Println(err) }
SDK demo代码解析
- 人脸检测
// detect face by base64 request := &model.DetectFaceByBase64Request{} attributesFaceDetectBase64Req := "2" request.Body = &model.FaceDetectBase64Req{ Attributes: &attributesFaceDetectBase64Req, ImageBase64: "/9j/4AAQSkZJRgABAQAAAQABAAD...", } response, err := client.DetectFaceByBase64(request) if err == nil { fmt.Printf("%+v\n", response) } else { fmt.Println(err) } // detect face by file file, err := os.Open("/root/picture.jpg") if err != nil { fmt.Println(err) } defer file.Close() request := &model.DetectFaceByFileRequest{} request.Body = &model.DetectFaceByFileRequestBody{ Attributes: def.NewMultiPart(attributes), ImageFile: def.NewFilePart(file), } response, err := client.DetectFaceByFile(request) if err == nil { fmt.Printf("%+v\n", response) } else { fmt.Println(err) }
- 人脸比对
// compare face by base64 request := &model.CompareFaceByBase64Request{} request.Body = &model.FaceCompareBase64Req{ Image1Base64: "/9j/4AAQSkZJRgABAQAAAQABAAD...", Image2Base64: "/9j/4AAQSkZJRgABAQAAAQABAAD...", } response, err := client.CompareFaceByBase64(request) if err == nil { fmt.Printf("%+v\n", response) } else { fmt.Println(err) } // compare face by file file1, err := os.Open("/root/picture1.jpg") if err != nil { fmt.Println(err) } defer file1.Close() file2, err := os.Open("/root/picture2.jpg") if err != nil { fmt.Println(err) } defer file2.Close() request := &model.CompareFaceByFileRequest{} request.Body = &model.CompareFaceByFileRequestBody{ Image1File: def.NewFilePart(file1), Image2File: def.NewFilePart(file2), } response, err := client.CompareFaceByFile(request) if err == nil { fmt.Printf("%+v\n", response) } else { fmt.Println(err) }
- 人脸搜索
// face search by base64 request := &model.SearchFaceByBase64Request{} request.FaceSetName = "face_set_name" var listReturnFieldsbody = []string{ "timestamp", "id", } request.Body = &model.FaceSearchBase64Req{ ReturnFields: &listReturnFieldsbody, ImageBase64: "/9j/4AAQSkZJRgABAQAAAQABAAD...", } response, err := client.SearchFaceByBase64(request) if err == nil { fmt.Printf("%+v\n", response) } else { fmt.Println(err) } // face search by file file, err := os.Open("/root/picture.jpg") if err != nil { fmt.Println(err) } defer file.Close() request := &model.SearchFaceByFileRequest{} request.FaceSetName = "face_set_name" request.Body = &model.SearchFaceByFileRequestBody{ ReturnFields: def.NewMultiPart("[\"timestamp\"]"), Filter: def.NewMultiPart("timestamp:10"), ImageFile: def.NewFilePart(file), } response, err := client.SearchFaceByFile(request) if err == nil { fmt.Printf("%+v\n", response) } else { fmt.Println(err) }
- 创建人脸库
request := &model.CreateFaceSetRequest{} request.Body = &model.CreateFaceSetReq{ FaceSetName: "face_set_name", } response, err := client.CreateFaceSet(request) if err == nil { fmt.Printf("%+v\n", response) } else { fmt.Println(err) }
- 查询人脸库
request := &model.ShowFaceSetRequest{} request.FaceSetName = "face_set_name" response, err := client.ShowFaceSet(request) if err == nil { fmt.Printf("%+v\n", response) } else { fmt.Println(err) }
- 查询所有人脸库
request := &model.ShowAllFaceSetsRequest{} response, err := client.ShowAllFaceSets(request) if err == nil { fmt.Printf("%+v\n", response) } else { fmt.Println(err) }
- 删除人脸库
request := &model.DeleteFaceSetRequest{} request.FaceSetName = "face_set_name" response, err := client.DeleteFaceSet(request) if err == nil { fmt.Printf("%+v\n", response) } else { fmt.Println(err) }
- 添加人脸
// add face by base64 request := &model.AddFacesByBase64Request{} request.FaceSetName = "face_set_name" var externalFieldsAddFacesBase64Req interface{} = "{\"timestamp\":12}" request.Body = &model.AddFacesBase64Req{ ExternalFields: &externalFieldsAddFacesBase64Req, ImageBase64: "/9j/4AAQSkZJRgABAQAAAQABAAD...", } response, err := client.AddFacesByBase64(request) if err == nil { fmt.Printf("%+v\n", response) } else { fmt.Println(err) } // add face by file file, err := os.Open("/root/picture.jpg") if err != nil { fmt.Println(err) } defer file.Close() request := &model.AddFacesByFileRequest{} request.FaceSetName = "face_set_name" request.Body = &model.AddFacesByFileRequestBody{ ExternalFields: def.NewMultiPart("{\"timestamp\":100}"), ImageFile: def.NewFilePart(file), } response, err := client.AddFacesByFile(request) if err == nil { fmt.Printf("%+v\n", response) } else { fmt.Println(err) }
- 删除人脸
request := &model.DeleteFaceByExternalImageIdRequest{} request.FaceSetName = "face_set_name" request.ExternalImageId = "external_image_id" response, err := client.DeleteFaceByExternalImageId(request) if err == nil { fmt.Printf("%+v\n", response) } else { fmt.Println(err) }
- 批量删除人脸
request := &model.BatchDeleteFacesRequest{} request.FaceSetName = "face_set_name" request.Body = &model.DeleteFacesBatchReq{ Filter: "age:[20 TO 30]", } response, err := client.BatchDeleteFaces(request) if err == nil { fmt.Printf("%+v\n", response) } else { fmt.Println(err) }
- 更新人脸
request := &model.UpdateFaceRequest{} request.FaceSetName = "face_set_name" externalImageIdUpdateFaceReq:= "external_image_id" var externalFieldsUpdateFaceReq interface{} = "{\"timestamp\":12}" request.Body = &model.UpdateFaceReq{ FaceId: "LkPJblq6", ExternalImageId: &externalImageIdUpdateFaceReq, ExternalFields: &externalFieldsUpdateFaceReq, } response, err := client.UpdateFace(request) if err == nil { fmt.Printf("%+v\n", response) } else { fmt.Println(err) }
- 查询人脸
//Show Faces By FaceId request := &model.ShowFacesByFaceIdRequest{} request.FaceSetName = "face_set_name" request.FaceId = "LkPJblq6" response, err := client.ShowFacesByFaceId(request) if err == nil { fmt.Printf("%+v\n", response) } else { fmt.Println(err) }
- 动作活体检测
//detect live by base64 request := &model.DetectLiveByBase64Request{} request.Body = &model.LiveDetectBase64Req{ Actions: "1,2,3,4", VideoBase64: "/9j/4AAQSkZJRgABAQAAAQABAAD...", } response, err := client.DetectLiveByBase64(request) if err == nil { fmt.Printf("%+v\n", response) } else { fmt.Println(err) } //detect live by file file, err := os.Open("/root/video.mp4") if err != nil { fmt.Println(err) } defer file.Close() request := &model.DetectLiveByFileRequest{} request.Body = &model.DetectLiveByFileRequestBody{ Actions: def.NewMultiPart(action), VideoFile: def.NewFilePart(file), } response, err := client.DetectLiveByFile(request) if err == nil { fmt.Printf("%+v\n", response) } else { fmt.Println(err) }
- 静默活体检测
//detect live face by base64 request := &model.DetectLiveFaceByBase64Request{} request.Body = &model.LiveDetectFaceBase64Req{ ImageBase64: "/9j/4AAQSkZJRgABAQAAAQABAAD...", } response, err := client.DetectLiveFaceByBase64(request) if err == nil { fmt.Printf("%+v\n", response) } else { fmt.Println(err) } //detect live face by file file, err := os.Open("/root/picture.jpg") if err != nil { fmt.Println(err) } defer file.Close() request := &model.DetectLiveFaceByFileRequest{} request.Body = &model.DetectLiveFaceByFileRequestBody{ ImageFile: def.NewFilePart(file), } response, err := client.DetectLiveFaceByFile(request) if err == nil { fmt.Printf("%+v\n", response) } else { fmt.Println(err) }
SDK代码自动生成
API Explorer提供API检索及平台调试,支持全量快速检索、可视化调试、帮助文档查看和在线咨询。
您只需要在API Explorer中修改接口参数,即可自动生成对应的代码示例。同时,可在集成开发环境CloudIDE中完成代码的构建、调试和运行等操作。