File size: 1,248 Bytes
52a9452
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from .quad import Quad


class TextLines:
    '''
    The abstrct class of text lines in an input image for scene text detection and recognition.
    Input:
        lines:
            - text: the text content of a text instance.
              poly: the quadrangle-box of the text instance.
              charboxes: the quadrangle-box of the characters inside the corresponding text instance.
    '''
    def __init__(self, lines, with_charboxes=True):
        self.texts = []
        quads = []
        self.charboxes = []
        for line in lines:
            self.texts.append(line['text'])
            quads.append(line['poly'])
            if with_charboxes and 'charboxes' in line:
                self.charboxes.append(Quad(line['charboxes']))
        self.with_charboxes = len(self.charboxes) > 0
        self.quads = Quad(quads)
        self._rects = None

    def __iter__(self):
        for text, quad in zip(self.texts, self.quads):
            yield(text, quad)

    @property
    def rects(self):
        if self._rects is None:
            self._rects = self.quads.rectify()
        return self._rects

    def __len__(self):
        return len(self.texts)

    def char_count(self):
        return sum([len(t) for t in self.texts])