Ejemplo n.º 1
0
class TestFeatureTrackSet(unittest.TestCase):
    @classmethod
    def setUp(self):
        self.track_state = TrackState(15)
        self.track = Track(23)
        self.track.append(TrackState(1))
        self.track.append(self.track_state)
        self._track_arr = [Track(15), Track(1),
                                    Track(150), Track(9),
                                    self.track]

    def test_construct(self):
      ftset()
      ftset(self._track_arr)

    def test_methods(self):
      test_feat_set = ftset(self._track_arr)
      self.assertEqual(test_feat_set.all_frame_ids(), {1, 15})
      nt.assert_equal(test_feat_set.get_track(1), self._track_arr[1] )
      self.assertEqual(0, test_feat_set.first_frame())
      self.assertEqual(15, test_feat_set.last_frame())
      self.assertEqual(5, len(test_feat_set.tracks()))
      self.assertEqual(len(test_feat_set), 5)
      cloned_test_feat_set = test_feat_set.clone()
      self.assertIsInstance(cloned_test_feat_set, ftset)
      self.assertEqual(cloned_test_feat_set.all_frame_ids(), test_feat_set.all_frame_ids())
      self.assertIsInstance(test_feat_set.last_frame_features(), sfs)
      self.assertIsInstance(test_feat_set.last_frame_descriptors(), DescriptorSet)
      self.assertIsInstance(test_feat_set.frame_features(), sfs)
      self.assertIsInstance(test_feat_set.frame_descriptors(), DescriptorSet)
      track_state_list = test_feat_set.frame_feature_track_states(15)
      self.assertListEqual(track_state_list, [])
      self.assertEqual(test_feat_set.keyframes(), set())
Ejemplo n.º 2
0
 def _create_object_track_set(self):
     bbox = BoundingBox(10, 10, 20, 20)
     dot = DetectedObjectType("test", 0.4)
     do = DetectedObject(bbox, 0.4, dot)
     track = Track()
     for i in range(10):
         track.append(ObjectTrackState(i, i, do))
     return ObjectTrackSet([track])
Ejemplo n.º 3
0
    def test_set_id(self):
        t = Track()
        nose.tools.assert_equal(t.id, 0)

        t.id = 2
        nose.tools.assert_equal(t.id, 2)

        t.id = 1345634
        nose.tools.assert_equal(t.id, 1345634)
Ejemplo n.º 4
0
    def test_all_frame_ids_single_track(self):
        # From a single track
        n = 10
        t = Track(1)
        for i in range(n):
            t.append(TrackState(i))
        ts = TrackSet([t])

        nt.assert_equal(ts.all_frame_ids(), set(range(10)))
Ejemplo n.º 5
0
    def test_initial_id(self):
        t = Track()
        nose.tools.assert_equal(t.id, 0)

        t = Track(0)
        nose.tools.assert_equal(t.id, 0)

        t = Track(-1)
        nose.tools.assert_equal(t.id, -1)

        t = Track(15)
        nose.tools.assert_equal(t.id, 15)
Ejemplo n.º 6
0
def to_ObjectTrackSet(tracks):
    """Create an ObjectTrackSet from a dict whose keys are track IDs
    and values are lists of pairs of Kwiver timestamps and Kwiver DetectedObjects"""
    # Modeled after similar code in srnn_tracker.py
    result = []
    for tid, states in tracks.items():
        t = Track(id=tid)
        for ts, do in states:
            ots = ObjectTrackState(ts.get_frame(), ts.get_time_usec(), do)
            if not t.append(ots):
                raise ValueError("Unsorted input to to_ObjectTrackSet")
        result.append(t)
    return ObjectTrackSet(result)
