File size: 27,175 Bytes
c310e19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
import cv2
import numpy as np
import pycocotools.mask as mask_utils
import torch
from maskrcnn_benchmark.utils.chars import char2num
import pyclipper
# from PIL import Image
from shapely import affinity
from shapely.geometry import Polygon as ShapePolygon


# transpose
FLIP_LEFT_RIGHT = 0
FLIP_TOP_BOTTOM = 1


def convert_2d_tuple(t):
    a = []
    for i in t:
        a.extend(list(i))
    return a


class Mask(object):
    """
    This class is unfinished and not meant for use yet
    It is supposed to contain the mask for an object as
    a 2d tensor
    """

    def __init__(self, masks, size, mode):
        self.masks = masks
        self.size = size
        self.mode = mode

    def transpose(self, method):
        if method not in (FLIP_LEFT_RIGHT, FLIP_TOP_BOTTOM):
            raise NotImplementedError(
                "Only FLIP_LEFT_RIGHT and FLIP_TOP_BOTTOM implemented"
            )

        width, height = self.size
        if method == FLIP_LEFT_RIGHT:
            dim = width
            # idx = 2
        elif method == FLIP_TOP_BOTTOM:
            dim = height
            # idx = 1

        flip_idx = list(range(dim)[::-1])
        flipped_masks = self.masks.index_select(dim, flip_idx)
        return Mask(flipped_masks, self.size, self.mode)

    def crop(self, box):
        w, h = box[2] - box[0], box[3] - box[1]

        cropped_masks = self.masks[:, box[1] : box[3], box[0] : box[2]]
        return Mask(cropped_masks, size=(w, h), mode=self.mode)

    def resize(self, size, *args, **kwargs):
        pass


