Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions pero_ocr/char_confidences.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import numpy as np


def greedy_filtration(line_probs, chars):
idx = -1
text = ""
last_char = None
probs = []

for i, (char_index, max_prob) in enumerate(zip(np.argmax(line_probs, axis=1), np.max(line_probs, axis=1))):
if char_index != (line_probs.shape[1] - 1):
if (last_char != chars[char_index]):
text = text + chars[char_index]
if char_index != len(chars) - 1:
if last_char != chars[char_index]:
text += chars[char_index]
probs.append([max_prob])
idx += 1
last_char = chars[char_index]
Expand All @@ -21,6 +20,6 @@ def greedy_filtration(line_probs, chars):
last_char = None

for i, item in enumerate(probs):
probs[i] = sum(probs[i]) / len(probs[i])
probs[i] = sum(item) / len(item)

return text, probs
14 changes: 6 additions & 8 deletions pero_ocr/transcription_io.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
def save_transcriptions(path, transcriptions):
with open(path, 'w') as f:
with open(path, 'w', encoding='utf-8') as f:
for key in transcriptions:
f.write('{} {}\n'.format(key, transcriptions[key]))


def load_transcriptions(path):
transcriptions = {}

with open(path, "r") as f:
for line_no, line in enumerate(f):
if len(line) == 0:
with open(path, "r", encoding='utf-8') as f:
for line_no, line in enumerate(f, start=1):
line = line.strip()
if not line:
continue

try:
Expand All @@ -24,8 +25,5 @@ def load_transcriptions(path):

def parse_transcription_line(line):
image_id, transcription = line.split(" ", maxsplit=1)

if transcription[-1] == '\n':
transcription = transcription[:-1]

transcription = transcription.rstrip()
return image_id, transcription