Visualize Measure Bounding Boxes

This notebook shows how to visualize bounding boxes of musical measures on sheet music images for the data set that is described in the following short paper:

Frank Zalkow, Angel Villar Corrales, TJ Tsai, Vlora Arifi-Müller, and Meinard Müller
Tools for Semi-Automatic Bounding Box Annotation of Musical Measures in Sheet Music
In Demos and Late Breaking News of the International Society for Music Information Retrieval Conference (ISMIR), 2019. PDF Details

In [1]:
import os
import pandas as pd
from PIL import Image, ImageDraw

directory_dataset = '..'

def draw_bounding_boxes(piece_name, page_idx=0, directory_dataset=''):
    directory_img = os.path.join(directory_dataset, piece_name, 'img')
    page_name = sorted(os.listdir(directory_img))[page_idx]
    img_path = os.path.join(directory_dataset, piece_name, 'img', page_name)
    csv_path = os.path.join(directory_dataset, piece_name, f'{piece_name}.csv')
    
    df = pd.read_csv(csv_path)
    df_page = df[df['Image'] == os.path.basename(img_path)]
    bboxes = df_page[['X', 'Y', 'Height', 'Width']].values

    img = Image.open(img_path).convert('RGB')
    draw = ImageDraw.Draw(img)
    
    for x, y, h, w in bboxes:
        x1, y1 = x, y
        x2, y2 = (x + w), (y + h)
        points = (x1, y1), (x2, y1), (x2, y2), (x1, y2), (x1, y1)
        draw.line(points, fill='red', width=2)
    
    return img

display(draw_bounding_boxes('Beethoven_Op026-01', directory_dataset=directory_dataset))
display(draw_bounding_boxes('Wagner_WWV086A', page_idx=2, directory_dataset=directory_dataset))
Acknowledgment: This notebook was created by Ken Leidal, Frank Zalkow, and Meinard Müller.