Esempio n. 1
0
    def convert(self):
        images_pathes = self._get_images_pathes()
        masks_map = self._get_masks_mapping()
        dataset_name = 'ds'
        out_pr = ProjectStructure(self.settings['res_names']['project'])

        for image_fp in images_pathes:
            base_name = os.path.basename(image_fp)
            image_ext = os.path.splitext(image_fp)[1]
            image_id = os.path.splitext(base_name)[0]
            if base_name.replace(image_ext, '') in masks_map:
                dt = {"image_ext": ".png", "image_orig_path": image_fp}
                out_pr.add_item(dataset_name, image_id, dt)

        out_pr_fs = ProjectFS(self.out_dir, out_pr)
        out_pr_fs.make_dirs()

        res_meta = ProjectMeta()
        # TODO: Fix it line by new meta and object classes
        res_meta.classes.add({
            'title':
            'leaf',
            'shape':
            'bitmap',
            'color':
            color_utils.rgb2hex(color_utils.random_rgb())
        })
        res_meta.to_dir(out_pr_fs.project_path)

        progress = progress_counter.progress_counter_import(
            out_pr.name, out_pr.image_cnt)
        for sample_info in out_pr_fs:
            self._convert_sample(sample_info, masks_map, res_meta)
            progress.iter_done_report()
Esempio n. 2
0
    def convert(self):
        images_pathes = self._get_images_pathes()
        dataset_name = os.path.basename(os.path.normpath(self.dataset_dir))
        out_pr = ProjectStructure(self.settings['res_names']['project'])

        for image_fp in images_pathes:
            image_ext = os.path.splitext(image_fp)[1]
            image_name = os.path.splitext(image_fp)[0]
            dt = {"image_ext": image_ext}
            out_pr.add_item(dataset_name, image_name, dt)

        out_pr_fs = ProjectFS(self.out_dir, out_pr)
        out_pr_fs.make_dirs()

        res_meta = ProjectMeta()
        for class_name in self.classes:
            # TODO: Fix it line by new meta and object classes
            res_meta.classes.add({
                'title':
                class_name,
                'shape':
                'bitmap',
                'color':
                color_utils.rgb2hex(color_utils.random_rgb())
            })
        res_meta.to_dir(out_pr_fs.project_path)

        progress = progress_counter.progress_counter_import(
            out_pr.name, out_pr.image_cnt)
        for sample_info in out_pr_fs:
            self._convert_sample(sample_info, res_meta)
            progress.iter_done_report()
Esempio n. 3
0
    def merge_metas(self, src_project_id, dst_project_id):
        if src_project_id == dst_project_id:
            return self.get_meta(src_project_id)

        src_meta = ProjectMeta.from_json(self.get_meta(src_project_id))
        dst_meta = ProjectMeta.from_json(self.get_meta(dst_project_id))

        new_dst_meta = src_meta.merge(dst_meta)
        new_dst_meta_json = new_dst_meta.to_json()
        self.update_meta(dst_project_id, new_dst_meta.to_json())

        return new_dst_meta_json
Esempio n. 4
0
def single_inference_process_fn(inference_initializer, inference_mode_config,
                                in_project_meta_json, request_queue,
                                response_queue):
    """Loads a separate model, processes requests from request_queue, results go to result_queue.

    None request signals the process to finish.
    """
    single_image_inference = inference_initializer()
    inference_mode = InferenceModeFactory.create(
        inference_mode_config, ProjectMeta.from_json(in_project_meta_json),
        single_image_inference)
    out_meta_json = inference_mode.out_meta.to_json()

    req = ''
    while req is not None:
        req = request_queue.get()
        if req is not None:
            in_img = sly_image.read(req.item_paths.img_path)
            in_ann = Annotation.load_json_file(req.item_paths.ann_path,
                                               inference_mode.out_meta)
            ann = inference_mode.infer_annotate(in_img, in_ann)
            resp = InferenceResponse(ds_name=req.ds_name,
                                     item_name=req.item_name,
                                     item_paths=req.item_paths,
                                     ann_json=ann.to_json(),
                                     meta_json=out_meta_json)
            response_queue.put(resp)
        request_queue.task_done()
