You need to enable JavaScript to run this app.
导航

异常图片检测(Go SDK)

最近更新时间2024.02.04 18:31:08

首次发布时间2023.10.12 11:15:43

异常图片检测用于检测图片中是否包含除图片以外的可疑文件,例如 MPEG-TS 文件。如果检测到图片包含可疑文件,您可以对该图片文件进行处理,防止存储空间被非法图床盗用。本文介绍如何通过 TOS Go SDK 进行异常图片检测。

注意事项

  • 检测的原图大小不能超过 20MB。
  • 仅支持对 JPG、JPEG、PNG、BMP、GIF 以及纯 TS 视频流格式文件进行异常图片检测。
  • 异常图片检测功能按处理的次数计费。详细说明,请参见数据处理费用

示例代码

以下代码展示如何检测异常图片。

package main

import (
    "context"
    "encoding/json"
    "fmt"
    "github.com/volcengine/ve-tos-golang-sdk/v2/tos"
    "io/ioutil"
)

type ImageInspectResponse struct {
    PicSize             int    `json:"picSize"`
    PicType             string `json:"picType"`
    Suspicious          bool   `json:"suspicious"`
    SuspiciousBeginByte int    `json:"suspiciousBeginByte"`
    SuspiciousEndByte   int    `json:"suspiciousEndByte"`
    SuspiciousSize      int    `json:"suspiciousSize"`
    SuspiciousType      string `json:"suspiciousType"`
}

func checkErr(err error) {
    if err != nil {
       if serverErr, ok := err.(*tos.TosServerError); ok {
          fmt.Println("Error:", serverErr.Error())
          fmt.Println("Request ID:", serverErr.RequestID)
          fmt.Println("Response Status Code:", serverErr.StatusCode)
          fmt.Println("Response Header:", serverErr.Header)
          fmt.Println("Response Err Code:", serverErr.Code)
          fmt.Println("Response Err Msg:", serverErr.Message)
       } else {
          fmt.Println("Error:", err)
       }
       panic(err)
    }
}

func main() {
    var (
       accessKey = os.Getenv("TOS_ACCESS_KEY")
       secretKey = os.Getenv("TOS_SECRET_KEY")
       // Bucket 所在区域对应的 Endpoint,如果以华北2(北京)为例,则 Endpoint 为 https://tos-cn-beijing.volces.com。
       endpoint = "https://tos-cn-beijing.volces.com"
       region   = "cn-beijing"
       // 填写 BucketName
       bucketName = "*** Provide your bucket name ***"

       // 图片名称
       imageKey = "image.png"
       ctx      = context.Background()
    )
    // 初始化客户端
    client, err := tos.NewClientV2(endpoint, tos.WithRegion(region), tos.WithCredentials(tos.NewStaticCredentials(accessKey, secretKey)))
    checkErr(err)

    // 请求并增加数据处理
    style := "image/inspect" /* 异常图片检测 */
    out, err := client.GetObjectV2(ctx, &tos.GetObjectV2Input{Bucket: bucketName, Key: imageKey, Process: style})
    checkErr(err)
    defer out.Content.Close()
    fmt.Println("ContentType:", out.ContentType)
    info, err := ioutil.ReadAll(out.Content)
    checkErr(err)
    imageInspect := &ImageInspectResponse{}
    err = json.Unmarshal(info, imageInspect)
    checkErr(err)
    fmt.Println("picSize:", imageInspect.PicSize, " picType:", imageInspect.PicType, " suspicious:", imageInspect.Suspicious)
}

相关文档

关于异常图片检测的详细介绍,请参见异常图片检测