Metadata-Version: 2.4
Name: homr
Version: 0.7.1
Summary: End-to-end Optical Music Recognition (OMR) system build on top of vision transformers.
License-Expression: AGPL-3.0
License-File: LICENSE
License-File: LICENSE_FAQ.md
Author: Christian Liebhardt
Author-email: christian.liebhardt@arcor.de
Requires-Python: <3.16,>=3.11
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Programming Language :: Python :: 3.15
Provides-Extra: cpu
Provides-Extra: cuda
Requires-Dist: Pillow (>=12.1.1,<13)
Requires-Dist: numpy (>=2.4.2,<3)
Requires-Dist: onnxruntime (>=1.24.1,<2) ; extra == "cpu"
Requires-Dist: onnxruntime-gpu[cuda,cudnn] (>=1.24.1,<2) ; extra == "cuda"
Requires-Dist: opencv-python-headless (>=4.13.0.92,<5)
Requires-Dist: pypdfium2 (>=4,<6)
Requires-Dist: rapidocr (>=3.7.0,<4)
Requires-Dist: requests (>=2.32.5,<3)
Requires-Dist: types-Pillow (>=10.2.0.20240822,<11)
Requires-Dist: typing_extensions (>=4.15.0,<5)
Description-Content-Type: text/markdown

# homr

