Beispiel #1
0
    def test_get_anno_vec(self):
        img_anno = model.ImageAnno()
        anno = model.TwoDAnno()
        anno.bbox = REF_BBOX
        img_anno.twod_annos.append(anno)
        anno = model.TwoDAnno()
        anno.point = REF_POINT
        img_anno.twod_annos.append(anno)
        anno = model.TwoDAnno()
        anno.line = REF_LINE
        img_anno.twod_annos.append(anno)
        anno = model.TwoDAnno()
        anno.polygon = REF_POLYGON
        img_anno.twod_annos.append(anno)

        print('Check BBox')
        bb_list = img_anno.get_anno_vec(anno_type='bbox')
        assert check_bbox(REF_BBOX, bb_list[0])
        
        print('Check Point')
        point_list = img_anno.get_anno_vec(anno_type='point')
        assert check_point(REF_POINT, point_list[0])        

        print('Check Line')
        line_list = img_anno.get_anno_vec(anno_type='line')
        line = line_list[0]
        check_line(REF_LINE, line)
        
        print('Check Polygon')
        polygon_list = img_anno.get_anno_vec(anno_type='polygon')
        check_polygon(REF_POLYGON, polygon_list[0])
Beispiel #2
0
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)
Beispiel #3
0
 def test_polygon_property(self):
     anno = model.TwoDAnno()
     anno.polygon = REF_POLYGON
     print('REF_POLYGON', REF_POLYGON)
     print('Annotation', anno.polygon)
     for i, point in enumerate(REF_POLYGON):
         for j, ref in enumerate(point):
             assert ref == anno.polygon[i][j]
Beispiel #4
0
 def test_line_property(self):
     anno = model.TwoDAnno()
     anno.line = REF_LINE
     print('REF_LINE', REF_LINE)
     print('Annotation', anno.line)
     for i, point in enumerate(REF_LINE):
         for j, ref in enumerate(point):
             assert ref == anno.line[i][j]
Beispiel #5
0
 def test_point_property(self):
     assert True
     return True
     anno = model.TwoDAnno()
     anno.point = REF_POINT
     for i, ref in enumerate(REF_POINT):
         print(i, anno.point[i])
         assert ref == anno.point[i]
Beispiel #6
0
 def test_bbox_property(self):
     assert True
     return True
     bbox_anno = model.TwoDAnno()
     bbox_anno.bbox = REF_BBOX
     for i, ref in enumerate(REF_BBOX):
         print(i, bbox_anno.bbox[i])
         assert ref == bbox_anno.bbox[i]
Beispiel #7
0
def simple_bbox_anno():
    dbm = DBMan(config.LOSTConfig())
    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)
    dbm.add(twod_anno)
    dbm.commit()
    yield twod_anno
    dbm.delete(twod_anno)
    dbm.commit()
Beispiel #8
0
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()
Beispiel #9
0
    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"
Beispiel #10
0
    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"
