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])
def empty_img_anno(): dbm = DBMan(config.LOSTConfig()) img_anno = model.ImageAnno(img_path='path/to/img1.jpg') fs = add_local_fs(dbm, 'local_fs_for_empty_img_anno') img_anno.fs_id = fs.idx dbm.add(img_anno) dbm.commit() yield img_anno dbm.delete(img_anno) dbm.commit() delete_local_fs(dbm, fs)
def empty_img_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] img_anno = model.ImageAnno(img_path='path/to/img1.jpg') dbm.add(img_anno) dbm.commit() yield img_anno dbm.delete(img_anno) dbm.commit()
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 _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)
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)
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)