homr is an Optical Music Recognition (OMR) software designed to transform camera pictures or PDFs of sheet music into
machine-readable MusicXML format. The resulting [MusicXML](https://www.w3.org/2021/06/musicxml40/) files can be further
processed using tools such as [musescore](https://musescore.com/).

For a quick try, visit our online demo at [homr.site](https://homr.site) or [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/liebharc/homr/blob/main/colab.ipynb)

You might also want to check out [Andromr](https://github.com/aicelen/Andromr), an Android app for optical music recognition using homr.

## Prerequisites

- Python 3.11 or 3.12
- Poetry or UV
- Optional
  - NVIDIA GPU with CUDA 12.1
  - AMD GPU with ROCm 7.0

## Getting started (uv)

The easiest way to get started is using `uvx` (`uv` must be installed). Select an inference backend:
- CPU: `uvx --from 'homr[cpu]' homr <image>`
- NVIDIA CUDA: `uvx --from 'homr[cuda]' homr <image>`
- AMD ROCm: `uvx --python 3.12 --from homr --with https://repo.radeon.com/rocm/manylinux/rocm-rel-7.0/onnxruntime_rocm-1.22.1-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl homr <image>`

- The resulting MusicXML file will be saved in the same directory as the input image

## Getting started (poetry)

- Clone the repository
- Install dependencies for:
  - Inference: `poetry install --only main --extras cpu`
  - Development: `poetry install --extras cpu`
  - If using GPU, replace `--extras cpu` with `--extras cuda` (NVIDIA) or `--with rocm` (AMD)
- Run the program using `poetry run homr <image>`
- The resulting MusicXML file will be saved in the same directory as the input image

## Run on multiple images
- Just add your image to the command, for example: `poetry run homr <image_1> <image_2>`
- This produces one file named `merged_image_1.musicxml`
- To combine MusicXML files, you can use [relieur](https://github.com/papoteur-mga/relieur)

## Example

The example below provides an overview of the current performance of the implementation. While some errors are present
in the output, the overall structure remains accurate.

|                                          Original Image                                           |                                                                               homr Result                                                                                |
| :-----------------------------------------------------------------------------------------------: | :----------------------------------------------------------------------------------------------------------------------------------------------------------------------: |
| <img src="https://github.com/BreezeWhite/oemer/blob/main/figures/tabi.jpg?raw=true" width="400" > | <img src="https://github.com/liebharc/homr/blob/main/figures/tabi.svg?raw=true" alt="Go to https://github.com/liebharc/homr if this image isn't displayed" width="400" > |

The homr result is obtained by processing the [homr output](figures/tabi.musicxml) and rendering it
with [musescore](https://musescore.com/).

## Preprocessing

Depending on the input, preprocessing the image can improve the results. A common step is to remove the background from scanned images.

## Limitations

See the symbol table in [Ideas for Contributors](IDEAS_FOR_CONTRIBUTORS.md#support-more-symbols) for what homr reads and what it doesn't, e.g. octave lines, octave clefs, beams and glissandos.

## Technical Details

homr uses a two-stage pipeline: **segmentation** for structural analysis followed by **semantic symbol recognition** via transformer models.

### Stage 1: Image Segmentation and Structural Analysis

homr employs UNet-based segmentation models (adapted from [oemer](https://github.com/BreezeWhite/oemer)) to extract structural components from the sheet music image:

- **Staff lines and symbols**: Detected via trained segmentation networks that identify:
  - Staff line fragments
  - Note heads
  - Stems and rests
  - Bar lines
  - Clefs and key signatures

The segmentation process generates bounding boxes for each detected element. These predictions serve as inputs for the staff detection algorithm.

### Stage 2: Staff Detection and Merging

Using the segmentation outputs, homr constructs staffs through the following steps:

1. **Staff Anchor Detection**: The algorithm identifies "staff anchors" (clefs and bar lines) that serve as reference points for accurate staff localization, even when symbols partially obscure staff lines.

2. **Unit Size Estimation**: For each staff, the algorithm calculates the "unit size" (distance between staff lines). This accommodates camera perspective variations and non-uniform staff spacing.

3. **Staff Reconstruction**: Around each anchor, five staff lines are located and the remaining staff structure is reconstructed using the estimated unit size.

4. **Grand Staff Merging**: Braces and brackets are identified to merge related staffs, supporting:
   - Grand staffs (piano, organ)
   - Multiple voices on a single staff
   - Mixed instrument groups

### Stage 3: Semantic Symbol Recognition via Transformer

Each staff is dewarped (perspective-corrected) and passed through a transformer-based model (based on [Polyphonic-TrOMR](https://github.com/NetEase/Polyphonic-TrOMR)) that performs **end-to-end symbol sequence recognition**. The model outputs:

- **Rhythm symbols**: Note durations, rests, and tuplet information
- **Pitch information**: Absolute pitch values with accidentals (sharps, flats, naturals)
- **Articulation marks**: Accents, staccato, tenuto, and slur markers
- **Performance annotations**: Dynamic expressions and other musical notation

The transformer model generates these predictions in sequence, processing the dewarped staff image to understand the spatial and temporal relationships between musical symbols.

**Note**: The transformer output provides the sequence of symbols but does not include explicit positional information (horizontal or vertical coordinates). However, the model computes the center of attention as a byproduct of the attention mechanism, which is used to estimate the focus point on the staff image. homr maps this point back to the input image and writes it as a comment into each note of the MusicXML output, e.g. `<!-- imgpos: 45, 231 -->`. These are rough positions: they point at or near the symbol, but aren't pixel-exact.

### Stage 4: MusicXML Output

The symbol sequence is converted into MusicXML format and saved to disk. The resulting file can be processed with tools like [musescore](https://musescore.com/) or [relieur](https://github.com/papoteur-mga/relieur) (for multi-image combinations).

## Related Projects

- [AEU-OMR](https://github.com/mtalhabalci/aeu-omr): an OMR model for printed Turkish makam music notation, based on homr.

## License

homr is licensed under AGPL-3.0. For commercial use and the licenses of the training data behind the model weights, see [LICENSE_FAQ.md](LICENSE_FAQ.md).

## Citation

If you use this code in your research work, please cite [oemer](https://github.com/BreezeWhite/oemer)
and [Polyphonic-TrOMR](https://github.com/NetEase/Polyphonic-TrOMR).

## Name

The name "homr" stands for Homer's Optical Music Recognition (OMR), leaving the interpretation of "Homer" to the user's
discretion, whether referring to the ancient poet [Homer](https://en.wikipedia.org/wiki/Homer) or the iconic character
from [The Simpsons](https://en.wikipedia.org/wiki/The_Simpsons).

## Thanks

This project builds upon previous work, including:

- The segmentation models of [oemer](https://github.com/BreezeWhite/oemer)
- The transformer model of [Polyphonic-TrOMR](https://github.com/NetEase/Polyphonic-TrOMR)
- The starter template provided by [Benjamin Roland](https://github.com/Parici75/python-poetry-bootstrap)

