-
Notifications
You must be signed in to change notification settings - Fork 13
/
data.py
1152 lines (1002 loc) · 54.1 KB
/
data.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
# coding:utf-8
import glob
import csv
import cv2
import time
import os
import numpy as np
import scipy.optimize
import matplotlib.pyplot as plt
import matplotlib.patches as Patches
from shapely.geometry import Polygon
import tensorflow as tf
from data_util import GeneratorEnqueuer
tf.app.flags.DEFINE_string('training_data_path', '/home/huluwa/Etranform/ver_data/train/',
'training dataset to use')
tf.app.flags.DEFINE_integer('max_image_large_side', 1280,
'max image size of training')
tf.app.flags.DEFINE_integer('max_text_size', 800,
'if the text in the input image is bigger than this, then we resize'
'the image according to this')
tf.app.flags.DEFINE_integer('min_text_size', 0,
'if the text size is smaller than this, we ignore it during training')
tf.app.flags.DEFINE_float('min_crop_side_ratio', 0.1,
'when doing random crop from input image, the'
'min length of min(H, W')
tf.app.flags.DEFINE_string('geometry', 'RBOX',
'which geometry to generate, RBOX or QUAD')
FLAGS = tf.app.flags.FLAGS
def get_images():
files = []
for ext in ['jpg', 'png', 'jpeg', 'JPG']:
files.extend(glob.glob(
os.path.join(FLAGS.training_data_path, '*.{}'.format(ext))))
return files
def load_annoataion(p):
'''
load annotation from the text file
:param p:
:return:
'''
text_polys = []
vertex_text_polys = []
text_tags = []
vertex_text_tags = []
vertex_1_text_polys = []
vertex_1_text_tags = []
vertex_2_text_polys = []
vertex_2_text_tags = []
vertex_3_text_polys = []
vertex_3_text_tags = []
vertex_4_text_polys = []
vertex_4_text_tags = []
if not os.path.exists(p):
return np.array(text_polys, dtype=np.float32)
with open(p, 'r') as f:
reader = csv.reader(f)
for line in reader:
label = line[-1]
# strip BOM. \ufeff for python3, \xef\xbb\bf for python2
line = [i.strip('\ufeff').strip('\xef\xbb\xbf') for i in line]
if ',' in line:
line = [i.strip('\ufeff').strip('\xef\xbb\xbf') for i in line]
else:
line = [i.strip(' ').strip('\xef\xbb\xbf') for i in line]
x1, y1, x2, y2, x3, y3, x4, y4 = list(map(float, line[:8]))
text_polys.append([[x1, y1], [x2, y2], [x3, y3], [x4, y4]])
if label == '*' or label == '###':
text_tags.append(True)
else:
text_tags.append(False)
vertex_h = 100
#leftup
vertex_1_x1 = max(x1 - vertex_h/2,0)
vertex_1_y1 = max(y1 - vertex_h/2,0)
vertex_1_x2 = max(x1 + vertex_h/2,0)
vertex_1_y2 = max(y1 - vertex_h/2,0)
vertex_1_x3 = max(x1 + vertex_h/2,0)
vertex_1_y3 = max(y1 + vertex_h/2,0)
vertex_1_x4 = max(x1 - vertex_h/2,0)
vertex_1_y4 = max(y1 + vertex_h/2,0)
vertex_text_polys.append([[vertex_1_x1,vertex_1_y1], [vertex_1_x2,vertex_1_y2],[vertex_1_x3,vertex_1_y3],[vertex_1_x4,vertex_1_y4]])
vertex_1_text_polys.append([[vertex_1_x1,vertex_1_y1], [vertex_1_x2,vertex_1_y2],[vertex_1_x3,vertex_1_y3],[vertex_1_x4,vertex_1_y4]])
if label == '*' or label == '###':
vertex_text_tags.append(True)
vertex_1_text_tags.append(True)
else:
vertex_text_tags.append(False)
vertex_1_text_tags.append(False)
#rightup
vertex_2_x1 = max(x2 - vertex_h/2,0)
vertex_2_y1 = max(y2 - vertex_h/2,0)
vertex_2_x2 = x2 + vertex_h/2
vertex_2_y2 = max(y2 - vertex_h/2,0)
vertex_2_x3 = x2 + vertex_h/2
vertex_2_y3 = y2 + vertex_h/2
vertex_2_x4 = x2 - vertex_h/2
vertex_2_y4 = y2 + vertex_h/2
vertex_text_polys.append([[vertex_2_x1,vertex_2_y1], [vertex_2_x2,vertex_2_y2],[vertex_2_x3,vertex_2_y3],[vertex_2_x4,vertex_2_y4]])
vertex_2_text_polys.append([[vertex_2_x1,vertex_2_y1], [vertex_2_x2,vertex_2_y2],[vertex_2_x3,vertex_2_y3],[vertex_2_x4,vertex_2_y4]])
if label == '*' or label == '###':
vertex_text_tags.append(True)
vertex_2_text_tags.append(True)
else:
vertex_text_tags.append(False)
vertex_2_text_tags.append(False)
#rightdown
vertex_3_x1 = x3 - vertex_h/2
vertex_3_y1 = y3 - vertex_h/2
vertex_3_x2 = x3 + vertex_h/2
vertex_3_y2 = y3 - vertex_h/2
vertex_3_x3 = x3 + vertex_h/2
vertex_3_y3 = y3 + vertex_h/2
vertex_3_x4 = x3 - vertex_h/2
vertex_3_y4 = y3 + vertex_h/2
vertex_text_polys.append([[vertex_3_x1,vertex_3_y1], [vertex_3_x2,vertex_3_y2],[vertex_3_x3,vertex_3_y3],[vertex_3_x4,vertex_3_y4]])
vertex_3_text_polys.append([[vertex_3_x1,vertex_3_y1], [vertex_3_x2,vertex_3_y2],[vertex_3_x3,vertex_3_y3],[vertex_3_x4,vertex_3_y4]])
if label == '*' or label == '###':
vertex_text_tags.append(True)
vertex_3_text_tags.append(True)
else:
vertex_text_tags.append(False)
vertex_3_text_tags.append(False)
#leftdown
vertex_4_x1 = max(x4 - vertex_h/2,0)
vertex_4_y1 = y4 - vertex_h/2
vertex_4_x2 = x4 + vertex_h/2
vertex_4_y2 = y4 - vertex_h/2
vertex_4_x3 = x4 + vertex_h/2
vertex_4_y3 = y4 + vertex_h/2
vertex_4_x4 = max(x4 - vertex_h/2,0)
vertex_4_y4 = y4 + vertex_h/2
vertex_text_polys.append([[vertex_4_x1,vertex_4_y1], [vertex_4_x2,vertex_4_y2],[vertex_4_x3,vertex_4_y3],[vertex_4_x4,vertex_4_y4]])
vertex_4_text_polys.append([[vertex_4_x1,vertex_4_y1], [vertex_4_x2,vertex_4_y2],[vertex_4_x3,vertex_4_y3],[vertex_4_x4,vertex_4_y4]])
if label == '*' or label == '###':
vertex_text_tags.append(True)
vertex_4_text_tags.append(True)
else:
vertex_text_tags.append(False)
vertex_4_text_tags.append(False)
return np.array(text_polys, dtype=np.float32), np.array(text_tags, dtype=np.bool), np.array(vertex_text_polys, dtype=np.float32), np.array(vertex_text_tags, dtype=np.bool),np.array(vertex_1_text_polys, dtype=np.float32), np.array(vertex_1_text_tags, dtype=np.bool), np.array(vertex_2_text_polys, dtype=np.float32), np.array(vertex_2_text_tags, dtype=np.bool), np.array(vertex_3_text_polys, dtype=np.float32), np.array(vertex_3_text_tags, dtype=np.bool), np.array(vertex_4_text_polys, dtype=np.float32), np.array(vertex_4_text_tags, dtype=np.bool)
def polygon_area(poly):
'''
compute area of a polygon
:param poly:
:return:
'''
edge = [
(poly[1][0] - poly[0][0]) * (poly[1][1] + poly[0][1]),
(poly[2][0] - poly[1][0]) * (poly[2][1] + poly[1][1]),
(poly[3][0] - poly[2][0]) * (poly[3][1] + poly[2][1]),
(poly[0][0] - poly[3][0]) * (poly[0][1] + poly[3][1])
]
return np.sum(edge)/2.
def check_and_validate_polys(polys, tags, xxx_todo_changeme):
'''
check so that the text poly is in the same direction,
and also filter some invalid polygons
:param polys:
:param tags:
:return:
'''
(h, w) = xxx_todo_changeme
if polys.shape[0] == 0:
return polys
polys[:, :, 0] = np.clip(polys[:, :, 0], 0, w-1)
polys[:, :, 1] = np.clip(polys[:, :, 1], 0, h-1)
validated_polys = []
validated_tags = []
for poly, tag in zip(polys, tags):
p_area = polygon_area(poly)
if abs(p_area) < 1:
# print poly
print('invalid poly')
continue
if p_area > 0:
print('poly in wrong direction')
poly = poly[(0, 3, 2, 1), :]
validated_polys.append(poly)
validated_tags.append(tag)
return np.array(validated_polys), np.array(validated_tags)
def crop_area(im, polys, tags, vertex_polys, vertex_tags, vertex_1_polys, vertex_1_tags, vertex_2_polys, vertex_2_tags, vertex_3_polys, vertex_3_tags, vertex_4_polys, vertex_4_tags, crop_background=False, max_tries=50):
'''
make random crop from the input image
:param im:
:param polys:
:param tags:
:param crop_background:
:param max_tries:
:return:
'''
h, w, _ = im.shape
pad_h = h//10
pad_w = w//10
h_array = np.zeros((h + pad_h*2), dtype=np.int32)
w_array = np.zeros((w + pad_w*2), dtype=np.int32)
vertex_1_h_array = np.zeros((h + pad_h*2), dtype=np.int32)
vertex_1_w_array = np.zeros((w + pad_w*2), dtype=np.int32)
vertex_2_h_array = np.zeros((h + pad_h*2), dtype=np.int32)
vertex_2_w_array = np.zeros((w + pad_w*2), dtype=np.int32)
vertex_3_h_array = np.zeros((h + pad_h*2), dtype=np.int32)
vertex_3_w_array = np.zeros((w + pad_w*2), dtype=np.int32)
vertex_4_h_array = np.zeros((h + pad_h*2), dtype=np.int32)
vertex_4_w_array = np.zeros((w + pad_w*2), dtype=np.int32)
vertex_h_array = np.zeros((h + pad_h*2), dtype=np.int32)
vertex_w_array = np.zeros((w + pad_w*2), dtype=np.int32)
for poly in polys:
poly = np.round(poly, decimals=0).astype(np.int32)
minx = np.min(poly[:, 0])
maxx = np.max(poly[:, 0])
w_array[minx+pad_w:maxx+pad_w] = 1
miny = np.min(poly[:, 1])
maxy = np.max(poly[:, 1])
h_array[miny+pad_h:maxy+pad_h] = 1
for vertex_poly in vertex_polys:
vertex_poly = np.round(vertex_poly, decimals=0).astype(np.int32)
minx = np.min(vertex_poly[:, 0])
maxx = np.max(vertex_poly[:, 0])
vertex_w_array[minx+pad_w:maxx+pad_w] = 1
miny = np.min(vertex_poly[:, 1])
maxy = np.max(vertex_poly[:, 1])
vertex_h_array[miny+pad_h:maxy+pad_h] = 1
for vertex_1_poly in vertex_1_polys:
vertex_1_poly = np.round(vertex_1_poly, decimals=0).astype(np.int32)
minx = np.min(vertex_1_poly[:, 0])
maxx = np.max(vertex_1_poly[:, 0])
vertex_1_w_array[minx+pad_w:maxx+pad_w] = 1
miny = np.min(vertex_1_poly[:, 1])
maxy = np.max(vertex_1_poly[:, 1])
vertex_1_h_array[miny+pad_h:maxy+pad_h] = 1
for vertex_2_poly in vertex_2_polys:
vertex_2_poly = np.round(vertex_2_poly, decimals=0).astype(np.int32)
minx = np.min(vertex_2_poly[:, 0])
maxx = np.max(vertex_2_poly[:, 0])
vertex_2_w_array[minx+pad_w:maxx+pad_w] = 1
miny = np.min(vertex_2_poly[:, 1])
maxy = np.max(vertex_poly[:, 1])
vertex_2_h_array[miny+pad_h:maxy+pad_h] = 1
for vertex_3_poly in vertex_3_polys:
vertex_3_poly = np.round(vertex_3_poly, decimals=0).astype(np.int32)
minx = np.min(vertex_3_poly[:, 0])
maxx = np.max(vertex_3_poly[:, 0])
vertex_3_w_array[minx+pad_w:maxx+pad_w] = 1
miny = np.min(vertex_3_poly[:, 1])
maxy = np.max(vertex_3_poly[:, 1])
vertex_3_h_array[miny+pad_h:maxy+pad_h] = 1
for vertex_4_poly in vertex_4_polys:
vertex_4_poly = np.round(vertex_4_poly, decimals=0).astype(np.int32)
minx = np.min(vertex_4_poly[:, 0])
maxx = np.max(vertex_4_poly[:, 0])
vertex_4_w_array[minx+pad_w:maxx+pad_w] = 1
miny = np.min(vertex_4_poly[:, 1])
maxy = np.max(vertex_4_poly[:, 1])
vertex_4_h_array[miny+pad_h:maxy+pad_h] = 1
# ensure the cropped area not across a text
h_axis = np.where(h_array == 0)[0]
w_axis = np.where(w_array == 0)[0]
vertex_h_axis = np.where(vertex_h_array == 0)[0]
vertex_w_axis = np.where(vertex_w_array == 0)[0]
if len(h_axis) == 0 or len(w_axis) == 0 or len(vertex_h_axis) == 0 or len(vertex_w_axis) == 0:
return im, polys, tags, vertex_polys, vertex_tags, vertex_1_polys, vertex_1_tags, vertex_2_polys, vertex_2_tags, vertex_3_polys, vertex_3_tags, vertex_4_polys, vertex_4_tags
for i in range(max_tries):
xx = np.random.choice(w_axis, size=2)
xmin = np.min(xx) - pad_w
xmax = np.max(xx) - pad_w
xmin = np.clip(xmin, 0, w-1)
xmax = np.clip(xmax, 0, w-1)
yy = np.random.choice(h_axis, size=2)
ymin = np.min(yy) - pad_h
ymax = np.max(yy) - pad_h
ymin = np.clip(ymin, 0, h-1)
ymax = np.clip(ymax, 0, h-1)
if xmax - xmin < FLAGS.min_crop_side_ratio*w or ymax - ymin < FLAGS.min_crop_side_ratio*h:
# area too small
continue
if polys.shape[0] != 0:
poly_axis_in_area = (polys[:, :, 0] >= xmin) & (polys[:, :, 0] <= xmax) \
& (polys[:, :, 1] >= ymin) & (polys[:, :, 1] <= ymax)
selected_polys = np.where(np.sum(poly_axis_in_area, axis=1) == 4)[0]
else:
selected_polys = []
if vertex_polys.shape[0] != 0:
vertex_poly_axis_in_area = (vertex_polys[:, :, 0] >= xmin) & (vertex_polys[:, :, 0] <= xmax) \
& (vertex_polys[:, :, 1] >= ymin) & (vertex_polys[:, :, 1] <= ymax)
vertex_selected_polys = np.where(np.sum(vertex_poly_axis_in_area, axis=1) == 4)[0]
else:
vertex_selected_polys = []
if vertex_1_polys.shape[0] != 0:
vertex_1_poly_axis_in_area = (vertex_1_polys[:, :, 0] >= xmin) & (vertex_1_polys[:, :, 0] <= xmax) \
& (vertex_1_polys[:, :, 1] >= ymin) & (vertex_1_polys[:, :, 1] <= ymax)
vertex_1_selected_polys = np.where(np.sum(vertex_1_poly_axis_in_area, axis=1) == 4)[0]
else:
vertex_1_selected_polys = []
if vertex_2_polys.shape[0] != 0:
vertex_2_poly_axis_in_area = (vertex_2_polys[:, :, 0] >= xmin) & (vertex_2_polys[:, :, 0] <= xmax) \
& (vertex_2_polys[:, :, 1] >= ymin) & (vertex_2_polys[:, :, 1] <= ymax)
vertex_2_selected_polys = np.where(np.sum(vertex_2_poly_axis_in_area, axis=1) == 4)[0]
else:
vertex_2_selected_polys = []
if vertex_3_polys.shape[0] != 0:
vertex_3_poly_axis_in_area = (vertex_3_polys[:, :, 0] >= xmin) & (vertex_3_polys[:, :, 0] <= xmax) \
& (vertex_3_polys[:, :, 1] >= ymin) & (vertex_3_polys[:, :, 1] <= ymax)
vertex_3_selected_polys = np.where(np.sum(vertex_3_poly_axis_in_area, axis=1) == 4)[0]
else:
vertex_3_selected_polys = []
if vertex_4_polys.shape[0] != 0:
vertex_4_poly_axis_in_area = (vertex_4_polys[:, :, 0] >= xmin) & (vertex_4_polys[:, :, 0] <= xmax) \
& (vertex_4_polys[:, :, 1] >= ymin) & (vertex_4_polys[:, :, 1] <= ymax)
vertex_4_selected_polys = np.where(np.sum(vertex_4_poly_axis_in_area, axis=1) == 4)[0]
else:
vertex_4_selected_polys = []
if len(selected_polys) == 0 or len(vertex_selected_polys) == 0 or len(vertex_1_selected_polys) == 0 or len(vertex_2_selected_polys) == 0 or len(vertex_3_selected_polys) == 0 or len(vertex_4_selected_polys) == 0:
# no text in this area
if crop_background:
return im[ymin:ymax+1, xmin:xmax+1, :], polys[selected_polys], tags[selected_polys], vertex_polys[vertex_selected_polys], vertex_tags[vertex_selected_polys], vertex_1_polys[vertex_1_selected_polys], vertex_1_tags[vertex_1_selected_polys], vertex_2_polys[vertex_2_selected_polys], vertex_2_tags[vertex_2_selected_polys], vertex_3_polys[vertex_3_selected_polys], vertex_3_tags[vertex_3_selected_polys], vertex_4_polys[vertex_4_selected_polys], vertex_4_tags[vertex_4_selected_polys]
else:
continue
im = im[ymin:ymax+1, xmin:xmax+1, :]
polys = polys[selected_polys]
vertex_polys = vertex_polys[vertex_selected_polys]
tags = tags[selected_polys]
vertex_tags = vertex_tags[vertex_selected_polys]
vertex_1_polys = vertex_1_polys[vertex_1_selected_polys]
vertex_1_tags = vertex_1_tags[vertex_1_selected_polys]
vertex_2_polys = vertex_2_polys[vertex_2_selected_polys]
vertex_2_tags = vertex_2_tags[vertex_2_selected_polys]
vertex_3_polys = vertex_3_polys[vertex_3_selected_polys]
vertex_3_tags = vertex_3_tags[vertex_3_selected_polys]
vertex_4_polys = vertex_4_polys[vertex_4_selected_polys]
vertex_4_tags = vertex_4_tags[vertex_4_selected_polys]
polys[:, :, 0] -= xmin
polys[:, :, 1] -= ymin
vertex_polys[:, :, 0] -= xmin
vertex_polys[:, :, 1] -= ymin
vertex_1_polys[:, :, 0] -= xmin
vertex_1_polys[:, :, 1] -= ymin
vertex_2_polys[:, :, 0] -= xmin
vertex_2_polys[:, :, 1] -= ymin
vertex_3_polys[:, :, 0] -= xmin
vertex_3_polys[:, :, 1] -= ymin
vertex_4_polys[:, :, 0] -= xmin
vertex_4_polys[:, :, 1] -= ymin
return im, polys, tags, vertex_polys, vertex_tags, vertex_1_polys, vertex_1_tags, vertex_2_polys, vertex_2_tags, vertex_3_polys, vertex_3_tags, vertex_4_polys, vertex_4_tags
return im, polys, tags, vertex_polys, vertex_tags, vertex_1_polys, vertex_1_tags, vertex_2_polys, vertex_2_tags, vertex_3_polys, vertex_3_tags, vertex_4_polys, vertex_4_tags
def shrink_poly(poly, r):
'''
fit a poly inside the origin poly, maybe bugs here...
used for generate the score map
:param poly: the text poly
:param r: r in the paper
:return: the shrinked poly
'''
# shrink ratio
R = 0.3
# find the longer pair
if np.linalg.norm(poly[0] - poly[1]) + np.linalg.norm(poly[2] - poly[3]) > \
np.linalg.norm(poly[0] - poly[3]) + np.linalg.norm(poly[1] - poly[2]):
# first move (p0, p1), (p2, p3), then (p0, p3), (p1, p2)
## p0, p1
theta = np.arctan2((poly[1][1] - poly[0][1]), (poly[1][0] - poly[0][0]))
poly[0][0] += R * r[0] * np.cos(theta)
poly[0][1] += R * r[0] * np.sin(theta)
poly[1][0] -= R * r[1] * np.cos(theta)
poly[1][1] -= R * r[1] * np.sin(theta)
## p2, p3
theta = np.arctan2((poly[2][1] - poly[3][1]), (poly[2][0] - poly[3][0]))
poly[3][0] += R * r[3] * np.cos(theta)
poly[3][1] += R * r[3] * np.sin(theta)
poly[2][0] -= R * r[2] * np.cos(theta)
poly[2][1] -= R * r[2] * np.sin(theta)
## p0, p3
theta = np.arctan2((poly[3][0] - poly[0][0]), (poly[3][1] - poly[0][1]))
poly[0][0] += R * r[0] * np.sin(theta)
poly[0][1] += R * r[0] * np.cos(theta)
poly[3][0] -= R * r[3] * np.sin(theta)
poly[3][1] -= R * r[3] * np.cos(theta)
## p1, p2
theta = np.arctan2((poly[2][0] - poly[1][0]), (poly[2][1] - poly[1][1]))
poly[1][0] += R * r[1] * np.sin(theta)
poly[1][1] += R * r[1] * np.cos(theta)
poly[2][0] -= R * r[2] * np.sin(theta)
poly[2][1] -= R * r[2] * np.cos(theta)
else:
## p0, p3
# print poly
theta = np.arctan2((poly[3][0] - poly[0][0]), (poly[3][1] - poly[0][1]))
poly[0][0] += R * r[0] * np.sin(theta)
poly[0][1] += R * r[0] * np.cos(theta)
poly[3][0] -= R * r[3] * np.sin(theta)
poly[3][1] -= R * r[3] * np.cos(theta)
## p1, p2
theta = np.arctan2((poly[2][0] - poly[1][0]), (poly[2][1] - poly[1][1]))
poly[1][0] += R * r[1] * np.sin(theta)
poly[1][1] += R * r[1] * np.cos(theta)
poly[2][0] -= R * r[2] * np.sin(theta)
poly[2][1] -= R * r[2] * np.cos(theta)
## p0, p1
theta = np.arctan2((poly[1][1] - poly[0][1]), (poly[1][0] - poly[0][0]))
poly[0][0] += R * r[0] * np.cos(theta)
poly[0][1] += R * r[0] * np.sin(theta)
poly[1][0] -= R * r[1] * np.cos(theta)
poly[1][1] -= R * r[1] * np.sin(theta)
## p2, p3
theta = np.arctan2((poly[2][1] - poly[3][1]), (poly[2][0] - poly[3][0]))
poly[3][0] += R * r[3] * np.cos(theta)
poly[3][1] += R * r[3] * np.sin(theta)
poly[2][0] -= R * r[2] * np.cos(theta)
poly[2][1] -= R * r[2] * np.sin(theta)
return poly
def point_dist_to_line(p1, p2, p3):
# compute the distance from p3 to p1-p2
return np.linalg.norm(np.cross(p2 - p1, p1 - p3)) / np.linalg.norm(p2 - p1)
def fit_line(p1, p2):
# fit a line ax+by+c = 0
if p1[0] == p1[1]:
return [1., 0., -p1[0]]
else:
[k, b] = np.polyfit(p1, p2, deg=1)
return [k, -1., b]
def line_cross_point(line1, line2):
# line1 0= ax+by+c, compute the cross point of line1 and line2
if line1[0] != 0 and line1[0] == line2[0]:
print('Cross point does not exist')
return None
if line1[0] == 0 and line2[0] == 0:
print('Cross point does not exist')
return None
if line1[1] == 0:
x = -line1[2]
y = line2[0] * x + line2[2]
elif line2[1] == 0:
x = -line2[2]
y = line1[0] * x + line1[2]
else:
k1, _, b1 = line1
k2, _, b2 = line2
x = -(b1-b2)/(k1-k2)
y = k1*x + b1
return np.array([x, y], dtype=np.float32)
def line_verticle(line, point):
# get the verticle line from line across point
if line[1] == 0:
verticle = [0, -1, point[1]]
else:
if line[0] == 0:
verticle = [1, 0, -point[0]]
else:
verticle = [-1./line[0], -1, point[1] - (-1/line[0] * point[0])]
return verticle
def rectangle_from_parallelogram(poly):
'''
fit a rectangle from a parallelogram
:param poly:
:return:
'''
p0, p1, p2, p3 = poly
angle_p0 = np.arccos(np.dot(p1-p0, p3-p0)/(np.linalg.norm(p0-p1) * np.linalg.norm(p3-p0)))
if angle_p0 < 0.5 * np.pi:
if np.linalg.norm(p0 - p1) > np.linalg.norm(p0-p3):
# p0 and p2
## p0
p2p3 = fit_line([p2[0], p3[0]], [p2[1], p3[1]])
p2p3_verticle = line_verticle(p2p3, p0)
new_p3 = line_cross_point(p2p3, p2p3_verticle)
## p2
p0p1 = fit_line([p0[0], p1[0]], [p0[1], p1[1]])
p0p1_verticle = line_verticle(p0p1, p2)
new_p1 = line_cross_point(p0p1, p0p1_verticle)
return np.array([p0, new_p1, p2, new_p3], dtype=np.float32)
else:
p1p2 = fit_line([p1[0], p2[0]], [p1[1], p2[1]])
p1p2_verticle = line_verticle(p1p2, p0)
new_p1 = line_cross_point(p1p2, p1p2_verticle)
p0p3 = fit_line([p0[0], p3[0]], [p0[1], p3[1]])
p0p3_verticle = line_verticle(p0p3, p2)
new_p3 = line_cross_point(p0p3, p0p3_verticle)
return np.array([p0, new_p1, p2, new_p3], dtype=np.float32)
else:
if np.linalg.norm(p0-p1) > np.linalg.norm(p0-p3):
# p1 and p3
## p1
p2p3 = fit_line([p2[0], p3[0]], [p2[1], p3[1]])
p2p3_verticle = line_verticle(p2p3, p1)
new_p2 = line_cross_point(p2p3, p2p3_verticle)
## p3
p0p1 = fit_line([p0[0], p1[0]], [p0[1], p1[1]])
p0p1_verticle = line_verticle(p0p1, p3)
new_p0 = line_cross_point(p0p1, p0p1_verticle)
return np.array([new_p0, p1, new_p2, p3], dtype=np.float32)
else:
p0p3 = fit_line([p0[0], p3[0]], [p0[1], p3[1]])
p0p3_verticle = line_verticle(p0p3, p1)
new_p0 = line_cross_point(p0p3, p0p3_verticle)
p1p2 = fit_line([p1[0], p2[0]], [p1[1], p2[1]])
p1p2_verticle = line_verticle(p1p2, p3)
new_p2 = line_cross_point(p1p2, p1p2_verticle)
return np.array([new_p0, p1, new_p2, p3], dtype=np.float32)
def sort_rectangle(poly):
# sort the four coordinates of the polygon, points in poly should be sorted clockwise
# First find the lowest point
p_lowest = np.argmax(poly[:, 1])
if np.count_nonzero(poly[:, 1] == poly[p_lowest, 1]) == 2:
# 底边平行于X轴, 那么p0为左上角 - if the bottom line is parallel to x-axis, then p0 must be the upper-left corner
p0_index = np.argmin(np.sum(poly, axis=1))
p1_index = (p0_index + 1) % 4
p2_index = (p0_index + 2) % 4
p3_index = (p0_index + 3) % 4
return poly[[p0_index, p1_index, p2_index, p3_index]], 0.
else:
# 找到最低点右边的点 - find the point that sits right to the lowest point
p_lowest_right = (p_lowest - 1) % 4
p_lowest_left = (p_lowest + 1) % 4
angle = np.arctan(-(poly[p_lowest][1] - poly[p_lowest_right][1])/(poly[p_lowest][0] - poly[p_lowest_right][0]))
# assert angle > 0
if angle <= 0:
print(angle, poly[p_lowest], poly[p_lowest_right])
if angle/np.pi * 180 > 45:
# 这个点为p2 - this point is p2
p2_index = p_lowest
p1_index = (p2_index - 1) % 4
p0_index = (p2_index - 2) % 4
p3_index = (p2_index + 1) % 4
return poly[[p0_index, p1_index, p2_index, p3_index]], -(np.pi/2 - angle)
else:
# 这个点为p3 - this point is p3
p3_index = p_lowest
p0_index = (p3_index + 1) % 4
p1_index = (p3_index + 2) % 4
p2_index = (p3_index + 3) % 4
return poly[[p0_index, p1_index, p2_index, p3_index]], angle
def restore_rectangle_rbox(origin, geometry):
d = geometry[:, :4]
angle = geometry[:, 4]
# for angle > 0
true_origin = []
origin_0 = origin[angle >= 0]
d_0 = d[angle >= 0]
angle_0 = angle[angle >= 0]
true_origin.extend(origin_0)
if origin_0.shape[0] > 0:
p = np.array([np.zeros(d_0.shape[0]), -d_0[:, 0] - d_0[:, 2],
d_0[:, 1] + d_0[:, 3], -d_0[:, 0] - d_0[:, 2],
d_0[:, 1] + d_0[:, 3], np.zeros(d_0.shape[0]),
np.zeros(d_0.shape[0]), np.zeros(d_0.shape[0]),
d_0[:, 3], -d_0[:, 2]])
p = p.transpose((1, 0)).reshape((-1, 5, 2)) # N*5*2
rotate_matrix_x = np.array([np.cos(angle_0), np.sin(angle_0)]).transpose((1, 0))
rotate_matrix_x = np.repeat(rotate_matrix_x, 5, axis=1).reshape(-1, 2, 5).transpose((0, 2, 1)) # N*5*2
rotate_matrix_y = np.array([-np.sin(angle_0), np.cos(angle_0)]).transpose((1, 0))
rotate_matrix_y = np.repeat(rotate_matrix_y, 5, axis=1).reshape(-1, 2, 5).transpose((0, 2, 1))
p_rotate_x = np.sum(rotate_matrix_x * p, axis=2)[:, :, np.newaxis] # N*5*1
p_rotate_y = np.sum(rotate_matrix_y * p, axis=2)[:, :, np.newaxis] # N*5*1
p_rotate = np.concatenate([p_rotate_x, p_rotate_y], axis=2) # N*5*2
p3_in_origin = origin_0 - p_rotate[:, 4, :]
new_p0 = p_rotate[:, 0, :] + p3_in_origin # N*2
new_p1 = p_rotate[:, 1, :] + p3_in_origin
new_p2 = p_rotate[:, 2, :] + p3_in_origin
new_p3 = p_rotate[:, 3, :] + p3_in_origin
new_p_0 = np.concatenate([new_p0[:, np.newaxis, :], new_p1[:, np.newaxis, :],
new_p2[:, np.newaxis, :], new_p3[:, np.newaxis, :]], axis=1)
# new_p_0 = np.concatenate([new_p0[:, np.newaxis, :], new_p1[:, np.newaxis, :],
# new_p2[:, np.newaxis, :], new_p3[:, np.newaxis, :], origin_0[:,np.newaxis,:]], axis=1) # N*4*2
else:
new_p_0 = np.zeros((0, 5, 2))
# for angle < 0
origin_1 = origin[angle < 0]
true_origin.extend(origin_1)
d_1 = d[angle < 0]
angle_1 = angle[angle < 0]
if origin_1.shape[0] > 0:
p = np.array([-d_1[:, 1] - d_1[:, 3], -d_1[:, 0] - d_1[:, 2],
np.zeros(d_1.shape[0]), -d_1[:, 0] - d_1[:, 2],
np.zeros(d_1.shape[0]), np.zeros(d_1.shape[0]),
-d_1[:, 1] - d_1[:, 3], np.zeros(d_1.shape[0]),
-d_1[:, 1], -d_1[:, 2]])
p = p.transpose((1, 0)).reshape((-1, 5, 2)) # N*5*2
rotate_matrix_x = np.array([np.cos(-angle_1), -np.sin(-angle_1)]).transpose((1, 0))
rotate_matrix_x = np.repeat(rotate_matrix_x, 5, axis=1).reshape(-1, 2, 5).transpose((0, 2, 1)) # N*5*2
rotate_matrix_y = np.array([np.sin(-angle_1), np.cos(-angle_1)]).transpose((1, 0))
rotate_matrix_y = np.repeat(rotate_matrix_y, 5, axis=1).reshape(-1, 2, 5).transpose((0, 2, 1))
p_rotate_x = np.sum(rotate_matrix_x * p, axis=2)[:, :, np.newaxis] # N*5*1
p_rotate_y = np.sum(rotate_matrix_y * p, axis=2)[:, :, np.newaxis] # N*5*1
p_rotate = np.concatenate([p_rotate_x, p_rotate_y], axis=2) # N*5*2
p3_in_origin = origin_1 - p_rotate[:, 4, :]
new_p0 = p_rotate[:, 0, :] + p3_in_origin # N*2
new_p1 = p_rotate[:, 1, :] + p3_in_origin
new_p2 = p_rotate[:, 2, :] + p3_in_origin
new_p3 = p_rotate[:, 3, :] + p3_in_origin
new_p_1 = np.concatenate([new_p0[:, np.newaxis, :], new_p1[:, np.newaxis, :],
new_p2[:, np.newaxis, :], new_p3[:, np.newaxis, :]], axis=1) # N*4*2
# new_p_1 = np.concatenate([new_p0[:, np.newaxis, :], new_p1[:, np.newaxis, :],
# new_p2[:, np.newaxis, :], new_p3[:, np.newaxis, :], origin_1[:,np.newaxis,:]], axis=1) # N*4*2
else:
new_p_1 = np.zeros((0, 5, 2))
return np.concatenate([new_p_0, new_p_1])
def restore_rectangle(origin, geometry):
return restore_rectangle_rbox(origin, geometry)
def generate_rbox(im_size, polys, tags):
h, w = im_size
poly_mask = np.zeros((h, w), dtype=np.uint8)
score_map = np.zeros((h, w), dtype=np.uint8)
geo_map = np.zeros((h, w, 5), dtype=np.float32)
# mask used during traning, to ignore some hard areas
training_mask = np.ones((h, w), dtype=np.uint8)
for poly_idx, poly_tag in enumerate(zip(polys, tags)):
poly = poly_tag[0]
tag = poly_tag[1]
r = [None, None, None, None]
for i in range(4):
r[i] = min(np.linalg.norm(poly[i] - poly[(i + 1) % 4]),
np.linalg.norm(poly[i] - poly[(i - 1) % 4]))
# score map
shrinked_poly = shrink_poly(poly.copy(), r).astype(np.int32)[np.newaxis, :, :]
cv2.fillPoly(score_map, shrinked_poly, 1)
cv2.fillPoly(poly_mask, shrinked_poly, poly_idx + 1)
# if the poly is too small, then ignore it during training
poly_h = min(np.linalg.norm(poly[0] - poly[3]), np.linalg.norm(poly[1] - poly[2]))
poly_w = min(np.linalg.norm(poly[0] - poly[1]), np.linalg.norm(poly[2] - poly[3]))
if min(poly_h, poly_w) < FLAGS.min_text_size:
cv2.fillPoly(training_mask, poly.astype(np.int32)[np.newaxis, :, :], 0)
if tag:
cv2.fillPoly(training_mask, poly.astype(np.int32)[np.newaxis, :, :], 0)
xy_in_poly = np.argwhere(poly_mask == (poly_idx + 1))
# if geometry == 'RBOX':
# 对任意两个顶点的组合生成一个平行四边形 - generate a parallelogram for any combination of two vertices
fitted_parallelograms = []
for i in range(4):
p0 = poly[i]
p1 = poly[(i + 1) % 4]
p2 = poly[(i + 2) % 4]
p3 = poly[(i + 3) % 4]
edge = fit_line([p0[0], p1[0]], [p0[1], p1[1]])
backward_edge = fit_line([p0[0], p3[0]], [p0[1], p3[1]])
forward_edge = fit_line([p1[0], p2[0]], [p1[1], p2[1]])
if point_dist_to_line(p0, p1, p2) > point_dist_to_line(p0, p1, p3):
# 平行线经过p2 - parallel lines through p2
if edge[1] == 0:
edge_opposite = [1, 0, -p2[0]]
else:
edge_opposite = [edge[0], -1, p2[1] - edge[0] * p2[0]]
else:
# 经过p3 - after p3
if edge[1] == 0:
edge_opposite = [1, 0, -p3[0]]
else:
edge_opposite = [edge[0], -1, p3[1] - edge[0] * p3[0]]
# move forward edge
new_p0 = p0
new_p1 = p1
new_p2 = p2
new_p3 = p3
new_p2 = line_cross_point(forward_edge, edge_opposite)
if point_dist_to_line(p1, new_p2, p0) > point_dist_to_line(p1, new_p2, p3):
# across p0
if forward_edge[1] == 0:
forward_opposite = [1, 0, -p0[0]]
else:
forward_opposite = [forward_edge[0], -1, p0[1] - forward_edge[0] * p0[0]]
else:
# across p3
if forward_edge[1] == 0:
forward_opposite = [1, 0, -p3[0]]
else:
forward_opposite = [forward_edge[0], -1, p3[1] - forward_edge[0] * p3[0]]
new_p0 = line_cross_point(forward_opposite, edge)
new_p3 = line_cross_point(forward_opposite, edge_opposite)
fitted_parallelograms.append([new_p0, new_p1, new_p2, new_p3, new_p0])
# or move backward edge
new_p0 = p0
new_p1 = p1
new_p2 = p2
new_p3 = p3
new_p3 = line_cross_point(backward_edge, edge_opposite)
if point_dist_to_line(p0, p3, p1) > point_dist_to_line(p0, p3, p2):
# across p1
if backward_edge[1] == 0:
backward_opposite = [1, 0, -p1[0]]
else:
backward_opposite = [backward_edge[0], -1, p1[1] - backward_edge[0] * p1[0]]
else:
# across p2
if backward_edge[1] == 0:
backward_opposite = [1, 0, -p2[0]]
else:
backward_opposite = [backward_edge[0], -1, p2[1] - backward_edge[0] * p2[0]]
new_p1 = line_cross_point(backward_opposite, edge)
new_p2 = line_cross_point(backward_opposite, edge_opposite)
fitted_parallelograms.append([new_p0, new_p1, new_p2, new_p3, new_p0])
areas = [Polygon(t).area for t in fitted_parallelograms]
parallelogram = np.array(fitted_parallelograms[np.argmin(areas)][:-1], dtype=np.float32)
# sort thie polygon
parallelogram_coord_sum = np.sum(parallelogram, axis=1)
min_coord_idx = np.argmin(parallelogram_coord_sum)
parallelogram = parallelogram[
[min_coord_idx, (min_coord_idx + 1) % 4, (min_coord_idx + 2) % 4, (min_coord_idx + 3) % 4]]
rectange = rectangle_from_parallelogram(parallelogram)
rectange, rotate_angle = sort_rectangle(rectange)
p0_rect, p1_rect, p2_rect, p3_rect = rectange
for y, x in xy_in_poly:
point = np.array([x, y], dtype=np.float32)
# top
geo_map[y, x, 0] = point_dist_to_line(p0_rect, p1_rect, point)
# right
geo_map[y, x, 1] = point_dist_to_line(p1_rect, p2_rect, point)
# down
geo_map[y, x, 2] = point_dist_to_line(p2_rect, p3_rect, point)
# left
geo_map[y, x, 3] = point_dist_to_line(p3_rect, p0_rect, point)
# angle
geo_map[y, x, 4] = rotate_angle
return score_map, geo_map, training_mask
def generator(input_size=512, batch_size=32,
background_ratio=3./8,
random_scale=np.array([0.5, 1, 2.0, 3.0]),
vis=False):
image_list = np.array(get_images())
print('{} training images in {}'.format(
image_list.shape[0], FLAGS.training_data_path))
index = np.arange(0, image_list.shape[0])
while True:
np.random.shuffle(index)
images = []
image_fns = []
score_maps = []
geo_maps = []
training_masks = []
vertex_score_maps = []
vertex_geo_maps = []
vertex_training_masks = []
vertex_1_score_maps = []
vertex_1_geo_maps = []
vertex_1_training_masks = []
vertex_2_score_maps = []
vertex_2_geo_maps = []
vertex_2_training_masks = []
vertex_3_score_maps = []
vertex_3_geo_maps = []
vertex_3_training_masks = []
vertex_4_score_maps = []
vertex_4_geo_maps = []
vertex_4_training_masks = []
for i in index:
try:
im_fn = image_list[i]
im = cv2.imread(im_fn)
# print im_fn
h, w, _ = im.shape
txt_fn = im_fn.replace(os.path.basename(im_fn).split('.')[-1], 'txt')
if not os.path.exists(txt_fn):
print('text file {} does not exists'.format(txt_fn))
continue
text_polys, text_tags, vertex_text_polys, vertex_text_tags, vertex_1_text_polys, vertex_1_text_tags, vertex_2_text_polys, vertex_2_text_tags, vertex_3_text_polys, vertex_3_text_tags, vertex_4_text_polys, vertex_4_text_tags = load_annoataion(txt_fn)
text_polys, text_tags = check_and_validate_polys(text_polys, text_tags, (h, w))
vertex_text_polys, vertex_text_tags = check_and_validate_polys(vertex_text_polys, vertex_text_tags, (h, w))
vertex_1_text_polys, vertex_1_text_tags = check_and_validate_polys(vertex_1_text_polys, vertex_1_text_tags,
(h, w))
vertex_2_text_polys, vertex_2_text_tags = check_and_validate_polys(vertex_2_text_polys, vertex_2_text_tags,
(h, w))
vertex_3_text_polys, vertex_3_text_tags = check_and_validate_polys(vertex_3_text_polys, vertex_3_text_tags,
(h, w))
vertex_4_text_polys, vertex_4_text_tags = check_and_validate_polys(vertex_4_text_polys, vertex_4_text_tags,
(h, w))
# if text_polys.shape[0] == 0:
# continue
# random scale this image
rd_scale = np.random.choice(random_scale)
im = cv2.resize(im, dsize=None, fx=rd_scale, fy=rd_scale)
text_polys *= rd_scale
vertex_1_text_polys *= rd_scale
vertex_2_text_polys *= rd_scale
vertex_3_text_polys *= rd_scale
vertex_4_text_polys *= rd_scale
vertex_text_polys *= rd_scale
# print rd_scale
# random crop a area from image
if np.random.rand() < background_ratio:
# crop background
im, text_polys, text_tags, vertex_text_polys, vertex_text_tags, vertex_1_text_polys, vertex_1_text_tags, vertex_2_text_polys, vertex_2_text_tags, vertex_3_text_polys, vertex_3_text_tags, vertex_4_text_polys, vertex_4_text_tags= crop_area(im, text_polys, text_tags, vertex_text_polys, vertex_text_tags, vertex_1_text_polys, vertex_1_text_tags, vertex_2_text_polys, vertex_2_text_tags, vertex_3_text_polys, vertex_3_text_tags, vertex_4_text_polys, vertex_4_text_tags, crop_background=True)
# im, text_polys, text_tags, vertex_text_polys, vertex_text_tags = crop_area(im, text_polys, text_tags, vertex_text_polys, vertex_text_tags, vertex_1_text_polys, vertex_1_text_tags, vertex_2_text_polys, vertex_2_text_tags, vertex_3_text_polys, vertex_3_text_tags, vertex_4_text_polys, vertex_4_text_tags, crop_background=True)
# vertex_im, vertex_text_polys, vertex_text_tags = crop_area(im, vertex_text_polys, vertex_text_tags, crop_background=True)
vertex_im = im
if text_polys.shape[0] > 0:
# cannot find background
continue
if vertex_1_text_polys.shape[0] > 0:
# cannot find background
continue
if vertex_2_text_polys.shape[0] > 0:
# cannot find background
continue
if vertex_3_text_polys.shape[0] > 0:
# cannot find background
continue
if vertex_4_text_polys.shape[0] > 0:
# cannot find background
continue
if vertex_text_polys.shape[0] > 0:
# cannot find background
continue
# pad and resize image
new_h, new_w, _ = im.shape
vertex_new_h, vertex_new_w, _= vertex_im.shape
max_h_w_i = np.max([new_h, new_w, input_size])
vertex_max_h_w_i = np.max([vertex_new_h, vertex_new_w, input_size])
im_padded = np.zeros((max_h_w_i, max_h_w_i, 3), dtype=np.uint8)
vertex_im_padded = np.zeros((vertex_max_h_w_i,vertex_max_h_w_i,3),dtype=np.uint8)
im_padded[:new_h, :new_w, :] = im.copy()
vertex_im_padded[:vertex_new_h,:vertex_new_w,:] = vertex_im.copy()
im = cv2.resize(im_padded, dsize=(input_size, input_size))
vertex_im = cv2.resize(vertex_im_padded, dsize=(input_size, input_size))
score_map = np.zeros((input_size, input_size), dtype=np.uint8)
vertex_score_map = np.zeros((input_size, input_size), dtype=np.uint8)
geo_map_channels = 5 if FLAGS.geometry == 'RBOX' else 8
vertex_geo_map_channels = 5 if FLAGS.geometry == 'RBOX' else 8
geo_map = np.zeros((input_size, input_size, geo_map_channels), dtype=np.float32)
vertex_geo_map = np.zeros((input_size, input_size, vertex_geo_map_channels), dtype=np.float32)
training_mask = np.ones((input_size, input_size), dtype=np.uint8)
vertex_training_mask = np.ones((input_size, input_size), dtype=np.uint8)
vertex_1_score_map = np.zeros((input_size, input_size), dtype=np.uint8)
vertex_1_geo_map_channels = 5 if FLAGS.geometry == 'RBOX' else 8
vertex_1_geo_map = np.zeros((input_size, input_size, vertex_1_geo_map_channels), dtype=np.float32)
vertex_1_training_mask = np.ones((input_size, input_size), dtype=np.uint8)
vertex_2_score_map = np.zeros((input_size, input_size), dtype=np.uint8)
vertex_2_geo_map_channels = 5 if FLAGS.geometry == 'RBOX' else 8
vertex_2_geo_map = np.zeros((input_size, input_size, vertex_2_geo_map_channels), dtype=np.float32)
vertex_2_training_mask = np.ones((input_size, input_size), dtype=np.uint8)
vertex_3_score_map = np.zeros((input_size, input_size), dtype=np.uint8)
vertex_3_geo_map_channels = 5 if FLAGS.geometry == 'RBOX' else 8
vertex_3_geo_map = np.zeros((input_size, input_size, vertex_3_geo_map_channels), dtype=np.float32)
vertex_3_training_mask = np.ones((input_size, input_size), dtype=np.uint8)
vertex_4_score_map = np.zeros((input_size, input_size), dtype=np.uint8)
vertex_4_geo_map_channels = 5 if FLAGS.geometry == 'RBOX' else 8
vertex_4_geo_map = np.zeros((input_size, input_size, vertex_4_geo_map_channels), dtype=np.float32)
vertex_4_training_mask = np.ones((input_size, input_size), dtype=np.uint8)
else:
im, text_polys, text_tags, vertex_text_polys, vertex_text_tags, vertex_1_text_polys, vertex_1_text_tags, vertex_2_text_polys, vertex_2_text_tags, vertex_3_text_polys, vertex_3_text_tags, vertex_4_text_polys, vertex_4_text_tags= crop_area(im, text_polys, text_tags, vertex_text_polys, vertex_text_tags, vertex_1_text_polys, vertex_1_text_tags, vertex_2_text_polys, vertex_2_text_tags, vertex_3_text_polys, vertex_3_text_tags, vertex_4_text_polys, vertex_4_text_tags, crop_background=False)
# im, text_polys, text_tags, vertex_text_polys, vertex_text_tags = crop_area(im, text_polys, text_tags, vertex_text_polys, vertex_text_tags, crop_background=False)
# vertex_im, vertex_text_polys, vertex_text_tags = crop_area(im, vertex_text_polys, vertex_text_tags, crop_background=False)
vertex_im = im
if text_polys.shape[0] == 0:
continue
if vertex_1_text_polys.shape[0] == 0:
continue
if vertex_2_text_polys.shape[0] == 0:
continue
if vertex_3_text_polys.shape[0] == 0:
continue
if vertex_4_text_polys.shape[0] == 0:
continue
if vertex_text_polys.shape[0] == 0:
continue
h, w, _ = im.shape
# pad the image to the training input size or the longer side of image
new_h, new_w, _ = im.shape
vertex_new_h, vertex_new_w, _ = vertex_im.shape
max_h_w_i = np.max([new_h, new_w, input_size])
vertex_max_h_w_i = np.max([vertex_new_h, vertex_new_w, input_size])
im_padded = np.zeros((max_h_w_i, max_h_w_i, 3), dtype=np.uint8)
vertex_im_padded = np.zeros((vertex_max_h_w_i, vertex_max_h_w_i, 3), dtype=np.uint8)
im_padded[:new_h, :new_w, :] = im.copy()
vertex_im_padded[:vertex_new_h, :vertex_new_w, :] = vertex_im.copy()
im = im_padded
vertex_im = vertex_im_padded
# resize the image to input size
new_h, new_w, _ = im.shape
vertex_new_h, vertex_new_w, _ = vertex_im.shape
resize_h = input_size
resize_w = input_size
im = cv2.resize(im, dsize=(resize_w, resize_h))
vertex_im = cv2.resize(vertex_im,dsize=(resize_w,resize_h))
resize_ratio_3_x = resize_w/float(new_w)