in moonlight/glyphs/convolutional.py [0:0]
def _build_detected_glyphs(self, predictions_arr):
"""Takes the convolutional output ndarray and builds the individual glyphs.
At each staff and y position, looks for short runs of the same detected
glyph, and then outputs a single glyph at the x coordinate of the center of
the run.
Args:
predictions_arr: A NumPy array with the result of `staffline_predictions`.
Shape (num_staves, num_stafflines, width).
Returns:
A 2D array of the glyph coordinates. Shape (num_glyphs, 4) with columns
corresponding to base.GlyphsTensorColumns.
"""
glyphs = []
num_staves, num_stafflines, width = predictions_arr.shape
for staff in range(num_staves):
for staffline in range(num_stafflines):
y_position = num_stafflines // 2 - staffline
run_start = -1
run_value = musicscore_pb2.Glyph.NONE
for x in range(width + 1):
if x < width:
value = predictions_arr[staff, staffline, x]
if x == width or value != run_value:
if run_value > musicscore_pb2.Glyph.NONE:
# Process the current run if it is at least run_min_length pixels.
if x - run_start >= self.run_min_length:
glyph_center_x = (run_start + x) // 2
glyphs.append(
self._create_glyph_arr(staff, y_position, glyph_center_x,
run_value))
run_value = value
run_start = x
# Convert to a 2D array.
glyphs = np.asarray(glyphs, np.int32)
return np.reshape(glyphs, (-1, 4))