# 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
    out_folder = "forwarded/" + model_str + "/tracking_data"
  os.makedirs(out_folder, exist_ok=True)
  out_filename = out_folder + "/" + tag + ".txt"
  with open(out_filename, "w") as f:
    start = 1 if start_time_at_1 else 0
    for t, tracks_t in enumerate(tracks, start):  # TODO this works?
      for track in tracks_t:
        if add_masks:
          print(t, track.track_id, track.class_,
                *track.mask['size'], track.mask['counts'].decode(encoding='UTF-8'), file=f)
        else:
          # TODO: class id to class name mapping atm harcoded
          if track.class_ == 1:
            class_str = "Car"
          elif track.class_ == 2:
            class_str = "Pedestrian"
          else:
            assert False, ("unknown class id", track.class_)
          print(t, track.track_id, class_str, -1, -1, -1, *track.box, -1, -1, -1, -1, -1, -1, -1, track.score, file=f)


def export_detections_for_sequence(tag, boxes, scores, reids, classes, masks, model_str, epoch, add_masks, out_folder=""):
  if out_folder == "":
    out_folder = "forwarded/" + model_str + "/detections/" + str(epoch)
  os.makedirs(out_folder, exist_ok=True)
  out_filename = out_folder + "/" + tag + ".txt"
  with open(out_filename, "w") as f:
    t = 0
    for boxes_t, scores_t, reids_t, classes_t, masks_t in zip(boxes, scores, reids, classes, masks):
      for box, score, reid, class_, mask in zip(boxes_t, scores_t, reids_t, classes_t, masks_t):

# 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

    out_folder = "forwarded/" + model_str + "/tracking_data"
  os.makedirs(out_folder, exist_ok=True)
  out_filename = out_folder + "/" + tag + ".txt"
  with open(out_filename, "w") as f:
    start = 1 if start_time_at_1 else 0
    for t, tracks_t in enumerate(tracks, start):  # TODO this works?
      for track in tracks_t:
        if add_masks:
          print(t, track.track_id, track.class_,
                *track.mask['size'], track.mask['counts'].decode(encoding='UTF-8'), file=f)
        else:
          # TODO: class id to class name mapping atm harcoded
          if track.class_ == 1:
            class_str = "Car"
          elif track.class_ == 2:
            class_str = "Pedestrian"
          else:
            assert False, ("unknown class id", track.class_)
          print(t, track.track_id, class_str, -1, -1, -1, *track.box, -1, -1, -1, -1, -1, -1, -1, track.score, file=f)


def export_detections_for_sequence(tag, boxes, scores, reids, classes, masks, model_str, epoch, add_masks, out_folder=""):
  if out_folder == "":
    out_folder = "forwarded/" + model_str + "/detections/" + str(epoch)
  os.makedirs(out_folder, exist_ok=True)
  out_filename = out_folder + "/" + tag + ".txt"
  with open(out_filename, "w") as f:
    t = 0
    for boxes_t, scores_t, reids_t, classes_t, masks_t in zip(boxes, scores, reids, classes, masks):
      for box, score, reid, class_, mask in zip(boxes_t, scores_t, reids_t, classes_t, masks_t):

        if add_masks: