Sort mixed PDF music parts by instrument, page by page. The script reads each page header with EasyOCR, matches Spanish and English instrument names (including common OCR errors), and writes single-page PDFs into instrument folders. Pages without a match go to No_Detectado for manual review.
I built this to organize a real music library containing hundreds of scores. It is a practical OCR automation tool, not a trained instrument-recognition model.
flowchart LR
A[PDFs in partituras_originales] --> B[Render pages with PyMuPDF]
B --> C[Crop header and preprocess with OpenCV]
C --> D[EasyOCR in Spanish and English]
D --> E[Normalize text and match instrument names]
E -->|matched| F[Instrument folder: one PDF per page]
E -->|no match| G[Full-page OCR retry]
G -->|matched| F
G -->|no match| H[No_Detectado and debug header image]
The matcher combines explicit instrument aliases with simple prefix and substring rules. debug_ocr/ keeps header crops so a person can inspect OCR results. The fallback OCR pass reads the full page when the header has no match.
Use Python 3.11. A first EasyOCR run downloads its language models and needs an internet connection.
python -m venv .venv
# Activate .venv using your shell's normal command.
python -m pip install -r requirements.txt
mkdir partituras_originales
python pdf_classifier.pyPlace one or more PDFs anywhere under partituras_originales/ before running. The script creates partituras_ordenadas/<instrument>/ for matched pages and partituras_ordenadas/No_Detectado/ for unmatched pages. It does not remove existing results automatically. Review that folder and the corresponding images in debug_ocr/ after each run. The included run.bat is an optional Windows shortcut.
pdf_classifier.py— OCR, text normalization, matching and PDF splitting.limpiar_carpetas.py/clean.bat— optional cleanup tools; inspect their effect before running.requirements.txt— Python dependencies.partituras_originales/— local input, ignored by Git.partituras_ordenadas/,debug_ocr/— local output, ignored by Git.
The rules depend on an instrument label being readable on the page. Poor scans, unusual fonts, handwritten parts and ambiguous labels may be unmatched or misclassified. Review outputs before using them as a final catalog. Files with the same base name in different input folders can produce the same output name, so give such files distinct names before a run. OCR and PDF rendering can take time on large collections. The script is designed for local processing and does not upload scores.
The existing repository is released under the included CC0 1.0 license. EasyOCR, OpenCV and other dependencies retain their own licenses.