aliyun.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package services
  2. import (
  3. "crypto/tls"
  4. "encoding/base64"
  5. "encoding/json"
  6. "errors"
  7. "fmt"
  8. openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
  9. ocr20191230 "github.com/alibabacloud-go/ocr-20191230/v3/client"
  10. util "github.com/alibabacloud-go/tea-utils/v2/service"
  11. "github.com/alibabacloud-go/tea/tea"
  12. "hongze/hz_eta_api/utils"
  13. "io/ioutil"
  14. "net/http"
  15. "strings"
  16. rhttp "github.com/rdlucklib/rdluck_tools/http"
  17. )
  18. const (
  19. AlAppKey = "203889624"
  20. AlAppSecret = "voampGGl0yGNx5xA1sZdmZlV2EiBct2P"
  21. AlAppCode = "22553c4ba74545568aba70ac6cfd441d"
  22. )
  23. type OcrItem struct {
  24. Image string `json:"image"`
  25. }
  26. type OcrResult struct {
  27. Addr []string `json:"addr"`
  28. Company []string `json:"company"`
  29. Success bool `json:"success"`
  30. TelCell []string `json:"tel_cell"`
  31. Departments []string `json:"departments"`
  32. Name string `json:"name"`
  33. OfficePhoneNumbers []string `json:"office_phone_numbers"`
  34. Titles []string `json:"titles"`
  35. }
  36. // /ocr_business_card.json
  37. func AliyunBusinessCard(imgUrl string) (result *OcrResult, err error) {
  38. postUrl := `https://dm-57.data.aliyun.com/rest/160601/ocr/ocr_business_card.json`
  39. imgBody, err := rhttp.Get(imgUrl)
  40. if err != nil {
  41. return
  42. }
  43. imageBase64 := base64.StdEncoding.EncodeToString(imgBody)
  44. item := new(OcrItem)
  45. item.Image = imageBase64
  46. jsonBody, err := json.Marshal(item)
  47. if err != nil {
  48. fmt.Println("json.Marshal Err:" + err.Error())
  49. return
  50. }
  51. tr := &http.Transport{
  52. TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
  53. }
  54. client := &http.Client{Transport: tr}
  55. req, err := http.NewRequest("POST", postUrl, strings.NewReader(string(jsonBody)))
  56. if err != nil {
  57. fmt.Println("err:" + err.Error())
  58. return
  59. }
  60. req.Header.Set("Content-Type", "application/json")
  61. req.Header.Set("Authorization", "APPCODE "+AlAppCode)
  62. resp, err := client.Do(req)
  63. if err != nil {
  64. fmt.Println("client.Do Err:" + err.Error())
  65. return
  66. }
  67. fmt.Println("resp")
  68. fmt.Println(resp)
  69. defer resp.Body.Close()
  70. body, err := ioutil.ReadAll(resp.Body)
  71. if err != nil {
  72. fmt.Println("err:" + err.Error())
  73. return
  74. }
  75. fmt.Println("body:", string(body))
  76. utils.FileLog.Info(fmt.Sprintf("aliyun card;imgUrl:%s; Body:%s", imgUrl, string(body)))
  77. err = json.Unmarshal(body, &result)
  78. return
  79. }
  80. // AliyunBusinessCardOcr 阿里云的ocr识别
  81. func AliyunBusinessCardOcr(imgUrl string) (result OcrResult, err error) {
  82. if utils.AccessKeyId == `` {
  83. err = errors.New("阿里云信息未配置")
  84. return
  85. }
  86. config := &openapi.Config{
  87. // 2、"YOUR_ACCESS_KEY_ID", "YOUR_ACCESS_KEY_SECRET" 的生成请参考https://help.aliyun.com/document_detail/175144.html
  88. // 如果您是用的子账号AccessKey,还需要为子账号授予权限AliyunVIAPIFullAccess,请参考https://help.aliyun.com/document_detail/145025.html
  89. // 您的 AccessKey ID
  90. AccessKeyId: tea.String(utils.AccessKeyId),
  91. // 您的 AccessKey Secret
  92. AccessKeySecret: tea.String(utils.AccessKeySecret),
  93. }
  94. // 3、访问的域名。注意:这个地方需要求改为相应类目的域名,参考:https://help.aliyun.com/document_detail/143103.html
  95. config.Endpoint = tea.String("ocr.cn-shanghai.aliyuncs.com")
  96. // 4、这里只是以ocr为例,其他能力请使用相应类目的包
  97. client, err := ocr20191230.NewClient(config)
  98. if err != nil {
  99. return
  100. }
  101. httpClient := http.Client{}
  102. file, _ := httpClient.Get(imgUrl)
  103. // 5、这里只是以ocr的RecognizeBankCard为例,其他能力请使用相应类目的包和类
  104. recognizeBusinessCardAdvanceRequest := &ocr20191230.RecognizeBusinessCardAdvanceRequest{
  105. ImageURLObject: file.Body,
  106. }
  107. runtime := &util.RuntimeOptions{}
  108. // 6、这里只是以ocr的RecognizeBankCard为例,其他能力请使用相应类目的包和类,注意RecognizeBankCardWithOptions也需要改成对应能力的方法名。方法名是根据能力名称按照一定规范形成的,如能力名称为SegmentCommonImage,对应方法名应该为SegmentCommonImageWithOptions。
  109. recognizeBusinessCardResponse, err := client.RecognizeBusinessCardAdvance(recognizeBusinessCardAdvanceRequest, runtime)
  110. if err != nil {
  111. // 获取整体报错信息
  112. //fmt.Println(err.Error())
  113. utils.FileLog.Info(fmt.Sprintf("aliyun card;imgUrl:%s; err:%s", imgUrl, err))
  114. return
  115. }
  116. // 获取整体结果
  117. //fmt.Println(recognizeBusinessCardResponse)
  118. //// 获取单个字段,这里只是一个例子,具体能力下的字段需要看具体能力的文档
  119. //fmt.Println(recognizeBusinessCardResponse.Body.Data.Name)
  120. // 地址
  121. for _, v := range recognizeBusinessCardResponse.Body.Data.Addresses {
  122. result.Addr = append(result.Addr, *v)
  123. }
  124. // company
  125. for _, v := range recognizeBusinessCardResponse.Body.Data.Companies {
  126. result.Company = append(result.Company, *v)
  127. }
  128. // tel_cell
  129. for _, v := range recognizeBusinessCardResponse.Body.Data.CellPhoneNumbers {
  130. result.TelCell = append(result.TelCell, *v)
  131. }
  132. // Departments
  133. for _, v := range recognizeBusinessCardResponse.Body.Data.Departments {
  134. result.Departments = append(result.Departments, *v)
  135. }
  136. // OfficePhoneNumbers
  137. for _, v := range recognizeBusinessCardResponse.Body.Data.OfficePhoneNumbers {
  138. result.OfficePhoneNumbers = append(result.OfficePhoneNumbers, *v)
  139. }
  140. // Titles
  141. for _, v := range recognizeBusinessCardResponse.Body.Data.Titles {
  142. result.Titles = append(result.Titles, *v)
  143. }
  144. if recognizeBusinessCardResponse.Body.Data.Name != nil {
  145. result.Name = *recognizeBusinessCardResponse.Body.Data.Name
  146. }
  147. result.Success = true
  148. return
  149. }