Beispiel #11
0
 def _add_annos(self,
                pe,
                img_path,
                img_labels=None,
                img_sim_class=None,
                annos=[],
                anno_types=[],
                anno_labels=[],
                anno_sim_classes=[],
                frame_n=None,
                video_path=None,
                anno_task_id=None,
                fs=None,
                img_meta=None,
                anno_meta=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_labels (list of int or str): Labels that will be assigned to the image. The label should
             represented by a label_leaf_id or label_name.
         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.
         fs (obj): The filesystem where image is located. 
             User lost standard filesystem if no filesystem was given.
             You can get this Filesystem object from a DataSource-Element by calling
             get_fs method. 
         img_meta (dict): Dictionary with meta information that should be added to the
             image annotation. Each meta key will be added as column during annotation export. 
             the dict-value will be row content.
         anno_meta (list of dict): List of dictionaries with meta information that 
             should be added to a specific annotation. Each meta key will be 
             added as column during annotation export. The dict-value will be row content.
     '''
     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)
     if fs is None:
         fs_db = self._script._dbm.get_fs(name='lost_data')
         fs = DummyFileMan(fs_db)
     fm = file_man.FileMan(fs_db=fs.lost_fs)
     rel_img_path = fm.make_path_relative(img_path)
     img_anno = model.ImageAnno(
         anno_task_id=anno_task_id,
         img_path=rel_img_path,
         abs_path=os.path.join(fm.root_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,
         fs_id=fs.lost_fs.idx)
     if img_meta is not None:
         img_anno.meta = json.dumps(img_meta, default=_json_default)
     self._script._dbm.add(img_anno)
     if img_labels is not None:
         self._update_labels(img_labels, img_anno)
     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_meta is not None:
             if len(annos) != len(anno_meta):
                 raise ValueError(
                     '*anno_meta* and *annos* need to be of same size!')
             anno.meta = json.dumps(anno_meta[i], default=_json_default)
         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_ids = anno_labels[i]
             self._update_labels(label_leaf_ids, anno)
         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)
Beispiel #12
0
 def _request_lds(self,
                  pe,
                  lds,
                  fm=None,
                  anno_meta_keys=[],
                  img_meta_keys=[],
                  img_path_key='img_path'):
     '''Request annos from LOSTDataset.
     
     Args:
         lds (LOSTDataset): A lost dataset object. Request all annotation in this 
             dataset again.
         pe (PipelineElement): PipelineElement of the annotations task where 
             annotations should be requested for.
         fm (FileMan): A file_man object.
         img_meta_keys (list): Keys that should be used for img_anno meta information
         anno_meta_keys (list or *all*): Keys that should be used for two_d_anno meta information.
             If all, all keys of lds will be added as meta information.
         img_path_key (str): Column that should be used as img_path
     '''
     if 'anno_format' in lds.df:
         if len(lds.df[lds.df['anno_format'] != 'rel']) > 0:
             raise Exception(
                 'All anno in LOSTDataset need to be in rel format!')
     else:
         self._script.logger.warning('anno_format column is missing in lds')
     if 'anno_style' in lds.df:
         bb_df = lds.df[lds.df['anno_dtype'] == 'bbox']
         if len(bb_df[bb_df['anno_style'] != 'xcycwh']) > 0:
             raise Exception(
                 'All anno in bboxes need to be in xcycwh anno_style!')
     else:
         self._script.logger.warning('anno_style column is missing in lds')
     fm_cache = dict()
     # db_anno_task = self._script._dbm.get_anno_task(anno_task_id=anno_task_id)
     anno_task = pipe_elements.AnnoTask(pe, self._script._dbm)
     lbl_map = anno_task.lbl_map
     for img_path, df in lds.df.groupby(img_path_key):
         fm = self._get_lds_fm(df, fm_cache, fm)
         if 'img_sim_class' in df:
             if df['img_sim_class'].values[0]:
                 img_sim_class = df['img_sim_class'].values[0]
             else:
                 img_sim_class = 1
         else:
             img_sim_class = 1
         rel_img_path = fm.make_path_relative(img_path)
         anno_task_id = pe.anno_task.idx
         img_anno = model.ImageAnno(
             anno_task_id=anno_task_id,
             img_path=rel_img_path,
             abs_path=os.path.join(fm.root_path, rel_img_path),
             state=state.Anno.UNLOCKED,
             result_id=self._result_map[pe.idx],
             iteration=self._script._pipe_element.iteration,
             # frame_n=df['img_frame_n'].values[0],
             sim_class=img_sim_class,
             fs_id=fm.fs.lost_fs.idx)
         if len(img_meta_keys) > 0:
             # anno.meta = json.dumps(row[img_meta_keys].to_dict())
             img_anno.meta = json.dumps(df.iloc[0][img_meta_keys].to_dict(),
                                        default=_json_default)
         self._script._dbm.add(img_anno)
         # if img_labels is not None:
         if 'img_lbl' in df:
             img_lbls = df['img_lbl'].values[0]
             if img_lbls:
                 if len(img_lbls) > 0:
                     self._update_labels(img_lbls, img_anno, lbl_map)
         if 'anno_data' in df:
             anno_df = df[~df['anno_data'].isnull()]
             if len(anno_df) > 0:
                 # for i, vec in enumerate(annos):
                 for idx, row in anno_df.iterrows():
                     anno = model.TwoDAnno(
                         iteration=self._script._pipe_element.iteration,
                         anno_task_id=anno_task_id,
                         state=state.Anno.UNLOCKED)
                     if len(anno_meta_keys) > 0:
                         if anno_meta_keys == 'all':
                             anno.meta = json.dumps(row.to_dict(),
                                                    default=_json_default)
                         else:
                             anno.meta = json.dumps(
                                 row[anno_meta_keys].to_dict(),
                                 default=_json_default)
                     if row['anno_dtype'] == 'point':
                         anno.point = row['anno_data']
                     elif row['anno_dtype'] == 'bbox':
                         anno.bbox = row['anno_data']
                     elif row['anno_dtype'] == 'line':
                         anno.line = row['anno_data']
                     elif row['anno_dtype'] == 'polygon':
                         anno.polygon = row['anno_data']
                     if 'anno_lbl' in row:
                         if len(row['anno_lbl']) > 0:
                             # if len(anno_labels) != len(annos):
                             #     raise ValueError('*anno_labels* and *annos* need to be of same size!')
                             # label_leaf_ids = anno_labels[i]
                             self._update_labels(row['anno_lbl'], anno,
                                                 lbl_map)
                     if 'anno_sim_class' in row:
                         if row['anno_sim_class']:
                             # if len(anno_sim_classes) != len(annos):
                             #     raise ValueError('*anno_sim_classes* and *annos* need to have same size!')
                             anno.sim_class = row['anno_sim_class']
                     else:
                         anno.sim_class = 1
                     img_anno.twod_annos.append(anno)
Beispiel #13
0
def get_next_anno_id(dbman):
    anno = model.TwoDAnno(timestamp=datetime.now())
    dbman.save_obj(anno)
    return anno.idx
Beispiel #14
0
 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)