def track_min_objects(video_info):

    previous_frame = None
    previous_num_obj = -1

    tracked_video = []

    frame_id = 0
    min_rects = []
    min_frame_id = None
    min_num_obj = None
    for frame_info in video_info:
        if (min_num_obj is None) & (len(frame_info.rects) > 0):
            min_num_obj = len(frame_info.rects)
            min_frame_id = frame_id
        if (len(frame_info.rects) < min_num_obj) & (len(frame_info.rects) > 0):
            min_num_obj = len(frame_info.rects)
            min_frame_id = frame_id
        frame_id = frame_id + 1
    min_rects = multiclass_rectangle.duplicate_rects(
        video_info[min_frame_id].rects)
    ("Min num object video:%d" % min_num_obj)

    for frame_info in video_info:
        print("Tracking Frame Nr: %d" % frame_info.frame)
        print("Len Rects Frame: %d" % len(frame_info.rects))
        current_frame = frame.Frame_Info()
        current_frame = frame_info.duplicate()
        current_frame.rects = []
        if previous_frame is not None:
            print("Min num object video:%d" % min_num_obj)
            print("Len Previous Rects Frame: %d" % len(previous_frame.rects))
            for rect in previous_frame.rects:
                print(len(current_frame.rects))
                if len(current_frame.rects) <= min_num_obj:
                    current_rect = multiclass_rectangle.pop_max_iou(
                        frame_info.rects, rect)
                    current_rect.load_trackID(rect.trackID)
                    current_frame.append_labeled_rect(current_rect)
        else:
            trackID = 1
            for rect in min_rects:
                if len(current_frame.rects) < min_num_obj:
                    current_rect = multiclass_rectangle.pop_max_iou(
                        frame_info.rects, rect)
                    current_rect.load_trackID(trackID)
                    current_frame.append_labeled_rect(current_rect)
                    trackID = trackID + 1

        previous_frame = current_frame.duplicate()
        previous_frame.rects = multiclass_rectangle.duplicate_rects(
            current_frame.rects)

        print("Current Frame obj:%d" % len(current_frame.rects))
        tracked_video.insert(len(tracked_video), current_frame)

    return tracked_video
def track_min_objects(video_info):

    previous_frame= None
    previous_num_obj=-1

    tracked_video=[]

    frame_id=0
    min_rects=[]
    min_frame_id=None
    min_num_obj=None
    for frame_info in video_info:
        if (min_num_obj is None) & (len(frame_info.rects) >0):
            min_num_obj = len(frame_info.rects)
            min_frame_id=frame_id
        if (len(frame_info.rects) < min_num_obj ) & (len(frame_info.rects) >0):
            min_num_obj = len(frame_info.rects)
            min_frame_id=frame_id
        frame_id=frame_id+1
    min_rects = multiclass_rectangle.duplicate_rects(video_info[min_frame_id].rects)
    print "Min num object video:%d"%min_num_obj

    for frame_info in video_info:
        print "Tracking Frame Nr: %d"%frame_info.frame
        print "Len Rects Frame: %d"%len(frame_info.rects)
        current_frame = frame.Frame_Info()
        current_frame=frame_info.duplicate()
        current_frame.rects=[]
        if previous_frame is not None:
            print "Min num object video:%d"%min_num_obj
            print "Len Previous Rects Frame: %d"%len(previous_frame.rects)
            for rect in previous_frame.rects:
                print len(current_frame.rects)
                if len(current_frame.rects)<=min_num_obj:               
                    current_rect = multiclass_rectangle.pop_max_iou(frame_info.rects,rect)
                    current_rect.load_trackID(rect.trackID)
                    current_frame.append_labeled_rect(current_rect)
        else:
            trackID=1
            for rect in min_rects:
                if len(current_frame.rects)<min_num_obj:               
                    current_rect = multiclass_rectangle.pop_max_iou(frame_info.rects,rect)
                    current_rect.load_trackID(trackID)
                    current_frame.append_labeled_rect(current_rect)
                    trackID=trackID+1

        previous_frame=current_frame.duplicate()
        previous_frame.rects= multiclass_rectangle.duplicate_rects(current_frame.rects)

        print "Current Frame obj:%d"%len(current_frame.rects)
        tracked_video.insert(len(tracked_video), current_frame)

    return tracked_video
