def test_ObjectTracker_prune_too_invisible(object_now, tracker_now): now = datetime.now() object_now.side_effect = [ now, # create now, # create now, # create now, # create ] tracker_now.side_effect = [ # update 1 now, # _prune # update 2 now + timedelta(seconds=10), # _prune ] ot = ObjectTracker(5) tos = [TrackedObject(np.array([0, 1, 2])), TrackedObject(np.array([0, 1, 2]))] ot.update(copy.deepcopy(tos)) assert len(ot._objects) == 2 ot._objects[0].last_seen = now + timedelta(seconds=9) ot._objects[1].last_seen = now + timedelta(seconds=3.9) ot.update([]) assert len(ot._objects) == 1
def test_ObjectTracker_update_multiple(): ot = ObjectTracker(5) tos = [TrackedObject(np.array([0, 1, 2])), TrackedObject(np.array([0, 1, 2]))] ot.update(tos) assert len(ot._objects) == 2 assert ot._objects[0].id == 0 assert ot._objects[1].id == 1
def test_simple_update_10_pct_delta(object_now, tracker_now): now = datetime.datetime.now() object_now.side_effect = [ now, # create now, # create now, # create now, # create now, # create now, # create now, # create now, # create now, # create now, # create now, # create now, # create now, # create now, # create now, # create now, # create ] tracker_now.side_effect = [ now + datetime.timedelta(seconds=1), # _track now + datetime.timedelta(seconds=1), # _prune now + datetime.timedelta(seconds=1), # _track now + datetime.timedelta(seconds=1), # _prune ] objects1 = [ TrackedObject(np.array([0, 0, 0])), TrackedObject(np.array([1, 0, 0])), TrackedObject(np.array([0, 1, 0])), TrackedObject(np.array([1, 1, 0])) ] objects2 = [ TrackedObject(np.array([0.1, 0, 0])), TrackedObject(np.array([0.9, 0, 0])), TrackedObject(np.array([0.1, 1, 0])), TrackedObject(np.array([0.9, 1, 0])) ] tracker = SimpleTracker(deletion_threshold=5) tracker.update(copy.deepcopy(objects1)) tracked_objects1 = copy.deepcopy(tracker.get_objects()) tracker.update(copy.deepcopy(objects2)) tracked_objects2 = copy.deepcopy(tracker.get_objects()) assert len(tracked_objects1) == len(tracked_objects2) obj_map = {obj.id: obj for obj in tracked_objects2} for i in range(len(objects1)): assert np.linalg.norm(tracked_objects1[i].pos - obj_map[tracked_objects1[i].id].pos) < .2
def test_ObjectTracker_prune_too_old(): dt = datetime.now() ot = ObjectTracker(5) tos = [TrackedObject(np.array([0, 1, 2])), TrackedObject(np.array([0, 1, 2]))] ot.update(tos) assert len(ot._objects) == 2 ot._objects[1].created = dt - timedelta(seconds=100) ot._objects[1].last_seen = dt - timedelta(seconds=6) ot.update([]) assert len(ot._objects) == 1
def _update(update: Dict, clients: List[socket.socket]): people = [ TrackedObject(np.asarray([person["x"], person["y"], person["z"]])) for person in update ] tracking_state.update(people) tracked = tracking_state.get_objects() update_dict = { "objects": [{ "x": person.pos[0], "y": person.pos[1], "z": person.pos[2], "id": person.id } for person in tracked] } bson_data = bson.dumps(update_dict) length = len(bson_data) size_data = struct.pack(size_fmt, length) data = size_data + bson_data excepted = [] for client in clients: try: client.sendall(data) except Exception: excepted.append(client) for client in excepted: clients.remove(client)
def test_kalman_update_stable_association(object_now, tracker_now): now = datetime.datetime.now() object_now.side_effect = [ now, # create now, # create now, # create now, # create now, # create now, # create now, # create now, # create ] tracker_now.side_effect = [ now + datetime.timedelta(seconds=1), # _predict now + datetime.timedelta(seconds=1), # _track now + datetime.timedelta(seconds=1), # _prune now + datetime.timedelta(seconds=2), # _predict now + datetime.timedelta(seconds=2), # _track now + datetime.timedelta(seconds=2), # _prune ] objects = [ TrackedObject(np.array([0, 0, 0])), TrackedObject(np.array([1, 0, 0])), TrackedObject(np.array([0, 1, 0])), TrackedObject(np.array([1, 1, 0])) ] tracker = KalmanTracker(deletion_threshold=5) tracker.update(copy.deepcopy(objects)) tracked_objects1 = copy.deepcopy(tracker.get_objects()) tracker.update(copy.deepcopy(objects)) tracked_objects2 = copy.deepcopy(tracker.get_objects()) assert len(tracked_objects1) == len(tracked_objects2) obj_map = {obj.id: obj for obj in tracked_objects1} for i in range(len(objects)): assert np.array_equal(tracked_objects2[i].pos, obj_map[tracked_objects2[i].id].pos)
def test_TrackedObject_creation(): time_thresh = timedelta(milliseconds=100) to = TrackedObject(np.array([0, 1, 2])) dt = datetime.now() assert np.array_equal(to.pos, np.array([0, 1, 2])) assert dt - to.last_seen < time_thresh assert to.id is None assert to.tracking_ctx is None assert dt - to.created < time_thresh assert to.history == []
def test_TrackedObject_notequal_different_ids(): to1 = TrackedObject(np.array([0, 1, 2])) to2 = TrackedObject(np.array([0, 1, 2])) to1.id = 1 to2.id = 2 assert to1 != to2
def test_TrackedObject_equal(): to1 = TrackedObject(np.array([0, 1, 2])) to2 = TrackedObject(np.array([0, 1, 2])) to1.id = 1 to2.id = 1 assert to1 == to2
def test_kalman_update(object_now, tracker_now): now = datetime.datetime.now() object_now.side_effect = [ now, # create now, # create now, # create now, # create now, # create now, # create now, # create now, # create ] tracker_now.side_effect = [ now + datetime.timedelta(seconds=1), # _predict now + datetime.timedelta(seconds=1), # _track now + datetime.timedelta(seconds=1), # _prune ] objects = [ TrackedObject(np.array([0, 0, 0])), TrackedObject(np.array([1, 0, 0])), TrackedObject(np.array([0, 1, 0])), TrackedObject(np.array([1, 1, 0])) ] tracker = KalmanTracker(deletion_threshold=5) tracker.update(copy.deepcopy(objects)) tracked_objects = tracker.get_objects() assert len(tracked_objects) == len(objects) for i in range(len(objects)): assert any( np.array_equal(obj.pos, objects[i].pos) for obj in tracked_objects)
def test_TrackedObject_notequal_different_types(): to1 = TrackedObject(np.array([0, 1, 2])) to2 = object() assert to1 != to2
def test_kalman_update_predictive_tracking(object_now, tracker_now): now = datetime.datetime.now() object_now.side_effect = [ now, # create 1 now, # create 1 now, # create 1 now, # create 1 now, # create 1 now, # create 1 now, # create 1 now, # create 1 now + datetime.timedelta(seconds=1), # create 2 now + datetime.timedelta(seconds=1), # create 2 now + datetime.timedelta(seconds=1), # create 2 now + datetime.timedelta(seconds=1), # create 2 now + datetime.timedelta(seconds=1), # create 2 now + datetime.timedelta(seconds=1), # create 2 now + datetime.timedelta(seconds=1), # create 2 now + datetime.timedelta(seconds=1), # create 2 now + datetime.timedelta(seconds=2), # create 3 now + datetime.timedelta(seconds=2), # create 3 now + datetime.timedelta(seconds=2), # create 3 now + datetime.timedelta(seconds=2), # create 3 now + datetime.timedelta(seconds=2), # create 3 now + datetime.timedelta(seconds=2), # create 3 now + datetime.timedelta(seconds=2), # create 3 now + datetime.timedelta(seconds=2), # create 3 ] tracker_now.side_effect = [ now + datetime.timedelta(seconds=1), # _predict now + datetime.timedelta(seconds=1), # _track now + datetime.timedelta(seconds=1), # _prune now + datetime.timedelta(seconds=2), # _predict now + datetime.timedelta(seconds=2), # _track now + datetime.timedelta(seconds=2), # _prune now + datetime.timedelta(seconds=3), # _predict now + datetime.timedelta(seconds=3), # _track now + datetime.timedelta(seconds=3), # _prune ] objects1 = [ TrackedObject(np.array([0, 0, 0])), TrackedObject(np.array([1, 0, 0])), TrackedObject(np.array([0, 1, 0])), TrackedObject(np.array([1, 1, 0])) ] objects2 = [ TrackedObject(np.array([0, .3, 0])), TrackedObject(np.array([1, .3, 0])), TrackedObject(np.array([0, .7, 0])), TrackedObject(np.array([1, .7, 0])) ] objects3 = [ TrackedObject(np.array([0, .6, 0])), TrackedObject(np.array([1, .6, 0])), TrackedObject(np.array([0, .4, 0])), TrackedObject(np.array([1, .4, 0])) ] tracker = KalmanTracker(deletion_threshold=5) tracker.update(copy.deepcopy(objects1)) tracked_objects_start = copy.deepcopy(tracker.get_objects()) tracker.update(copy.deepcopy(objects2)) tracker.update([]) tracked_objects_final = copy.deepcopy(tracker.get_objects()) assert len(tracked_objects_start) == len(tracked_objects_final) index_map = {} for obj in tracked_objects_start: index = [ i for i, elem in enumerate(objects1) if np.array_equal(elem.pos, obj.pos) ][0] index_map[obj.id] = index for obj in tracked_objects_final: index = index_map[obj.id] assert np.all(abs(objects3[index].pos - obj.pos) < .001)
def test_kalman_update_10_pct_delta(object_now, tracker_now): ''' This tests the case where an update is closer to the a different object in the previous test_kalman_update ''' now = datetime.datetime.now() object_now.side_effect = [ now, # create now, # create now, # create now, # create now, # create now, # create now, # create now, # create now, # create now, # create now, # create now, # create now, # create now, # create now, # create now, # create ] tracker_now.side_effect = [ now + datetime.timedelta(seconds=1), # _predict now + datetime.timedelta(seconds=1), # _track now + datetime.timedelta(seconds=1), # _prune now + datetime.timedelta(seconds=2), # _predict now + datetime.timedelta(seconds=2), # _track now + datetime.timedelta(seconds=2), # _prune ] objects1 = [ TrackedObject(np.array([0, 0, 0])), TrackedObject(np.array([1, 0, 0])), TrackedObject(np.array([0, 1, 0])), TrackedObject(np.array([1, 1, 0])) ] objects2 = [ TrackedObject(np.array([0.1, 0, 0])), TrackedObject(np.array([0.9, 0, 0])), TrackedObject(np.array([0.1, 1, 0])), TrackedObject(np.array([0.9, 1, 0])) ] tracker = KalmanTracker(deletion_threshold=5) tracker.update(copy.deepcopy(objects1)) tracked_objects1 = copy.deepcopy(tracker.get_objects()) tracker.update(copy.deepcopy(objects2)) tracked_objects2 = copy.deepcopy(tracker.get_objects()) assert len(tracked_objects1) == len(tracked_objects2) obj_map = {obj.id: obj for obj in tracked_objects2} for i in range(len(objects1)): assert np.linalg.norm(tracked_objects1[i].pos - obj_map[tracked_objects1[i].id].pos) < .2