def compute()

in moonlight/structure/__init__.py [0: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)