def save_best_iou(val_bbox, output_bbox):

    progress = progressbar.ProgressBar(widgets=[
        progressbar.Bar('=', '[', ']'), ' ',
        progressbar.Percentage(), ' ',
        progressbar.ETA()
    ])
    count_best_bbox = 0
    len_val_bbox = len(val_bbox)
    len_output_bbox = len(output_bbox)
    count_missing_boxes = 0
    with open("best_iou.txt", 'a') as d:
        for i in progress(range(0, len(val_bbox))):
            for rect in val_bbox[i].rects:
                if (len(output_bbox[i].rects) > 0):
                    selected = multiclass_rectangle.pop_max_iou(
                        output_bbox[i].rects, rect)
                    count_best_bbox = count_best_bbox + 1
                    d.write(
                        str(val_bbox[i].frame) + ' ' + str(rect.label_chall) +
                        ' 0.5 ' + str(selected.x1) + ' ' + str(selected.y1) +
                        ' ' + str(selected.x2) + ' ' + str(selected.y2) +
                        os.linesep)
                else:
                    count_missing_boxes = count_missing_boxes + 1
def track_objects(video_info):

    previous_frame = None
    previous_num_obj = -1

    tracked_video = []

    for frame_info in video_info:
        print("Tracking Frame Nr: %d" % frame_info.frame)
        print("Len Rects Frame: %d" % len(frame_info.rects))
        current_frame = frame.Frame_Info()
        current_frame = frame_info.duplicate()
        current_frame.rects = []
        if previous_frame is not None:
            print("Len Previous Rects Frame: %d" % len(previous_frame.rects))
            for rect in previous_frame.rects:
                print(len(current_frame.rects))
                current_rect = multiclass_rectangle.pop_max_iou(
                    frame_info.rects, rect)
                current_rect.load_trackID(rect.trackID)
                current_frame.append_labeled_rect(current_rect)
        else:
            trackID = 1
            picked_rect = Utils_Tensorbox.NMS(frame_info.rects)
            for rect in picked_rect:
                current_rect = rect.duplicate()
                current_rect.load_trackID(trackID)
                current_frame.append_labeled_rect(current_rect)
                trackID = trackID + 1

        previous_frame = current_frame.duplicate()
        previous_frame.rects = multiclass_rectangle.duplicate_rects(
            current_frame.rects)

        print("Current Frame obj:%d" % len(current_frame.rects))
        tracked_video.insert(len(tracked_video), current_frame)

    return tracked_video
def track_objects(video_info):

    previous_frame= None
    previous_num_obj=-1

    tracked_video=[]


    for frame_info in video_info:
        print "Tracking Frame Nr: %d"%frame_info.frame
        print "Len Rects Frame: %d"%len(frame_info.rects)
        current_frame = frame.Frame_Info()
        current_frame=frame_info.duplicate()
        current_frame.rects=[]
        if previous_frame is not None:
            print "Len Previous Rects Frame: %d"%len(previous_frame.rects)
            for rect in previous_frame.rects:
                print len(current_frame.rects)
                current_rect = multiclass_rectangle.pop_max_iou(frame_info.rects,rect)
                current_rect.load_trackID(rect.trackID)
                current_frame.append_labeled_rect(current_rect)
        else:
            trackID=1
            picked_rect=Utils_Tensorbox.NMS(frame_info.rects)
            for rect in picked_rect:       
                current_rect = rect.duplicate()
                current_rect.load_trackID(trackID)
                current_frame.append_labeled_rect(current_rect)
                trackID=trackID+1

        previous_frame=current_frame.duplicate()
        previous_frame.rects= multiclass_rectangle.duplicate_rects(current_frame.rects)

        print "Current Frame obj:%d"%len(current_frame.rects)
        tracked_video.insert(len(tracked_video), current_frame)

    return tracked_video
def save_best_iou(val_bbox, output_bbox):

    progress = progressbar.ProgressBar(widgets=[
        progressbar.Bar('=', '[', ']'), ' ',
        progressbar.Percentage(), ' ',
        progressbar.ETA()
    ])
    count_best_bbox = 0
    len_val_bbox = len(val_bbox)
    len_output_bbox = len(output_bbox)
    count_missing_boxes = 0
    with open("best_iou.txt", 'a') as d:
        for i in progress(range(0, len(val_bbox))):
            for rect in val_bbox[i].rects:
                if (len(output_bbox[i].rects) > 0):
                    selected = multiclass_rectangle.pop_max_iou(
                        output_bbox[i].rects, rect)
                    count_best_bbox = count_best_bbox + 1
                    d.write(
                        str(val_bbox[i].frame) + ' ' + str(rect.label_chall) +
                        ' 0.5 ' + str(selected.x1) + ' ' + str(selected.y1) +
                        ' ' + str(selected.x2) + ' ' + str(selected.y2) +
                        os.linesep)
                else:
                    count_missing_boxes = count_missing_boxes + 1
    print "Total Frame Number: " + str(len_val_bbox)
    print "Total Output Bounding Boxes: " + str(len_output_bbox)
    print "Total Best Bounding Boxes: " + str(count_best_bbox)
    print "Total Missing Bounding Boxes: " + str(count_missing_boxes)
    print "Total False Positive Bounding Boxes: " + str(len_output_bbox -
                                                        count_best_bbox)
    print "BBox/Frame Number: " + str(
        float(count_best_bbox) / float(len_val_bbox))
    print "Missing BBox/Frame Number: " + str(
        float(float(count_missing_boxes) / float(len_val_bbox)))
    print "False Positive BBox/Frame Number: " + str(
        float(float(len_output_bbox - count_best_bbox) / float(len_val_bbox)))
