# Path: forwarding/tracking/Util_tracking.py
# def track_single_sequence(tracker_options, boxes, scores, reids, classes, masks, optical_flow=None):
# def tracker_per_class(tracker_options, boxes, scores, reids, classes, masks, class_to_track, start_track_id,
#                       optical_flow=None):
# def tracker_per_class_new_reid(tracker_options, boxes, scores, reids, classes, masks, class_to_track, start_track_id,
#                                optical_flow=None):
# def calculate_association_similarities(detections_t, last_tracks, flow_tm1_t, tracker_options):
# def warp_flow(mask_as_rle, flow):
# def _warp(img, flow):
# def bbox_iou(box1, box2):
# def warp_box(box, flow):
#   I = max(x1_min - x0_max, 0) * max(y1_min - y0_max, 0)
#   U = (x1_max - x0_min) * (y1_max - y0_min)


import glob
import os
import numpy as np
  import pickle
    import png
from pycocotools import mask as cocomask
from forwarding.tracking.Util_tracking import TrackElement




def make_disjoint(tracks, strategy):
  def get_max_y(obj):
    _, y, _, h = cocomask.toBbox(obj.mask)
    return y + h

  for frame, objects in enumerate(tracks):
    if len(objects) == 0:
      continue
    if strategy == "y_pos":
      objects_sorted = sorted(objects, key=lambda x: get_max_y(x), reverse=True)
    elif strategy == "score":
      objects_sorted = sorted(objects, key=lambda x: x.score, reverse=True)
    else:
      assert False, "Unknown mask_disjoint_strategy"
    objects_disjoint = [objects_sorted[0]]
    used_pixels = objects_sorted[0].mask
    for obj in objects_sorted[1:]:
      new_mask = obj.mask
      if cocomask.area(cocomask.merge([used_pixels, obj.mask], intersect=True)) > 0.0:
        obj_mask_decoded = cocomask.decode(obj.mask)
        used_pixels_decoded = cocomask.decode(used_pixels)
        obj_mask_decoded[np.where(used_pixels_decoded > 0)] = 0
        new_mask = cocomask.encode(obj_mask_decoded)
      used_pixels = cocomask.merge([used_pixels, obj.mask], intersect=False)

# Path: forwarding/tracking/Util_tracking.py
# def track_single_sequence(tracker_options, boxes, scores, reids, classes, masks, optical_flow=None):
# def tracker_per_class(tracker_options, boxes, scores, reids, classes, masks, class_to_track, start_track_id,
#                       optical_flow=None):
# def tracker_per_class_new_reid(tracker_options, boxes, scores, reids, classes, masks, class_to_track, start_track_id,
#                                optical_flow=None):
# def calculate_association_similarities(detections_t, last_tracks, flow_tm1_t, tracker_options):
# def warp_flow(mask_as_rle, flow):
# def _warp(img, flow):
# def bbox_iou(box1, box2):
# def warp_box(box, flow):
#   I = max(x1_min - x0_max, 0) * max(y1_min - y0_max, 0)
#   U = (x1_max - x0_min) * (y1_max - y0_min)


# Path: forwarding/tracking/TrackingForwarder_util.py
import glob
import os
import numpy as np
  import pickle
    import png
from pycocotools import mask as cocomask
from forwarding.tracking.Util_tracking import TrackElement





def make_disjoint(tracks, strategy):
  def get_max_y(obj):
    _, y, _, h = cocomask.toBbox(obj.mask)
    return y + h

  for frame, objects in enumerate(tracks):
    if len(objects) == 0:
      continue
    if strategy == "y_pos":
      objects_sorted = sorted(objects, key=lambda x: get_max_y(x), reverse=True)
    elif strategy == "score":
      objects_sorted = sorted(objects, key=lambda x: x.score, reverse=True)
    else:
      assert False, "Unknown mask_disjoint_strategy"
    objects_disjoint = [objects_sorted[0]]
    used_pixels = objects_sorted[0].mask
    for obj in objects_sorted[1:]:
      new_mask = obj.mask
      if cocomask.area(cocomask.merge([used_pixels, obj.mask], intersect=True)) > 0.0:
        obj_mask_decoded = cocomask.decode(obj.mask)
        used_pixels_decoded = cocomask.decode(used_pixels)
        obj_mask_decoded[np.where(used_pixels_decoded > 0)] = 0
        new_mask = cocomask.encode(obj_mask_decoded)
      used_pixels = cocomask.merge([used_pixels, obj.mask], intersect=False)

      objects_disjoint.append(TrackElement(box=obj.box, track_id=obj.track_id, class_=obj.class_, score=obj.score,