baidu.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. package services
  2. import (
  3. "encoding/base64"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "github.com/rdlucklib/rdluck_tools/http"
  8. "net/url"
  9. )
  10. /*
  11. //名片识别
  12. func init() {
  13. fmt.Println("start")
  14. services.GetBusinessCard()
  15. fmt.Println("end")
  16. }
  17. */
  18. type BdAccessToken struct {
  19. AccessToken string `json:"access_token"`
  20. Error string
  21. }
  22. func GetAccessToken() (accessTokenStr string, err error) {
  23. baiduApiKey := "nMfRuZ0qBCsVWfGGKekaoMqh"
  24. baiduBdSecretKey := "N6xcrUizYijMf47iTDDyPBm3lTOrvVSF"
  25. getUrl := `https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=` + baiduApiKey + `&client_secret=` + baiduBdSecretKey + `&`
  26. body, err := http.Post(getUrl, "")
  27. var accessToken BdAccessToken
  28. err = json.Unmarshal(body, &accessToken)
  29. if err != nil {
  30. fmt.Println("err:" + err.Error())
  31. return
  32. }
  33. if accessToken.Error != "" {
  34. err = errors.New("获取token失败,Err:" + accessToken.Error)
  35. return
  36. }
  37. accessTokenStr = accessToken.AccessToken
  38. return
  39. }
  40. type BdBussinessCard struct {
  41. Errno int `json:"error_code"`
  42. ErrorMsg string `json:"error_msg"`
  43. Logid string
  44. WordsResultNum int
  45. WordsResult struct {
  46. ADDR []string `json:"ADDR"`
  47. FAX []string `json:"FAX"`
  48. MOBILE []string `json:"MOBILE"`
  49. NAME []string `json:"NAME"`
  50. PC []string `json:"PC"`
  51. URL []string `json:"URL"`
  52. TEL []string `json:"TEL"`
  53. COMPANY []string `json:"COMPANY"`
  54. TITLE []string `TITLE`
  55. EMAIL []string `EMAIL`
  56. } `json:"words_result"`
  57. //{"log_id":1332206693559304192,"error_msg":"target detect error","error_code":282102}
  58. }
  59. func GetBusinessCard(imgUrl string) (item *BdBussinessCard, err error) {
  60. imgBody, err := http.Get(imgUrl)
  61. if err != nil {
  62. return
  63. }
  64. //rnStr := utils.GetRandStringNoSpecialChar(5)
  65. //savePath := time.Now().Format(utils.FormatDateTimeUnSpace) + rnStr + ".jpg"
  66. //err = file.SaveFile(imgBody, savePath)
  67. //if err != nil {
  68. // return
  69. //}
  70. //defer func() {
  71. // os.Remove(savePath)
  72. //}()
  73. accessToken, err := GetAccessToken()
  74. fmt.Println("accessToken")
  75. fmt.Println(accessToken)
  76. if err != nil {
  77. fmt.Print("GetAccessToken Err:" + err.Error())
  78. }
  79. bussinessCardUrl := `https://aip.baidubce.com/rest/2.0/ocr/v1/business_card?access_token=` + accessToken
  80. //ff, _ := os.Open(savePath)
  81. //defer ff.Close()
  82. //sourcebuffer := make([]byte, 500000)
  83. //n, _ := ff.Read(sourcebuffer)
  84. //sourcestring := base64.StdEncoding.EncodeToString(sourcebuffer[:n])
  85. imageBase64 := base64.StdEncoding.EncodeToString(imgBody)
  86. imgData := imageBase64
  87. param := url.Values{}
  88. param.Set("image", imgData)
  89. cardBody, err := http.Post(bussinessCardUrl, param.Encode())
  90. fmt.Println("body:", string(cardBody))
  91. //fmt.Println(string(cardBody))
  92. if err != nil {
  93. fmt.Println("err:", err.Error())
  94. return
  95. }
  96. err = json.Unmarshal(cardBody, &item)
  97. if err != nil {
  98. fmt.Println("line err:", err.Error())
  99. }
  100. return
  101. }
  102. type CardAccurate struct {
  103. WordsResultNum int `json:"words_result_num"`
  104. WordsResult []struct {
  105. Words string `json:"words"`
  106. } `json:"words_result"`
  107. ErrorCode int `json:"error_code"`
  108. ErrorMsg string `json:"error_msg"`
  109. }
  110. //通用文字(高精度)
  111. func GetBusinessCardByAccurate(imgUrl string) (item *CardAccurate, err error) {
  112. imgBody, err := http.Get(imgUrl)
  113. if err != nil {
  114. return
  115. }
  116. //rnStr := utils.GetRandStringNoSpecialChar(5)
  117. //savePath := time.Now().Format(utils.FormatDateTimeUnSpace) + rnStr + ".jpg"
  118. //err = file.SaveFile(imgBody, savePath)
  119. //if err != nil {
  120. // return
  121. //}
  122. //defer func() {
  123. // os.Remove(savePath)
  124. //}()
  125. //
  126. //nowStr:=time.Now().Format(utils.FormatDateTimeUnSpace)
  127. //outPath := "./"+nowStr+".jpg"
  128. //缩放
  129. //file, err := os.Open(savePath)
  130. //if err != nil {
  131. // fmt.Println("file err:" + err.Error())
  132. // return
  133. //}
  134. //
  135. //// decode jpeg into image.Image
  136. //img, err := jpeg.Decode(file)
  137. //if err != nil {
  138. // fmt.Println("jpeg Decode:" + err.Error())
  139. // return
  140. //}
  141. //width:=img.Bounds().Dx()
  142. //height:=img.Bounds().Dy()
  143. //fmt.Println(width,height)
  144. //
  145. //if width>=4096 {
  146. // width=4000
  147. //}
  148. //
  149. //if height>=4096 {
  150. // height=4000
  151. //}
  152. //
  153. //file.Close()
  154. //
  155. //fmt.Println(width,height)
  156. //
  157. //m := resize.Resize(uint(width),uint(height), img, resize.Lanczos3)
  158. //
  159. //out, err := os.Create(outPath)
  160. //if err != nil {
  161. // fmt.Println("Create:" + err.Error())
  162. // return
  163. //}
  164. //defer out.Close()
  165. //
  166. //jpeg.Encode(out, m, nil)
  167. accessToken, err := GetAccessToken()
  168. fmt.Println("accessToken")
  169. fmt.Println(accessToken)
  170. if err != nil {
  171. fmt.Print("GetAccessToken Err:" + err.Error())
  172. }
  173. bussinessCardUrl := `https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic?access_token=` + accessToken
  174. //ff, _ := os.Open(outPath)
  175. //defer ff.Close()
  176. //sourcebuffer := make([]byte, 500000)
  177. //n, _ := ff.Read(sourcebuffer)
  178. //sourcestring := base64.StdEncoding.EncodeToString(sourcebuffer[:n])
  179. //imgData := sourcestring
  180. imageBase64 := base64.StdEncoding.EncodeToString(imgBody)
  181. param := url.Values{}
  182. param.Set("image", imageBase64)
  183. cardBody, err := http.Post(bussinessCardUrl, param.Encode())
  184. if err != nil {
  185. fmt.Println("err:", err.Error())
  186. return
  187. }
  188. err = json.Unmarshal(cardBody, &item)
  189. return
  190. }
  191. //通用文字(高精度)
  192. func GetBdGeneralBasic(imgUrl string) (item *CardAccurate, err error) {
  193. imgBody, err := http.Get(imgUrl)
  194. if err != nil {
  195. return
  196. }
  197. accessToken, err := GetAccessToken()
  198. if err != nil {
  199. fmt.Print("GetAccessToken Err:" + err.Error())
  200. }
  201. bussinessCardUrl := `https://aip.baidubce.com/rest/2.0/ocr/v1/handwriting?access_token=` + accessToken
  202. imageBase64 := base64.StdEncoding.EncodeToString(imgBody)
  203. param := url.Values{}
  204. param.Set("image", imageBase64)
  205. cardBody, err := http.Post(bussinessCardUrl, param.Encode())
  206. if err != nil {
  207. fmt.Println("err:", err.Error())
  208. return
  209. }
  210. err = json.Unmarshal(cardBody, &item)
  211. return
  212. }