Ejemplo n.º 1
0
 def testDummy(self):
     # Create a single staff, and a single vertical which is the correct height
     # of a stem. The vertical has x = 20 and goes from
     struct = structure.Structure(
         staff_detector=staves_base.ComputedStaves(
             staves=[[[10, 50], [90, 50]]],
             staffline_distance=[12],
             staffline_thickness=2,
             staves_interpolated_y=[[50] * 100]),
         beams=beams.ComputedBeams(np.zeros((0, 2, 2))),
         verticals=verticals.ComputedVerticals(
             lines=[[[20, 38], [20, 38 + 12 * 4]]]),
         connected_components=components.ComputedComponents([]))
     stems = stems_module.Stems(struct)
     # Create a Page with Glyphs.
     input_page = musicscore_pb2.Page(system=[
         musicscore_pb2.StaffSystem(staff=[
             musicscore_pb2.Staff(
                 staffline_distance=12,
                 center_line=[
                     musicscore_pb2.Point(x=10, y=50),
                     musicscore_pb2.Point(x=90, y=50)
                 ],
                 glyph=[
                     # Cannot have a stem because it's a flat.
                     musicscore_pb2.Glyph(type=musicscore_pb2.Glyph.FLAT,
                                          x=15,
                                          y_position=-1),
                     # On the right side of the stem, the correct distance away.
                     musicscore_pb2.Glyph(
                         type=musicscore_pb2.Glyph.NOTEHEAD_FILLED,
                         x=25,
                         y_position=-1),
                     # Too high for the stem.
                     musicscore_pb2.Glyph(
                         type=musicscore_pb2.Glyph.NOTEHEAD_FILLED,
                         x=25,
                         y_position=4),
                     # Too far right from the stem.
                     musicscore_pb2.Glyph(
                         type=musicscore_pb2.Glyph.NOTEHEAD_FILLED,
                         x=35,
                         y_position=-1),
                 ])
         ])
     ])
     page = stems.apply(input_page)
     self.assertFalse(page.system[0].staff[0].glyph[0].HasField("stem"))
     self.assertTrue(page.system[0].staff[0].glyph[1].HasField("stem"))
     self.assertEqual(
         page.system[0].staff[0].glyph[1].stem,
         musicscore_pb2.LineSegment(start=Point(x=20, y=38),
                                    end=Point(x=20, y=38 + 12 * 4)))
     self.assertFalse(page.system[0].staff[0].glyph[2].HasField("stem"))
     self.assertFalse(page.system[0].staff[0].glyph[3].HasField("stem"))
Ejemplo n.º 2
0
    def _get_page(self, feed_dict=None, process_structure=True):
        """Returns the Page holding Glyphs for the page.

    Args:
      feed_dict: The feed dict to use for the TensorFlow graph. The image must
        be fed in.
      process_structure: If True, run the page_processors, which add staff
        locations and other structural information. If False, return a Page
        containing only Glyphs for each staff.

    Returns:
      A Page message holding Staff protos which have location information and
          detected Glyphs.
    """
        # If structure is given, output all structural information in addition to
        # the Page message.
        structure_data = (_nested_ndarrays_to_tensors(self.structure.data)
                          if self.structure else [])
        structure_data, glyphs = self.session.run(
            [structure_data,
             self.glyph_classifier.get_detected_glyphs()],
            feed_dict=feed_dict)
        computed_staves, computed_beams, computed_verticals, computed_components = (
            structure_data)

        # Construct and return a computed Structure.
        computed_structure = structure_module.Structure(
            staves_base.ComputedStaves(*computed_staves),
            beams.ComputedBeams(*computed_beams),
            verticals.ComputedVerticals(*computed_verticals),
            components.ComputedComponents(*computed_components))

        # The Page without staff location information.
        labels_page = self.glyph_classifier.glyph_predictions_to_page(glyphs)

        # Process the Page using the computed structure.
        if process_structure:
            processed_page = page_processors.process(
                labels_page, computed_structure,
                self.glyph_classifier.staffline_extractor)
        else:
            processed_page = labels_page
        return processed_page
Ejemplo n.º 3
0
    def compute(self, session=None, image=None):
        """Computes the structure.

    If the staves are already `ComputedStaves` and the verticals are already
    `ComputedVerticals`, returns `self`. Otherwise, runs staff detection and/or
    verticals detection in the TensorFlow `session`.

    Args:
      session: The TensorFlow session to use instead of the default session.
      image: If non-None, fed as the value of `self.staff_detector.image`.

    Returns:
      A computed `Structure` object. `staff_detector` and `verticals` hold NumPy
          arrays with the result of the TensorFlow graph.
    """

        if isinstance(self.staff_detector, staves_base.ComputedStaves):
            staff_detector_data = []
        else:
            staff_detector_data = self.staff_detector.data
        if isinstance(self.beams, beams_module.ComputedBeams):
            beams_data = []
        else:
            beams_data = self.beams.data
        if isinstance(self.verticals, verticals_module.ComputedVerticals):
            verticals_data = []
        else:
            verticals_data = self.verticals.data
        if isinstance(self.connected_components,
                      components_module.ComputedComponents):
            components_data = []
        else:
            components_data = self.connected_components.data
        if not (staff_detector_data or beams_data or verticals_data
                or components_data):
            return self

        if not session:
            session = tf.get_default_session()
        if image is not None:
            feed_dict = {self.staff_detector.image: image}
        else:
            feed_dict = {}
        staff_detector_data, beams_data, verticals_data, components_data = (
            session.run([
                staff_detector_data, beams_data, verticals_data,
                components_data
            ],
                        feed_dict=feed_dict))
        staff_detector_data = staff_detector_data or self.staff_detector.data
        staff_detector = staves_base.ComputedStaves(*staff_detector_data)
        beams_data = beams_data or self.beams.data
        beams = beams_module.ComputedBeams(*beams_data)
        verticals_data = verticals_data or self.verticals.data
        verticals = verticals_module.ComputedVerticals(*verticals_data)
        connected_components = components_module.ConnectedComponents(
            *components_data)
        return Structure(staff_detector,
                         beams,
                         verticals,
                         connected_components,
                         image=image)
