def _update_labels(self, ll_ids, anno): if isinstance(ll_ids, list): if len(ll_ids) > 0: for ll_id in ll_ids: if ll_id is not None: anno.labels.append(model.Label(label_leaf_id=ll_id)) else: if ll_ids is not None: anno.labels.append(model.Label(label_leaf_id=ll_ids))
def full_img_anno(): dbm = DBMan(config.LOSTConfig()) test_user = testils.get_user(dbm) tree = testils.get_voc_label_tree(dbm) label_vec = tree.get_child_vec(tree.root.idx) twod_anno = model.TwoDAnno(data=json.dumps({ 'x': REF_BBOX[0], 'y': REF_BBOX[1], 'w': REF_BBOX[2], 'h': REF_BBOX[3] }), dtype=dtype.TwoDAnno.BBOX) lbl = model.Label(label_leaf_id=label_vec[0]) dbm.add(lbl) twod_anno.labels.append(lbl) twod_anno.annotator = test_user twod_anno2 = model.TwoDAnno(data=json.dumps({ 'x': REF_POINT[0], 'y': REF_POINT[1] }), dtype=dtype.TwoDAnno.POINT) lbl = model.Label(label_leaf_id=label_vec[1]) dbm.add(lbl) twod_anno2.labels.append(lbl) twod_anno2.annotator = test_user line = model.TwoDAnno() lbl = model.Label(label_leaf_id=label_vec[4]) line.labels.append(lbl) dbm.add(lbl) line.annotator = test_user line.line = REF_LINE img_anno = model.ImageAnno(img_path='path/to/img1.jpg') fs = add_local_fs(dbm, 'local_fs_for_full_img_anno') img_anno.fs_id = fs.idx lbl = model.Label(label_leaf_id=label_vec[3]) dbm.add(lbl) img_anno.labels.append(lbl) dbm.commit() img_anno.twod_annos.append(twod_anno) img_anno.twod_annos.append(twod_anno2) img_anno.twod_annos.append(line) # dbm.add(img_anno.labels) dbm.add(img_anno) # dbm.add(twod_anno) dbm.commit() yield img_anno # dbm.delete(twod_anno.labels) dbm.delete(twod_anno) # dbm.delete(twod_anno2.labels) dbm.delete(twod_anno2) # dbm.delete(img_anno.labels) dbm.delete(img_anno) dbm.commit() delete_local_fs(dbm, fs)
def _update_labels(self, ll_ids, anno, lbl_map=None): if isinstance(ll_ids, list) or isinstance(ll_ids, np.ndarray): if len(ll_ids) > 0: for ll_id in ll_ids: if ll_id is not None: ll_id = self._lbl_name_to_id(ll_id, lbl_map) if ll_id is not None: anno.labels.append( model.Label(label_leaf_id=ll_id)) else: if ll_ids is not None: ll_ids = self._lbl_name_to_id(ll_ids, lbl_map) if ll_ids is not None: anno.labels.append(model.Label(label_leaf_id=ll_ids))
def __update_two_d_annotation(db_man, user_id, data): anno_time = None anno_count = len( list(filter(lambda x: x['is_active'] is True, data['images']))) for img in data['images']: two_d_anno = db_man.get_two_d_annotation(two_d_anno_id=img['id']) if img['is_active']: two_d_anno.state = state.Anno.LABELED two_d_anno.timestamp = datetime.now() if anno_time is None and anno_count > 0: anno_time = (two_d_anno.timestamp - two_d_anno.timestamp_lock).total_seconds() anno_time = anno_time / anno_count two_d_anno.user_id = user_id two_d_anno.anno_time = anno_time db_man.add(two_d_anno) for label in data['labels']: lab = model.Label(dtype=dtype.Label.TWO_D_ANNO, label_leaf_id=label['id'], annotator_id=user_id, timestamp=two_d_anno.timestamp, timestamp_lock=two_d_anno.timestamp_lock, anno_time=anno_time, two_d_anno_id=two_d_anno.idx) db_man.add(lab) else: two_d_anno.state = state.Anno.LOCKED_PRIORITY db_man.add(two_d_anno) db_man.commit()
def __update_image_annotation(db_man, user_id, data): anno_time = None anno_count = len( list(filter(lambda x: x['is_active'] is True, data['images']))) for img in data['images']: image = db_man.get_image_annotation(img_anno_id=img['id']) if img['is_active']: image.state = state.Anno.LABELED image.timestamp = datetime.now() if anno_time is None and anno_count > 0: anno_time = (image.timestamp - image.timestamp_lock).total_seconds() anno_time = anno_time / anno_count image.user_id = user_id image.anno_time = anno_time db_man.add(image) for label in data['labels']: lab = model.Label(dtype=dtype.Label.IMG_ANNO, label_leaf_id=label['id'], annotator_id=user_id, timestamp=image.timestamp, timestamp_lock=image.timestamp_lock, anno_time=anno_time, img_anno_id=image.idx) db_man.add(lab) else: image.state = state.Anno.LOCKED_PRIORITY db_man.add(image) db_man.commit()
def _update_img_labels(self, data): if (data['imgLabelChanged']): old = set([lbl.label_leaf_id for lbl in self.image_anno.labels]) new = set(data['imgLabelIds']) to_delete = old - new to_add = new - old for lbl in self.image_anno.labels: if lbl.label_leaf_id in to_delete: self.image_anno.labels.remove(lbl) # self.db_man.delete(lbl) for ll_id in to_add: self.image_anno.labels.append(model.Label(label_leaf_id=ll_id))
def full_bbox_anno(): dbm = DBMan(config.LOSTConfig()) test_user = testils.get_user(dbm) tree = testils.get_voc_label_tree(dbm) label_leaf_id = tree.get_child_vec(tree.root.idx)[0] twod_anno = model.TwoDAnno(data=json.dumps({ 'x': REF_BBOX[0], 'y': REF_BBOX[1], 'w': REF_BBOX[2], 'h': REF_BBOX[3] }), dtype=dtype.TwoDAnno.BBOX) lbl = model.Label(label_leaf_id=label_leaf_id) twod_anno.labels.append(lbl) twod_anno.annotator = test_user dbm.add(twod_anno) dbm.commit() yield twod_anno dbm.delete(twod_anno) dbm.commit()
def __update_annotations(self, annotations, two_d_type): annotation_json = dict() annotation_json['unchanged'] = list() annotation_json['deleted'] = list() annotation_json['new'] = list() annotation_json['changed'] = list() # calculate average_anno_time: # average anno time = anno_time of image_annotation divided by number of annotations (also deleted ones !) # the average anno_time will be added to the certain annotations anno time # caution: anno_time will be added to *every* two_d anno - # we assume that the attention of every box is equally valid average_anno_time = 0 if len(annotations) > 0: average_anno_time = self.image_anno.anno_time / len(annotations) for annotation in annotations: if annotation['status'] != "database" \ and annotation['status'] != "deleted" \ and annotation['status'] != "new" \ and annotation['status'] != "changed": error_msg = "Status: '" + str( annotation['status']) + "' is not valid." raise SiaStatusNotFoundError(error_msg) for annotation in annotations: if annotation['status'] == "database": two_d = self.db_man.get_two_d_anno( annotation['id']) #type: lost.db.model.TwoDAnno two_d.user_id = self.user_id two_d.state = state.Anno.LABELED two_d.timestamp = self.timestamp two_d.timestamp_lock = self.image_anno.timestamp_lock if two_d.anno_time is None: two_d.anno_time = 0.0 two_d.anno_time += average_anno_time two_d_json = self.__serialize_two_d_json(two_d) annotation_json['unchanged'].append(two_d_json) self.db_man.save_obj(two_d) elif annotation['status'] == "deleted": two_d = self.db_man.get_two_d_anno( annotation['id']) #type: lost.db.model.TwoDAnno two_d_json = self.__serialize_two_d_json(two_d) annotation_json['deleted'].append(two_d_json) for label in self.db_man.get_all_two_d_label(two_d.idx): self.db_man.delete(label) self.db_man.delete(two_d) elif annotation['status'] == "new": annotation_data = annotation['data'] try: annotation_data.pop('left') annotation_data.pop('right') annotation_data.pop('top') annotation_data.pop('bottom') except: pass two_d = model.TwoDAnno( anno_task_id=self.at.idx, img_anno_id=self.image_anno.idx, timestamp=self.timestamp, timestamp_lock=self.image_anno.timestamp_lock, anno_time=average_anno_time, data=json.dumps(annotation_data), user_id=self.user_id, iteration=self.iteration, dtype=two_d_type, state=state.Anno.LABELED) self.db_man.save_obj(two_d) for l_id in annotation['labelIds']: label = model.Label( two_d_anno_id=two_d.idx, label_leaf_id=l_id, dtype=dtype.Label.TWO_D_ANNO, timestamp=self.timestamp, annotator_id=self.user_id, timestamp_lock=self.image_anno.timestamp_lock, anno_time=average_anno_time) self.db_man.save_obj(label) two_d_json = self.__serialize_two_d_json(two_d) annotation_json['new'].append(two_d_json) elif annotation['status'] == "changed": annotation_data = annotation['data'] try: annotation_data.pop('left') annotation_data.pop('right') annotation_data.pop('top') annotation_data.pop('bottom') except: pass two_d = self.db_man.get_two_d_anno( annotation['id']) #type: lost.db.model.TwoDAnno two_d.timestamp = self.timestamp two_d.timestamp_lock = self.image_anno.timestamp_lock two_d.data = json.dumps(annotation_data) two_d.user_id = self.user_id if two_d.anno_time is None: two_d.anno_time = 0.0 two_d.anno_time += average_anno_time two_d.state = state.Anno.LABELED l_id_list = list() # get all labels of that two_d_anno. for label in self.db_man.get_all_two_d_label(two_d.idx): # save id. l_id_list.append(label.idx) # delete labels, that are not in user labels list. if label.idx not in annotation['labelIds']: self.db_man.delete(label) # labels that are in the list get a new anno_time else: if label.anno_time is None: label.anno_time = 0.0 label.anno_time += average_anno_time label.timestamp = self.timestamp label.annotator_id = self.user_id, label.timestamp_lock = self.image_anno.timestamp_lock self.db_man.save_obj(label) # new labels for l_id in annotation['labelIds']: if l_id not in l_id_list: label = model.Label( two_d_anno_id=two_d.idx, label_leaf_id=l_id, dtype=dtype.Label.TWO_D_ANNO, timestamp=self.timestamp, annotator_id=self.user_id, timestamp_lock=self.image_anno.timestamp_lock, anno_time=average_anno_time) self.db_man.save_obj(label) self.db_man.save_obj(two_d) two_d_json = self.__serialize_two_d_json(two_d) annotation_json['changed'].append(two_d_json) else: continue self.history_json['annotations'] = annotation_json return "success"
def __update_annotations(self, annotations, two_d_type): annotation_json = dict() annotation_json['unchanged'] = list() annotation_json['deleted'] = list() annotation_json['new'] = list() annotation_json['changed'] = list() for annotation in annotations: if annotation['status'] != "database" \ and annotation['status'] != "deleted" \ and annotation['status'] != "new" \ and annotation['status'] != "changed": error_msg = "Status: '" + str( annotation['status']) + "' is not valid." raise SiaStatusNotFoundError(error_msg) for annotation in annotations: if annotation['status'] == "database": two_d = self.db_man.get_two_d_anno( annotation['id']) #type: lost.db.model.TwoDAnno two_d.user_id = self.user_id two_d.state = state.Anno.LABELED two_d.timestamp = self.timestamp two_d.timestamp_lock = self.image_anno.timestamp_lock if two_d.anno_time is None: two_d.anno_time = 0.0 # two_d.anno_time += average_anno_time two_d_json = self.__serialize_two_d_json(two_d) annotation_json['unchanged'].append(two_d_json) self.db_man.save_obj(two_d) elif annotation['status'] == "deleted": try: two_d = self.db_man.get_two_d_anno( annotation['id']) #type: lost.db.model.TwoDAnno two_d_json = self.__serialize_two_d_json(two_d) annotation_json['deleted'].append(two_d_json) for label in self.db_man.get_all_two_d_label(two_d.idx): self.db_man.delete(label) self.db_man.delete(two_d) except KeyError: print( 'SIA bug backend fix! Do not try to delete annotations that are not in db!' ) elif annotation['status'] == "new": annotation_data = annotation['data'] try: annotation_data.pop('left') annotation_data.pop('right') annotation_data.pop('top') annotation_data.pop('bottom') except: pass two_d = model.TwoDAnno( anno_task_id=self.at.idx, img_anno_id=self.image_anno.idx, timestamp=self.timestamp, timestamp_lock=self.image_anno.timestamp_lock, anno_time=annotation['annoTime'], data=json.dumps(annotation_data), user_id=self.user_id, iteration=self.iteration, dtype=two_d_type, state=state.Anno.LABELED) self.db_man.save_obj(two_d) for l_id in annotation['labelIds']: label = model.Label( two_d_anno_id=two_d.idx, label_leaf_id=l_id, dtype=dtype.Label.TWO_D_ANNO, timestamp=self.timestamp, annotator_id=self.user_id, timestamp_lock=self.image_anno.timestamp_lock, anno_time=annotation['annoTime']) self.db_man.save_obj(label) two_d_json = self.__serialize_two_d_json(two_d) annotation_json['new'].append(two_d_json) elif annotation['status'] == "changed": annotation_data = annotation['data'] try: annotation_data.pop('left') annotation_data.pop('right') annotation_data.pop('top') annotation_data.pop('bottom') except: pass two_d = self.db_man.get_two_d_anno( annotation['id']) #type: lost.db.model.TwoDAnno two_d.timestamp = self.timestamp two_d.timestamp_lock = self.image_anno.timestamp_lock two_d.data = json.dumps(annotation_data) two_d.user_id = self.user_id if two_d.anno_time is None: two_d.anno_time = 0.0 two_d.anno_time = annotation['annoTime'] two_d.state = state.Anno.LABELED l_id_list = list() # get all labels of that two_d_anno. for label in self.db_man.get_all_two_d_label(two_d.idx): # save id. l_id_list.append(label.idx) # delete labels, that are not in user labels list. if label.idx not in annotation['labelIds']: self.db_man.delete(label) # labels that are in the list get a new anno_time else: if label.anno_time is None: label.anno_time = 0.0 label.anno_time = annotation['annoTime'] label.timestamp = self.timestamp label.annotator_id = self.user_id, label.timestamp_lock = self.image_anno.timestamp_lock self.db_man.save_obj(label) # new labels for l_id in annotation['labelIds']: if l_id not in l_id_list: label = model.Label( two_d_anno_id=two_d.idx, label_leaf_id=l_id, dtype=dtype.Label.TWO_D_ANNO, timestamp=self.timestamp, annotator_id=self.user_id, timestamp_lock=self.image_anno.timestamp_lock, anno_time=annotation['annoTime']) self.db_man.save_obj(label) self.db_man.save_obj(two_d) two_d_json = self.__serialize_two_d_json(two_d) annotation_json['changed'].append(two_d_json) else: continue self.history_json['annotations'] = annotation_json return "success"
def _add_annos(self, pe, img_path, img_label=None, img_sim_class=None, annos=[], anno_types=[], anno_labels=[], anno_sim_classes=[], frame_n=None, video_path=None, anno_task_id=None): '''Add annos in list style to an image. Args: pe (PipeElement): The connected PipeElement where annotation should be provided for. img_path (str): Path to the image where annotations are added for. img_label (int): Labels that will be assigned to the image. The label should represented by a label_leaf_id. img_sim_class (int): A culster id that will be used to cluster this image in the MIA annotation tool. annos (list of list): A list of POINTs: [x,y] BBOXes: [x,y,w,h] LINEs or POLYGONs: [[x,y], [x,y], ...] anno_types (list of str): Can be 'point', 'bbox', 'line', 'polygon' anno_labels (list of int): Labels for the twod annos. Each label in the list is represented by a label_leaf_id. (see also :class:`model.LabelLeaf`). anno_sim_classes (list of ints): List of arbitrary cluster ids that are used to cluster annotations in the MIA annotation tool. frame_n (int): If *img_path* belongs to a video *frame_n* indicates the framenumber. video_path (str): If *img_path* belongs to a video this is the path to this video. anno_task_id (int): Id of the assigned annotation task. ''' if img_sim_class is None: img_sim_class = 1 if video_path is not None: video_path = self._script.get_rel_path(video_path) rel_img_path = self._script.file_man.make_path_relative(img_path) img_anno = model.ImageAnno(anno_task_id=anno_task_id, img_path=rel_img_path, state=state.Anno.UNLOCKED, result_id=self._result_map[pe.idx], iteration=self._script._pipe_element.iteration, frame_n=frame_n, video_path=video_path, sim_class=img_sim_class) self._script._dbm.add(img_anno) if img_label is not None: img_anno.label = model.Label(label_leaf_id=img_label) if len(annos) != len(anno_types): raise ValueError('*anno_types* and *annos* need to be of same size!') for i, vec in enumerate(annos): anno = model.TwoDAnno(iteration=self._script._pipe_element.iteration, anno_task_id=anno_task_id, state=state.Anno.UNLOCKED) if anno_types[i] == 'point': anno.point = vec elif anno_types[i] == 'bbox': anno.bbox = vec elif anno_types[i] == 'line': anno.line = vec elif anno_types[i] == 'polygon': anno.polygon = vec if anno_labels: if len(anno_labels) != len(annos): raise ValueError('*anno_labels* and *annos* need to be of same size!') label_leaf_id = anno_labels[i] if label_leaf_id is not None: anno.label = model.Label(label_leaf_id=label_leaf_id) if anno_sim_classes: if len(anno_sim_classes) != len(annos): raise ValueError('*anno_sim_classes* and *annos* need to have same size!') anno.sim_class = anno_sim_classes[i] else: anno.sim_class = 1 img_anno.twod_annos.append(anno)