Skip to content

Commit

Permalink
商品抠图
Browse files Browse the repository at this point in the history
  • Loading branch information
lilang authored and jojoliang committed Aug 8, 2022
1 parent 01fad12 commit e3215ad
Show file tree
Hide file tree
Showing 2 changed files with 154 additions and 0 deletions.
12 changes: 12 additions & 0 deletions ci.go
Original file line number Diff line number Diff line change
Expand Up @@ -1832,3 +1832,15 @@ func (s *CIService) LivenessRecognitionWhenUpload(ctx context.Context, obj, file

return &res, resp, err
}

// GoodsMatting 商品抠图
func (s *CIService) GoodsMatting(ctx context.Context, key string) (*Response, error) {
sendOpt := sendOptions{
baseURL: s.client.BaseURL.BucketURL,
uri: "/" + encodeURIComponent(key) + "?ci-process=GoodsMatting",
method: http.MethodGet,
disableCloseBody: true,
}
resp, err := s.client.send(ctx, &sendOpt)
return resp, err
}
142 changes: 142 additions & 0 deletions example/CI/ai_recognition/goods_matting.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package main

import (
"context"
"fmt"
"io"
"net/http"
"net/url"
"os"

"github.com/tencentyun/cos-go-sdk-v5"
"github.com/tencentyun/cos-go-sdk-v5/debug"
)

func log_status(err error) {
if err == nil {
return
}
if cos.IsNotFoundError(err) {
// WARN
fmt.Println("WARN: Resource is not existed")
} else if e, ok := cos.IsCOSError(err); ok {
fmt.Printf("ERROR: Code: %v\n", e.Code)
fmt.Printf("ERROR: Message: %v\n", e.Message)
fmt.Printf("ERROR: Resource: %v\n", e.Resource)
fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
// ERROR
} else {
fmt.Printf("ERROR: %v\n", err)
// ERROR
}
}

// 商品抠图(上传时)
func goodsMattingWhenUpload() {
u, _ := url.Parse("https://test-1253960454.cos.ap-chongqing.myqcloud.com")
b := &cos.BaseURL{BucketURL: u}
c := cos.NewClient(b, &http.Client{
Transport: &cos.AuthorizationTransport{
SecretID: os.Getenv("COS_SECRETID"),
SecretKey: os.Getenv("COS_SECRETKEY"),
Transport: &debug.DebugRequestTransport{
RequestHeader: true,
RequestBody: false,
ResponseHeader: true,
ResponseBody: false,
},
},
})

opt := &cos.ObjectPutOptions{
nil,
&cos.ObjectPutHeaderOptions{
XOptionHeader: &http.Header{},
},
}
pic := &cos.PicOperations{
IsPicInfo: 1,
Rules: []cos.PicOperationsRules{
{
FileId: "pic/out.jpeg",
Rule: "ci-process=GoodsMatting",
},
},
}
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 goodsMattingWhenDownload() {
u, _ := url.Parse("https://test-1253960454.pic.ap-chongqing.myqcloud.com")
// u, _ := url.Parse("https://test-1253960454.cos.ap-chongqing.myqcloud.com")
b := &cos.BaseURL{BucketURL: u}
c := cos.NewClient(b, &http.Client{
Transport: &cos.AuthorizationTransport{
SecretID: os.Getenv("COS_SECRETID"),
SecretKey: os.Getenv("COS_SECRETKEY"),
Transport: &debug.DebugRequestTransport{
RequestHeader: true,
// Notice when put a large file and set need the request body, might happend out of memory error.
RequestBody: false,
ResponseHeader: true,
ResponseBody: false,
},
},
})

key := "pic/cup.jpeg"
localPath := "cup.jpeg"
resp, err := c.CI.GoodsMatting(context.Background(), key)
log_status(err)
fd, _ := os.OpenFile(localPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0660)
io.Copy(fd, resp.Body)
fd.Close()
}

// 商品抠图(云上数据处理)
func goodsMattingWhenCloud() {
u, _ := url.Parse("https://test-1253960454.cos.ap-chongqing.myqcloud.com")
b := &cos.BaseURL{BucketURL: u}
c := cos.NewClient(b, &http.Client{
Transport: &cos.AuthorizationTransport{
SecretID: os.Getenv("COS_SECRETID"),
SecretKey: os.Getenv("COS_SECRETKEY"),
Transport: &debug.DebugRequestTransport{
RequestHeader: true,
// Notice when put a large file and set need the request body, might happend out of memory error.
RequestBody: false,
ResponseHeader: true,
ResponseBody: false,
},
},
})

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

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

func main() {
goodsMattingWhenDownload()
// goodsMattingWhenUpload()
// goodsMattingWhenCloud()
}

0 comments on commit e3215ad

Please sign in to comment.