Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved file handling, error handling, and runtime optimization in transcription parser #51

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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