Esempio n. 5
0
def _download_project_optimized(api: Api,
                                project_id,
                                project_dir,
                                datasets_whitelist=None,
                                cache=None,
                                progress_cb=None):
    project_info = api.project.get_info_by_id(project_id)
    project_id = project_info.id
    logger.info(
        f"Annotations are not cached (always download latest version from server)"
    )
    project_fs = Project(project_dir, OpenMode.CREATE)
    meta = ProjectMeta.from_json(api.project.get_meta(project_id))
    project_fs.set_meta(meta)
    for dataset_info in api.dataset.get_list(project_id):
        dataset_name = dataset_info.name
        dataset_id = dataset_info.id
        need_download = True
        if datasets_whitelist is not None and dataset_id not in datasets_whitelist:
            need_download = False
        if need_download is True:
            dataset = project_fs.create_dataset(dataset_name)
            _download_dataset(api,
                              dataset,
                              dataset_id,
                              cache=cache,
                              progress_cb=progress_cb)
Esempio n. 6
0
 def _create(self):
     if dir_exists(self.directory):
         raise RuntimeError(
             "Can not create new project {!r}. Directory {!r} already exists"
             .format(self.name, self.directory))
     mkdir(self.directory)
     self.set_meta(ProjectMeta())
 def from_json(cls, data, project_meta: ProjectMeta):
     obj_class_name = data[LabelJsonFields.OBJ_CLASS_NAME]
     obj_class = project_meta.get_obj_class(obj_class_name)
     return cls(geometry=obj_class.geometry_type.from_json(data),
                obj_class=obj_class,
                tags=TagCollection.from_json(data[LabelJsonFields.TAGS], project_meta.obj_tag_metas),
                description=data.get(LabelJsonFields.DESCRIPTION, ""))
Esempio n. 8
0
    def from_json(cls, data, project_meta: ProjectMeta):
        '''
        The function from_json convert Label from json format to Label class object. If there is no ObjClass from
        input json format in ProjectMeta, it generate RuntimeError error.
        :param data: input label in json format
        :param project_meta: ProjectMeta class object
        :return: Label class object
        '''
        obj_class_name = data[LabelJsonFields.OBJ_CLASS_NAME]
        obj_class = project_meta.get_obj_class(obj_class_name)
        if obj_class is None:
            raise RuntimeError(
                f'Failed to deserialize a Label object from JSON: label class name {obj_class_name!r} '
                f'was not found in the given project meta.')

        if obj_class.geometry_type is AnyGeometry:
            geometry_type_actual = GET_GEOMETRY_FROM_STR(
                data[GEOMETRY_TYPE] if GEOMETRY_TYPE in
                data else data[GEOMETRY_SHAPE])
            geometry = geometry_type_actual.from_json(data)
        else:
            geometry = obj_class.geometry_type.from_json(data)

        return cls(geometry=geometry,
                   obj_class=obj_class,
                   tags=TagCollection.from_json(data[LabelJsonFields.TAGS],
                                                project_meta.tag_metas),
                   description=data.get(LabelJsonFields.DESCRIPTION, ""))
