Skip to content

Commit

Permalink
图片处理3期 sdk补充
Browse files Browse the repository at this point in the history
  • Loading branch information
lilang authored and jojoliang committed Jul 27, 2022
1 parent 8acf953 commit f53f4c6
Show file tree
Hide file tree
Showing 6 changed files with 593 additions and 1 deletion.
166 changes: 165 additions & 1 deletion ci.go
Original file line number Diff line number Diff line change
Expand Up @@ -1261,7 +1261,6 @@ func (s *CIService) DeleteStyle(ctx context.Context, opt *DeleteStyleOptions) (*
return resp, err
}


// ImageQualityResult TODO
type ImageQualityResult struct {
XMLName xml.Name `xml:"Response"`
Expand Down Expand Up @@ -1417,3 +1416,168 @@ func (s *CIService) DetectCar(ctx context.Context, obj string) (*DetectCarResult
resp, err := s.client.send(ctx, sendOpt)
return &res, resp, err
}

type CIServiceResult struct {
XMLName xml.Name `xml:"CIStatus"`
CIStatus string `xml:",chardata"`
}

func (s *CIService) OpenCIService(ctx context.Context) (*Response, error) {
sendOpt := &sendOptions{
baseURL: s.client.BaseURL.CIURL,
method: http.MethodPut,
uri: "/",
}
resp, err := s.client.send(ctx, sendOpt)
return resp, err
}

func (s *CIService) GetCIService(ctx context.Context) (*CIServiceResult, *Response, error) {
var res CIServiceResult
sendOpt := &sendOptions{
baseURL: s.client.BaseURL.CIURL,
method: http.MethodGet,
uri: "/",
result: &res,
}
resp, err := s.client.send(ctx, sendOpt)
return &res, resp, err
}

func (s *CIService) CloseCIService(ctx context.Context) (*Response, error) {
sendOpt := &sendOptions{
baseURL: s.client.BaseURL.CIURL,
method: http.MethodPut,
uri: "/?unbind",
}
resp, err := s.client.send(ctx, sendOpt)
return resp, err
}

type HotLinkOptions struct {
XMLName xml.Name `xml:"Hotlink"`
Url []string `xml:"Url,omitempty"`
Type string `xml:"Type,omitempty"`
}

type HotLinkResult struct {
XMLName xml.Name `xml:"Hotlink"`
Status string `xml:"Status,omitempty"`
Type string `xml:"Type,omitempty"`
Url []string `xml:"Url,omitempty"`
}

func (s *CIService) SetHotLink(ctx context.Context, opt *HotLinkOptions) (*Response, error) {
sendOpt := &sendOptions{
baseURL: s.client.BaseURL.CIURL,
method: http.MethodPut,
uri: "/?hotlink",
body: opt,
}
resp, err := s.client.send(ctx, sendOpt)
return resp, err
}

func (s *CIService) GetHotLink(ctx context.Context) (*HotLinkResult, *Response, error) {
var res HotLinkResult
sendOpt := &sendOptions{
baseURL: s.client.BaseURL.CIURL,
method: http.MethodGet,
uri: "/?hotlink",
result: &res,
}
resp, err := s.client.send(ctx, sendOpt)
return &res, resp, err
}

type OriginProtectResult struct {
XMLName xml.Name `xml:"OriginProtectStatus"`
OriginProtectStatus string `xml:",chardata"`
}

func (s *CIService) OpenOriginProtect(ctx context.Context) (*Response, error) {
sendOpt := &sendOptions{
baseURL: s.client.BaseURL.CIURL,
method: http.MethodPut,
uri: "/?origin-protect",
}
resp, err := s.client.send(ctx, sendOpt)
return resp, err
}

func (s *CIService) GetOriginProtect(ctx context.Context) (*OriginProtectResult, *Response, error) {
var res OriginProtectResult
sendOpt := &sendOptions{
baseURL: s.client.BaseURL.CIURL,
method: http.MethodGet,
uri: "/?origin-protect",
result: &res,
}
resp, err := s.client.send(ctx, sendOpt)
return &res, resp, err
}

func (s *CIService) CloseOriginProtect(ctx context.Context) (*Response, error) {
sendOpt := &sendOptions{
baseURL: s.client.BaseURL.CIURL,
method: http.MethodDelete,
uri: "/?origin-protect",
}
resp, err := s.client.send(ctx, sendOpt)
return resp, err
}

type PicTagResult struct {
XMLName xml.Name `xml:"RecognitionResult"`
Labels []PicTag `xml:"Labels,omitempty"`
}

type PicTag struct {
Confidence int `xml:"Confidence,omitempty"`
Name string `xml:"Name,omitempty"`
}

func (s *CIService) PicTag(ctx context.Context, obj string) (*PicTagResult, *Response, error) {
var res PicTagResult
sendOpt := &sendOptions{
baseURL: s.client.BaseURL.CIURL,
method: http.MethodGet,
uri: "/" + encodeURIComponent(obj) + "?ci-process=detect-label",
result: &res,
}
resp, err := s.client.send(ctx, sendOpt)
return &res, resp, err
}

type DetectFaceOptions struct {
MaxFaceNum int `url:"max-face-num,omitempty"`
}

type DetectFaceResult struct {
XMLName xml.Name `xml:"Response"`
ImageWidth int `xml:"ImageWidth,omitempty"`
ImageHeight int `xml:"ImageHeight,omitempty"`
FaceModelVersion string `xml:"FaceModelVersion,omitempty"`
RequestId string `xml:"RequestId,omitempty"`
FaceInfos []FaceInfos `xml:"FaceInfos,omitempty"`
}

type FaceInfos struct {
X int `xml:"X,omitempty"`
Y int `xml:"Y,omitempty"`
Width int `xml:"Width,omitempty"`
Height int `xml:"Height,omitempty"`
}

func (s *CIService) DetectFace(ctx context.Context, obj string, opt *DetectFaceOptions) (*DetectFaceResult, *Response, error) {
var res DetectFaceResult
sendOpt := &sendOptions{
baseURL: s.client.BaseURL.BucketURL,
method: http.MethodGet,
uri: "/" + encodeURIComponent(obj) + "?ci-process=detect-face",
optQuery: opt,
result: &res,
}
resp, err := s.client.send(ctx, sendOpt)
return &res, resp, err
}
56 changes: 56 additions & 0 deletions example/CI/ai_recognition/detect_face.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package main

import (
"context"
"fmt"
"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 main() {
u, _ := url.Parse("https://lilang-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: true,
},
},
})
obj := "pic/deer.jpg"
opt := &cos.DetectFaceOptions{
MaxFaceNum: 2,
}
res, _, err := c.CI.DetectFace(context.Background(), obj, opt)
log_status(err)
fmt.Printf("%+v\n", res)
}
58 changes: 58 additions & 0 deletions example/CI/ai_recognition/pic_tag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package main

import (
"context"
"fmt"
"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 picTag() {
u, _ := url.Parse("https://lilang-1253960454.cos.ap-chongqing.myqcloud.com")
b := &cos.BaseURL{CIURL: 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: true,
ResponseHeader: true,
ResponseBody: true,
},
},
})

obj := "pic/deer.jpg"
res, _, err := c.CI.PicTag(context.Background(), obj)
log_status(err)
fmt.Printf("%+v\n", res)
}

func main() {
picTag()
}

0 comments on commit f53f4c6

Please sign in to comment.