Ejemplo n.º 4
0
    def testDummy(self):
        # Create a single staff, and a single vertical which is the correct height
        # of a stem. The vertical has x = 20 and goes from
        struct = structure.Structure(
            staff_detector=staves_base.ComputedStaves(
                staves=[[[10, 50], [90, 50]], [[11, 150], [91, 150]],
                        [[10, 250], [90, 250]], [[10, 350], [90, 350]]],
                staffline_distance=[12] * 4,
                staffline_thickness=2,
                staves_interpolated_y=[[50] * 100, [150] * 100, [250] * 100,
                                       [350] * 100]),
            beams=beams.ComputedBeams(np.zeros((0, 2, 2))),
            connected_components=components.ComputedComponents(np.zeros(
                (0, 5))),
            verticals=verticals.ComputedVerticals(lines=[
                # Joins the first 2 staves.
                [[10, 50 - 12 * 2], [10, 150 + 12 * 2]],
                # Another barline, too close to the first one.
                [[12, 50 - 12 * 2], [12, 150 + 12 * 2]],
                # This barline is far enough, because the second barline was
                # skipped.
                [[13, 50 - 12 * 2], [13, 150 + 12 * 2]],
                # Single staff barlines are skipped.
                [[30, 50 - 12 * 2], [30, 50 + 12 * 2]],
                [[31, 150 - 12 * 2], [31, 150 + 12 * 2]],
                # Too close to a stem.
                [[70, 50 - 12 * 2], [70, 50 + 12 * 2]],
                # Too short.
                [[90, 50 - 12 * 2], [90, 50 + 12 * 2]],
                # Another barline which is kept.
                [[90, 50 - 12 * 2], [90, 150 + 12 * 2]],
                # Staff 1 has no barlines.
                # Staff 2 has 2 barlines.
                [[11, 350 - 12 * 2], [11, 350 + 12 * 2]],
                [[90, 350 - 12 * 2], [90, 350 + 12 * 2]],
            ]))
        barlines = barlines_module.Barlines(struct, close_barline_threshold=3)
        # Create a Page with Glyphs.
        input_page = musicscore_pb2.Page(system=[
            musicscore_pb2.StaffSystem(staff=[
                musicscore_pb2.Staff(
                    staffline_distance=12,
                    center_line=[
                        musicscore_pb2.Point(x=10, y=50),
                        musicscore_pb2.Point(x=90, y=50)
                    ],
                    glyph=[
                        # Stem is close to the last vertical on the first staff, so
                        # a barline will not be detected there.
                        musicscore_pb2.Glyph(
                            type=musicscore_pb2.Glyph.NOTEHEAD_FILLED,
                            x=60,
                            y_position=2,
                            stem=musicscore_pb2.LineSegment(
                                start=musicscore_pb2.Point(x=72, y=40),
                                end=musicscore_pb2.Point(x=72, y=80))),
                    ]),
                musicscore_pb2.Staff(staffline_distance=12,
                                     center_line=[
                                         musicscore_pb2.Point(x=10, y=150),
                                         musicscore_pb2.Point(x=90, y=150)
                                     ]),
                musicscore_pb2.Staff(staffline_distance=12,
                                     center_line=[
                                         musicscore_pb2.Point(x=10, y=250),
                                         musicscore_pb2.Point(x=90, y=250)
                                     ]),
                musicscore_pb2.Staff(staffline_distance=12,
                                     center_line=[
                                         musicscore_pb2.Point(x=10, y=350),
                                         musicscore_pb2.Point(x=90, y=350)
                                     ]),
            ])
        ])
        page = barlines.apply(input_page)
        self.assertEqual(3, len(page.system))

        self.assertEqual(2, len(page.system[0].staff))
        self.assertItemsEqual([10, 13, 90],
                              (bar.x for bar in page.system[0].bar))

        self.assertEqual(1, len(page.system[1].staff))
        self.assertEqual(0, len(page.system[1].bar))

        self.assertEqual(1, len(page.system[2].staff))
        self.assertEqual(2, len(page.system[2].bar))
        self.assertItemsEqual([11, 90], (bar.x for bar in page.system[2].bar))