更新时间:2022-03-31 GMT+08:00
分享

Go SDK

本章节介绍人脸识别服务Go SDK,您可以参考本章节进行快速集成开发。

准备工作

  • 已注册华为云帐号,并完成实名认证,帐号不能处于欠费或冻结状态。
  • 已开通人脸识别服务。如未开通,请登录人脸识别管理控制台人脸识别管理控制台开通所需服务。
  • 已具备开发环境,Go SDK 支持 go 1.14 及以上版本,可执行 go version 检查当前 Go 的版本信息。
  • 登录“我的凭证 > 访问秘钥”页面,获取Access Key(AK)和Secret Access Key(SK)。
  • 登录“我的凭证”页面,获取“IAM用户名”“、帐号名”以及待使用区域的“项目ID”。调用服务时会用到这些信息,请提前保存。

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

    图1 我的凭证

安装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 SDK使用指导Go SDK使用视频

  1. 导入依赖模块
    import (
    	"fmt"
    	"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"
    )
  2. 配置认证信息
    配置AK、SK信息。华为云通过AK识别用户的身份,通过SK对请求数据进行签名验证,用于确保请求的机密性、完整性和请求者身份的正确性。AK、SK获取方法请参见准备工作
    func GetCredential(ak, sk string) basic.Credentials {
    	// Init Auth Info
    	return basic.NewCredentialsBuilder().
    		WithAk(ak).
    		WithSk(sk).
    		Build()
    }
  3. 初始化客户端
    指定region方式
    // # 初始化人脸识别服务的客户端,并选择服务部署区域
    func GetClient(auth basic.Credentials) *frs.FrsClient {
    	return frs.NewFrsClient(
    		frs.FrsClientBuilder().
    			WithRegion(region.CN_NORTH_4).
    			WithCredential(auth).
    			Build())
    }

    服务部署区域请参见终端节点

  4. 发送请求并查看下响应
    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。

  5. 异常处理
    表1 异常处理

    一级分类

    一级分类说明

    ServiceResponseError

    服务响应异常

    url.Error

    url异常

    response, err := client.DetectFaceByBase64(request)
    if err == nil {
        fmt.Printf("%+v\n", response)
    } else {
        fmt.Println(err)
    }

SDK demo代码解析

  • 人脸检测
    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)
    }
  • 人脸比对
    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)
    }
  • 人脸搜索
    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)
    }
  • 创建人脸库
    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)
    }
  • 添加人脸
    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)
    }
  • 删除人脸
    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 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)
    }

SDK代码自动生成

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

您只需要在API Explorer中修改接口参数,即可自动生成对应的代码示例。同时,可在集成开发环境CloudIDE中完成代码的构建、调试、运行等操作。

图2 API Explorer
分享:

    相关文档

    相关产品

close