00001
00002 import numpy as np
00003 import colorsys
00004 from .image_viewer import ImageViewer
00005
00006
00007 def create_unique_color_float(tag, hue_step=0.41):
00008 """Create a unique RGB color code for a given track id (tag).
00009
00010 The color code is generated in HSV color space by moving along the
00011 hue angle and gradually changing the saturation.
00012
00013 Parameters
00014 ----------
00015 tag : int
00016 The unique target identifying tag.
00017 hue_step : float
00018 Difference between two neighboring color codes in HSV space (more
00019 specifically, the distance in hue channel).
00020
00021 Returns
00022 -------
00023 (float, float, float)
00024 RGB color code in range [0, 1]
00025
00026 """
00027 h, v = (tag * hue_step) % 1, 1. - (int(tag * hue_step) % 4) / 5.
00028 r, g, b = colorsys.hsv_to_rgb(h, 1., v)
00029 return r, g, b
00030
00031
00032 def create_unique_color_uchar(tag, hue_step=0.41):
00033 """Create a unique RGB color code for a given track id (tag).
00034
00035 The color code is generated in HSV color space by moving along the
00036 hue angle and gradually changing the saturation.
00037
00038 Parameters
00039 ----------
00040 tag : int
00041 The unique target identifying tag.
00042 hue_step : float
00043 Difference between two neighboring color codes in HSV space (more
00044 specifically, the distance in hue channel).
00045
00046 Returns
00047 -------
00048 (int, int, int)
00049 RGB color code in range [0, 255]
00050
00051 """
00052 r, g, b = create_unique_color_float(tag, hue_step)
00053 return int(255*r), int(255*g), int(255*b)
00054
00055
00056 class NoVisualization(object):
00057 """
00058 A dummy visualization object that loops through all frames in a given
00059 sequence to update the tracker without performing any visualization.
00060 """
00061
00062 def __init__(self, seq_info):
00063 self.frame_idx = seq_info["min_frame_idx"]
00064 self.last_idx = seq_info["max_frame_idx"]
00065
00066 def set_image(self, image):
00067 pass
00068
00069 def draw_groundtruth(self, track_ids, boxes):
00070 pass
00071
00072 def draw_detections(self, detections):
00073 pass
00074
00075 def draw_trackers(self, trackers):
00076 pass
00077
00078 def run(self, frame_callback):
00079 while self.frame_idx <= self.last_idx:
00080 frame_callback(self, self.frame_idx)
00081 self.frame_idx += 1
00082
00083
00084 class Visualization(object):
00085 """
00086 This class shows tracking output in an OpenCV image viewer.
00087 """
00088
00089 def __init__(self, seq_info, update_ms):
00090 image_shape = seq_info["image_size"][::-1]
00091 aspect_ratio = float(image_shape[1]) / image_shape[0]
00092 image_shape = 1024, int(aspect_ratio * 1024)
00093 self.viewer = ImageViewer(
00094 update_ms, image_shape, "Figure %s" % seq_info["sequence_name"])
00095 self.viewer.thickness = 2
00096 self.frame_idx = seq_info["min_frame_idx"]
00097 self.last_idx = seq_info["max_frame_idx"]
00098
00099 def run(self, frame_callback):
00100 self.viewer.run(lambda: self._update_fun(frame_callback))
00101
00102 def _update_fun(self, frame_callback):
00103 if self.frame_idx > self.last_idx:
00104 return False
00105 frame_callback(self, self.frame_idx)
00106 self.frame_idx += 1
00107 return True
00108
00109 def set_image(self, image):
00110 self.viewer.image = image
00111
00112 def draw_groundtruth(self, track_ids, boxes):
00113 self.viewer.thickness = 2
00114 for track_id, box in zip(track_ids, boxes):
00115 self.viewer.color = create_unique_color_uchar(track_id)
00116 self.viewer.rectangle(*box.astype(np.int), label=str(track_id))
00117
00118 def draw_detections(self, detections):
00119 self.viewer.thickness = 2
00120 self.viewer.color = 0, 0, 255
00121 for i, detection in enumerate(detections):
00122 self.viewer.rectangle(*detection.tlwh)
00123
00124 def draw_trackers(self, tracks):
00125 self.viewer.thickness = 2
00126 for track in tracks:
00127 if not track.is_confirmed() or track.time_since_update > 0:
00128 continue
00129 self.viewer.color = create_unique_color_uchar(track.track_id)
00130 self.viewer.rectangle(
00131 *track.to_tlwh().astype(np.int), label=str(track.track_id))
00132
00133
00134