def save_best_iou(val_bbox, output_bbox):

    progress = progressbar.ProgressBar(widgets=[progressbar.Bar('=', '[', ']'), ' ',progressbar.Percentage(), ' ',progressbar.ETA()])
    count_best_bbox=0
    len_val_bbox=len(val_bbox)
    len_output_bbox=len(output_bbox)
    count_missing_boxes=0
    with open("best_iou.txt", 'a') as d:
        for i in progress(range(0, len(val_bbox))):
            for rect in val_bbox[i].rects:
                if(len(output_bbox[i].rects)>0):
                    selected=multiclass_rectangle.pop_max_iou(output_bbox[i].rects,rect)
                    count_best_bbox=count_best_bbox+1
                    d.write(str(val_bbox[i].frame)+' '+str(rect.label_chall)+ ' 0.5 '+str(selected.x1)+' '+str(selected.y1)+' '+str(selected.x2)+' '+str(selected.y2) + os.linesep)
                else:
                    count_missing_boxes=count_missing_boxes+1
    print "Total Frame Number: "+ str(len_val_bbox) 
    print "Total Output Bounding Boxes: "+ str(len_output_bbox) 
    print "Total Best Bounding Boxes: "+ str(count_best_bbox) 
    print "Total Missing Bounding Boxes: "+ str(count_missing_boxes) 
    print "Total False Positive Bounding Boxes: "+ str(len_output_bbox-count_best_bbox) 
    print "BBox/Frame Number: "+ str(float(count_best_bbox)/float(len_val_bbox)) 
    print "Missing BBox/Frame Number: "+ str(float(float(count_missing_boxes)/float(len_val_bbox)))
    print "False Positive BBox/Frame Number: "+ str(float(float(len_output_bbox-count_best_bbox)/float(len_val_bbox)))
def recurrent_track_objects(video_info):

    previous_frame= None
    previous_num_obj=-1

    tracked_video=[]
    deltas_video=[]
    deltas_frame=[]
    dx1,dx2,dy1,dy2=0,0,0,0

    for frame_info in video_info:
        print "Tracking Frame Nr: %d"%frame_info.frame
        print "Len Rects Frame: %d"%len(frame_info.rects)
        current_frame = frame.Frame_Info()
        current_frame=frame_info.duplicate()
        current_frame.rects=[]
        if previous_frame is not None:
            deltas_frame=[]
            if frame_info.frame>1:
                print "Len Previous Rects Frame: %d"%len(previous_frame.rects)
                rect_idx=0
                for rect in previous_frame.rects:
                    print len(current_frame.rects)
                    rect.add_delta(deltas_video[frame_info.frame-2][rect_idx][0],deltas_video[frame_info.frame-2][rect_idx][1],deltas_video[frame_info.frame-2][rect_idx][2],deltas_video[frame_info.frame-2][rect_idx][3])
                    current_rect = multiclass_rectangle.pop_max_iou(frame_info.rects,rect)
                    if current_rect is not None:
                        current_rect.load_trackID(rect.trackID)
                        current_rect.check_rects_motion(frame_info.filename, rect, deltas_video[frame_info.frame-2][rect_idx][0],deltas_video[frame_info.frame-2][rect_idx][1],deltas_video[frame_info.frame-2][rect_idx][2],deltas_video[frame_info.frame-2][rect_idx][3])
                        current_frame.append_labeled_rect(current_rect)
                        dx1=current_rect.x1-rect.x1
                        dx2=current_rect.x2-rect.x2
                        dy1=current_rect.y1-rect.y1
                        dy2=current_rect.y2-rect.y2
                        deltas_frame.append((dx1,dx2,dy1,dy2))
                    else: break
            else:
                print "Len Previous Rects Frame: %d"%len(previous_frame.rects)
                for rect in previous_frame.rects:
                    print len(current_frame.rects)
                    current_rect = multiclass_rectangle.pop_max_iou(frame_info.rects,rect)
                    if current_rect is not None:
                        dx1=current_rect.x1-rect.x1
                        dx2=current_rect.x2-rect.x2
                        dy1=current_rect.y1-rect.y1
                        dy2=current_rect.y2-rect.y2
                        deltas_frame.append((dx1,dx2,dy1,dy2))
                        current_rect.load_trackID(rect.trackID)
                        current_frame.append_labeled_rect(current_rect)
                    else: break
            deltas_video.append(deltas_frame)
        else:
            trackID=1
            picked_rect=Utils_Tensorbox.NMS(frame_info.rects)
            for rect in picked_rect:       
                current_rect = rect.duplicate()
                current_rect.load_trackID(trackID)
                current_frame.append_labeled_rect(current_rect)
                trackID=trackID+1

        previous_frame=current_frame.duplicate()
        previous_frame.rects= multiclass_rectangle.duplicate_rects(current_frame.rects)

        print "Current Frame obj:%d"%len(current_frame.rects)
        tracked_video.insert(len(tracked_video), current_frame)

    return tracked_video