Esempio n. 9
0
def download_project(api, project_id, dest_dir, dataset_ids=None, log_progress=False, batch_size=10):
    dataset_ids = set(dataset_ids) if (dataset_ids is not None) else None
    project_fs = Project(dest_dir, OpenMode.CREATE)
    meta = ProjectMeta.from_json(api.project.get_meta(project_id))
    project_fs.set_meta(meta)

    for dataset_info in api.dataset.get_list(project_id):
        dataset_id = dataset_info.id
        if dataset_ids is not None and dataset_id not in dataset_ids:
            continue

        dataset_fs = project_fs.create_dataset(dataset_info.name)
        images = api.image.get_list(dataset_id)

        ds_progress = None
        if log_progress:
            ds_progress = Progress(
                'Downloading dataset: {!r}'.format(dataset_info.name), total_cnt=len(images))

        for batch in batched(images, batch_size):
            image_ids = [image_info.id for image_info in batch]
            image_names = [image_info.name for image_info in batch]

            # download images in numpy format
            batch_imgs_bytes = api.image.download_bytes(dataset_id, image_ids)

            # download annotations in json format
            ann_infos = api.annotation.download_batch(dataset_id, image_ids)
            ann_jsons = [ann_info.annotation for ann_info in ann_infos]

            for name, img_bytes, ann in zip(image_names, batch_imgs_bytes, ann_jsons):
                dataset_fs.add_item_raw_bytes(name, img_bytes, ann)

            if log_progress:
                ds_progress.iters_done_report(len(batch))
    def run_inference(self):
        progress_report_thread = Thread(target=progress_report_thread_fn,
                                        args=(self._in_project,
                                              self._progress_report_queue),
                                        daemon=True)
        progress_report_thread.start()

        feed_status = populate_inference_requests_queue(
            self._in_project, self._inference_processes,
            self._inference_request_queue)
        for _ in self._inference_processes:
            self._inference_request_queue.put(None)

        out_meta_json = self._result_meta_queue.get()
        self._out_project.set_meta(ProjectMeta.from_json(out_meta_json))

        for p in self._inference_processes:
            p.join()

        if not feed_status or not all(p.exitcode == 0
                                      for p in self._inference_processes):
            raise RuntimeError(
                'One of the inference processes encountered an error.')

        self._progress_report_queue.put(None)
        progress_report_thread.join()
        report_inference_finished()
Esempio n. 11
0
        def post(self):
            args = self._parser.parse_args()
            img_bytes = args[IMAGE].stream.read()
            img = sly_image.read_bytes(img_bytes)

            meta = args[META]
            ann = args[ANNOTATION]
            mode = args[MODE]

            data = {
                "request_type":
                INFERENCE,
                "meta":
                json.loads(meta.stream.read().decode("utf-8"))
                if meta is not None else ProjectMeta().to_json(),
                "annotation":
                json.loads(ann.stream.read().decode("utf-8"))
                if ann is not None else None,
                "mode":
                json.loads(mode.stream.read().decode("utf-8"))
                if mode is not None else {},
                'image_arr':
                img
            }
            return self._model._final_processing(data)
Esempio n. 12
0
def download_video_project(api, project_id, dest_dir, dataset_ids=None, download_videos=True, log_progress=False):
    '''
    Download project with given id in destination directory
    :param api: Api class object
    :param project_id: int
    :param dest_dir: str
    :param dataset_ids: list of integers
    :param download_videos: bool
    :param log_progress: bool
    '''
    LOG_BATCH_SIZE = 1

    key_id_map = KeyIdMap()

    project_fs = VideoProject(dest_dir, OpenMode.CREATE)

    meta = ProjectMeta.from_json(api.project.get_meta(project_id))
    project_fs.set_meta(meta)

    datasets_infos = []
    if dataset_ids is not None:
        for ds_id in dataset_ids:
            datasets_infos.append(api.dataset.get_info_by_id(ds_id))
    else:
        datasets_infos = api.dataset.get_list(project_id)

    for dataset in datasets_infos:
        dataset_fs = project_fs.create_dataset(dataset.name)
        videos = api.video.get_list(dataset.id)

        ds_progress = None
        if log_progress:
            ds_progress = Progress('Downloading dataset: {!r}'.format(dataset.name), total_cnt=len(videos))
        for batch in batched(videos, batch_size=LOG_BATCH_SIZE):
            video_ids = [video_info.id for video_info in batch]
            video_names = [video_info.name for video_info in batch]

            ann_jsons = api.video.annotation.download_bulk(dataset.id, video_ids)

            for video_id, video_name, ann_json in zip(video_ids, video_names, ann_jsons):
                if video_name != ann_json[ApiField.VIDEO_NAME]:
                    raise RuntimeError("Error in api.video.annotation.download_batch: broken order")

                video_file_path = dataset_fs.generate_item_path(video_name)
                if download_videos is True:
                    api.video.download_path(video_id, video_file_path)
                else:
                    touch(video_file_path)

                dataset_fs.add_item_file(video_name,
                                         video_file_path,
                                         ann=VideoAnnotation.from_json(ann_json, project_fs.meta, key_id_map),
                                         _validate_item=False)

            ds_progress.iters_done_report(len(batch))

    project_fs.set_key_id_map(key_id_map)
