def tracked_objects_per_instance(): head, data = simple_anno.head, simple_anno.data_per_instance tobjs = [] for d in data: tobj = None _tobjs = [] for instance in d: instance_dict = {} track_id = instance[head.index("track_id")] object_class = instance[head.index("object")] tags = {} for h in head: if h in INSTANT_CONSTANT_VARS: instance_dict[h] = instance[head.index(h)] elif h not in TRACKED_OBJECT_CONSTANT_VARS: tags[h] = instance[head.index(h)] instance_dict["tags"] = tags if tobj is None: tobj = TrackedObject(track_id, object_class, instance_dict) else: tobj.add_instance(instance_dict) tobjs.append(tobj) return tobjs
def test_TO_add_instances(s_anno_rli): # TODO: Improve this head, data = s_anno_rli tracked = None for d in data: track_id = d.pop(head.index("track_id")) object_class = d.pop(head.index("object")) if tracked is None: tracked = TrackedObject(track_id, object_class, d) else: tracked.add_instance(d) assert len(tracked) == len(data)
def _read_from_input(self): """Read data from CSV.""" to_number = ["x1", "x2", "y1", "y2", "frame_id"] try: if self.input_file: f_csv = self.input_file.open(mode="r") else: f_csv = StringIO(self.input_str) csv_reader = csv.DictReader(f_csv) line_count = 0 for instance in csv_reader: new_instance: dict = {} track_id = int(instance.pop("track_id")) object_class = instance.pop("object_class") # TEMP: Repair this if object_class not in self.object_classes: raise ValueError(f"Class of {object_class} is not valid.") for key in to_number: if key == "frame_id": new_instance[key] = int(float(instance.pop("frame_id"))) else: # the numbers could be of type `int` of `float` try: val = instance.pop(key) new_instance[key] = int(val) except ValueError: try: new_instance[key] = float(val) except ValueError as v: raise v(f"Cannot convert data of key {key}" "to `int` of `float`.") # XXX: Forcing the tags... tags = {} for key in self.all_tags: val = instance[key] if val in self.all_tags[key]: tags[key] = val else: raise ValueError(f"Problem with tags of key: {key}, val: {val}") new_instance["tags"] = tags # for tag, val in instance.items(): # tags[tag] = val # new_instance["tags"] = tags if self.tracked_objs.get(track_id) is None: # TODO: Should we use the internal methods? self.tracked_objs[track_id] = TrackedObject( track_id=track_id, object_class=object_class, instance=new_instance) else: self.tracked_objs[track_id].add_instance(new_instance, update=False) # For possibly performance purpose for tracked_obj in self.tracked_objs.values(): tracked_obj._update() finally: if self.input_file: f_csv.close()
def tracked_objs_factory(track_id, object_class): tobj = TrackedObject( track_id=track_id, object_class=object_class, instance = {"x1": 10, "y1": 10, "x2": 20, "y2": 20, "frame_id": 2, "tags": {}} ) return tobj
def test_TO_single_instances_init(s_anno_rl): """Test whether `TrackedObject` can be properly instantiate.""" # Get only the first data head, anno = s_anno_rl # Preprocess the data. We already know that only `track_id` and # `object_class` will need to be passed directly. # Anything else will be handled as `instance`. instance = {} for i, h in enumerate(head): if h not in ["track_id", "object"]: instance[h] = anno[i] tracked = TrackedObject(track_id=anno[head.index("track_id")], object_class=anno[head.index("object")], instance=instance)
def tracked_objects(): head, data = simple_anno.head, simple_anno.data tobjs = [] for d in data: instance_dict = {} track_id = d[head.index("track_id")] object_class = d[head.index("object")] tags = {} for h in head: if h in INSTANT_CONSTANT_VARS: instance_dict[h] = d[head.index(h)] elif h not in TRACKED_OBJECT_CONSTANT_VARS: tags[h] = d[head.index(h)] instance_dict["tags"] = tags tobj = TrackedObject(track_id, object_class, instance_dict) tobjs.append(tobj) return tobjs
def _make_new_data(self): new_tobj = False if self.loc_boxes[0][1].currentIndex() == 0: # New TrackedObject. t_id = sum([len(t_id) for t_id in self.obj_clsses_map.values()]) ins_id = 0 new_tobj = True else: t_id = int(self.loc_boxes[0][1].currentText()) if t_id == self.instance.track_id: # Same TrackedObject. ins_id = self.instance.instance_id else: # Different TrackedObject ins_id = -1 obj_cls = self.combo_boxes["object_class"][1].currentText() obj_cls = (self.instance.object_class if (obj_cls[:len(self.new)] == self.new or obj_cls == "") else obj_cls) tags = {} for key, (label, combo_box) in self.combo_boxes.items(): if key == "object_class": continue val = combo_box.currentText() val = None if (val[:len(self.new)] == self.new or val == "") else val tags[key] = val out = Instance(t_id, obj_cls, ins_id, self.instance.x1, self.instance.y1, self.instance.x2, self.instance.y2, self.instance.frame_id, tags) if new_tobj: out = TrackedObject(t_id, obj_cls, out) return out