Ejemplo n.º 7
0
    def test_track_getitem(self):
        # this is the same test as test_track_find, but using the get-item
        # accessor syntax
        t = Track()
        t.append(TrackState(0))
        t.append(TrackState(1))
        t.append(TrackState(5))
        t.append(TrackState(9))

        ts = t[0]
        nose.tools.assert_is_not_none(ts)
        nose.tools.assert_equal(ts.frame_id, 0)

        ts = t[1]
        nose.tools.assert_is_not_none(ts)
        nose.tools.assert_equal(ts.frame_id, 1)

        ts = t[5]
        nose.tools.assert_is_not_none(ts)
        nose.tools.assert_equal(ts.frame_id, 5)

        ts = t[9]
        nose.tools.assert_is_not_none(ts)
        nose.tools.assert_equal(ts.frame_id, 9)

        nose.tools.assert_raises(IndexError, t.find_state, 10)
        t.append(TrackState(10))
        nose.tools.assert_is_not_none(t[10])
        nose.tools.assert_equal(t[10].frame_id, 10)
Ejemplo n.º 8
0
 def _create_track(self):
     """
     Helper function to create a track
     :return: Track with 10 object track state. Every track state has same
                 detected object however the fram number and time varies from
                 [0, 10)
     """
     bbox = BoundingBox(10, 10, 20, 20)
     cm = ClassMap("test", 0.4)
     do = DetectedObject(bbox, 0.4, cm)
     track = Track()
     for i in range(10):
         track.append(ObjectTrackState(i, i, do))
     return track
Ejemplo n.º 9
0
 def setUp(self):
     bbox = BoundingBox(10, 10, 20, 20)
     dot = DetectedObjectType("test", 0.4)
     do = DetectedObject(bbox, 0.4, dot)
     track = Track()
     for i in range(10):
         track.append(ObjectTrackState(i, i, do))
     self.track_ = track
     self.time_1 = Timestamp()
     self.time_1.set_time_seconds(1234)
     self.time_2 = Timestamp()
     self.time_2.set_time_seconds(4321)
     self.obj_ts = ObjectTrackSet([self.track_])
     self.act_type = ActivityType("self_act", 0.87)
     self.act = Activity(1, "self_act", 0.87, self.act_type, self.time_1,
                         self.time_2, self.obj_ts)
Ejemplo n.º 10
0
    def test_tracklist_accessor(self):
        n = 10
        tracks = [Track(i) for i in range(n)]
        ts = TrackSet(tracks)
        ts_tracks = ts.tracks()

        nt.assert_equal(len(ts_tracks), n)
        for i, t in enumerate(ts_tracks):
            nt.assert_equal(t.id, i)
Ejemplo n.º 11
0
 def test_all_frame_ids_multitrack(self):
     # Across multiple tracks
     n = 10
     t1 = Track(1)
     for i in range(0, n):
         t1.append(TrackState(i))
     t2 = Track(2)
     for i in range(n, n + 5):
         t2.append(TrackState(i))
     ts = TrackSet([t1, t2])
     nt.assert_equal(ts.all_frame_ids(), set(range(n + 5)))
Ejemplo n.º 12
0
    def test_ts_append(self):
        t = Track()
        nose.tools.assert_equal(t.size, 0)
        nose.tools.assert_equal(len(t), 0)

        ts = TrackState(10)
        nose.tools.assert_true(t.append(ts))
        nose.tools.assert_equal(t.size, 1)
        nose.tools.assert_equal(len(t), 1)

        ts = TrackState(11)
        nose.tools.assert_true(t.append(ts))
        nose.tools.assert_equal(t.size, 2)
        nose.tools.assert_equal(len(t), 2)

        # Other properties that should not be different than default
        nose.tools.assert_equal(t.first_frame, 10)
        nose.tools.assert_equal(t.last_frame, 11)
        nose.tools.assert_false(t.is_empty)
