# Path: moonlight/engine.py
# CONFIG = tf.ConfigProto(device_count={'GPU': 0})
# class OMREngine(object):
#   def __init__(self, glyph_classifier_fn=None):
#   def run(self, input_pngs, output_notesequence=False):
#   def process_image(self, image_arr, process_structure=True):
#   def _get_page(self, feed_dict=None, process_structure=True):
# def _nested_ndarrays_to_tensors(data):
# 
# Path: moonlight/staves/staffline_distance.py
# _MAX_STAFFLINE_DISTANCE_THICKNESS_VALUE = 256
# _MIN_STAFFLINE_DISTANCE_SCORE = 10000
# _MAX_ALLOWED_UNIQUE_STAFFLINE_DISTANCES = 3
# _STAFFLINE_DISTANCE_INVALIDATE_DISTANCE = 1
# _STAFFLINE_THICKNESS_INVALIDATE_DISTANCE = 1
# _PEAK_CUTOFF = 0.5
# def _single_peak(values, relative_cutoff, minval, invalidate_distance):
# def _estimate_staffline_distance(columns, lengths):
#     def do_filter_peaks():
# def _estimate_staffline_thickness(columns, values, lengths, staffline_distance):
#     def do_estimate():
# def estimate_staffline_distance_and_thickness(image, threshold=127):
# 
# Path: moonlight/staves/staffline_extractor.py
# DEFAULT_TARGET_HEIGHT = 18
# DEFAULT_NUM_SECTIONS = 19
# DEFAULT_STAFFLINE_DISTANCE_MULTIPLE = 3
#   STAFF = 0
#   POSITION = 1
#   Y = 2
#   X = 3
# class Axes(enum.IntEnum):
# class StafflineExtractor(object):
# class StafflinePatchExtractor(object):
# def get_staffline(y_position, extracted_staff_arr):
# def y_position_to_index(y_position, num_stafflines):
#   def __init__(self,
#                image,
#                staves,
#                target_height=DEFAULT_TARGET_HEIGHT,
#                num_sections=DEFAULT_NUM_SECTIONS,
#                staffline_distance_multiple=DEFAULT_STAFFLINE_DISTANCE_MULTIPLE):
#   def extract_staves(self):
#     def do_extract_staves():
#       def extract_staff(i):
#         def extract_staffline_by_index(j):
#   def _extract_staffline(self, staff_y, staffline_distance, staffline_num):
#   def _get_resized_width(self, staffline_distance):
#   def _get_staffline_window_size(self, staffline_distance):
#   def __init__(self,
#                num_sections=DEFAULT_NUM_SECTIONS,
#                patch_height=15,
#                patch_width=12,
#                run_options=None):
#   def extract_staff_strip(self, filename, staff_index, y_position):
#   def extract_staff_patch(self, filename, staff_index, y_position, image_x):
#   def page_patch_iterator(self, filename):
#     def generator():
#   def make_patch_id(filename, staff_index, y_position, image_x):


