def display_image(image): return imp.display_image(image)
def analyze_staff(img_wo_lines, staff, index, avg_staff_spacing, avg_staff_distance): print("Analyzing staff %s" % (index + 1)) staff_image_top = staff[0][0] - avg_staff_distance//2 staff_image_bot = staff[-1][-1] + avg_staff_distance//2 staff_image = img_wo_lines[staff_image_top: staff_image_bot] staff_copy = staff_image.copy() img_vert_lines = open_image_vertically(staff_image, avg_staff_spacing) vertical_lines = find_regions(img_vert_lines, pixel_span=1, eight_way=False)[1] img_vert_objects, vertical_regions = \ find_vertical_regions(staff_image, img_vert_lines, avg_staff_spacing, pixel_span=1, eight_way=False) bar_lines = get_bar_lines(vertical_regions, vertical_lines, staff) remove_bar_lines([staff_image, img_vert_lines], bar_lines, []) img_vert_objects, vertical_regions = \ find_vertical_regions(staff_image, img_vert_lines, avg_staff_spacing, pixel_span=3) clefs = get_clefs(staff_image, vertical_regions, bar_lines) remove_clefs([staff_image, img_vert_objects, img_vert_lines], clefs, vertical_regions) endings = get_endings(staff_image, vertical_regions, staff[0][0] - staff_image_top) remove_endings([staff_image, img_vert_objects, img_vert_lines], endings, vertical_regions) regions = find_regions(staff_image, pixel_span=1, eight_way=False)[1] dots = find_dots(staff_image, regions, avg_staff_spacing) remove_dots([staff_image], dots, [regions]) img_vert_lines = imo.open_image_vertically(staff_image, avg_staff_spacing, 3.5) img_vert_objects, vertical_regions = \ find_vertical_regions(staff_image, img_vert_lines, avg_staff_spacing, pixel_span=2, eight_way=True) img_vert_lines = imo.open_image_vertically(staff_image, avg_staff_spacing, 1.5) img_vert_objects, vertical_regions = \ find_vertical_regions(imo.image_subtract(staff_image, img_vert_objects), img_vert_lines, avg_staff_spacing, pixel_span=1, eight_way=False) accidentals = find_accidentals(img_vert_objects, vertical_regions) remove_accidentals([staff_image], accidentals, None) img_vert_objects, vertical_regions = \ find_vertical_regions(staff_image, img_vert_lines, avg_staff_spacing, pixel_span=2) time_signatures = get_time_signatures(staff_image, vertical_regions, bar_lines, [clef[0] for clef in clefs]) remove_time_signatures([staff_image, img_vert_objects, img_vert_lines], time_signatures, vertical_regions) img_vert_objects, vertical_regions = \ find_vertical_regions(staff_image, img_vert_lines, avg_staff_spacing, pixel_span=4, eight_way=True) notes = find_vertical_notes(img_vert_objects, vertical_regions, staff, avg_staff_spacing, avg_staff_distance) remove_vertical_notes([staff_image, img_vert_objects, img_vert_lines], notes, vertical_regions) remove_ledgers([staff_image], regions, staff, avg_staff_distance) regions = find_regions(staff_image, pixel_span=2)[1] rests = find_rests(staff_image, regions, bar_lines) remove_rests([staff_image], [rest[0] for rest in rests], [regions]) regions = find_regions(staff_image, pixel_span=3)[1] whole_notes = find_whole_notes(staff_image, regions, bar_lines, [clef[0] for clef in clefs], [time_signature[0] for time_signature in time_signatures], staff, avg_staff_spacing, avg_staff_distance) remove_whole_notes([staff_image], [note[0] for note in whole_notes], [regions]) export_data(index, bar_lines, clefs, time_signatures, endings, notes, accidentals, dots, whole_notes, rests, staff, avg_staff_spacing, avg_staff_distance) imp.display_image(staff_copy)