def recurrent_track_objects(video_info):

    previous_frame = None
    previous_num_obj = -1

    tracked_video = []
    deltas_video = []
    deltas_frame = []
    dx1, dx2, dy1, dy2 = 0, 0, 0, 0

    for frame_info in video_info:
        print("Tracking Frame Nr: %d" % frame_info.frame)
        print("Len Rects Frame: %d" % len(frame_info.rects))
        current_frame = frame.Frame_Info()
        current_frame = frame_info.duplicate()
        current_frame.rects = []
        if previous_frame is not None:
            deltas_frame = []
            if frame_info.frame > 1:
                print("Len Previous Rects Frame: %d" %
                      len(previous_frame.rects))
                rect_idx = 0
                for rect in previous_frame.rects:
                    print(len(current_frame.rects))
                    rect.add_delta(
                        deltas_video[frame_info.frame - 2][rect_idx][0],
                        deltas_video[frame_info.frame - 2][rect_idx][1],
                        deltas_video[frame_info.frame - 2][rect_idx][2],
                        deltas_video[frame_info.frame - 2][rect_idx][3])
                    current_rect = multiclass_rectangle.pop_max_iou(
                        frame_info.rects, rect)
                    if current_rect is not None:
                        current_rect.load_trackID(rect.trackID)
                        current_rect.check_rects_motion(
                            frame_info.filename, rect,
                            deltas_video[frame_info.frame - 2][rect_idx][0],
                            deltas_video[frame_info.frame - 2][rect_idx][1],
                            deltas_video[frame_info.frame - 2][rect_idx][2],
                            deltas_video[frame_info.frame - 2][rect_idx][3])
                        current_frame.append_labeled_rect(current_rect)
                        dx1 = current_rect.x1 - rect.x1
                        dx2 = current_rect.x2 - rect.x2
                        dy1 = current_rect.y1 - rect.y1
                        dy2 = current_rect.y2 - rect.y2
                        deltas_frame.append((dx1, dx2, dy1, dy2))
                    else:
                        break
            else:
                print("Len Previous Rects Frame: %d" %
                      len(previous_frame.rects))
                for rect in previous_frame.rects:
                    print(len(current_frame.rects))
                    current_rect = multiclass_rectangle.pop_max_iou(
                        frame_info.rects, rect)
                    if current_rect is not None:
                        dx1 = current_rect.x1 - rect.x1
                        dx2 = current_rect.x2 - rect.x2
                        dy1 = current_rect.y1 - rect.y1
                        dy2 = current_rect.y2 - rect.y2
                        deltas_frame.append((dx1, dx2, dy1, dy2))
                        current_rect.load_trackID(rect.trackID)
                        current_frame.append_labeled_rect(current_rect)
                    else:
                        break
            deltas_video.append(deltas_frame)
        else:
            trackID = 1
            picked_rect = Utils_Tensorbox.NMS(frame_info.rects)
            for rect in picked_rect:
                current_rect = rect.duplicate()
                current_rect.load_trackID(trackID)
                current_frame.append_labeled_rect(current_rect)
                trackID = trackID + 1

        previous_frame = current_frame.duplicate()
        previous_frame.rects = multiclass_rectangle.duplicate_rects(
            current_frame.rects)

        print("Current Frame obj:%d" % len(current_frame.rects))
        tracked_video.insert(len(tracked_video), current_frame)

    return tracked_video