-
Notifications
You must be signed in to change notification settings - Fork 1
/
hocr_ocr_result_line.py
67 lines (47 loc) · 1.89 KB
/
hocr_ocr_result_line.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
from dataclasses import dataclass, field
from PySide6 import QtCore
from hocr_data import HOCR_Data
from hocr_ocr_result_word import HOCR_OCRResultWord
@dataclass
class HOCR_OCRResultLine(HOCR_Data):
x_size = 0.0
baseline = (0.0, 0)
words: list[HOCR_OCRResultWord] = field(default_factory=list)
def split_title_data(self, line):
if line:
self.title_data = line['title']
super().split_title_data()
data_lines = self.title_data.split('; ')
for data_line in data_lines:
tokens = data_line.split(' ')
if tokens[0] == 'x_size':
self.x_size = float(tokens[1])
elif tokens[0] == 'baseline':
self.baseline = (float(tokens[1]), int(tokens[2]))
for span in line.find_all('span', class_='ocrx_word'):
word = HOCR_OCRResultWord()
word.split_title_data(span)
self.words.append(word)
def translate(self, distance: QtCore.QPoint):
'''Translate coordinates by a distance'''
self.bbox = self.bbox.translated(distance)
for word in self.words:
word.translate(distance)
def write(self, file: QtCore.QDataStream):
file.writeQVariant(self.bbox)
file.writeFloat(self.x_size)
file.writeFloat(self.baseline[0])
file.writeInt16(self.baseline[1])
file.writeInt16(len(self.words))
for word in self.words:
word.write(file)
def read(self, file: QtCore.QDataStream):
self.bbox = file.readQVariant()
self.x_size = file.readFloat()
baseline = (file.readFloat(), file.readInt16())
self.baseline = baseline
words_count = file.readInt16()
for w in range(words_count):
word = HOCR_OCRResultWord()
word.read(file)
self.words.append(word)