diff --git a/libs/association.py b/libs/association.py index 80c3464..77af071 100644 --- a/libs/association.py +++ b/libs/association.py @@ -155,8 +155,7 @@ def _filter_low_iou_low_area(matched_indices_ext, matched_indices, iou_matrix_ex return matches_ext_com, del_ind -def associate_detections_to_trackers(mot_tracker, detections, trackers, groundtruths, average_area, - iou_threshold=0.3, ): +def associate_detections_to_trackers(mot_tracker, detections, trackers, groundtruths, average_area, iou_threshold): """ Assigns detections to tracked object (both represented as bounding boxes) Returns 5 lists of matches, unmatched_detections, unmatched_trackers, occluded_trackers and unmatched ground truths diff --git a/libs/calculate_cost.py b/libs/calculate_cost.py index 146956e..22c6099 100644 --- a/libs/calculate_cost.py +++ b/libs/calculate_cost.py @@ -1,4 +1,4 @@ -from numba import jit +# from numba import jit import numpy as np diff --git a/libs/tracker.py b/libs/tracker.py index c1ae341..860866a 100644 --- a/libs/tracker.py +++ b/libs/tracker.py @@ -69,7 +69,7 @@ def _update_matched_trackers(dets, kalman_trackers, unmatched_trks, occluded_trk return unmatched_trks_pos -def _build_new_targets(unmatched_before_before, unmatched_before, unmatched, area_avg, kalman_trackers): +def _build_new_targets(unmatched_before_before, unmatched_before, unmatched, area_avg, kalman_trackers, three_frame_certainty): if (len(unmatched_before_before) != 0) and (len(unmatched_before) != 0) and (len(unmatched) != 0): new_trackers, del_ind = association.find_new_trackers_2(unmatched, unmatched_before, unmatched_before_before, area_avg) @@ -82,7 +82,7 @@ def _build_new_targets(unmatched_before_before, unmatched_before, unmatched, are del_ind = np.flip(del_ind, axis=0) for i, new_tracker in enumerate(new_trackers): new_trk_certainty = unm[new_tracker[0], 4] + unmb[new_tracker[1], 4] + unmbb[new_tracker[2], 4] - if new_trk_certainty > 2: + if new_trk_certainty > three_frame_certainty: trk = kalman_tracker.KalmanBoxTracker(unm[new_tracker[0], :], 1, unmb[new_tracker[1], :]) kalman_trackers.append(trk) # remove matched detection from unmatched arrays @@ -116,31 +116,31 @@ def __init__(self, max_age=3, min_hits=3, scene=np.array([1920, 1080])): self.max_age = max_age self.min_hits = min_hits self.trackers = [] - self.area_avg_array = [] self.frame_count = 0 - self.unmatched_before_before = [] - self.unmatched_before = [] + self.unmatched_history = [] self.unmatched = [] + self.unmatched_before = [] + self.unmatched_before_before = [] self.scene = scene self.conf_trgt = 0 self.conf_objt = 0 + self.conf_three_frame_certainty = .4 + self.conf_iou_threshold = .3 + self.conf_unmatched_history_size = 3 def to_json(self): """ Returns a dict object that can be used to serialize this a JSON """ return { - "area_avg_array": list(map(lambda x: x if np.isscalar(x) else x[0], self.area_avg_array)), "conf_objt": self.conf_objt, "conf_trgt": self.conf_trgt, + "conf_unmatched_history_size": self.conf_unmatched_history_size, "frame_count": self.frame_count, "max_age": self.max_age, "min_hits": self.min_hits, "scene": self.scene.tolist(), "trackers": list(map(lambda x: x.to_json(), self.trackers)), - "unmatched": list(map(lambda x: x.tolist(), self.unmatched)), - "unmatched_before": list(map(lambda x: x.tolist(), self.unmatched_before)), - "unmatched_before_before": list(map(lambda x: x.tolist(), self.unmatched_before_before)), } def update(self, dets, gts): @@ -155,11 +155,10 @@ def update(self, dets, gts): self.frame_count += 1 trks, area_avg = _init_area_and_trackers(self.trackers) - self.area_avg_array.append(area_avg) trks = _remove_outside_trackers(trks, self.trackers, self.scene) - matched, unmatched_dets, unmatched_trks, occluded_trks, unmatched_gts = association.associate_detections_to_trackers(self, dets, trks, gts, area_avg) + matched, unmatched_dets, unmatched_trks, occluded_trks, unmatched_gts = association.associate_detections_to_trackers(self, dets, trks, gts, area_avg, self.conf_iou_threshold) # update matched trackers with assigned detections unmatched_trks_pos = _update_matched_trackers(dets, self.trackers, unmatched_trks, occluded_trks, matched, trks) @@ -173,15 +172,28 @@ def update(self, dets, gts): trk = kalman_tracker.KalmanBoxTracker(dets[i, :], 0, dets[i, :]) self.trackers.append(trk) else: + # create current unmatched list self.unmatched = [] for i in unmatched_dets: self.unmatched.append(dets[i, :]) + # find previous unmatched in history + unmatched_available = list(filter(lambda check: len(check), self.unmatched_history)) + # take last two + unmatched_available = unmatched_available[-2:] + self.unmatched_before_before = [] + self.unmatched_before = [] + if len(unmatched_available) == 1: + self.unmatched_before = unmatched_available[0] + elif len(unmatched_available) == 2: + self.unmatched_before_before, self.unmatched_before = unmatched_available + + self.unmatched_history.append(self.unmatched) + self.unmatched_history = self.unmatched_history[-self.conf_unmatched_history_size:] + # Build new targets _build_new_targets(self.unmatched_before_before, self.unmatched_before, self.unmatched, - self.area_avg_array[len(self.area_avg_array) - 1], self.trackers) - self.unmatched_before_before = self.unmatched_before - self.unmatched_before = self.unmatched + area_avg, self.trackers, self.conf_three_frame_certainty) # get position of unmatched ground truths unmatched_gts_pos = [] diff --git a/libs/visualization.py b/libs/visualization.py index 3d7707f..faa3251 100644 --- a/libs/visualization.py +++ b/libs/visualization.py @@ -1,8 +1,5 @@ import numpy as np -import matplotlib.pyplot as plt -import matplotlib.patches as patches import os.path -import cv2 from PIL import Image from . import convert @@ -50,6 +47,9 @@ class DisplayState: # display details such as bounding boxes in the image def dispaly_details(common_path, imgFolder, frame, dets, gts, trackers, unmatched_trckr, unmatched_gts, mot_trackers): + import matplotlib.pyplot as plt + import matplotlib.patches as patches + # display image if DisplayState.display: img = Image.open('%s/img1/%06d.jpg' % (common_path, frame)) @@ -128,6 +128,7 @@ def generate_video(image_folder, video_name, fps): if img.endswith(".jpg") or img.endswith(".jpeg") or img.endswith(".png")] + import cv2 frame = cv2.imread(os.path.join(image_folder, images[0])) # setting the frame width, height width according to the width & height of first image