Ejemplo n.º 1
0
    def get_alien_element(self, pe_id):
        '''Get an pipeline element by id from somewhere in the LOST system.

        It is an alien element since it is most likely not part of the 
        pipeline instance this script belongs to.

        Args:
            pe_id (int): PipeElementID of the alien element.
        
        Returns:
            * :class:`lost.pyapi.script.Script`
            * :class:`lost.pyapi.pipe_elements.AnnoTask`
            * :class:`lost.pyapi.pipe_elements.Datasource`
            * :class:`lost.pyapi.pipe_elements.VisualOutput`
            * :class:`lost.pyapi.pipe_elements.DataExport`
            * :class:`lost.pyapi.pipe_elements.Loop`

        '''
        pe = self._dbm.get_pipe_element(pe_id)

        if pe.dtype == dtype.PipeElement.SCRIPT:
            return Script(pe_id=pe_id)
        elif pe.dtype == dtype.PipeElement.ANNO_TASK:
            return pipe_elements.AnnoTask(pe, self._dbm)
        elif pe.dtype == dtype.PipeElement.DATASOURCE:
            return pipe_elements.Datasource(pe, self._dbm)
        elif pe.dtype == dtype.PipeElement.VISUALIZATION:
            return pipe_elements.VisualOutput(pe, self._dbm)
        elif pe.dtype == dtype.PipeElement.DATA_EXPORT:
            return pipe_elements.DataExport(pe, self._dbm)
        elif pe.dtype == dtype.PipeElement.LOOP:
            return pipe_elements.Loop(pe, self._dbm)
        else:
            raise Exception('Unknown pipe element type!')
Ejemplo n.º 2
0
 def anno_tasks(self):
     '''list of :class:`lost.pyapi.pipe_elements.AnnoTask` objects'''
     res_list = []
     for pe in self._connected_pes:
         if pe.dtype == dtype.PipeElement.ANNO_TASK:
             res_list.append(pipe_elements.AnnoTask(pe, self._element._dbm))
     return res_list
Ejemplo n.º 3
0
def __get_amount_per_label(dbm, pipeelement, finished, anno_type):
    dist = list()
    annotask = pipe_elements.AnnoTask(pipeelement, dbm)
    for index, row in annotask.possible_label_df.iterrows():
        row['idx']
        result = dbm.get_amount_per_label(annotask.idx, row['idx'],
                                          anno_type)[0]
        if result > 0:
            dist.append({'label': row['name'], 'amount': result})
    return dist
Ejemplo n.º 4
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)