Skip to content

Commit

Permalink
Less noisy output
Browse files Browse the repository at this point in the history
  • Loading branch information
liebharc committed Jul 3, 2024
1 parent 8a4d309 commit bfb405b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
9 changes: 7 additions & 2 deletions training/convert_grandstaff.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
from homr.staff_dewarping import warp_image_randomly
from homr.staff_parsing import add_image_into_tr_omr_canvas
from homr.type_definitions import NDArray
from training.music_xml import music_xml_to_semantic
from training.musescore_svg import SvgValidationError
from training.music_xml import MusicXmlValidationError, music_xml_to_semantic

script_location = os.path.dirname(os.path.realpath(__file__))
git_root = Path(script_location).parent.absolute()
Expand Down Expand Up @@ -205,7 +206,9 @@ def _music_xml_to_semantic(path: str, basename: str) -> tuple[str | None, str |
return basename + "_upper.semantic", basename + "_lower.semantic"


def _convert_file(path: Path, ony_recreate_semantic_files: bool = False) -> list[str]:
def _convert_file( # noqa: PLR0911
path: Path, ony_recreate_semantic_files: bool = False
) -> list[str]:
try:
basename = str(path).replace(".krn", "")
image_file = str(path).replace(".krn", ".jpg")
Expand Down Expand Up @@ -238,6 +241,8 @@ def _convert_file(path: Path, ony_recreate_semantic_files: bool = False) -> list
+ ","
+ str(Path(lower_semantic).relative_to(git_root)),
]
except (SvgValidationError, MusicXmlValidationError):
return []
except Exception as e:
eprint("Failed to convert ", path, e)
return []
Expand Down
6 changes: 5 additions & 1 deletion training/musescore_svg.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
from homr.simple_logging import eprint


class SvgValidationError(Exception):
pass


class SvgRectangle:
def __init__(self, x: int, y: int, width: int, height: int):
self.x = x
Expand Down Expand Up @@ -150,7 +154,7 @@ def get_position_information_from_svg(svg_file: str) -> SvgMusicFile:
number_of_clefs += 1
combined = _combine_staff_lines_and_bar_lines(staff_lines, bar_lines)
if len(combined) != number_of_clefs:
raise ValueError(
raise SvgValidationError(
f"Number of clefs {number_of_clefs} does not match the number of staffs {len(combined)}"
)
return SvgMusicFile(svg_file, width, height, combined)
6 changes: 5 additions & 1 deletion training/music_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
from homr.simple_logging import eprint


class MusicXmlValidationError(Exception):
pass


class SymbolWithPosition:
def __init__(self, position: int, symbol: str) -> None:
self.position = position
Expand Down Expand Up @@ -92,7 +96,7 @@ def append_clefs(self, clefs: list[str]) -> None:
raise ValueError("Number of clefs changed")
for staff, clef in enumerate(clefs):
if not any(symbol.symbol == clef for symbol in self.current_measure.staffs[staff]):
raise ValueError("Clef changed")
raise MusicXmlValidationError("Clef changed")
return
self.staffs = [[] for _ in range(len(clefs))]
measure = SemanticMeasure(len(clefs))
Expand Down

0 comments on commit bfb405b

Please sign in to comment.