Skip to content

Commit 98eab28

Browse files
author
jojoliang
committed
add CI图片审核
1 parent 5e69c19 commit 98eab28

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed

ci.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"encoding/json"
66
"encoding/xml"
7+
"io"
78
"net/http"
89
)
910

@@ -72,3 +73,28 @@ func (s *ObjectService) PostCI(ctx context.Context, name string, opt *CloudImage
7273
resp, err := s.client.send(ctx, &sendOpt)
7374
return &res, resp, err
7475
}
76+
77+
type CloudImageRecognitionInfo struct {
78+
Code int `xml:"Code,omitempty"`
79+
Msg string `xml:"Msg,omitempty"`
80+
HitFlag int `xml:"HitFlag,omitempty"`
81+
Score int `xml:"Score,omitempty"`
82+
Label string `xml:"Label,omitempty"`
83+
Count int `xml:"Count,omitempty"`
84+
}
85+
86+
type CloudImageRecognitionResult struct {
87+
PornInfo *CloudImageRecognitionInfo `xml:"PornInfo,omitempty"`
88+
TerroristInfo *CloudImageRecognitionInfo `xml:"TerroristInfo,omitempty"`
89+
PoliticsInfo *CloudImageRecognitionInfo `xml:"PoliticsInfo,omitempty"`
90+
AdsInfo *CloudImageRecognitionInfo `xml:"AdsInfo,omitempty"`
91+
}
92+
93+
func GetRecognitionResult(body io.ReadCloser) *CloudImageRecognitionResult {
94+
var res CloudImageRecognitionResult
95+
err := xml.NewDecoder(body).Decode(&res)
96+
if err != nil && err != io.EOF {
97+
return nil
98+
}
99+
return &res
100+
}

example/object/ci_get.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"net/http"
7+
"net/url"
8+
"os"
9+
10+
"github.com/tencentyun/cos-go-sdk-v5"
11+
"github.com/tencentyun/cos-go-sdk-v5/debug"
12+
)
13+
14+
func log_status(err error) {
15+
if err == nil {
16+
return
17+
}
18+
if cos.IsNotFoundError(err) {
19+
// WARN
20+
fmt.Println("WARN: Resource is not existed")
21+
} else if e, ok := cos.IsCOSError(err); ok {
22+
fmt.Printf("ERROR: Code: %v\n", e.Code)
23+
fmt.Printf("ERROR: Message: %v\n", e.Message)
24+
fmt.Printf("ERROR: Resource: %v\n", e.Resource)
25+
fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
26+
// ERROR
27+
} else {
28+
fmt.Printf("ERROR: %v\n", err)
29+
// ERROR
30+
}
31+
}
32+
33+
func main() {
34+
u, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com")
35+
b := &cos.BaseURL{BucketURL: u}
36+
c := cos.NewClient(b, &http.Client{
37+
Transport: &cos.AuthorizationTransport{
38+
SecretID: os.Getenv("COS_SECRETID"),
39+
SecretKey: os.Getenv("COS_SECRETKEY"),
40+
Transport: &debug.DebugRequestTransport{
41+
RequestHeader: true,
42+
RequestBody: true,
43+
ResponseHeader: true,
44+
ResponseBody: true,
45+
},
46+
},
47+
})
48+
opt := &cos.ObjectGetOptions{
49+
CIProcess: "sensitive-content-recognition",
50+
CIDetectType: "porn,terrorist,politics,ads",
51+
}
52+
53+
// Case1 Download object into ReadCloser(). the body needs to be closed
54+
name := "test.jpg"
55+
resp, err := c.Object.Get(context.Background(), name, opt)
56+
log_status(err)
57+
resp.Body.Close()
58+
res := cos.GetRecognitionResult(resp.Body)
59+
if res != nil {
60+
fmt.Printf("%+v\n", res)
61+
}
62+
}

object.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ type ObjectGetOptions struct {
3535
XCosSSECustomerKeyMD5 string `header:"x-cos-server-side-encryption-customer-key-MD5,omitempty" url:"-" xml:"-"`
3636

3737
XCosTrafficLimit int `header:"x-cos-traffic-limit,omitempty" url:"-" xml:"-"`
38+
39+
// CI 图片审核
40+
CIProcess string `header:"-" url:"ci-process" xml:"-"`
41+
CIDetectType string `header:"-" url:"detect-type" xml:"-"`
3842
}
3943

4044
// presignedURLTestingOptions is the opt of presigned url

0 commit comments

Comments
 (0)