Esempio n. 13
0
    def merge_metas(self, src_project_id, dst_project_id):
        '''
        Add metadata from given progect to given destination project
        :param src_project_id: int
        :param dst_project_id: int
        :return: merged project metainformation
        '''
        if src_project_id == dst_project_id:
            return self.get_meta(src_project_id)

        src_meta = ProjectMeta.from_json(self.get_meta(src_project_id))
        dst_meta = ProjectMeta.from_json(self.get_meta(dst_project_id))

        new_dst_meta = src_meta.merge(dst_meta)
        new_dst_meta_json = new_dst_meta.to_json()
        self.update_meta(dst_project_id, new_dst_meta.to_json())

        return new_dst_meta_json
Esempio n. 14
0
 def _create(self):
     if dir_exists(self.directory):
         if len(list_files_recursively(self.directory)) > 0:
             raise RuntimeError(
                 "Cannot create new project {!r}. Directory {!r} already exists and is not empty"
                 .format(self.name, self.directory))
     else:
         mkdir(self.directory)
     self.set_meta(ProjectMeta())
 def __init__(self, config: dict, in_meta: ProjectMeta, model: SingleImageInferenceBase):
     validation_schema_path = pkg_resources.resource_filename(
         __name__, 'inference_modes_schemas/{}.json'.format(self.mode_name()))
     MultiTypeValidator(validation_schema_path).val(INFERENCE_MODE_CONFIG, config)
     self._config = deepcopy(config)
     self._out_meta = in_meta.clone()
     self._model = model
     model_out_meta = self._model.model_out_meta
     renamer_model = Renamer.from_json(config[MODEL_CLASSES])
     for out_class in make_renamed_classes(model_out_meta.obj_classes, renamer_model, skip_missing=True):
         self._out_meta = self._out_meta.add_obj_class(out_class)
     self._model_class_mapper = RenamingObjClassMapper(dest_obj_classes=self._out_meta.obj_classes,
                                                       renamer=renamer_model)
     self._model_img_tags_renamer = Renamer(add_suffix=config[MODEL_CLASSES][Renamer.ADD_SUFFIX])
     self._renamed_model_img_tags = make_renamed_tag_metas(model_out_meta.img_tag_metas, self._model_img_tags_renamer,
                                                           skip_missing=True)
     self._model_img_tag_meta_mapper = RenamingTagMetaMapper(dest_tag_meta_dict=self._renamed_model_img_tags,
                                                             renamer=self._model_img_tags_renamer)
     self._out_meta = self._out_meta.merge(ProjectMeta(obj_tag_metas=model_out_meta.obj_tag_metas))
