def _extract_staffline()

in moonlight/staves/staffline_extractor.py [0:0]


  def _extract_staffline(self, staff_y, staffline_distance, staffline_num):
    """Extracts a single staffline from a single staff."""
    # Use a float image on a 0.0-1.0 scale for classification.
    image_shape = tf.shape(self.float_image)
    height = image_shape[0]  # Can't unpack a tensor object.
    width = image_shape[1]

    # Calculate the height of the extracted staffline in the unscaled image.
    staff_window = self._get_staffline_window_size(staffline_distance)

    # Calculate the coordinates to extract for the window.
    # Note: tf.meshgrid uses xs before ys by default, but y is the 0th axis
    # for indexing.
    xs, ys = tf.meshgrid(
        tf.range(width),
        tf.range(staff_window) - (staff_window // 2))
    # ys are centered around 0. Add the staff_y, repeating along the
    # 0th axis.
    ys += tf.tile(staff_y[None, :], [staff_window, 1])
    # Add the offset for the staff line within the staff.
    # Round up in case the y position is not whole (in between staff lines with
    # an odd staffline distance). This puts the center of the staff space closer
    # to the center of the window.
    ys += tf.cast(
        tf.ceil(tf.truediv(staffline_num * staffline_distance, 2)), tf.int32)

    invalid = tf.logical_not((0 <= ys) & (ys < height) & (0 <= xs)
                             & (xs < width))
    # Use a coordinate of (0, 0) for pixels outside of the original image.
    # We will then fill in those pixels with zeros.
    ys = tf.where(invalid, tf.zeros_like(ys), ys)
    xs = tf.where(invalid, tf.zeros_like(xs), xs)
    inds = tf.stack([ys, xs], axis=2)
    staffline_image = tf.gather_nd(self.float_image, inds)
    # Fill the pixels outside of the original image with zeros.
    staffline_image = tf.where(invalid, tf.zeros_like(staffline_image),
                               staffline_image)

    # Calculate the proportional width after scaling the height to
    # self.target_height.
    resized_width = self._get_resized_width(staffline_distance)
    # Use area resizing because we expect the output to be smaller.
    # Add extra axes, because we only have 1 image and 1 channel.
    staffline_image = tf.image.resize_area(
        staffline_image[None, :, :,
                        None], [self.target_height, resized_width])[0, :, :, 0]
    # Pad to make the width consistent with target_width.
    staffline_image = tf.pad(staffline_image,
                             [[0, 0], [0, self.target_width - resized_width]])
    return staffline_image