-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhocr_ocr_result_block.py
129 lines (99 loc) · 4.44 KB
/
hocr_ocr_result_block.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
from dataclasses import dataclass, field
from iso639 import Lang
from PySide6 import QtCore, QtGui
from document_helper import DocumentHelper
from hocr_data import HOCR_Data
from hocr_ocr_result_paragraph import HOCR_OCRResultParagraph
from hocr_ocr_result_word import HOCR_OCRResultWord
from bs4 import Tag
@dataclass
class HOCR_OCRResultBlock(HOCR_Data):
image_size: QtCore.QSize = QtCore.QSize()
ppi: float = 0.0
language: Lang = Lang('English')
font: QtGui.QFont = QtGui.QFont()
foreground_color = QtGui.QColorConstants.Svg.black
paragraphs: list[HOCR_OCRResultParagraph] = field(default_factory=list)
def __post_init__(self):
self.font = QtGui.QFontDatabase().systemFont(QtGui.QFontDatabase().SystemFont.GeneralFont)
def split_title_data(self, block):
if block.name:
self.title_data = block['title']
super().split_title_data()
for p in block.find_all('p', class_='ocr_par'):
paragraph = HOCR_OCRResultParagraph()
paragraph.split_title_data(p)
self.paragraphs.append(paragraph)
# Store box properties from box editor
#self.properties = properties
def write(self, file: QtCore.QDataStream) -> None:
file.writeInt16(len(self.paragraphs))
for paragraph in self.paragraphs:
paragraph.write(file)
file.writeQVariant(self.image_size)
file.writeFloat(self.ppi)
def read(self, file: QtCore.QDataStream):
paragraphs_count = file.readInt16()
for p in range(paragraphs_count):
paragraph = HOCR_OCRResultParagraph()
paragraph.read(file)
self.image_size = file.readQVariant()
self.ppi = file.readFloat()
def get_words(self) -> list[HOCR_OCRResultWord]:
'''Get list of words'''
words: list[HOCR_OCRResultWord] = []
for p in self.paragraphs:
for l in p.lines:
words += l.words
return words
def get_avg_confidence(self) -> float:
confidence = 0
avg_confidence = confidence
words = self.get_words()
if words:
for word in words:
confidence += word.confidence
avg_confidence = confidence / len(words)
return avg_confidence
def get_text(self, diagnostics: bool = False, remove_hyphens=False) -> QtGui.QTextDocument:
'''Get text as QTextDocument'''
document = QtGui.QTextDocument()
cursor = QtGui.QTextCursor(document)
format = QtGui.QTextCharFormat()
for p, paragraph in enumerate(self.paragraphs):
if paragraph.lines:
block_format = QtGui.QTextBlockFormat()
# block_format.setBottomMargin(15.0)
cursor.setBlockFormat(block_format)
height = self.ppi * paragraph.get_avg_height() * 3
format.setFont(self.font)
format.setFontPointSize(round(height))
format.setForeground(self.foreground_color)
cursor.setCharFormat(format)
# cursor.insertBlock(block_format)
# cursor.deletePreviousChar()
for l, line in enumerate(paragraph.lines):
for w, word in enumerate(line.words):
if diagnostics:
if word.confidence < 90:
format.setBackground(QtGui.QColor(255, 0, 0, int(1 - (word.confidence / 100)) * 200))
cursor.setCharFormat(format)
cursor.insertText(word.text)
format.clearBackground()
cursor.setCharFormat(format)
if w < (len(line.words) - 1):
cursor.insertText(' ')
if l < (len(paragraph.lines) - 1):
# cursor.insertText(' ')
cursor.insertText('\n')
if p < (len(self.paragraphs) - 1):
cursor.insertText('\n\n')
if remove_hyphens:
document_helper = DocumentHelper(document, self.language.pt1)
document = document_helper.remove_hyphens()
return document
def translate(self, distance: QtCore.QPoint):
'''Translate coordinates by a distance (ignore block itself)'''
# self.bbox.translated(distance)
for paragraph in self.paragraphs:
paragraph.translate(distance)