|
| 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 | +// 图⽚⼈⻋通⽤识别功能(上传时) |
| 34 | +func imageTargetRecWhenUpload() { |
| 35 | + u, _ := url.Parse("https://test-1234567890.ci.ap-chongqing.myqcloud.com") |
| 36 | + b := &cos.BaseURL{BucketURL: u} |
| 37 | + c := cos.NewClient(b, &http.Client{ |
| 38 | + Transport: &cos.AuthorizationTransport{ |
| 39 | + SecretID: os.Getenv("COS_SECRETID"), |
| 40 | + SecretKey: os.Getenv("COS_SECRETKEY"), |
| 41 | + Transport: &debug.DebugRequestTransport{ |
| 42 | + RequestHeader: true, |
| 43 | + RequestBody: false, |
| 44 | + ResponseHeader: true, |
| 45 | + ResponseBody: true, |
| 46 | + }, |
| 47 | + }, |
| 48 | + }) |
| 49 | + |
| 50 | + opt := &cos.ObjectPutOptions{ |
| 51 | + ACLHeaderOptions: nil, |
| 52 | + ObjectPutHeaderOptions: &cos.ObjectPutHeaderOptions{ |
| 53 | + XOptionHeader: &http.Header{}, |
| 54 | + }, |
| 55 | + } |
| 56 | + pic := &cos.PicOperations{ |
| 57 | + IsPicInfo: 1, |
| 58 | + Rules: []cos.PicOperationsRules{ |
| 59 | + { |
| 60 | + FileId: "/pic/out.jpeg", |
| 61 | + Rule: "ci-process=ImageTargetRec&detect-type=car,face,plate,body&cover=1", |
| 62 | + }, |
| 63 | + }, |
| 64 | + } |
| 65 | + opt.XOptionHeader.Add("Pic-Operations", cos.EncodePicOperations(pic)) |
| 66 | + name := "pic/cup.png" |
| 67 | + local_filename := "./test_output_02_000.jpg" |
| 68 | + res, _, err := c.CI.PutFromFile(context.Background(), name, local_filename, opt) |
| 69 | + log_status(err) |
| 70 | + fmt.Printf("%+v\n", res) |
| 71 | + fmt.Printf("%+v\n", res.OriginalInfo) |
| 72 | + fmt.Printf("%+v\n", res.ProcessResults) |
| 73 | + fmt.Printf("%+v\n", res.ImgTargetRecResult) |
| 74 | +} |
| 75 | + |
| 76 | +// 图⽚⼈⻋通⽤识别功能(云上数据处理) |
| 77 | +func imageTargetRecWhenCloud() { |
| 78 | + u, _ := url.Parse("https://test-1234567890.ci.ap-chongqing.myqcloud.com") |
| 79 | + b := &cos.BaseURL{BucketURL: u} |
| 80 | + c := cos.NewClient(b, &http.Client{ |
| 81 | + Transport: &cos.AuthorizationTransport{ |
| 82 | + SecretID: os.Getenv("COS_SECRETID"), |
| 83 | + SecretKey: os.Getenv("COS_SECRETKEY"), |
| 84 | + Transport: &debug.DebugRequestTransport{ |
| 85 | + RequestHeader: true, |
| 86 | + // Notice when put a large file and set need the request body, might happend out of memory error. |
| 87 | + RequestBody: false, |
| 88 | + ResponseHeader: true, |
| 89 | + ResponseBody: true, |
| 90 | + }, |
| 91 | + }, |
| 92 | + }) |
| 93 | + |
| 94 | + pic := &cos.PicOperations{ |
| 95 | + IsPicInfo: 1, |
| 96 | + Rules: []cos.PicOperationsRules{ |
| 97 | + { |
| 98 | + FileId: "/pic/cup_out.jpeg", |
| 99 | + Rule: "ci-process=ImageTargetRec&detect-type=car,face,plate,body&cover=1", |
| 100 | + }, |
| 101 | + }, |
| 102 | + } |
| 103 | + |
| 104 | + key := "pic/cup.png" |
| 105 | + res, _, err := c.CI.ImageProcess(context.Background(), key, pic) |
| 106 | + log_status(err) |
| 107 | + fmt.Printf("%+v\n", res) |
| 108 | + fmt.Printf("%+v\n", res.ImgTargetRecResult) |
| 109 | +} |
| 110 | + |
| 111 | +func main() { |
| 112 | + // imageTargetRecWhenUpload() |
| 113 | + imageTargetRecWhenCloud() |
| 114 | +} |
0 commit comments