Esempio n. 16
0
    def from_json(cls,
                  data,
                  project_meta: ProjectMeta,
                  key_id_map: KeyIdMap = None):
        '''
        The function from_json convert PointcloudObject from json format to PointcloudObject class object. Raise error if object class name is not found in the given project meta
        :param data: input PointcloudObject in json format
        :param project_meta: ProjectMeta class object
        :param key_id_map: KeyIdMap class object
        :return: PointcloudObject class object
        '''
        obj_class_name = data[LabelJsonFields.OBJ_CLASS_NAME]
        obj_class = project_meta.get_obj_class(obj_class_name)
        if obj_class is None:
            raise RuntimeError(
                f'Failed to deserialize a object from JSON: class name {obj_class_name!r} '
                f'was not found in the given project meta.')

        object_id = data.get(ID, None)

        existing_key = None
        if object_id is not None and key_id_map is not None:
            existing_key = key_id_map.get_object_key(object_id)
        json_key = uuid.UUID(data[KEY]) if KEY in data else None
        if (existing_key
                is not None) and (json_key
                                  is not None) and (existing_key != json_key):
            raise RuntimeError(
                "Object id = {!r}: existing_key {!r} != json_key {!r}".format(
                    object_id, existing_key, json_key))

        if existing_key is not None:
            key = existing_key
        elif json_key is not None:
            key = json_key
        else:
            key = uuid.uuid4()

        if key_id_map is not None and existing_key is None:
            key_id_map.add_object(key, object_id)

        class_id = data.get(CLASS_ID, None)
        labeler_login = data.get(LABELER_LOGIN, None)
        updated_at = data.get(UPDATED_AT, None)
        created_at = data.get(CREATED_AT, None)

        return cls(obj_class=obj_class,
                   key=key,
                   tags=VideoTagCollection.from_json(
                       data[LabelJsonFields.TAGS], project_meta.tag_metas),
                   class_id=class_id,
                   labeler_login=labeler_login,
                   updated_at=updated_at,
                   created_at=created_at)
Esempio n. 17
0
    def from_imgaug(cls,
                    img,
                    ia_boxes=None,
                    ia_masks=None,
                    index_to_class=None,
                    meta: ProjectMeta = None):
        if ((ia_boxes is not None) or (ia_masks is not None)) and meta is None:
            raise ValueError("Project meta has to be provided")

        labels = []
        if ia_boxes is not None:
            for ia_box in ia_boxes:
                obj_class = meta.get_obj_class(ia_box.label)
                if obj_class is None:
                    raise KeyError(
                        "Class {!r} not found in project meta".format(
                            ia_box.label))
                lbl = Label(
                    Rectangle(top=ia_box.y1,
                              left=ia_box.x1,
                              bottom=ia_box.y2,
                              right=ia_box.x2), obj_class)
                labels.append(lbl)

        if ia_masks is not None:
            if index_to_class is None:
                raise ValueError(
                    "mapping from index to class name is needed to transform masks to SLY format"
                )
            class_mask = ia_masks.get_arr()
            # mask = white_mask == 255
            (unique, counts) = np.unique(class_mask, return_counts=True)
            for index, count in zip(unique, counts):
                if index == 0:
                    continue
                mask = class_mask == index
                bitmap = Bitmap(data=mask[:, :, 0])
                restore_class = meta.get_obj_class(index_to_class[index])
                labels.append(Label(geometry=bitmap, obj_class=restore_class))

        return cls(img_size=img.shape[:2], labels=labels)
Esempio n. 18
0
    def _read(self):
        meta_json = load_json_file(self._get_project_meta_path())
        self._meta = ProjectMeta.from_json(meta_json)

        possible_datasets = get_subdirs(self.directory)
        for ds_name in possible_datasets:
            current_dataset = Dataset(os.path.join(self.directory, ds_name),
                                      OpenMode.READ)
            self._datasets = self._datasets.add(current_dataset)

        if self.total_items == 0:
            raise RuntimeError('Project is empty')
