-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathocr_space.go
207 lines (179 loc) · 4.73 KB
/
ocr_space.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package go_ocr_space
import (
"bytes"
"encoding/json"
"io"
"io/ioutil"
"log"
"mime/multipart"
"net/http"
"net/url"
"os"
"path/filepath"
)
type OCREngine string
const (
OCREngine1 OCREngine = "1"
OCREngine2 OCREngine = "2"
OCREngine3 OCREngine = "3"
)
type OCRText struct {
ParsedResults []struct {
TextOverlay struct {
Lines []struct {
Words []struct {
WordText string `json:"WordText"`
Left float64 `json:"Left"`
Top float64 `json:"Top"`
Height float64 `json:"Height"`
Width float64 `json:"Width"`
} `json:"Words"`
MaxHeight float64 `json:"MaxHeight"`
MinTop float64 `json:"MinTop"`
} `json:"Lines"`
HasOverlay bool `json:"HasOverlay"`
Message string `json:"Message"`
} `json:"TextOverlay"`
TextOrientation string `json:"TextOrientation"`
FileParseExitCode int `json:"FileParseExitCode"`
ParsedText string `json:"ParsedText"`
ErrorMessage string `json:"ErrorMessage"`
ErrorDetails string `json:"ErrorDetails"`
} `json:"ParsedResults"`
OCRExitCode int `json:"OCRExitCode"`
IsErroredOnProcessing bool `json:"IsErroredOnProcessing"`
ErrorMessage []string `json:"ErrorMessage"`
ErrorDetails string `json:"ErrorDetails"`
ProcessingTimeInMilliseconds string `json:"ProcessingTimeInMilliseconds"`
SearchablePDFURL string `json:"SearchablePDFURL"`
}
type Config struct {
ApiKey string
Language string
Url string
OCREngine string
}
func InitConfig(apiKey string, language string, OCREngine OCREngine) Config {
var config Config
config.ApiKey = apiKey
config.Language = language
config.Url = "https://api.ocr.space/parse/image"
config.OCREngine = string(OCREngine)
return config
}
func (c Config) ParseFromUrl(fileUrl string) (OCRText, error) {
var results OCRText
var resp, err = http.PostForm(c.Url,
url.Values{
"url": {fileUrl},
"language": {c.Language},
"apikey": {c.ApiKey},
"isOverlayRequired": {"true"},
"isSearchablePdfHideTextLayer": {"true"},
"scale": {"true"},
"OCREngine": {c.OCREngine},
},
)
if err != nil {
return results, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return results, err
}
err = json.Unmarshal(body, &results)
if err != nil {
return results, err
}
return results, nil
}
func (c Config) ParseFromBase64(baseString string) (OCRText, error) {
var results OCRText
resp, err := http.PostForm("https://api.ocr.space/parse/image",
url.Values{
"base64Image": {baseString},
"language": {c.Language},
"apikey": {c.ApiKey},
"isOverlayRequired": {"true"},
"isSearchablePdfHideTextLayer": {"true"},
"scale": {"true"},
"OCREngine": {c.OCREngine},
},
)
if err != nil {
return results, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return results, err
}
err = json.Unmarshal(body, &results)
if err != nil {
return results, err
}
return results, nil
}
func (c Config) ParseFromLocal(localPath string) (OCRText, error) {
var results OCRText
params := map[string]string{
"language": c.Language,
"apikey": c.ApiKey,
"isOverlayRequired": "true",
"isSearchablePdfHideTextLayer": "true",
"scale": "true",
"OCREngine": c.OCREngine,
}
file, err := os.Open(localPath)
if err != nil {
return results, err
}
defer file.Close()
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
part, err := writer.CreateFormFile("file", filepath.Base(localPath))
if err != nil {
return results, err
}
_, err = io.Copy(part, file)
for key, val := range params {
_ = writer.WriteField(key, val)
}
err = writer.Close()
if err != nil {
return results, err
}
req, err := http.NewRequest("POST", c.Url, body)
req.Header.Set("Content-Type", writer.FormDataContentType())
client := &http.Client{}
response, err := client.Do(req)
if err != nil {
log.Fatal(err)
} else {
body := &bytes.Buffer{}
_, err := body.ReadFrom(response.Body)
if err != nil {
log.Fatal(err)
}
response.Body.Close()
err = json.Unmarshal(body.Bytes(), &results)
if err != nil {
return results, err
}
}
return results, nil
}
func (ocr OCRText) JustText() string {
text := ""
if ocr.IsErroredOnProcessing {
for _, page := range ocr.ErrorMessage {
text += page
}
} else {
for _, page := range ocr.ParsedResults {
text += page.ParsedText
}
}
return text
}