From 784d431c7b0d8102905ec309875a1bc55b54b068 Mon Sep 17 00:00:00 2001 From: Christian Liebhardt Date: Fri, 12 Jul 2024 18:44:00 +0200 Subject: [PATCH] Another attempt to improve results if at the corner of the image, a staff from a different piece is visible --- homr/staff_detection.py | 2 ++ homr/staff_parsing.py | 22 ++++++++++++++++++---- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/homr/staff_detection.py b/homr/staff_detection.py index 78f0405..2e9244f 100644 --- a/homr/staff_detection.py +++ b/homr/staff_detection.py @@ -327,9 +327,11 @@ def find_staff_anchors( ] else: adjacent = [ + center_symbol.move_to_x_horizontal_by(-10), center_symbol.move_to_x_horizontal_by(-5), center_symbol, center_symbol.move_to_x_horizontal_by(5), + center_symbol.move_to_x_horizontal_by(10), ] for symbol in adjacent: estimated_unit_size = round(symbol.size[1] / (constants.number_of_lines_on_a_staff - 1)) diff --git a/homr/staff_parsing.py b/homr/staff_parsing.py index 30b607f..824dd3f 100644 --- a/homr/staff_parsing.py +++ b/homr/staff_parsing.py @@ -26,14 +26,28 @@ def _have_all_the_same_number_of_staffs(staffs: list[MultiStaff]) -> bool: return True -def _ensure_same_number_of_staffs(staffs: list[MultiStaff]) -> list[MultiStaff]: +def _is_close_to_image_top_or_bottom(staff: MultiStaff, predictions: InputPredictions) -> bool: + tolerance = 50 + closest_distance_to_top_or_bottom = [ + min(s.min_x, predictions.preprocessed.shape[0] - s.max_x) for s in staff.staffs + ] + return min(closest_distance_to_top_or_bottom) < tolerance + + +def _ensure_same_number_of_staffs( + staffs: list[MultiStaff], predictions: InputPredictions +) -> list[MultiStaff]: if _have_all_the_same_number_of_staffs(staffs): return staffs if len(staffs) > 2: # noqa: PLR2004 - if _have_all_the_same_number_of_staffs(staffs[1:]): + if _is_close_to_image_top_or_bottom( + staffs[0], predictions + ) and _have_all_the_same_number_of_staffs(staffs[1:]): eprint("Removing first system from all voices, as it has a different number of staffs") return staffs[1:] - if _have_all_the_same_number_of_staffs(staffs[:-1]): + if _is_close_to_image_top_or_bottom( + staffs[-1], predictions + ) and _have_all_the_same_number_of_staffs(staffs[:-1]): eprint("Removing last system from all voices, as it has a different number of staffs") return staffs[:-1] result: list[MultiStaff] = [] @@ -395,7 +409,7 @@ def parse_staffs( Dewarps each staff and then runs it through an algorithm which extracts the rhythm and pitch information. """ - staffs = _ensure_same_number_of_staffs(staffs) + staffs = _ensure_same_number_of_staffs(staffs, predictions) # For simplicity we call every staff in a multi staff a voice, # even if it's part of a grand staff. number_of_voices = _get_number_of_voices(staffs)