class SegmentationMask(object):
    """
    This class stores the segmentations for all objects in the image
    """

    def __init__(self, polygons, size, mode=None):
        """
        Arguments:
            polygons: a list of list of lists of numbers. The first
                level of the list correspond to individual instances,
                the second level to all the polygons that compose the
                object, and the third level to the polygon coordinates.
        """
        assert isinstance(polygons, list)

        self.polygons = [Polygons(p, size, mode) for p in polygons]
        self.size = size
        self.mode = mode

    def transpose(self, method):
        if method not in (FLIP_LEFT_RIGHT, FLIP_TOP_BOTTOM):
            raise NotImplementedError(
                "Only FLIP_LEFT_RIGHT and FLIP_TOP_BOTTOM implemented"
            )

        flipped = []
        for polygon in self.polygons:
            flipped.append(polygon.transpose(method))
        return SegmentationMask(flipped, size=self.size, mode=self.mode)

    def crop(self, box, keep_ind=None):
        w, h = box[2] - box[0], box[3] - box[1]
        if keep_ind is not None:
            self.polygons = np.array(self.polygons)
            self.polygons = self.polygons[keep_ind]
        cropped = []
        for polygon in self.polygons:
            cropped.append(polygon.crop(box))
        return SegmentationMask(cropped, size=(w, h), mode=self.mode)

    def rotate(self, angle, r_c, start_h, start_w):
        rotated = []
        for polygon in self.polygons:
            rotated.append(polygon.rotate(angle, r_c, start_h, start_w))
        return SegmentationMask(rotated, size=(r_c[0] * 2, r_c[1] * 2), mode=self.mode)

    def resize(self, size, *args, **kwargs):
        scaled = []
        for polygon in self.polygons:
            scaled.append(polygon.resize(size, *args, **kwargs))
        return SegmentationMask(scaled, size=size, mode=self.mode)

    def set_size(self, size):
        self.size = size
        for polygon in self.polygons:
            polygon.set_size(size)

    def to(self, *args, **kwargs):
        return self

    def __getitem__(self, item):
        if isinstance(item, (int, slice)):
            selected_polygons = [self.polygons[item]]
        else:
            # advanced indexing on a single dimension
            selected_polygons = []
            if isinstance(item, torch.Tensor) and item.dtype == torch.bool:
                item = item.nonzero()
                item = item.squeeze(1) if item.numel() > 0 else item
                item = item.tolist()
            for i in item:
                selected_polygons.append(self.polygons[i])
        return SegmentationMask(selected_polygons, size=self.size, mode=self.mode)

    def __iter__(self):
        return iter(self.polygons)

    def __repr__(self):
        s = self.__class__.__name__ + "("
        s += "num_instances={}, ".format(len(self.polygons))
        s += "image_width={}, ".format(self.size[0])
        s += "image_height={})".format(self.size[1])
        return s

    def size(self):
        return self.size

    def get_polygons(self):
        return self.polygons

    def to_np_polygon(self):
        np_polygons = []
        for polygon in self.polygons:
            polys = polygon.get_polygons()
            for poly in polys:
                np_poly = poly.numpy()
                np_polygons.append(np_poly)
        return np_polygons


    def convert_seg_map(self, labels, shrink_ratio, seg_size, ignore_difficult=True):
        # width, height = self.size
        # assert self.size[0] == seg_size[1]
        # assert self.size[1] == seg_size[0]
        height, width = seg_size[0], seg_size[1]
        seg_map = np.zeros((1, height, width), dtype=np.uint8)
        training_mask = np.ones((height, width), dtype=np.uint8)
        for poly, label in zip(self.polygons, labels):
            poly = poly.get_polygons()[0]
            poly = poly.reshape((-1, 2)).numpy()
            if ignore_difficult and label.item() == -1:
                cv2.fillPoly(training_mask, poly.astype(np.int32)[np.newaxis, :, :], 0)
                continue
            if poly.shape[0] < 4:
                continue
            p = ShapePolygon(poly)
            if p.length == 0:
                continue
            try:
                d = p.area * (1 - np.power(shrink_ratio, 2)) / p.length
            except:
                continue
            subj = [tuple(s) for s in poly]
            pco = pyclipper.PyclipperOffset()
            pco.AddPath(subj, pyclipper.JT_ROUND, pyclipper.ET_CLOSEDPOLYGON)
            s = pco.Execute(-d)
            if s == []:
                cv2.fillPoly(training_mask, poly.astype(np.int32)[np.newaxis, :, :], 0)
                continue
            out = convert_2d_tuple(s[0])
            out = np.array(out).reshape(-1, 2)
            cv2.fillPoly(seg_map[0, :, :], [out.astype(np.int32)], 1)
        return seg_map, training_mask