Ejemplo n.º 13
0
    def test_ts_append_outoforder(self):
        t = Track()
        nose.tools.assert_true(t.append(TrackState(10)))
        nose.tools.assert_false(t.append(TrackState(10)))
        nose.tools.assert_false(t.append(TrackState(9)))
        nose.tools.assert_false(t.append(TrackState(0)))
        nose.tools.assert_false(t.append(TrackState(-1)))

        nose.tools.assert_true(t.append(TrackState(11)))
        nose.tools.assert_false(t.append(TrackState(11)))

        # After all that there should only be two states in there for frames 10
        # and 11.
        nose.tools.assert_equal(t.size, 2)
        nose.tools.assert_equal(len(t), 2)
        nose.tools.assert_equal(t.all_frame_ids(), {10, 11})
Ejemplo n.º 14
0
def ts2ot_list(track_set):
    ot_list = [Track(id=t.track_id) for t in track_set]

    for idx, t in enumerate(track_set):
        ot = ot_list[idx]
        for ti in t:
            ot_state = ObjectTrackState(ti.sys_frame_id, ti.sys_frame_time,
                                        ti.detected_object)
            if not ot.append(ot_state):
                print('Error: Cannot add ObjectTrackState')
    return ot_list
Ejemplo n.º 15
0
 def setUp(self):
     self.track_state = TrackState(15)
     self.track = Track(23)
     self.track.append(TrackState(1))
     self.track.append(self.track_state)
     self._track_arr = [Track(15), Track(1),
                                 Track(150), Track(9),
                                 self.track]
Ejemplo n.º 16
0
    def test_iteration(self):
        t = Track()
        t.append(TrackState(0))
        t.append(TrackState(1))
        t.append(TrackState(5))
        t.append(TrackState(9))

        nose.tools.assert_equal([ts.frame_id for ts in t], [0, 1, 5, 9])
Ejemplo n.º 17
0
 def test_id(self):
     a = self.act
     self.assertEqual(a.id, 1)
     a.id = 10
     self.assertEqual(a.id, 10)
     self.assertEqual(a.label, "self_act")
     a.label = "second_act"
     self.assertEqual(a.label, "second_act")
     self.assertEqual(a.activity_type.score("self_act"), 0.87)
     a.activity_type = ActivityType()
     self.assertEqual(a.confidence, 0.87)
     a.confidence = 1
     self.assertEqual(a.confidence, 1)
     self.assertEqual(a.start_time.get_time_seconds(), 1234)
     tmp_time = Timestamp().set_time_seconds(1237)
     a.start_time = tmp_time
     self.assertEqual(a.start_time.get_time_seconds(), 1237)
     self.assertEqual(a.end_time.get_time_seconds(), 4321)
     tmp_time = Timestamp()
     tmp_time.set_time_seconds(4322)
     a.end_time = tmp_time
     self.assertEqual(a.end_time.get_time_seconds(), 4322)
     self.assertEqual(a.participants.all_frame_ids(), set(range(10)))
     bbox = BoundingBox(10, 10, 20, 20)
     dot = DetectedObjectType("test", 0.4)
     do = DetectedObject(bbox, 0.4, dot)
     track = Track()
     for i in range(5):
         track.append(ObjectTrackState(i, i, do))
     new_t = track
     new_ots = ObjectTrackSet([new_t])
     a.participants = new_ots
     self.assertEqual(a.participants.all_frame_ids(), set(range(5)))
     self.assertEqual(a.duration[0].get_time_seconds(),
                      a.start_time.get_time_seconds())
     self.assertEqual(a.duration[1].get_time_seconds(),
                      a.end_time.get_time_seconds())
Ejemplo n.º 18
0
    def test_first_frame(self):
        # no tracks
        ts = TrackSet()
        nt.assert_equal(ts.first_frame(), 0)

        # one track
        t = Track(1)
        t.append(TrackState(1))
        t.append(TrackState(2))
        ts = TrackSet([t])
        nt.assert_equal(ts.first_frame(), 1)

        # two tracks
        t2 = Track(2)
        t2.append(TrackState(3))
        ts = TrackSet([t, t2])
        nt.assert_equal(ts.first_frame(), 1)