Esempio n. 19
0
 def _create(self):
     '''
     Creates a leaf directory and empty meta.json file. Generate exception error if project directory already exists and is not empty.
     '''
     if dir_exists(self.directory):
         if len(list_files_recursively(self.directory)) > 0:
             raise RuntimeError(
                 "Cannot create new project {!r}. Directory {!r} already exists and is not empty"
                 .format(self.name, self.directory))
     else:
         mkdir(self.directory)
     self.set_meta(ProjectMeta())
Esempio n. 20
0
 def get_output_meta(self, id, input_meta=None, inference_mode=None):
     data = {
         "request_type": "get_out_meta",
         "meta": input_meta or ProjectMeta().to_json(),
         "mode": inference_mode or {}
     }
     encoder = MultipartEncoder({'id': str(id).encode('utf-8'),
                                 'data': json.dumps(data)})
     response = self._api.post('models.infer', MultipartEncoderMonitor(encoder))
     response_json = response.json()
     if 'out_meta' in response_json:
         return response_json['out_meta']
     return response.json()
Esempio n. 21
0
    def _load_train_config(self):  # @TODO: partly copypasted from SingleImageInferenceBase
        self._load_raw_model_config_json()

        self.classification_tags = self._model_out_img_tags()
        logger.info('Read model out tags', extra={'tags': self.classification_tags.to_json()})
        self.classification_tags_to_idx = self.train_config[self.classification_tags_to_idx_key]
        logger.info('Read model internal tags mapping', extra={'tags_mapping': self.classification_tags_to_idx})

        self._model_out_meta = ProjectMeta(obj_classes=ObjClassCollection(),
                                           img_tag_metas=self.classification_tags,
                                           obj_tag_metas=self._model_out_obj_tags())

        self.idx_to_classification_tags = {v: k for k, v in self.classification_tags_to_idx.items()}
        self._determine_model_input_size()
Esempio n. 22
0
 def inference_remote_image(self, id, image_hash, ann=None, meta=None, mode=None):
     data = {
         "request_type": "inference",
         "meta": meta or ProjectMeta().to_json(),
         "annotation": ann or None,
         "mode": mode or {},
         "image_hash": image_hash
     }
     fake_img_data = sly_image.write_bytes(np.zeros([5, 5, 3]), '.jpg')
     encoder = MultipartEncoder({'id': str(id).encode('utf-8'),
                                 'data': json.dumps(data),
                                 'image': ("img", fake_img_data, "")})
     response = self._api.post('models.infer', MultipartEncoderMonitor(encoder))
     return response.json()
Esempio n. 23
0
    def from_json(cls, data, project_meta):
        '''
        The function from_json convert annotation from json format to Annotation class object. If one of the labels
        of annotation in json format cannot be convert to Label class object it generate exception error.
        :param data: input annotation in json format
        :param project_meta: ProjectMeta class object
        :return: Annotation class object
        '''
        img_size_dict = data[AnnotationJsonFields.IMG_SIZE]
        img_height = img_size_dict[AnnotationJsonFields.IMG_SIZE_HEIGHT]
        img_width = img_size_dict[AnnotationJsonFields.IMG_SIZE_WIDTH]
        img_size = (img_height, img_width)
        try:
            labels = [
                Label.from_json(label_json, project_meta)
                for label_json in data[AnnotationJsonFields.LABELS]
            ]
        except Exception:
            logger.fatal(
                'Failed to deserialize annotation from JSON format. One of the Label objects could not be '
                'deserialized')
            raise

        custom_data = data.get(AnnotationJsonFields.CUSTOM_DATA, {})
        prob_labels = None
        if AnnotationJsonFields.PROBABILITY_LABELS in custom_data and \
                AnnotationJsonFields.PROBABILITY_CLASSES in custom_data:

            prob_classes = ObjClassCollection.from_json(
                custom_data[AnnotationJsonFields.PROBABILITY_CLASSES])

            # @TODO: tony, maybe link with project meta (add probability classes???)
            prob_project_meta = ProjectMeta(obj_classes=prob_classes)
            prob_labels = [
                Label.from_json(label_json, prob_project_meta) for label_json
                in custom_data[AnnotationJsonFields.PROBABILITY_LABELS]
            ]

            custom_data.pop(AnnotationJsonFields.PROBABILITY_CLASSES)
            custom_data.pop(AnnotationJsonFields.PROBABILITY_LABELS)

        return cls(img_size=img_size,
                   labels=labels,
                   img_tags=TagCollection.from_json(
                       data[AnnotationJsonFields.IMG_TAGS],
                       project_meta.tag_metas),
                   img_description=data.get(
                       AnnotationJsonFields.IMG_DESCRIPTION, ""),
                   pixelwise_scores_labels=prob_labels,
                   custom_data=custom_data)