class Polygons(object):
    """
    This class holds a set of polygons that represents a single instance
    of an object mask. The object can be represented as a set of
    polygons
    """

    def __init__(self, polygons, size, mode):
        # assert isinstance(polygons, list), '{}'.format(polygons)
        if isinstance(polygons, list):
            polygons = [torch.as_tensor(p, dtype=torch.float32) for p in polygons]
        elif isinstance(polygons, Polygons):
            polygons = polygons.polygons

        self.polygons = polygons
        self.size = size
        self.mode = mode

    def transpose(self, method):
        if method not in (FLIP_LEFT_RIGHT, FLIP_TOP_BOTTOM):
            raise NotImplementedError(
                "Only FLIP_LEFT_RIGHT and FLIP_TOP_BOTTOM implemented"
            )

        flipped_polygons = []
        width, height = self.size
        if method == FLIP_LEFT_RIGHT:
            dim = width
            idx = 0
        elif method == FLIP_TOP_BOTTOM:
            dim = height
            idx = 1

        for poly in self.polygons:
            p = poly.clone()
            TO_REMOVE = 1
            p[idx::2] = dim - poly[idx::2] - TO_REMOVE
            flipped_polygons.append(p)

        return Polygons(flipped_polygons, size=self.size, mode=self.mode)

    def rotate(self, angle, r_c, start_h, start_w):
        poly = self.polygons[0].numpy().reshape(-1, 2)
        poly[:, 0] += start_w
        poly[:, 1] += start_h
        polys = ShapePolygon(poly)
        r_polys = list(affinity.rotate(polys, angle, r_c).boundary.coords[:-1])
        p = []
        for r in r_polys:
            p += list(r)
        return Polygons([p], size=self.size, mode=self.mode)

    def crop(self, box):
        w, h = box[2] - box[0], box[3] - box[1]

        # TODO chck if necessary
        w = max(w, 1)
        h = max(h, 1)

        cropped_polygons = []

        for poly in self.polygons:
            p = poly.clone()
            p[0::2] = p[0::2] - box[0]  # .clamp(min=0, max=w)
            p[1::2] = p[1::2] - box[1]  # .clamp(min=0, max=h)
            cropped_polygons.append(p)

        return Polygons(cropped_polygons, size=(w, h), mode=self.mode)

    def resize(self, size, *args, **kwargs):
        ratios = tuple(float(s) / float(s_orig) for s, s_orig in zip(size, self.size))
        if ratios[0] == ratios[1]:
            ratio = ratios[0]
            scaled_polys = [p * ratio for p in self.polygons]
            return Polygons(scaled_polys, size, mode=self.mode)

        ratio_w, ratio_h = ratios
        scaled_polygons = []
        for poly in self.polygons:
            p = poly.clone()
            p[0::2] *= ratio_w
            p[1::2] *= ratio_h
            scaled_polygons.append(p)

        return Polygons(scaled_polygons, size=size, mode=self.mode)

    def convert(self, mode):
        width, height = self.size
        if mode == "mask":
            # print([p.numpy() for p in self.polygons])
            try:
                rles = mask_utils.frPyObjects(
                    [p.numpy() for p in self.polygons], height, width
                )
            except:
                print([p.numpy() for p in self.polygons])
                mask = torch.ones((height, width), dtype=torch.uint8)
                return mask
            rle = mask_utils.merge(rles)
            mask = mask_utils.decode(rle)
            mask = torch.from_numpy(mask)
            # TODO add squeeze?
            return mask
        
    def set_size(self, size):
        self.size = size

    def get_polygons(self):
        return self.polygons

    def __repr__(self):
        s = self.__class__.__name__ + "("
        s += "num_polygons={}, ".format(len(self.polygons))
        s += "image_width={}, ".format(self.size[0])
        s += "image_height={}, ".format(self.size[1])
        s += "mode={})".format(self.mode)
        return s