Ejemplo n.º 19
0
def subset_tracks(trackset, keep_fraction=0.75):
    """
    randomly drop a fraction of the track states per track in the given set,
    creating and returning new tracks in a new track-set.

    :type trackset: TrackSet
    :type keep_fraction: float
    """
    log = logging.getLogger(__name__)

    new_tracks = []
    for t in trackset.tracks():
        nt = Track(t.id)

        msg = 'track %d:' % t.id,
        for ts in t:
            if numpy.random.rand() < keep_fraction:
                nt.append(ts)
                msg += '.',
            else:
                msg += 'X',
        log.info(' '.join(msg))
        new_tracks.append(nt)
    return TrackSet(new_tracks)
Ejemplo n.º 20
0
 def test_initial_firstlast_frame(self):
     t = Track()
     nose.tools.assert_equal(t.first_frame, 0)
     nose.tools.assert_equal(t.last_frame, 0)
Ejemplo n.º 21
0
    def test_track_find(self):
        t = Track()
        t.append(TrackState(0))
        t.append(TrackState(1))
        t.append(TrackState(5))
        t.append(TrackState(9))

        ts = t.find_state(0)
        nose.tools.assert_is_not_none(ts)
        nose.tools.assert_equal(ts.frame_id, 0)

        ts = t.find_state(1)
        nose.tools.assert_is_not_none(ts)
        nose.tools.assert_equal(ts.frame_id, 1)

        ts = t.find_state(5)
        nose.tools.assert_is_not_none(ts)
        nose.tools.assert_equal(ts.frame_id, 5)

        ts = t.find_state(9)
        nose.tools.assert_is_not_none(ts)
        nose.tools.assert_equal(ts.frame_id, 9)

        nose.tools.assert_raises(IndexError, t.find_state, 10)
        t.append(TrackState(10))
        nose.tools.assert_is_not_none(t.find_state(10))
        nose.tools.assert_equal(t.find_state(10).frame_id, 10)
Ejemplo n.º 22
0
 def test_new(self):
     t = Track()
Ejemplo n.º 23
0
 def test_new_nonempty(self):
     n = 10
     tracks = [Track(i) for i in range(n)]
     ts = TrackSet(tracks)
     nt.assert_equal(len(ts), n)
     nt.assert_equal(ts.size(), n)
Ejemplo n.º 24
0
def create_object_track_set():
    object_track_state = create_object_track_state()
    track = Track()
    track.append(create_track_state())
    return ObjectTrackSet([track])
Ejemplo n.º 25
0
 def test_initial_all_frame_ids(self):
     t = Track()
     s = t.all_frame_ids()
     nose.tools.assert_equal(len(s), 0)
Ejemplo n.º 26
0
 def test_initial_size(self):
     t = Track()
     nose.tools.assert_equal(t.size, 0)
     nose.tools.assert_equal(len(t), 0)
Ejemplo n.º 27
0
def create_track():
    track = Track()
    track.append(create_track_state())
    return track
Ejemplo n.º 28
0
 def test_initial_is_empty(self):
     t = Track()
     nose.tools.assert_true(t.is_empty)