Esempio n. 24
0
    def _load_train_config(self):
        self._load_raw_model_config_json()

        self.class_title_to_idx = self.train_config[self.class_title_to_idx_key]
        logger.info('Read model internal class mapping', extra={'class_mapping': self.class_title_to_idx})
        train_classes = ObjClassCollection.from_json(self.train_config[self.train_classes_key])
        logger.info('Read model out classes', extra={'classes': train_classes.to_json()})

        # TODO: Factor out meta constructing from _load_train_config method.
        self._model_out_meta = ProjectMeta(obj_classes=train_classes, tag_metas=self._model_out_tags())
        # Make a separate [index] --> [class] map that excludes the 'special' classes that should not be in the`
        # final output.
        self.out_class_mapping = {idx: train_classes.get(title) for title, idx in self.class_title_to_idx.items() if
                                  train_classes.has_key(title)}
Esempio n. 25
0
    def from_json(cls, data, project_meta: ProjectMeta, key_id_map: KeyIdMap = None):
        obj_class_name = data[LabelJsonFields.OBJ_CLASS_NAME]
        obj_class = project_meta.get_obj_class(obj_class_name)
        if obj_class is None:
            raise RuntimeError(f'Failed to deserialize a object from JSON: class name {obj_class_name!r} '
                               f'was not found in the given project meta.')

        key = uuid.UUID(data[KEY]) if KEY in data else uuid.uuid4()

        if key_id_map is not None:
            key_id_map.add_object(key, data.get(ID, None))

        return cls(obj_class=obj_class,
                   key=key,
                   tags=VideoTagCollection.from_json(data[LabelJsonFields.TAGS], project_meta.tag_metas))
Esempio n. 26
0
    def _do_single_img_inference(self, img, in_msg):
        in_project_meta = self._in_project_meta_from_msg(in_msg)
        ann_json = in_msg.get('annotation')
        if ann_json is not None:
            if in_project_meta is None:
                raise ValueError('In order to perform inference with annotation you must specify the appropriate'
                                 ' project meta.')
            ann = Annotation.from_json(ann_json, in_project_meta)
        else:
            in_project_meta = in_project_meta or ProjectMeta()
            ann = Annotation(img.shape[:2])

        inference_mode = self._make_inference_mode(in_msg.get(MODE, {}), in_project_meta)
        inference_result = inference_mode.infer_annotate(img, ann)
        return inference_result.to_json()
Esempio n. 27
0
    def _read(self):
        '''
        Download project from given project directory. Checks item and annotation directoris existing and dataset not empty.
        Consistency checks. Every image must have an annotation, and the correspondence must be one to one.
        '''
        meta_json = load_json_file(self._get_project_meta_path())
        self._meta = ProjectMeta.from_json(meta_json)

        possible_datasets = get_subdirs(self.directory)
        for ds_name in possible_datasets:
            current_dataset = self.dataset_class(os.path.join(self.directory, ds_name), OpenMode.READ)
            self._datasets = self._datasets.add(current_dataset)

        if self.total_items == 0:
            raise RuntimeError('Project is empty')
    def inference(self, id, img, ann=None, meta=None, mode=None, ext=None):
        data = {
            "request_type": "inference",
            "meta": meta or ProjectMeta().to_json(),
            "annotation": ann or None,
            "mode": mode or {},
        }
        img_data = sly_image.write_bytes(img, ext or '.jpg')
        encoder = MultipartEncoder({
            'id': str(id).encode('utf-8'),
            'data': json.dumps(data),
            'image': ("img", img_data, "")
        })

        response = self.api.post('models.infer',
                                 MultipartEncoderMonitor(encoder))
        return response.json()
