-
Notifications
You must be signed in to change notification settings - Fork 3
/
recognize.go
61 lines (54 loc) · 1.5 KB
/
recognize.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package ocrKit
import (
"fmt"
httpClientKit2 "github.com/richelieu-yang/chimera/v2/src/component/web/request/httpClientKit"
"github.com/richelieu-yang/chimera/v2/src/core/errorKit"
"github.com/richelieu-yang/chimera/v2/src/crypto/base64Kit"
"github.com/richelieu-yang/chimera/v2/src/json/jsonKit"
"github.com/richelieu-yang/chimera/v2/src/urlKit"
)
const (
// 固定参数
grantType = "client_credentials"
)
// RecognizeUniversalWords 通用文字识别(标准版)
/*
文档:
https://cloud.baidu.com/doc/OCR/s/zk3h7xz52
https://ai.baidu.com/ai-doc/OCR/zk3h7xz52
*/
func RecognizeUniversalWords(imagePath string) (*Words, error) {
// url
token, err := getAccessToken()
if err != nil {
return nil, err
}
url := fmt.Sprintf("%s?access_token=%s", "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic", token.AccessToken)
// params
base64Str, err := base64Kit.EncodeFileToString(imagePath)
if err != nil {
return nil, err
}
params := map[string][]string{
"language_type": {"CHN_ENG"},
"image": {urlKit.EncodeURIComponent(base64Str)},
}
// 发请求
_, respData, err := httpClientKit2.Post(url, httpClientKit2.WithPostParams(params))
if err != nil {
return nil, err
}
m := make(map[string]interface{})
if err := jsonKit.Unmarshal(respData, &m); err != nil {
return nil, err
}
// 解析响应
words, err := parseMapToWords(m)
if err != nil {
return nil, err
}
if words == nil {
return nil, errorKit.New("failure response(%s)", string(respData))
}
return words, nil
}