Exemple #1
0
 def testVerticalLines_singleColumn(self):
     image = np.zeros((20, 4), bool)
     image[5:10, 0] = True
     image[11:15, 1] = True
     image[:5, 3] = True
     staff_detector = testing.FakeStaves(tf.constant(
         np.where(image, 0, 255), tf.uint8),
                                         staves_t=None,
                                         staffline_distance_t=[1],
                                         staffline_thickness_t=0.5)
     verticals_detector = verticals.ColumnBasedVerticals(
         staff_detector,
         max_gap_staffline_distance=[1],
         min_length_staffline_distance=3)
     lines_t = verticals_detector.lines
     with self.test_session():
         lines = [line.tolist() for line in lines_t.eval()]
     # Start is dilated by 1 pixel since the start is actually in this column.
     self.assertIn([[0, 4], [0, 14]], lines)
     # Only the end is contained in this column, so it is dilated but the start
     # is not.
     self.assertIn([[1, 5], [1, 15]], lines)
     self.assertNotIn([[2, 4], [2, 14]], lines)
     self.assertNotIn([[2, 5], [2, 15]], lines)
     self.assertIn([[3, 0], [3, 5]], lines)
     # Out of bounds.
     self.assertNotIn([[4, 0], [4, 5]], lines)
Exemple #2
0
 def test_staves_interpolated_y_empty(self):
     with self.test_session():
         self.assertAllEqual(
             testing.FakeStaves(
                 tf.zeros([50, 25]),
                 tf.zeros([0, 2, 2],
                          np.int32)).staves_interpolated_y.eval().shape,
             [0, 25])
Exemple #3
0
 def test_staves_interpolated_y_staves_dont_extend_to_edge(self):
     staff = tf.constant(np.array([[[5, 10], [12, 8]]], np.int32))
     with self.test_session():
         # The y values should extend past the endpoints to the edge of the image,
         # and should be equal to the y value at the nearest endpoint.
         self.assertAllEqual(
             testing.FakeStaves(tf.zeros([50, 15]),
                                staff).staves_interpolated_y[0].eval(),
             [10, 10, 10, 10, 10, 10, 10, 9, 9, 9, 9, 8, 8, 8, 8])
Exemple #4
0
  def testGetPage_x_scale(self):
    # Random staffline images matching the dimensions of PREDICTIONS.
    dummy_stafflines = np.random.random((2, 3, 5, 6))
    classifier = glyphs_testing.DummyGlyphClassifier(glyphs_testing.PREDICTIONS)
    image = np.random.randint(0, 255, (30, 20), dtype=np.uint8)
    staves = staves_testing.FakeStaves(
        image_t=image,
        staves_t=np.asarray([[[0, 10], [19, 10]], [[0, 20], [19, 20]]],
                            np.int32),
        staffline_distance_t=np.asarray([5, 20], np.int32),
        staffline_thickness_t=np.asarray(1, np.int32))
    structure = structure_module.create_structure(image,
                                                  lambda unused_image: staves)

    class DummyStafflineExtractor(object):
      """A placeholder for StafflineExtractor.

      It only contains the constants necessary to scale the x coordinates.
      """
      staffline_distance_multiple = 2
      target_height = 10

    omr = engine.OMREngine(lambda _: classifier)
    page = omr.process_image(
        # Feed in a dummy image. It doesn't matter because FakeStaves has
        # hard-coded staff values.
        np.random.randint(0, 255, (100, 100)),
        process_structure=False)
    page = staff_processor.StaffProcessor(structure,
                                          DummyStafflineExtractor()).apply(page)
    self.assertEqual(len(page.system[0].staff), 2)
    # The first staff has a staffline distance of 5.
    # The extracted staffline slices have an original height of
    # staffline_distance * staffline_distance_multiple (10), which equals
    # target_height here, so there is no scaling.
    self.assertEqual(
        musicscore_pb2.Staff(glyph=page.system[0].staff[0].glyph),
        glyphs_testing.GLYPHS_PAGE.system[0].staff[0])
    # Glyphs in the second staff have a scaled x coordinate.
    self.assertEqual(
        len(page.system[0].staff[1].glyph),
        len(glyphs_testing.GLYPHS_PAGE.system[0].staff[1].glyph))
    for glyph in glyphs_testing.GLYPHS_PAGE.system[0].staff[1].glyph:
      expected_glyph = copy.deepcopy(glyph)
      # The second staff has a staffline distance of 20. The extracted staffline
      # slice would be 4 times the size of the scaled staffline, so x
      # coordinates are scaled by 4. Also, the glyphs may be in a different
      # order.
      expected_glyph.x *= 4
      self.assertIn(expected_glyph, page.system[0].staff[1].glyph)
Exemple #5
0
    def test_staves_interpolated_y(self):
        # Test staff center line interpolation.
        # The sequence of (x, y) points always starts at x = 0 and ends at
        # x = width - 1.
        staff = tf.constant(
            np.array(
                [[[0, 10], [5, 5], [11, 0], [15, 10], [20, 20], [23, 49]]],
                np.int32))

        with self.test_session():
            line_y = testing.FakeStaves(tf.zeros([50, 24]),
                                        staff).staves_interpolated_y[0].eval()
        self.assertEquals(list(line_y), [
            10, 9, 8, 7, 6, 5, 4, 3, 3, 2, 1, 0, 2, 5, 8, 10, 12, 14, 16, 18,
            20, 30, 39, 49
        ])