Esempio n. 29
0
    def setUp(self):
        self._obj_class_gt = ObjClass(name='a', geometry_type=Rectangle)
        self._obj_class_pred = ObjClass(name='b', geometry_type=Rectangle)
        self._confidence_tag_meta = TagMeta(name='confidence', value_type=TagValueType.ANY_NUMBER)
        self._meta = ProjectMeta(
            obj_classes=ObjClassCollection([self._obj_class_gt, self._obj_class_pred]),
            tag_metas=TagMetaCollection([self._confidence_tag_meta]))

        # Will match self._pred_obj_1
        self._gt_obj_1 = Label(obj_class=self._obj_class_gt, geometry=Rectangle(0, 0, 10, 10))

        # Will match self._pred_obj_3
        self._gt_obj_2 = Label(obj_class=self._obj_class_gt, geometry=Rectangle(13, 13, 15, 15))

        # Will be a false negative
        self._gt_obj_3 = Label(obj_class=self._obj_class_gt, geometry=Rectangle(43, 43, 45, 45))

        # Will match self._gt_obj_1
        self._pred_obj_1 = Label(
            obj_class=self._obj_class_pred,
            geometry=Rectangle(0, 0, 9, 9),
            tags=TagCollection([Tag(meta=self._confidence_tag_meta, value=0.7)]))

        # Will be a false positive (self._pred_obj_1 has higher IoU).
        self._pred_obj_2 = Label(
            obj_class=self._obj_class_pred,
            geometry=Rectangle(0, 0, 8, 8),
            tags=TagCollection([Tag(meta=self._confidence_tag_meta, value=0.6)]))

        # Will match self._gt_obj_2
        self._pred_obj_3 = Label(
            obj_class=self._obj_class_pred,
            geometry=Rectangle(13, 13, 15, 15),
            tags=TagCollection([Tag(meta=self._confidence_tag_meta, value=0.1)]))

        # More false positives.
        self._pred_objs_fp = [
            Label(obj_class=self._obj_class_pred,
                  geometry=Rectangle(20, 20, 30, 30),
                  tags=TagCollection([Tag(meta=self._confidence_tag_meta, value=v / 100)]))
            for v in range(15, 85, 10)]

        self._metric_calculator = MAPMetric(class_mapping={'a': 'b'}, iou_threshold=0.5)
Esempio n. 30
0
    def from_json(cls, data, project_meta: ProjectMeta):
        obj_class_name = data[LabelJsonFields.OBJ_CLASS_NAME]
        obj_class = project_meta.get_obj_class(obj_class_name)
        if obj_class is None:
            raise RuntimeError(
                f'Failed to deserialize a Label object from JSON: label class name {obj_class_name!r} '
                f'was not found in the given project meta.')

        if obj_class.geometry_type is AnyGeometry:
            geometry_type_actual = GET_GEOMETRY_FROM_STR(
                data[GEOMETRY_TYPE] if GEOMETRY_TYPE in
                data else data[GEOMETRY_SHAPE])
            geometry = geometry_type_actual.from_json(data)
        else:
            geometry = obj_class.geometry_type.from_json(data)

        return cls(geometry=geometry,
                   obj_class=obj_class,
                   tags=TagCollection.from_json(data[LabelJsonFields.TAGS],
                                                project_meta.tag_metas),
                   description=data.get(LabelJsonFields.DESCRIPTION, ""))