@@ -49,10 +49,14 @@ cdef load_img(img, _clstm.Tensor2 *data):
49
49
:param img: Image
50
50
:type img: :py:class:`PIL.Image.Image`
51
51
"""
52
- data.resize(img.width, img.height)
52
+ if hasattr (img, ' width' ):
53
+ width, height = img.width, img.height
54
+ elif hasattr (img. ' size' ):
55
+ width, height = img.size
56
+ data.resize(width, height)
53
57
imgdata = img.load()
54
- for i in range (img. width):
55
- for j in range (img. height):
58
+ for i in range (width):
59
+ for j in range (height):
56
60
px = imgdata[i, j]
57
61
# Pillow returns pixels as [0, 255], but we need [0, 1]
58
62
if isinstance (px, tuple ):
@@ -182,10 +186,10 @@ cdef class ClstmOcr:
182
186
:rtype: unicode
183
187
"""
184
188
cdef _clstm.Tensor2 data
185
- if hasattr (img, ' width' ):
186
- load_img(img, & data)
187
- elif hasattr (img, ' shape' ):
189
+ if hasattr (img, ' shape' ):
188
190
load_nparray(img, & data)
191
+ else :
192
+ load_img(img, & data)
189
193
return self ._ocr.train_utf8(
190
194
data.map(), text.encode(' utf8' )).decode(' utf8' )
191
195
@@ -198,10 +202,10 @@ cdef class ClstmOcr:
198
202
:rtype: unicode
199
203
"""
200
204
cdef _clstm.Tensor2 data
201
- if hasattr (img, ' width' ):
202
- load_img(img, & data)
203
- elif hasattr (img, ' shape' ):
205
+ if hasattr (img, ' shape' ):
204
206
load_nparray(img, & data)
207
+ else :
208
+ load_img(img, & data)
205
209
return self ._ocr.predict_utf8(data.map()).decode(' utf8' )
206
210
207
211
def recognize_chars (self , img ):
@@ -218,10 +222,10 @@ cdef class ClstmOcr:
218
222
cdef vector[_clstm.CharPrediction] preds
219
223
cdef vector[_clstm.CharPrediction].iterator pred_it
220
224
cdef wchar_t[2 ] cur_char
221
- if hasattr (img, ' width' ):
222
- load_img(img, & data)
223
- elif hasattr (img, ' shape' ):
225
+ if hasattr (img, ' shape' ):
224
226
load_nparray(img, & data)
227
+ else :
228
+ load_img(img, & data)
225
229
self ._ocr.predict(preds, data.map())
226
230
for i in range (preds.size()):
227
231
cur_char[0 ] = preds[i].c
0 commit comments