import json
import os.path
import random
import subprocess
import sys
import apache_beam as beam
import numpy as np
import tensorflow as tf
from apache_beam.metrics import Metrics
from google.protobuf import text_format
from moonlight import engine
from moonlight.protobuf import musicscore_pb2
from moonlight.staves import staffline_distance
from moonlight.staves import staffline_extractor
    png_contents, staff_message = item
    staff_message = musicscore_pb2.Staff.FromString(staff_message)
    with tf.Session(graph=self.omr.graph) as sess:
      # Load the image, then feed it in to apply noise.
      # Randomly rotate the image and apply noise, then dump it back out as a
      # PNG.
      # TODO(ringw): Expose a way to pass in the image contents to the main
      # OMR TF graph.
      img = tf.to_float(tf.image.decode_png(png_contents))
      # Collapse the RGB channels, if any. No-op for a monochrome PNG.
      img = tf.reduce_mean(img[:, :, :3], axis=2)[:, :, None]
      # Fix the stafflines being #999.
      img = tf.clip_by_value(img * 2. - 255., 0., 255.)
      img = self.noise_fn(img)
      # Get a 2D uint8 image array for OMR.
      noisy_image = sess.run(
          tf.cast(tf.clip_by_value(img, 0, 255)[:, :, 0], tf.uint8))
      # Run OMR staffline extraction and staffline distance estimation. The
      # stafflines are used to get patches from the generated image.
      stafflines, image_staffline_distance = sess.run(
          [
              self.omr.glyph_classifier.staffline_extractor.extract_staves(),
              self.omr.structure.staff_detector.staffline_distance[0]
          ],
          feed_dict={self.omr.image: noisy_image})
    if stafflines.shape[0] != 1:
      raise ValueError('Image should have one detected staff, got shape: ' +
                       str(stafflines.shape))
    positive_example_count = 0
    negative_example_whitelist = np.ones(

# Path: moonlight/engine.py
# CONFIG = tf.ConfigProto(device_count={'GPU': 0})
# class OMREngine(object):
#   def __init__(self, glyph_classifier_fn=None):
#   def run(self, input_pngs, output_notesequence=False):
#   def process_image(self, image_arr, process_structure=True):
#   def _get_page(self, feed_dict=None, process_structure=True):
# def _nested_ndarrays_to_tensors(data):
# 
# Path: moonlight/staves/staffline_distance.py
# _MAX_STAFFLINE_DISTANCE_THICKNESS_VALUE = 256
# _MIN_STAFFLINE_DISTANCE_SCORE = 10000
# _MAX_ALLOWED_UNIQUE_STAFFLINE_DISTANCES = 3
# _STAFFLINE_DISTANCE_INVALIDATE_DISTANCE = 1
# _STAFFLINE_THICKNESS_INVALIDATE_DISTANCE = 1
# _PEAK_CUTOFF = 0.5
# def _single_peak(values, relative_cutoff, minval, invalidate_distance):
# def _estimate_staffline_distance(columns, lengths):
#     def do_filter_peaks():
# def _estimate_staffline_thickness(columns, values, lengths, staffline_distance):
#     def do_estimate():
# def estimate_staffline_distance_and_thickness(image, threshold=127):
# 
# Path: moonlight/staves/staffline_extractor.py
# DEFAULT_TARGET_HEIGHT = 18
# DEFAULT_NUM_SECTIONS = 19
# DEFAULT_STAFFLINE_DISTANCE_MULTIPLE = 3
#   STAFF = 0
#   POSITION = 1
#   Y = 2
#   X = 3
# class Axes(enum.IntEnum):
# class StafflineExtractor(object):
# class StafflinePatchExtractor(object):
# def get_staffline(y_position, extracted_staff_arr):
# def y_position_to_index(y_position, num_stafflines):
#   def __init__(self,
#                image,
#                staves,
#                target_height=DEFAULT_TARGET_HEIGHT,
#                num_sections=DEFAULT_NUM_SECTIONS,
#                staffline_distance_multiple=DEFAULT_STAFFLINE_DISTANCE_MULTIPLE):
#   def extract_staves(self):
#     def do_extract_staves():
#       def extract_staff(i):
#         def extract_staffline_by_index(j):
#   def _extract_staffline(self, staff_y, staffline_distance, staffline_num):
#   def _get_resized_width(self, staffline_distance):
#   def _get_staffline_window_size(self, staffline_distance):
#   def __init__(self,
#                num_sections=DEFAULT_NUM_SECTIONS,
#                patch_height=15,
#                patch_width=12,
#                run_options=None):
#   def extract_staff_strip(self, filename, staff_index, y_position):
#   def extract_staff_patch(self, filename, staff_index, y_position, image_x):
#   def page_patch_iterator(self, filename):
#     def generator():
#   def make_patch_id(filename, staff_index, y_position, image_x):


# Path: moonlight/training/generation/generation.py
import json
import os.path
import random
import subprocess
import sys
import apache_beam as beam
import numpy as np
import tensorflow as tf
from apache_beam.metrics import Metrics
from google.protobuf import text_format
from moonlight import engine
from moonlight.protobuf import musicscore_pb2
from moonlight.staves import staffline_distance
from moonlight.staves import staffline_extractor

    png_contents, staff_message = item
    staff_message = musicscore_pb2.Staff.FromString(staff_message)
    with tf.Session(graph=self.omr.graph) as sess:
      # Load the image, then feed it in to apply noise.
      # Randomly rotate the image and apply noise, then dump it back out as a
      # PNG.
      # TODO(ringw): Expose a way to pass in the image contents to the main
      # OMR TF graph.
      img = tf.to_float(tf.image.decode_png(png_contents))
      # Collapse the RGB channels, if any. No-op for a monochrome PNG.
      img = tf.reduce_mean(img[:, :, :3], axis=2)[:, :, None]
      # Fix the stafflines being #999.
      img = tf.clip_by_value(img * 2. - 255., 0., 255.)
      img = self.noise_fn(img)
      # Get a 2D uint8 image array for OMR.
      noisy_image = sess.run(
          tf.cast(tf.clip_by_value(img, 0, 255)[:, :, 0], tf.uint8))
      # Run OMR staffline extraction and staffline distance estimation. The
      # stafflines are used to get patches from the generated image.
      stafflines, image_staffline_distance = sess.run(
          [
              self.omr.glyph_classifier.staffline_extractor.extract_staves(),
              self.omr.structure.staff_detector.staffline_distance[0]
          ],
          feed_dict={self.omr.image: noisy_image})
    if stafflines.shape[0] != 1:
      raise ValueError('Image should have one detected staff, got shape: ' +
                       str(stafflines.shape))
    positive_example_count = 0
    negative_example_whitelist = np.ones(

        (stafflines.shape[staffline_extractor.Axes.POSITION],