plateNumber.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package eventServer
  2. import (
  3. "encoding/base64"
  4. "fmt"
  5. "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
  6. "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors"
  7. "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile"
  8. "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/regions"
  9. ocr "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/ocr/v20181119"
  10. )
  11. // CallLicensePlateRecognitionAPI 识别车牌
  12. func CallLicensePlateRecognitionAPI(file []byte) (number string, err error) {
  13. // 实例化一个认证对象,入参需要传入腾讯云账户 SecretId 和 SecretKey,此处还需注意密钥对的保密
  14. credential := common.NewCredential(
  15. "SecretId",
  16. "SecretKey",
  17. )
  18. // 实例化一个client选项
  19. cpf := profile.NewClientProfile()
  20. cpf.HttpProfile.Endpoint = "ocr.tencentcloudapi.com"
  21. // 实例化要请求产品的client对象,clientProfile是可选的
  22. client, _ := ocr.NewClient(credential, regions.Shanghai, cpf)
  23. // 实例化一个请求对象,每个接口都会对应一个request对象
  24. request := ocr.NewLicensePlateOCRRequest()
  25. base64String := base64.StdEncoding.EncodeToString(file)
  26. request.ImageBase64 = common.StringPtr(base64String)
  27. // 返回的resp是一个LicensePlateOCRResponse的实例,与请求对象对应
  28. response, err := client.LicensePlateOCR(request)
  29. if _, ok := err.(*errors.TencentCloudSDKError); ok {
  30. fmt.Printf("An API error has returned: %s", err)
  31. return
  32. }
  33. if err != nil {
  34. panic(err)
  35. }
  36. // 输出json格式的字符串回包
  37. fmt.Printf("%s\n", response.ToJsonString())
  38. fmt.Printf("车牌:%s\n", *response.Response.Number)
  39. return *response.Response.Number, err
  40. }
  41. type AutoGenerated struct {
  42. Response struct {
  43. Color string `json:"Color"`
  44. Confidence int `json:"Confidence"`
  45. LicensePlateInfos []struct {
  46. Color string `json:"Color"`
  47. Confidence int `json:"Confidence"`
  48. Number string `json:"Number"`
  49. Rect struct {
  50. Height int `json:"Height"`
  51. Width int `json:"Width"`
  52. X int `json:"X"`
  53. Y int `json:"Y"`
  54. } `json:"Rect"`
  55. } `json:"LicensePlateInfos"`
  56. Number string `json:"Number"`
  57. Rect struct {
  58. Height int `json:"Height"`
  59. Width int `json:"Width"`
  60. X int `json:"X"`
  61. Y int `json:"Y"`
  62. } `json:"Rect"`
  63. RequestID string `json:"RequestId"`
  64. } `json:"Response"`
  65. }