商品抠图

最近更新时间:2024-09-12 16:57:21

我的收藏

简介

本文档提供关于商品抠图的相关 API 概览以及 SDK 示例代码。
API
操作描述
腾讯云数据万象通过 GoodsMatting 接口检测图片中的商品信息,生成只包含商品信息的图片,支持持久化、云上处理及下载时处理。

方式一:下载时处理

方法原型

func (s *CIService) GoodsMattingWithOpt(ctx context.Context, key string, opt *GoodsMattingptions) (*Response, error)

请求示例

// 将 examplebucket-1250000000 和 COS_REGION 修改为真实的信息
bu, _ := url.Parse("https://examplebucket-1250000000.cos.COS_REGION.myqcloud.com")
b := &cos.BaseURL{BucketURL: bu}
c := cos.NewClient(b, &http.Client{
Transport: &cos.AuthorizationTransport{
SecretID: os.Getenv("SECRETID"),
SecretKey: os.Getenv("SECRETKEY"),
},
})
key := "pic/cup.jpeg"
localPath := "cup.jpeg"
opt := &cos.GoodsMattingptions{
CenterLayout: "1",
PaddingLayout: "500x500",
}
resp, err := c.CI.GoodsMattingWithOpt(context.Background(), key, opt)
log_status(err)
fd, _ := os.OpenFile(localPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0660)
io.Copy(fd, resp.Body)
fd.Close()

参数说明

type GoodsMattingptions struct {
CenterLayout string `url:"center-layout,omitempty"`
PaddingLayout string `url:"padding-layout,omitempty"`
DetectUrl string `url:"detect-url,omitempty"`
}
参数名称
参数描述
类型
是否必填
CenterLayout
抠图主体居中显示; 值为1时居中显示,值为0时不作处理,默认为0
string
PaddingLayout
将处理后的图片四边进行留白,形式为 padding-layout=<dx>x<dy>,左右两边各进行 dx 像素的留白,上下两边各进行 dy 像素的留白,例如:padding-layout=20x10
默认不进行留白操作,dx、dy最大值为1000像素。
string
DetectUrl
您可以通过填写 detect-url 处理任意公网可访问的图片链接。不填写 detect-url 时,后台会默认处理 key ,填写了 detect-url 时,后台会处理 detect-url 链接,无需再填写 key。
string

结果说明

处理后的图片直接返回至响应体。


方式二:上传时处理

方法原型

func (s *CIService) PutFromFile(ctx context.Context, name string, filePath string, opt *ObjectPutOptions) (*ImageProcessResult, *Response, error)

请求示例

// 将 examplebucket-1250000000 和 COS_REGION 修改为真实的信息
bu, _ := url.Parse("https://examplebucket-1250000000.cos.COS_REGION.myqcloud.com")
b := &cos.BaseURL{BucketURL: bu}
c := cos.NewClient(b, &http.Client{
Transport: &cos.AuthorizationTransport{
SecretID: os.Getenv("SECRETID"),
SecretKey: os.Getenv("SECRETKEY"),
},
})
opt := &cos.ObjectPutOptions{
ACLHeaderOptions: nil,
ObjectPutHeaderOptions: &cos.ObjectPutHeaderOptions{
XOptionHeader: &http.Header{},
},
}
pic := &cos.PicOperations{
IsPicInfo: 1,
Rules: []cos.PicOperationsRules{
{
FileId: "pic/out.jpeg",
Rule: "ci-process=GoodsMatting&center-layout=1&padding-layout=500x500",
},
},
}
opt.XOptionHeader.Add("Pic-Operations", cos.EncodePicOperations(pic))
name := "pic/test/cup.jpeg"
local_filename := "./cup.jpeg"
res, _, err := c.CI.PutFromFile(context.Background(), name, local_filename, opt)
log_status(err)
fmt.Printf("%+v\\n", res)
fmt.Printf("%+v\\n", res.OriginalInfo)
fmt.Printf("%+v\\n", res.ProcessResults)

方式三:云上数据处理

方法原型

func (s *CIService) ImageProcess(ctx context.Context, name string, opt *ImageProcessOptions) (*ImageProcessResult, *Response, error)

请求示例

// 将 examplebucket-1250000000 和 COS_REGION 修改为真实的信息
bu, _ := url.Parse("https://examplebucket-1250000000.cos.COS_REGION.myqcloud.com")
b := &cos.BaseURL{BucketURL: bu}
c := cos.NewClient(b, &http.Client{
Transport: &cos.AuthorizationTransport{
SecretID: os.Getenv("SECRETID"),
SecretKey: os.Getenv("SECRETKEY"),
},
})

pic := &cos.PicOperations{
IsPicInfo: 1,
Rules: []cos.PicOperationsRules{
{
FileId: "pic/cup_out.jpeg",
Rule: "ci-process=GoodsMatting&center-layout=1&padding-layout=500x500",
},
},
}

key := "pic/cup.jpeg"
res, _, err := c.CI.ImageProcess(context.Background(), key, pic)
log_status(err)
fmt.Printf("%+v\\n", res)