class CharPolygons(object):
    """
    This class holds a set of polygons that represents a single instance
    of an object mask. The object can be represented as a set of
    polygons
    """

    def __init__(
        self,
        char_boxes,
        word=None,
        use_char_ann=False,
        char_classes=None,
        size=None,
        mode=None,
        char_num_classes=37,
    ):
        if isinstance(char_boxes, CharPolygons):
            if char_classes is None:
                char_classes = char_boxes.char_classes
            self.word = char_boxes.word
            char_boxes = char_boxes.char_boxes
        else:
            if char_classes is None:
                char_classes = [
                    torch.as_tensor(p[8], dtype=torch.float32) for p in char_boxes
                ]
            char_boxes = [
                torch.as_tensor(p[:8], dtype=torch.float32) for p in char_boxes
            ]
            self.word = word
        self.char_boxes = char_boxes
        self.char_classes = char_classes
        self.size = size
        self.mode = mode
        self.use_char_ann = use_char_ann
        self.char_num_classes = char_num_classes

    def transpose(self, method):
        if method not in (FLIP_LEFT_RIGHT, FLIP_TOP_BOTTOM):
            raise NotImplementedError(
                "Only FLIP_LEFT_RIGHT and FLIP_TOP_BOTTOM implemented"
            )

        flipped_polygons = []
        width, height = self.size
        if method == FLIP_LEFT_RIGHT:
            dim = width
            idx = 0
        elif method == FLIP_TOP_BOTTOM:
            dim = height
            idx = 1

        for char_box in self.char_boxes:
            p = char_box.clone()
            TO_REMOVE = 1
            p[idx::2] = dim - char_box[idx::2] - TO_REMOVE
            flipped_polygons.append(p)

        return CharPolygons(
            flipped_polygons,
            word=self.word,
            use_char_ann=self.use_char_ann,
            char_classes=self.char_classes,
            size=self.size,
            mode=self.mode,
            char_num_classes=self.char_num_classes,
        )

    def crop(self, box):
        w, h = box[2] - box[0], box[3] - box[1]

        # TODO chck if necessary
        w = max(w, 1)
        h = max(h, 1)
        cropped_polygons = []
        for char_box in self.char_boxes:
            p = char_box.clone()
            p[0::2] = p[0::2] - box[0]  # .clamp(min=0, max=w)
            p[1::2] = p[1::2] - box[1]  # .clamp(min=0, max=h)
            cropped_polygons.append(p)

        return CharPolygons(
            cropped_polygons,
            word=self.word,
            use_char_ann=self.use_char_ann,
            char_classes=self.char_classes,
            size=(w, h),
            mode=self.mode,
            char_num_classes=self.char_num_classes,
        )

    def rotate(self, angle, r_c, start_h, start_w):
        r_polys = []
        for poly in self.char_boxes:
            poly = poly.numpy()
            poly[0::2] += start_w
            poly[1::2] += start_h
            poly = ShapePolygon(np.array(poly).reshape(4, 2))
            r_poly = np.array(
                list(affinity.rotate(poly, angle, r_c).boundary.coords[:-1])
            ).reshape(-1, 8)
            r_polys.append(r_poly[0])
        return CharPolygons(
            r_polys,
            word=self.word,
            use_char_ann=self.use_char_ann,
            char_classes=self.char_classes,
            size=(r_c[0] * 2, r_c[1] * 2),
            mode=self.mode,
            char_num_classes=self.char_num_classes,
        )

    def resize(self, size, *args, **kwargs):
        ratios = tuple(float(s) / float(s_orig) for s, s_orig in zip(size, self.size))
        if ratios[0] == ratios[1]:
            ratio = ratios[0]
            scaled_polys = [p * ratio for p in self.char_boxes]
            return CharPolygons(
                scaled_polys,
                word=self.word,
                use_char_ann=self.use_char_ann,
                char_classes=self.char_classes,
                size=size,
                mode=self.mode,
                char_num_classes=self.char_num_classes,
            )

        ratio_w, ratio_h = ratios
        scaled_polygons = []
        for poly in self.char_boxes:
            p = poly.clone()
            p[0::2] *= ratio_w
            p[1::2] *= ratio_h
            scaled_polygons.append(p)

        return CharPolygons(
            scaled_polygons,
            word=self.word,
            use_char_ann=self.use_char_ann,
            char_classes=self.char_classes,
            size=size,
            mode=self.mode,
            char_num_classes=self.char_num_classes,
        )

    def set_size(self, size):
        self.size = size

    def convert(self, mode):
        width, height = self.size
        if mode == "char_mask":
            if not self.use_char_ann:
                char_map = -np.ones((height, width))
                char_map_weight = np.zeros((self.char_num_classes,))
            else:
                char_map = np.zeros((height, width))
                char_map_weight = np.ones((self.char_num_classes,))
                for i, p in enumerate(self.char_boxes):
                    poly = p.numpy().reshape(4, 2)
                    poly = shrink_poly(poly, 0.25)
                    cv2.fillPoly(
                        char_map, [poly.astype(np.int32)], int(self.char_classes[i])
                    )
                pos_index = np.where(char_map > 0)
                pos_num = pos_index[0].size
                if pos_num > 0:
                    pos_weight = 1.0 * (height * width - pos_num) / pos_num
                    char_map_weight[1:] = pos_weight
            return torch.from_numpy(char_map), torch.from_numpy(char_map_weight)
        elif mode == "seq_char_mask":
            decoder_target = self.char_num_classes * np.ones((32,))
            word_target = -np.ones((32,))
            if not self.use_char_ann:
                char_map = -np.ones((height, width))
                char_map_weight = np.zeros((self.char_num_classes,))
                for i, char in enumerate(self.word):
                    if i > 31:
                        break
                    decoder_target[i] = char2num(char)
                    word_target[i] = char2num(char)
                end_point = min(max(1, len(self.word)), 31)
                word_target[end_point] = self.char_num_classes
            else:
                char_map = np.zeros((height, width))
                char_map_weight = np.ones((self.char_num_classes,))
                word_length = 0
                for i, p in enumerate(self.char_boxes):
                    poly = p.numpy().reshape(4, 2)
                    if i < 32:
                        decoder_target[i] = int(self.char_classes[i])
                        word_target[i] = int(self.char_classes[i])
                        word_length += 1
                    poly = shrink_poly(poly, 0.25)
                    cv2.fillPoly(
                        char_map, [poly.astype(np.int32)], int(self.char_classes[i])
                    )
                end_point = min(max(1, word_length), 31)
                word_target[end_point] = self.char_num_classes
                pos_index = np.where(char_map > 0)
                pos_num = pos_index[0].size
                if pos_num > 0:
                    pos_weight = 1.0 * (height * width - pos_num) / pos_num
                    char_map_weight[1:] = pos_weight
            return (
                torch.from_numpy(char_map),
                torch.from_numpy(char_map_weight),
                torch.from_numpy(decoder_target),
                torch.from_numpy(word_target),
            )

    def creat_color_map(self, n_class, width):
        splits = int(np.ceil(np.power((n_class * 1.0), 1.0 / 3)))
        maps = []
        for i in range(splits):
            r = int(i * width * 1.0 / (splits - 1))
            for j in range(splits):
                g = int(j * width * 1.0 / (splits - 1))
                for k in range(splits - 1):
                    b = int(k * width * 1.0 / (splits - 1))
                    maps.append([r, g, b])
        return np.array(maps)

    def __repr__(self):
        s = self.__class__.__name__ + "("
        s += "num_char_boxes={}, ".format(len(self.char_boxes))
        s += "num_char_classes={}, ".format(len(self.char_classes))
        s += "image_width={}, ".format(self.size[0])
        s += "image_height={}, ".format(self.size[1])
        s += "mode={})".format(self.mode)
        return s


