-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathrecognize.py
1258 lines (1065 loc) · 47.8 KB
/
recognize.py
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#! /usr/bin/python
# encoding: utf-8
'''Primary routines that manage OCR recognition'''
from PIL import Image
from bisect import bisect, bisect_left
import cPickle as pickle
from classify import load_cls
import codecs
from config_manager import Config
from cv2 import drawContours
import cv2 as cv
import datetime
from fast_utils import fadd_padding, ftrim
from feature_extraction import normalize_and_extract_features
from line_breaker import LineCut, LineCluster
import logging
import numpy as np
import os
from page_elements2 import PageElements as PE2
from random import choice
from root_based_finder import is_non_std, word_parts
from segment import Segmenter, combine_many_boxes
import shelve
import signal
import simplejson as json
from sklearn.externals import joblib
import sys
from termset import syllables
from tparser import parse_syllables
from utils import local_file
from viterbi_cython import viterbi_cython
# from viterbi_search import viterbi_search, word_bigram
import warnings
cls = load_cls('logistic-cls')
## Ignore warnings. THis is mostlu in response to incessant sklearn
## warnings about passing in 1d arrays
warnings.filterwarnings("ignore")
print 'ignoring all warnings'
###
rbfcls = load_cls('rbf-cls')
predict_log_proba = cls.predict_log_proba
predict_proba = cls.predict_proba
# Trained characters are labeled by number. Open the shelve that contains
# the mappings between the Unicode character and its number label.
allchars = shelve.open(local_file('allchars_dict2'))
char_to_dig = allchars['allchars']
dig_to_char = allchars['label_chars']
allchars.close()
## Uncomment the line below when enabling viterbi_hidden_tsek
gram3 = pickle.load(open(local_file('3gram_stack_dict.pkl'),'rb'))
word_parts = set(word_parts)
PCA_TRANS = False
trs_prob = np.load(open(local_file('stack_bigram_mat.npz')))
trs_prob = trs_prob[trs_prob.files[0]]
cdmap = pickle.load(open(local_file('extended_char_dig.pkl')))
# HMM data structures
trans_p = np.load(open(local_file('stack_bigram_logprob32.npz')))
trans_p = trans_p[trans_p.files[0]].transpose()
start_p = np.load(open(local_file('stack_start_logprob32.npz')))
start_p = start_p[start_p.files[0]]
start_p_nonlog = np.exp(start_p)
## Uncomment below for syllable bigram
syllable_bigram = pickle.load(open(local_file('syllable_bigram.pkl'), 'rb')) #THIS ONE
def get_trans_prob(stack1, stack2):
try:
return trs_prob[cdmap[stack1], cdmap[stack2]]
except KeyError:
print 'Warning: Transition matrix char-dig map has not been updated with new chars'
return .25
#############################################
### Post-processing functions ###
#############################################
def viterbi(states, start_p, trans_p, emit_prob):
'''A basic viterbi decoder implementation
states: a vector or list of states 0 to n
start_p: a matrix or vector of start probabilities
trans_p: a matrix of transition probabilities
emit_prob: an nxT matrix of per-class output probabilities
where n is the number of states and t is the number
of transitions
'''
V = [{}]
path = {}
for y in states:
V[0][y] = start_p[y] * emit_prob[0][y]
path[y] = [y]
# Run Viterbi for t > 0
for t in range(1,len(emit_prob)):
V.append({})
newpath = {}
for y in states:
(prob, state) = max([(V[t-1][y0] * trans_p[y0][y] * emit_prob[t][y], y0) for y0 in states])
V[t][y] = prob
newpath[y] = path[state] + [y]
path = newpath
(prob, state) = max([(V[len(emit_prob) - 1][y], y) for y in states])
return ''.join(dig_to_char[s] for s in path[state])
def viterbi_hidden_tsek(states, start_p, trans_p, emit_prob):
'''Given a series of recognized characters, infer
likely positions of missing punctuation
Parameters
--------
states: the possible classes that can be assigned to (integer codes of stacks)
start_p: pre-computed starting probabilities of Tibetan syllables
trans_p: pre-computed transition probabilities between Tibetan stacks
emit_prob: matrix of per-class probability for t steps
Returns:
List of possible string candidates with tsek inserted
'''
V = [{}]
path = {}
tsek_dig = char_to_dig[u'་']
# Initialize base cases (t == 0)
for y in states:
V[0][y] = start_p[y] * emit_prob[0][y]
path[y] = [y]
num_obs = len(emit_prob)
# Run Viterbi for t > 0
for t in range(1,num_obs*2-1):
V.append({})
newpath = {}
if t % 2 == 1:
prob_states = []
for y0 in states:
im_path = path.get(y0)
if not im_path:
continue
if len(im_path) > 1:
run_without_tsek = 0
for i in im_path[::-1]:
if i != tsek_dig:
run_without_tsek += 1
else:
break
pr3 = gram3.get(path[y0][-2], {}).get(path[y0][-1],{}).get(tsek_dig,.5)*(1+run_without_tsek*2)
else:
pr3 = .75
try:
prob_states.append((V[t-1][y0]*trans_p[y0][tsek_dig]*pr3, y0))
except:
print '-'*20
print trans_p[y0]
print V[t-1]
print '-'*20
raise
prob, state = max(prob_states)
V[t][tsek_dig] = prob
newpath[tsek_dig] = path[state] + [tsek_dig]
path.update(newpath)
else:
srted = np.argsort(emit_prob[t/2])
for y in srted[-50:]:
#### normal
# prob, state = max([(V[t-2][y0]*trans_p[y0][y]*emit_prob[t/2][y], y0) for y0 in states])
####
#### Experimental
prob_states = []
for y0 in states:
im_path = path.get(y0,[])[-4:] # immediate n-2 in path
t_m2 = V[t-2].get(y0)
if not im_path or not t_m2:
continue
prob_states.append((V[t-2][y0]*trans_p[y0][y]*emit_prob[t/2][y], y0))
if not prob_states:
continue
prob, state = max(prob_states)
tsek_prob, tsek_dig = (V[t-1][tsek_dig]*trans_p[tsek_dig][y]*emit_prob[t/2][y], tsek_dig)
if tsek_prob > prob:
prob = tsek_prob
state = tsek_dig
V[t][y] = prob
newpath[y] = path[state] + [y]
path = newpath
if not V[t].keys():
raise ValueError
(prob, state) = max([(V[t][y], y) for y in V[t].keys()])
(prob, state) = max([(V[len(V)-1][y], y) for y in V[len(V)-1].keys()])
str_perms = _get_tsek_permutations(''.join(dig_to_char[s] for s in path[state]))
return str_perms
def _get_tsek_permutations(tsr):
tsek_count = tsr.count(u'་')
syls = parse_syllables(tsr, omit_tsek=False)
all_candidates = []
if tsek_count > 8:
print 'too many permutations'
return [tsr]
elif tsek_count == 0:
print 'no tsek'
return [tsr]
else:
ops = [['0','1'] for i in range(tsek_count)]
allops = iter(_enumrate_full_paths(ops))
for op in allops:
nstr = []
op = list(op[::-1])
for i in syls:
if i == u'་' :
cur_op = op.pop()
if cur_op == '0':
continue
else:
nstr.append(i)
else:
nstr.append(i)
nstr = ''.join(nstr)
new_parse = parse_syllables(nstr)
for p in new_parse:
if is_non_std(p) and p not in syllables:
print nstr, 'rejected'
break
else:
print nstr, 'accepted'
all_candidates.append(nstr)
if len(all_candidates) == 0:
all_candidates = [tsr]
return all_candidates
def hmm_recognize(segmentation):
'''Only used in speical case where doing tsek-insertion post-process
Parameters:
__________
segmentioatn: a segmentation object
Returns
_______
A tuple (prob, string) corresponding the probability of a
segmented and recognized string, and its probability
'''
nstates = trs_prob.shape[0]
states = range(start_p.shape[0])
obs = []
bxs = []
for num, line in enumerate(segmentation.vectors):
line_boxes = segmentation.new_boxes[num]
for obn, ob in enumerate(line):
if not isinstance(ob, unicode):
obs.append(ob.flatten())
bxs.append(line_boxes[obn])
else:
print ob,
print 'hmm omitting unicode part'
if bxs:
outbox = list(combine_many_boxes(bxs))
else:
print 'RETURNED NONE'
return (0, '')
emit_p = cls.predict_proba(obs)
results = []
syllable = []
for em in emit_p:
char = dig_to_char[np.argmax(em)]
if char in (u'་', u'།'):
if syllable:
prob, res = viterbi_hidden_tsek(states, start_p, trs_prob, syllable)
results.append(res)
results.append(char)
syllable = []
else:
syllable.append(em)
if syllable:
prob, hmm_out = viterbi_hidden_tsek(states, start_p, trs_prob, syllable)
results.append(hmm_out)
else:
prob = 0
hmm_out = ''
results = ''.join(results)
print results, '<---RESULT'
return (prob, results)
def _enumrate_full_paths(tree):
if len(tree) == 1:
return tree[0]
combs = []
frow = tree[-1]
srow = tree[-2]
for s in srow:
for f in frow:
combs.append(s+f)
tree.pop()
tree.pop()
tree.append(combs)
return _enumrate_full_paths(tree)
def bigram_prob(syl_list):
return np.prod([syllable_bigram.get(syl_list[i], {}).get(syl_list[i+1], 1e-5) \
for i in range(len(syl_list) -1 )])
def max_syllable_bigram(choices):
best_prob = 0.0
best_s = ''
for s in choices:
print s, 'is a choice'
if not isinstance(s, list):
s = parse_syllables(s)
prob = bigram_prob(s)
if prob > best_prob:
best_prob = prob
best_s = s
best_s = u'་'.join(best_s)
return best_prob, best_s
def hmm_recognize_bigram(segmentation):
states = range(start_p.shape[0])
obs = []
bxs = []
for num, line in enumerate(segmentation.vectors):
line_boxes = segmentation.new_boxes[num]
for obn, ob in enumerate(line):
if hasattr(ob, 'flatten'):
obs.append(ob.flatten())
bxs.append(line_boxes[obn])
else:
print ob,
print 'hmm omitting unicode part'
if not obs:
return (0, '')
emit_p = cls.predict_proba(obs)
results = []
syllable = []
for em in emit_p:
char = dig_to_char[np.argmax(em)]
if char in (u'་', u'།'):
if syllable:
res = viterbi_hidden_tsek(states, start_p_nonlog, trs_prob, syllable)
results.append(res)
results.append(char)
syllable = []
else:
syllable.append(em)
if syllable:
hmm_out = viterbi_hidden_tsek(states, start_p_nonlog, trs_prob, syllable)
results.append(hmm_out)
else:
prob = 0
hmm_out = ''
all_paths = _enumrate_full_paths(results)
prob, results = max_syllable_bigram(all_paths)
print results, 'RESULTS'
return (prob, results)
#############################################
### Recognizers
#############################################
def recognize_chars(segmentation, tsek_insert_method='baseline', ):
'''Recognize characters using segmented char data
Parameters:
--------------------
segmentation: an instance of PechaCharSegmenter or Segmenter
Returns:
--------------
results: Unicode string containing recognized text'''
results = []
tsek_mean = segmentation.final_box_info.tsek_mean
width_dists = {}
for l, vectors in enumerate(segmentation.vectors):
if not vectors:
print 'no vectors...'
continue
tmp_result = []
new_boxes = segmentation.new_boxes[l]
small_chars = segmentation.line_info.small_cc_lines_chars[l]
#FIXME: define emph lines for line cut
#### Line Cut has no emph_lines object so need to work around for now...
emph_markers = getattr(segmentation.line_info, 'emph_lines', [])
if emph_markers:
emph_markers = emph_markers[l]
img_arr = segmentation.line_info.shapes.img_arr
left_edges = [b[0] for b in new_boxes]
tsek_widths = []
for s in small_chars[::-1]: # consider small char from end of line going backward. backward useful for misplaced tsek often and maybe for TOC though should check
# for s in small_chars: # consider small char from end of line going backward. backward useful for misplaced tsek often and maybe for TOC though should check
cnt = segmentation.line_info.shapes.contours[s]
bx = segmentation.line_info.shapes.get_boxes()[s]
bx = list(bx)
x,y,w,h = bx
char_arr = np.ones((h,w), dtype=np.uint8)
offset = (-x, -y)
drawContours(char_arr, [cnt], -1,0, thickness = -1, offset=offset)
feature_vect = normalize_and_extract_features(char_arr)
prd = classify(feature_vect)
insertion_pos = bisect(left_edges, x)
left_items = 6
right_items = 5
if insertion_pos >= len(new_boxes):
# insertion is at or near end of line and needs more left
# neighbors to compensate for there being less chars to define the baseline
left_items = 12
elif insertion_pos <= len(new_boxes):
# same as above except at front of line
right_items = 12
if tsek_insert_method == 'baseline':
top = 1000000 # arbitrary high number
bottom = 0
#### Get min or max index to avoid reaching beyond edges of the line
lower = max(insertion_pos - left_items, 0)
upper = min(len(new_boxes)-1, insertion_pos+right_items)
####
left = new_boxes[lower][0]
right = new_boxes[upper][0] + new_boxes[upper][2]
if insertion_pos < len(new_boxes):
mid = new_boxes[insertion_pos][0] + new_boxes[insertion_pos][2]
else:
mid = right
for j in new_boxes[lower:upper]:
if j[1] < top:
top = j[1]
if j[1] + j[3] > bottom:
bottom = j[1] + j[3]
local_span = bottom - top
if prd == u'་' and local_span > 0:
left_sum = img_arr[top:bottom,left:mid].sum(axis=1)
right_sum = img_arr[top:bottom,mid:right].sum(axis=1)
local_baseline_left = top + left_sum.argmin()
if mid != right:
local_baseline_right = top + right_sum.argmin()
else:
local_baseline_right = local_baseline_left
if ((local_baseline_left >= bx[1] and local_baseline_left <= bx[1] + bx[3]) or
(local_baseline_right >= bx[1] and local_baseline_right <= bx[1] + bx[3])): #or
# (entire_local_baseline >= bx[1] and entire_local_baseline <= bx[1] + bx[3])):
### Account for fact that the placement of a tsek could be
# before or after its indicated insertion pos
### experimental.. only need with certain fonts e.g. "book 6"
## in samples
if insertion_pos <= len(new_boxes):
# cur_box_in_pos = new_boxes[insertion_pos]
prev_box = new_boxes[insertion_pos-1]
# left_cur = cur_box_in_pos[0]
left_prev = prev_box[0]
if 0 <= x - left_prev < w and 2*w < prev_box[2]:
insertion_pos -= 1
vectors.insert(insertion_pos, prd)
new_boxes.insert(insertion_pos, bx)
left_edges.insert(insertion_pos, bx[0])
tsek_widths.append(bx[2])
elif bx[1] >= top -.25*local_span and bx[1] + bx[3] <= bottom + local_span*.25:
vectors.insert(insertion_pos, prd)
new_boxes.insert(insertion_pos, bx)
left_edges.insert(insertion_pos, bx[0])
else:
vectors.insert(insertion_pos, prd)
new_boxes.insert(insertion_pos, bx)
left_edges.insert(insertion_pos, bx[0])
tsek_mean = np.mean(tsek_widths)
for em in emph_markers:
marker = dig_to_char[segmentation.line_info.shapes.cached_pred_prob[em][0]]
marker_prob = segmentation.line_info.shapes.cached_pred_prob[em][1]
bx = segmentation.line_info.shapes.get_boxes()[em]
bx = list(bx)
x,y,w,h = bx
insertion_pos = bisect(left_edges, x)
bx.append(marker_prob)
bx.append(marker)
vectors.insert(insertion_pos, marker)
new_boxes.insert(insertion_pos, bx)
left_edges.insert(insertion_pos, bx[0])
# tsek_std = np.std(tsek_widths)
if len(vectors) == 1: i = -1
for i, v in enumerate(vectors[:-1]):
if new_boxes[i+1][0] - (new_boxes[i][0] + new_boxes[i][2]) >= 2*tsek_mean:
if not isinstance(v, unicode):
prd = classify(v, pca_trans=PCA_TRANS, multi=False)
else:
prd = v
new_boxes[i].append(prd)
tmp_result.append(new_boxes[i])
tmp_result.append([-1,-1,-1,-1, u' '])
else:
if not isinstance(v, unicode):
prd = classify(v, pca_trans=PCA_TRANS, multi=False)
### Assume that a tsek shouldn't show up at this point
### a more reliable way to do this is to better
# if prd == u'་':
# prbs = cls.predict_proba(v)[0]
# ind_probs = zip(range(len(prbs)), prbs)
# ind_probs.sort(key=lambda x: x[1])
# prd = dig_to_char[ind_probs[-2][0]]
else:
prd = v
if not width_dists.get(prd):
width_dists[prd] = [new_boxes[i][2]]
else:
width_dists[prd].append(new_boxes[i][2])
new_boxes[i].append(prd)
tmp_result.append(new_boxes[i])
if not isinstance(vectors[-1], unicode):
prd = classify(vectors[-1], pca_trans=PCA_TRANS, multi=False)
else:
prd = vectors[-1]
new_boxes[-1].append(prd)
tmp_result.append(new_boxes[-1])
results.append(tmp_result)
return results
def recognize_chars_hmm(segmentation, tsek_insert_method='baseline', ):
'''Recognize characters using segmented char data
Parameters:
--------------------
segmentation: an instance of PechaCharSegmenter or Segmenter
Returns:
--------------
results: list of lists containing [x,y,width, height, prob, unicode], specifying the
coordinates of the bounding box of stack, it probability, and its unicode
characters -- on each line of the page
'''
n_states = trans_p.shape[0]
results = []
tsek_mean = segmentation.final_box_info.tsek_mean
cached_features = segmentation.line_info.shapes.cached_features
cached_pred_prob = segmentation.line_info.shapes.cached_pred_prob
# width_dists = {}
# times = []
for l, vectors in enumerate(segmentation.vectors):
if not vectors:
print 'no vectors...'
continue
tmp_result = []
new_boxes = segmentation.new_boxes[l]
small_chars = segmentation.line_info.small_cc_lines_chars[l]
#FIXME: define emph lines for line cut
#### Line Cut has no emph_lines object so need to work around for now...
emph_markers = getattr(segmentation.line_info, 'emph_lines', [])
if emph_markers:
emph_markers = emph_markers[l]
img_arr = segmentation.line_info.shapes.img_arr
left_edges = [b[0] for b in new_boxes]
tsek_widths = []
for s in small_chars[::-1]: # consider small char from end of line going backward. backward useful for misplaced tsek often and maybe for TOC though should check
bx = segmentation.line_info.shapes.get_boxes()[s]
bx = list(bx)
x,y,w,h = bx
try:
feature_vect = cached_features[s]
inx, probs = cached_pred_prob[s]
prob = probs[inx]
prd = dig_to_char[inx]
# else:
# vect = normalize_and_extract_features(letter)
except:
cnt = segmentation.line_info.shapes.contours[s]
char_arr = np.ones((h,w), dtype=np.uint8)
offset = (-x, -y)
drawContours(char_arr, [cnt], -1,0, thickness = -1, offset=offset)
feature_vect = normalize_and_extract_features(char_arr)
# prd = classify(feature_vect)
prd, prob = prd_prob(feature_vect)
# print prd, max(cls.predict_proba(feature_vect)[0])
insertion_pos = bisect(left_edges, x)
left_items = 6
right_items = 5
if insertion_pos >= len(new_boxes):
left_items = 12
elif insertion_pos <= len(new_boxes):
# same as above except at front of line
right_items = 12
if tsek_insert_method == 'baseline':
top = 1000000 # arbitrary high number
bottom = 0
#### Get min or max index to avoid reaching beyond edges of the line
lower = max(insertion_pos - left_items, 0)
upper = min(len(new_boxes)-1, insertion_pos+right_items)
####
left = new_boxes[lower][0]
right = new_boxes[upper][0] + new_boxes[upper][2]
if insertion_pos < len(new_boxes):
mid = new_boxes[insertion_pos][0] + new_boxes[insertion_pos][2]
else:
mid = right
for j in new_boxes[lower:upper]:
if j[1] < top:
top = j[1]
try:
if j[1] + j[3] > bottom:
bottom = j[1] + j[3]
except IndexError:
print new_boxes[lower:upper]
print j
raise
local_span = bottom - top
left_sum = img_arr[top:bottom,left:mid].sum(axis=1)
right_sum = img_arr[top:bottom,mid:right].sum(axis=1)
try:
local_baseline_left = top + left_sum.argmin()
except:
local_baseline_left = top
if mid != right:
local_baseline_right = top + right_sum.argmin()
else:
local_baseline_right = local_baseline_left
if prd == u'་' and local_span > 0:
if ((local_baseline_left >= bx[1] and local_baseline_left <= bx[1] + bx[3]) or
(local_baseline_right >= bx[1] and local_baseline_right <= bx[1] + bx[3])) or (insertion_pos == len(vectors)): #or
if insertion_pos <= len(new_boxes):
prev_box = new_boxes[insertion_pos-1]
left_prev = prev_box[0]
if 0 <= x - left_prev < w and 2*w < prev_box[2]:
insertion_pos -= 1
new_boxes.insert(insertion_pos, bx)
bx.append(prob)
bx.append(prd)
vectors.insert(insertion_pos, bx)
left_edges.insert(insertion_pos, bx[0])
tsek_widths.append(bx[2])
elif ((bx[1] >= top -.25*local_span and bx[1] + bx[3] <=
bottom + local_span*.25) or
(insertion_pos == len(vectors))) and bx[1] - local_baseline_left < 2*tsek_mean:
vectors.insert(insertion_pos, prd)
new_boxes.insert(insertion_pos, bx)
new_boxes[insertion_pos].append(prob)
new_boxes[insertion_pos].append(prd)
left_edges.insert(insertion_pos, bx[0])
else:
print 'small contour reject at', l, s, 'local height span', local_span, 'box height', bx[3]
else:
vectors.insert(insertion_pos, prd)
new_boxes.insert(insertion_pos, bx)
new_boxes[insertion_pos].append(prob)
new_boxes[insertion_pos].append(prd)
left_edges.insert(insertion_pos, bx[0])
for em in emph_markers:
mkinx = segmentation.line_info.shapes.cached_pred_prob[em][0]
marker = dig_to_char[mkinx]
marker_prob = segmentation.line_info.shapes.cached_pred_prob[em][1][mkinx]
bx = segmentation.line_info.shapes.get_boxes()[em]
bx = list(bx)
x,y,w,h = bx
insertion_pos = bisect(left_edges, x)
vectors.insert(insertion_pos, marker)
bx.append(marker_prob)
bx.append(marker)
new_boxes.insert(insertion_pos, bx)
left_edges.insert(insertion_pos, bx[0])
if len(vectors) == 1: i = -1
skip_next_n = 0
###HMM PHASE
allstrs = []
curstr = []
allinx = []
curinx = []
for j, v in enumerate(vectors):
islist = isinstance(v, list)
if isinstance(v, unicode) or islist:
allstrs.append(curstr)
allinx.append(curinx)
curstr = []
curinx = []
else:
curstr.append(v)
curinx.append(j)
if curstr:
allstrs.append(curstr)
allinx.append(curinx)
for f, group in enumerate(allstrs):
if not group: continue
try:
probs = predict_log_proba(group)
except:
print v,
# raise
LPROB = len(probs)
if LPROB == 1:
inx = probs[0].argmax()
prb = probs[0][inx]
prds = [inx]
else:
probs = probs.astype(np.float32)
prb, prds = viterbi_cython(LPROB, n_states, start_p, trans_p, probs)
prb = np.exp(prb)
inx = allinx[f]
for vv, c in enumerate(range(len(prds))):
ind = inx[c]
cprob = probs[c].max()
#######replace low prob stacks using svm rbf classifier
####### warning: this may undo decisions made by hmm classifier
# if np.exp(cprob) <= .98:
# # print prds, type(prds)
# print 'replacing', dig_to_char[prds[c]], 'with',
# prds[c] = rbfcls.predict(group[vv])[0]
# # print prds, type(prds)
# # print prds[c]
# print dig_to_char[prds[c]]
# print
#######################
new_boxes[ind].append(np.exp(cprob))
try:
new_boxes[ind].append(dig_to_char[prds[c]])
except KeyError:
new_boxes[ind].append('PROB')
for ind, b in enumerate(new_boxes):
tmp_result.append(new_boxes[ind])
if not len(new_boxes[ind]) == 6:
print l, ind, new_boxes[ind], '<-----'
if ind + 1 < len(new_boxes) and new_boxes[ind+1][0] - (new_boxes[ind][0] + new_boxes[ind][2]) >= 1.5*tsek_mean:
tmp_result.append([-1,-1,-1,-1, 1.0, u' '])
results.append(tmp_result)
return results
def recognize_chars_probout(segmentation, tsek_insert_method='baseline', ):
'''Recognize characters using segmented char data
Parameters:
--------------------
segmentation: an instance of PechaCharSegmenter or Segmenter
Returns:
--------------
results: list of lists containing [x,y,width, height, prob, unicode], specifying the
coordinates of the bounding box of stack, it probability, and its unicode
characters -- on each line of the page'''
results = []
tsek_mean = segmentation.final_box_info.tsek_mean
cached_features = segmentation.line_info.shapes.cached_features
cached_pred_prob = segmentation.line_info.shapes.cached_pred_prob
for l, vectors in enumerate(segmentation.vectors):
if not vectors:
print 'no vectors...'
continue
tmp_result = []
new_boxes = segmentation.new_boxes[l]
scale_w = segmentation.final_box_info.transitions[l]
small_chars = segmentation.line_info.small_cc_lines_chars[l]
#FIXME: define emph lines for line cut
#### Line Cut has no emph_lines object so need to work around for now...
emph_markers = getattr(segmentation.line_info, 'emph_lines', [])
if emph_markers:
emph_markers = emph_markers[l]
img_arr = segmentation.line_info.shapes.img_arr
left_edges = [b[0] for b in new_boxes]
tsek_widths = []
for s in small_chars[::-1]: # consider small char from end of line going backward. backward useful for misplaced tsek often and maybe for TOC though should check
bx = segmentation.line_info.shapes.get_boxes()[s]
bx = list(bx)
x,y,w,h = bx
try:
feature_vect = cached_features[s]
inx, probs = cached_pred_prob[s]
prob = probs[inx]
prd = dig_to_char[inx]
except:
cnt = segmentation.line_info.shapes.contours[s]
char_arr = np.ones((h,w), dtype=np.uint8)
offset = (-x, -y)
drawContours(char_arr, [cnt], -1,0, thickness = -1, offset=offset)
feature_vect = normalize_and_extract_features(char_arr)
prd, prob = prd_prob(feature_vect)
insertion_pos = bisect(left_edges, x)
left_items = 6
right_items = 5
if insertion_pos >= len(new_boxes):
# insertion is at or near end of line and needs more left
# neighbors to compensate for there being less chars to define the baseline
left_items = 12
elif insertion_pos <= len(new_boxes):
# same as above except at front of line
right_items = 12
# right_items = 5 # bias slightly toward the left.
if tsek_insert_method == 'baseline':
top = 1000000 # arbitrary high number
bottom = 0
#### Get min or max index to avoid reaching beyond edges of the line
lower = max(insertion_pos - left_items, 0)
upper = min(len(new_boxes)-1, insertion_pos+right_items)
left = new_boxes[lower][0]
right = new_boxes[upper][0] + new_boxes[upper][2]
if insertion_pos < len(new_boxes):
mid = new_boxes[insertion_pos][0] + new_boxes[insertion_pos][2]
else:
mid = right
for j in new_boxes[lower:upper]:
if j[1] < top:
top = j[1]
if j[1] + j[3] > bottom:
bottom = j[1] + j[3]
local_span = bottom - top
top, bottom, left, right, mid = [int(np.round(ff)) for ff in [top, bottom, left, right, mid]]
if prd == u'་' and local_span > 0:
left_sum = img_arr[top:bottom,left:mid].sum(axis=1)
right_sum = img_arr[top:bottom,mid:right].sum(axis=1)
local_baseline_left = top + left_sum.argmin()
if mid != right:
local_baseline_right = top + right_sum.argmin()
else:
local_baseline_right = local_baseline_left
if ((local_baseline_left >= bx[1] and local_baseline_left <= bx[1] + bx[3]) or
(local_baseline_right >= bx[1] and local_baseline_right <= bx[1] + bx[3])) or (insertion_pos == len(vectors)): #or
# (entire_local_baseline >= bx[1] and entire_local_baseline <= bx[1] + bx[3])):
### Account for fact that the placement of a tsek could be
# before or after its indicated insertion pos
### experimental.. only need with certain fonts e.g. "book 6"
## in samples
if insertion_pos <= len(new_boxes):
prev_box = new_boxes[insertion_pos-1]
left_prev = prev_box[0]
if 0 <= x - left_prev < w and 2*w < prev_box[2]:
insertion_pos -= 1
vectors.insert(insertion_pos, prd)
new_boxes.insert(insertion_pos, bx)
new_boxes[insertion_pos].append(prob)
new_boxes[insertion_pos].append(prd)
left_edges.insert(insertion_pos, bx[0])
tsek_widths.append(bx[2])
elif (bx[1] >= top -.25*local_span and bx[1] + bx[3] <= bottom + local_span*.25) or (insertion_pos == len(vectors)):
vectors.insert(insertion_pos, prd)
new_boxes.insert(insertion_pos, bx)
new_boxes[insertion_pos].append(prob)
new_boxes[insertion_pos].append(prd)
left_edges.insert(insertion_pos, bx[0])
else:
vectors.insert(insertion_pos, prd)
new_boxes.insert(insertion_pos, bx)
new_boxes[insertion_pos].append(prob)
new_boxes[insertion_pos].append(prd)
left_edges.insert(insertion_pos, bx[0])
for em in emph_markers:
bx = segmentation.line_info.shapes.get_boxes()[em]
mkinx = segmentation.line_info.shapes.cached_pred_prob[em][0]
marker = dig_to_char[mkinx]
marker_prob = segmentation.line_info.shapes.cached_pred_prob[em][1][mkinx]
bx = list(bx)
x,y,w,h = bx
bx.append(marker_prob)
bx.append(marker)
insertion_pos = bisect(left_edges, x)
vectors.insert(insertion_pos, marker)
new_boxes.insert(insertion_pos, bx)
left_edges.insert(insertion_pos, bx[0])
if len(vectors) == 1: i = -1
skip_next_n = 0
for i, v in enumerate(vectors[:-1]):
if skip_next_n:
skip_next_n -= 1
continue
if new_boxes[i+1][0] - (new_boxes[i][0] + new_boxes[i][2]) >= 2*tsek_mean:
if not len(new_boxes[i]) == 6 and not isinstance(v, unicode):
prd, prob = prd_prob(v)
else:
if len(new_boxes[i]) == 6:
prob, prd = new_boxes[i][4:]
else:
## v is unicode stack, likely from segmentation step
prd = v
prob = .95 # NEED ACTUAL PROB
new_boxes[i].append(prob)
new_boxes[i].append(prd)
tmp_result.append(new_boxes[i])
tmp_result.append([-1,-1,-1,-1, 1.0, u' '])
else:
if hasattr(v, 'dtype'):
try:
prd, prob = prd_prob(v)
except:
print v
new_boxes[i].append(prob)
new_boxes[i].append(prd)
else:
if len(new_boxes[i]) == 6:
prob, prd = new_boxes[i][4:]
else:
prd = v
if len(new_boxes[i]) < 6:
try: