@@ -69,87 +69,87 @@ func NewClient() *Client {
69
69
}
70
70
71
71
// Close frees allocated API. This MUST be called for ANY client constructed by "NewClient" function.
72
- func (c * Client ) Close () (err error ) {
72
+ func (client * Client ) Close () (err error ) {
73
73
// defer func() {
74
74
// if e := recover(); e != nil {
75
75
// err = fmt.Errorf("%v", e)
76
76
// }
77
77
// }()
78
- C .Free (c .api )
78
+ C .Free (client .api )
79
79
return err
80
80
}
81
81
82
82
// SetImage sets path to image file to be processed OCR.
83
- func (c * Client ) SetImage (imagepath string ) * Client {
84
- c .ImagePath = imagepath
85
- return c
83
+ func (client * Client ) SetImage (imagepath string ) * Client {
84
+ client .ImagePath = imagepath
85
+ return client
86
86
}
87
87
88
88
// SetLanguage sets languages to use. English as default.
89
- func (c * Client ) SetLanguage (langs ... string ) * Client {
90
- c .Languages = langs
91
- return c
89
+ func (client * Client ) SetLanguage (langs ... string ) * Client {
90
+ client .Languages = langs
91
+ return client
92
92
}
93
93
94
94
// SetWhitelist sets whitelist chars.
95
95
// See official documentation for whitelist here https://github.com/tesseract-ocr/tesseract/wiki/ImproveQuality#dictionaries-word-lists-and-patterns
96
- func (c * Client ) SetWhitelist (whitelist string ) * Client {
97
- return c .SetVariable ("tessedit_char_whitelist" , whitelist )
96
+ func (client * Client ) SetWhitelist (whitelist string ) * Client {
97
+ return client .SetVariable ("tessedit_char_whitelist" , whitelist )
98
98
}
99
99
100
100
// SetVariable sets parameters, representing tesseract::TessBaseAPI->SetVariable.
101
101
// See official documentation here https://zdenop.github.io/tesseract-doc/classtesseract_1_1_tess_base_a_p_i.html#a2e09259c558c6d8e0f7e523cbaf5adf5
102
- func (c * Client ) SetVariable (key , value string ) * Client {
103
- c .Variables [key ] = value
104
- return c
102
+ func (client * Client ) SetVariable (key , value string ) * Client {
103
+ client .Variables [key ] = value
104
+ return client
105
105
}
106
106
107
107
// SetPageSegMode sets "Page Segmentation Mode" (PSM) to detect layout of characters.
108
108
// See official documentation for PSM here https://github.com/tesseract-ocr/tesseract/wiki/ImproveQuality#page-segmentation-method
109
- func (c * Client ) SetPageSegMode (mode PageSegMode ) * Client {
110
- c .PageSegMode = & mode
111
- return c
109
+ func (client * Client ) SetPageSegMode (mode PageSegMode ) * Client {
110
+ client .PageSegMode = & mode
111
+ return client
112
112
}
113
113
114
114
// SetConfigFile sets the file path to config file.
115
- func (c * Client ) SetConfigFile (fpath string ) error {
115
+ func (client * Client ) SetConfigFile (fpath string ) error {
116
116
info , err := os .Stat (fpath )
117
117
if err != nil {
118
118
return err
119
119
}
120
120
if info .IsDir () {
121
121
return fmt .Errorf ("the specified config file path seems to be a directory" )
122
122
}
123
- c .ConfigFilePath = fpath
123
+ client .ConfigFilePath = fpath
124
124
return nil
125
125
}
126
126
127
127
// It's due to the caller to free this char pointer.
128
- func (c * Client ) charLangs () * C.char {
128
+ func (client * Client ) charLangs () * C.char {
129
129
var langs * C.char
130
- if len (c .Languages ) != 0 {
131
- langs = C .CString (strings .Join (c .Languages , "+" ))
130
+ if len (client .Languages ) != 0 {
131
+ langs = C .CString (strings .Join (client .Languages , "+" ))
132
132
}
133
133
return langs
134
134
}
135
135
136
136
// It's due to the caller to free this char pointer.
137
- func (c * Client ) charConfig () * C.char {
137
+ func (client * Client ) charConfig () * C.char {
138
138
var config * C.char
139
- if _ , err := os .Stat (c .ConfigFilePath ); err == nil {
140
- config = C .CString (c .ConfigFilePath )
139
+ if _ , err := os .Stat (client .ConfigFilePath ); err == nil {
140
+ config = C .CString (client .ConfigFilePath )
141
141
}
142
142
return config
143
143
}
144
144
145
145
// Initialize tesseract::TessBaseAPI
146
146
// TODO: add tessdata prefix
147
- func (c * Client ) init () error {
148
- langs := c .charLangs ()
147
+ func (client * Client ) init () error {
148
+ langs := client .charLangs ()
149
149
defer C .free (unsafe .Pointer (langs ))
150
- config := c .charConfig ()
150
+ config := client .charConfig ()
151
151
defer C .free (unsafe .Pointer (config ))
152
- res := C .Init (c .api , nil , langs , config )
152
+ res := C .Init (client .api , nil , langs , config )
153
153
if res != 0 {
154
154
// TODO: capture and vacuum stderr from Cgo
155
155
return fmt .Errorf ("failed to initialize TessBaseAPI with code %d" , res )
@@ -159,59 +159,59 @@ func (c *Client) init() error {
159
159
160
160
// Prepare tesseract::TessBaseAPI options,
161
161
// must be called after `init`.
162
- func (c * Client ) prepare () error {
162
+ func (client * Client ) prepare () error {
163
163
// Set Image by giving path
164
- imagepath := C .CString (c .ImagePath )
164
+ imagepath := C .CString (client .ImagePath )
165
165
defer C .free (unsafe .Pointer (imagepath ))
166
- C .SetImage (c .api , imagepath )
166
+ C .SetImage (client .api , imagepath )
167
167
168
- for key , value := range c .Variables {
169
- if ok := c .bind (key , value ); ! ok {
168
+ for key , value := range client .Variables {
169
+ if ok := client .bind (key , value ); ! ok {
170
170
return fmt .Errorf ("failed to set variable with key(%s):value(%s)" , key , value )
171
171
}
172
172
}
173
173
174
- if c .PageSegMode != nil {
175
- mode := C .int (* c .PageSegMode )
176
- C .SetPageSegMode (c .api , mode )
174
+ if client .PageSegMode != nil {
175
+ mode := C .int (* client .PageSegMode )
176
+ C .SetPageSegMode (client .api , mode )
177
177
}
178
178
return nil
179
179
}
180
180
181
181
// Binds variable to API object.
182
182
// Must be called from inside `prepare`.
183
- func (c * Client ) bind (key , value string ) bool {
183
+ func (client * Client ) bind (key , value string ) bool {
184
184
k , v := C .CString (key ), C .CString (value )
185
185
defer C .free (unsafe .Pointer (k ))
186
186
defer C .free (unsafe .Pointer (v ))
187
- res := C .SetVariable (c .api , k , v )
187
+ res := C .SetVariable (client .api , k , v )
188
188
return bool (res )
189
189
}
190
190
191
191
// Text finally initialize tesseract::TessBaseAPI, execute OCR and extract text detected as string.
192
- func (c * Client ) Text () (out string , err error ) {
193
- if err = c .init (); err != nil {
192
+ func (client * Client ) Text () (out string , err error ) {
193
+ if err = client .init (); err != nil {
194
194
return
195
195
}
196
- if err = c .prepare (); err != nil {
196
+ if err = client .prepare (); err != nil {
197
197
return
198
198
}
199
- out = C .GoString (C .UTF8Text (c .api ))
200
- if c .Trim {
199
+ out = C .GoString (C .UTF8Text (client .api ))
200
+ if client .Trim {
201
201
out = strings .Trim (out , "\n " )
202
202
}
203
203
return out , err
204
204
}
205
205
206
206
// HTML finally initialize tesseract::TessBaseAPI, execute OCR and returns hOCR text.
207
207
// See https://en.wikipedia.org/wiki/HOCR for more information of hOCR.
208
- func (c * Client ) HTML () (out string , err error ) {
209
- if err = c .init (); err != nil {
208
+ func (client * Client ) HOCRText () (out string , err error ) {
209
+ if err = client .init (); err != nil {
210
210
return
211
211
}
212
- if err = c .prepare (); err != nil {
212
+ if err = client .prepare (); err != nil {
213
213
return
214
214
}
215
- out = C .GoString (C .HOCRText (c .api ))
215
+ out = C .GoString (C .HOCRText (client .api ))
216
216
return
217
217
}
0 commit comments