class SegmentationCharMask(object):
    def __init__(
        self, chars_boxes, words=None, use_char_ann=True, size=None, mode=None, char_num_classes=37
    ):
        # self.chars_boxes=[CharPolygons(char_boxes, word=word, use_char_ann=use_char_ann, size=size, mode=mode) for char_boxes, word in zip(chars_boxes, words)]
        if words is None:
            self.chars_boxes = [
                CharPolygons(
                    char_boxes,
                    word=None,
                    use_char_ann=use_char_ann,
                    size=size,
                    mode=mode,
                    char_num_classes=char_num_classes,
                )
                for char_boxes in chars_boxes
            ]
        else:
            self.chars_boxes = [
                CharPolygons(
                    char_boxes,
                    word=words[i],
                    use_char_ann=use_char_ann,
                    size=size,
                    mode=mode,
                    char_num_classes=char_num_classes,
                )
                for i, char_boxes in enumerate(chars_boxes)
            ]
        self.size = size
        self.mode = mode
        self.use_char_ann = use_char_ann
        self.char_num_classes = char_num_classes

    def transpose(self, method):
        if method not in (FLIP_LEFT_RIGHT, FLIP_TOP_BOTTOM):
            raise NotImplementedError(
                "Only FLIP_LEFT_RIGHT and FLIP_TOP_BOTTOM implemented"
            )

        flipped = []
        for char_boxes in self.chars_boxes:
            flipped.append(char_boxes.transpose(method))
        return SegmentationCharMask(
            flipped, use_char_ann=self.use_char_ann, size=self.size, mode=self.mode, char_num_classes=self.char_num_classes
        )

    def crop(self, box, keep_ind):
        cropped = []
        w, h = box[2] - box[0], box[3] - box[1]
        if keep_ind is not None:
            self.chars_boxes = np.array(self.chars_boxes)
            self.chars_boxes = self.chars_boxes[keep_ind]
        for char_boxes in self.chars_boxes:
            cropped.append(char_boxes.crop(box))
        return SegmentationCharMask(
            cropped, use_char_ann=self.use_char_ann, size=(w, h), mode=self.mode
        )

    def resize(self, size, *args, **kwargs):
        scaled = []
        for char_boxes in self.chars_boxes:
            scaled.append(char_boxes.resize(size, *args, **kwargs))
        return SegmentationCharMask(
            scaled, use_char_ann=self.use_char_ann, size=size, mode=self.mode, char_num_classes=self.char_num_classes
        )

    def set_size(self, size):
        self.size = size
        for char_box in self.chars_boxes:
            char_box.set_size(size)

    def rotate(self, angle, r_c, start_h, start_w):
        rotated = []
        for char_boxes in self.chars_boxes:
            rotated.append(char_boxes.rotate(angle, r_c, start_h, start_w))
        return SegmentationCharMask(
            rotated,
            use_char_ann=self.use_char_ann,
            size=(r_c[0] * 2, r_c[1] * 2),
            mode=self.mode,
            char_num_classes=self.char_num_classes,
        )

    def __iter__(self):
        return iter(self.chars_boxes)

    def __getitem__(self, item):
        if isinstance(item, (int, slice)):
            selected_chars_boxes = [self.chars_boxes[item]]
        else:
            # advanced indexing on a single dimension
            selected_chars_boxes = []
            if isinstance(item, torch.Tensor) and item.dtype == torch.bool:
                item = item.nonzero()
                item = item.squeeze(1) if item.numel() > 0 else item
                item = item.tolist()
            for i in item:
                if i >= len(self.chars_boxes):
                    print(i)
                    print("chars_boxes.shape: ", len(self.chars_boxes))
                    input()
                selected_chars_boxes.append(self.chars_boxes[i])
        return SegmentationCharMask(
            selected_chars_boxes,
            use_char_ann=self.use_char_ann,
            size=self.size,
            mode=self.mode,
            char_num_classes=self.char_num_classes,
        )

    def __repr__(self):
        s = self.__class__.__name__ + "("
        s += "num_char_boxes={}, ".format(len(self.chars_boxes))
        s += "image_width={}, ".format(self.size[0])
        s += "image_height={})".format(self.size[1])
        return s


def shrink_poly(poly, shrink):
    # shrink ratio
    R = shrink
    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]),
        )
    # 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 shrink_rect(poly, shrink):
    xmin = min(poly[:, 0])
    xmax = max(poly[:, 0])
    ymin = min(poly[:, 1])
    ymax = max(poly[:, 1])
    # assert xmax > xmin and ymax > ymin
    xc = (xmax + xmin) / 2
    yc = (ymax + ymin) / 2
    w = xmax - xmin
    h = ymax - ymin
    sxmin = xc - w / 2 * shrink
    sxmax = xc + w / 2 * shrink
    symin = yc - h / 2 * shrink
    symax = yc + h / 2 * shrink
    return np.array([sxmin, symin, sxmax, symin, sxmax, symax, sxmin, symax]).reshape(
        (4, 2)
    )


def is_poly_inbox(poly, height, width):
    min_x = min(poly[:, 0])
    min_y = min(poly[:, 1])
    max_x = max(poly[:, 0])
    max_y = max(poly[:, 1])
    if (max_x < 0 and max_y < 0) or (min_x > width and min_y > height):
        return False
    else:
        return True