Ejemplo n.º 29
0
    def _step(self):

        # Get all inputs even ones we don't use
        in_img_c = self.grab_input_using_trait('image')
        timestamp = self.grab_input_using_trait('timestamp')

        if not timestamp.has_valid_frame():
            raise RuntimeError("Frame timestamps must contain frame IDs")

        frame_id = timestamp.get_frame()

        if self.has_input_port_edge_using_trait('detected_object_set'):
            detections = self.grab_input_using_trait('detected_object_set')
        else:
            detections = DetectedObjectSet()
        if self.has_input_port_edge_using_trait('initializations'):
            initializations = self.grab_input_using_trait('initializations')
        else:
            initializations = ObjectTrackSet()
        if self.has_input_port_edge_using_trait('recommendations'):
            recommendations = self.grab_input_using_trait('recommendations')
        else:
            recommendations = ObjectTrackSet()
        if self.has_input_port_edge_using_trait('evaluation_requests'):
            requests = self.grab_input_using_trait('evaluation_requests')
        else:
            requests = DetectedObjectSet()

        print('mdnet tracker timestamp = {!r}'.format(timestamp))

        # Handle new track external initialization
        init_track_pool = initializations.tracks()
        recc_track_pool = recommendations.tracks()
        init_track_ids = []
        img_used = False

        if len(init_track_pool) != 0 or len(self._trackers) != 0:
            img_npy = self.format_image(in_img_c)
            img_used = True

        for trk in init_track_pool:
            # Special case, initialize a track on a previous frame
            if trk[trk.last_frame].frame_id == self._last_frame_id and \
              ( not trk.id in self._track_init_frames or \
              self._track_init_frames[ trk.id ] < self._last_frame_id ):
                tid = trk.id
                cbox = trk[trk.last_frame].detection().bounding_box()
                bbox = [
                    cbox.min_x(),
                    cbox.min_y(),
                    cbox.width(),
                    cbox.height()
                ]
                self._last_frame = self.format_image(self._last_frame)
                self._trackers[tid] = mdnet.MDNetTracker(
                    self._last_frame, bbox)
                self._tracks[tid] = [ObjectTrackState(timestamp, cbox, 1.0)]
                self._track_init_frames[tid] = self._last_frame_id
            # This track has an initialization signal for the current frame
            elif trk[trk.last_frame].frame_id == frame_id:
                tid = trk.id
                cbox = trk[trk.last_frame].detection().bounding_box()
                bbox = [
                    cbox.min_x(),
                    cbox.min_y(),
                    cbox.width(),
                    cbox.height()
                ]
                self._trackers[tid] = mdnet.MDNetTracker(img_npy, bbox)
                self._tracks[tid] = [ObjectTrackState(timestamp, cbox, 1.0)]
                init_track_ids.append(tid)
                self._track_init_frames[tid] = frame_id

        # Update existing tracks
        for tid in self._trackers.keys():
            if tid in init_track_ids:
                continue  # Already processed (initialized) on frame
            # Check if there's a recommendation for the update
            recc_bbox = []
            for trk in recc_track_pool:
                if trk.id == tid and trk[trk.last_frame].frame_id == frame_id:
                    cbox = trk[trk.last_frame].detection().bounding_box()
                    recc_bbox = [
                        cbox.min_x(),
                        cbox.min_y(),
                        cbox.width(),
                        cbox.height()
                    ]
                    break
            bbox, score = self._trackers[tid].update(img_npy,
                                                     likely_bbox=recc_bbox)
            if score > mdnet.opts['success_thr']:
                cbox = BoundingBoxD(bbox[0], bbox[1], bbox[0] + bbox[2],
                                    bbox[1] + bbox[3])
                new_state = ObjectTrackState(timestamp, cbox, score)
                self._tracks[tid].append(new_state)

        # Handle track termination
        # TODO: Remove old or dead tracks

        # Classify requested evaluations
        # TODO: Evaluate input detections
        output_evaluations = DetectedObjectSet()

        # Output results
        output_tracks = ObjectTrackSet(
            [Track(tid, trk) for tid, trk in self._tracks.items()])

        self.push_to_port_using_trait('timestamp', timestamp)
        self.push_to_port_using_trait('object_track_set', output_tracks)
        self.push_to_port_using_trait('evaluations', output_evaluations)

        self._last_frame_id = timestamp.get_frame()
        if img_used:
            self._last_frame = img_npy
        else:
            self._last_frame = in_img_c
        self._base_step()
Ejemplo n.º 30
0
 def test_get_track_missing(self):
     # test failure to get track from set when set does have contents, but
     # TID is not present
     ts = TrackSet([Track(0), Track(1), Track(3)])
     nt.assert_raises(IndexError, ts.get_track, 2)