Skip to content

Commit

Permalink
Another attempt to improve results if at the corner of the image, a s…
Browse files Browse the repository at this point in the history
…taff from a different piece is visible
liebharc committed Jul 12, 2024
1 parent a4be1f8 commit 784d431
Showing 2 changed files with 20 additions and 4 deletions.
2 changes: 2 additions & 0 deletions homr/staff_detection.py
Original file line number Diff line number Diff line change
@@ -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))
22 changes: 18 additions & 4 deletions homr/staff_parsing.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 784d431

Please sign in to comment.