功能介绍
用户传入图片通过后台算法判断图片主体,并返回主体坐标。
前置条件
申请服务
申请主体识别服务的具体操作步骤请参见《图像识别API参考》的“如何调用API > 申请服务”章节。
获取AK/SK
开发者在使用前需先获取账号的ak、sk、endpoint。
您需要拥有华为云账号以及该账号对应的 Access Key(AK)和 Secret Access Key(SK)。请在华为云控制台“我的凭证-访问密钥”页面上创建和查看您的 AK/SK。更多信息请查看访问密钥。
endpoint 华为云各服务应用区域和各服务的终端节点,详情请查看地区和终端节点。
运行环境
GO 1.16及其以上版本。
SDK获取和安装
Copied!
go get -u github.com/huaweicloud/huaweicloud-sdk-go-v3
示例代码
主体识别示例代码只需将AK/SK信息替换为实际AK/SK,代码中可以使用ImageMainObjectDetectionReq中的withUrl或withImage方法配置图像信息(image和url参数二选一),示例中以url为例,配置完成后运行即可。
Copied!
package main
import (
imageRegion "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/image/v2/region"
coreConfig "github.com/huaweicloud/huaweicloud-sdk-go-v3/core/config"
"fmt"
"os"
coreBasic "github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
image "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/image/v2"
imageModel "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/image/v2/model"
)
type ImageMainObjectDetectionDemo struct{}
func NewImageMainObjectDetectionDemoDefault() ImageMainObjectDetectionDemo {
i := ImageMainObjectDetectionDemo{}
return i
}
func main() {
ak := os.Getenv("HUAWEICLOUD_SDK_AK")
sk := os.Getenv("HUAWEICLOUD_SDK_SK")
auth := coreBasic.NewCredentialsBuilder().WithAk(ak).WithSk(sk).Build()
config := coreConfig.DefaultHttpConfig()
config.WithIgnoreSSLVerification(true)
client := *image.NewImageClient(image.ImageClientBuilder().
WithCredential(auth).
WithRegion(imageRegion.ValueOf("cn-north-4")).
WithHttpConfig(config).
Build())
request := imageModel.RunImageMainObjectDetectionRequest{}
body := imageModel.ImageMainObjectDetectionReq{}
imageMainObjectDetectionReqUrlParam := "图片url地址"
body.Url = &imageMainObjectDetectionReqUrlParam
request.Body = &body
response, err := client.RunImageMainObjectDetection(&request)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(response.String())
}
修订记录
发布日期 |
文档版本 |
修订说明 |
2022-09-28 |
1.0 |
文档首次发布 |
版本说明
本示例基于华为云SDK开发
功能介绍
用户传入图片通过后台算法判断图片主体,并返回主体坐标。
前置条件
申请服务
申请主体识别服务的具体操作步骤请参见《图像识别API参考》的“如何调用API > 申请服务”章节。
获取AK/SK
开发者在使用前需先获取账号的ak、sk、endpoint。
您需要拥有华为云账号以及该账号对应的 Access Key(AK)和 Secret Access Key(SK)。请在华为云控制台“我的凭证-访问密钥”页面上创建和查看您的 AK/SK。更多信息请查看访问密钥。 endpoint 华为云各服务应用区域和各服务的终端节点,详情请查看地区和终端节点。
运行环境
GO 1.16及其以上版本。
SDK获取和安装
示例代码
主体识别示例代码只需将AK/SK信息替换为实际AK/SK,代码中可以使用ImageMainObjectDetectionReq中的withUrl或withImage方法配置图像信息(image和url参数二选一),示例中以url为例,配置完成后运行即可。
package main import ( imageRegion "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/image/v2/region" coreConfig "github.com/huaweicloud/huaweicloud-sdk-go-v3/core/config" "fmt" "os" coreBasic "github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic" image "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/image/v2" imageModel "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/image/v2/model" ) type ImageMainObjectDetectionDemo struct{} func NewImageMainObjectDetectionDemoDefault() ImageMainObjectDetectionDemo { i := ImageMainObjectDetectionDemo{} return i } func main() { // 设置AK和SK // 认证用的ak和sk硬编码到代码中或者明文存储都有很大的安全风险,建议在配置文件或者环境变量中密文存放,使用时解密,确保安全; // 本示例以ak和sk保存在环境变量中来实现身份验证为例,运行本示例前请先在本地环境中设置环境变量HUAWEICLOUD_SDK_AK和HUAWEICLOUD_SDK_SK。 ak := os.Getenv("HUAWEICLOUD_SDK_AK") sk := os.Getenv("HUAWEICLOUD_SDK_SK") auth := coreBasic.NewCredentialsBuilder().WithAk(ak).WithSk(sk).Build() config := coreConfig.DefaultHttpConfig() config.WithIgnoreSSLVerification(true) client := *image.NewImageClient(image.ImageClientBuilder(). WithCredential(auth). WithRegion(imageRegion.ValueOf("cn-north-4")). WithHttpConfig(config). Build()) request := imageModel.RunImageMainObjectDetectionRequest{} body := imageModel.ImageMainObjectDetectionReq{} // 替换示例中的图像地址 imageMainObjectDetectionReqUrlParam := "图片url地址" body.Url = &imageMainObjectDetectionReqUrlParam request.Body = &body response, err := client.RunImageMainObjectDetection(&request) if err != nil { fmt.Println(err) return } fmt.Println(response.String()) }
参考
更多信息请参考主体识别API
修订记录