Exemplo n.º 1
0
 def download(self,
              team_id,
              remote_path,
              local_save_path,
              cache: FileCache = None,
              progress_cb=None):
     if cache is None:
         self._download(team_id, remote_path, local_save_path, progress_cb)
     else:
         file_info = self.get_info_by_path(team_id, remote_path)
         if file_info.hash is None:
             self._download(team_id, remote_path, local_save_path,
                            progress_cb)
         else:
             cache_path = cache.check_storage_object(
                 file_info.hash, get_file_ext(remote_path))
             if cache_path is None:
                 # file not in cache
                 self._download(team_id, remote_path, local_save_path,
                                progress_cb)
                 if file_info.hash != get_file_hash(local_save_path):
                     raise KeyError(
                         f"Remote and local hashes are different (team id: {team_id}, file: {remote_path})"
                     )
                 cache.write_object(local_save_path, file_info.hash)
             else:
                 cache.read_object(file_info.hash, local_save_path)
                 if progress_cb is not None:
                     progress_cb(get_file_size(local_save_path))
Exemplo n.º 2
0
 def _convert_json_info(self, info: dict, skip_missing=True):
     if info is None:
         return None
     temp_ext = None
     field_values = []
     for field_name in self.info_sequence():
         if field_name == ApiField.EXT:
             continue
         if skip_missing is True:
             val = info.get(field_name, None)
         else:
             val = info[field_name]
         field_values.append(val)
         if field_name == ApiField.MIME:
             temp_ext = val.split('/')[1]
             field_values.append(temp_ext)
     for idx, field_name in enumerate(self.info_sequence()):
         if field_name == ApiField.NAME:
             cur_ext = get_file_ext(field_values[idx]).replace(".", "").lower()
             if not cur_ext:
                 field_values[idx] = "{}.{}".format(field_values[idx], temp_ext)
                 break
             if temp_ext == 'jpeg' and cur_ext in ['jpg', 'jpeg', 'mpo']:
                 break
             if temp_ext != cur_ext:
                 field_values[idx] = "{}.{}".format(field_values[idx], temp_ext)
             break
     return self.InfoType(*field_values)
Exemplo n.º 3
0
def generate_names(base_name, count):
    name = sly_fs.get_file_name(base_name)
    ext = sly_fs.get_file_ext(base_name)

    names = [base_name]
    for idx in range(1, count):
        names.append('{}_{:02d}{}'.format(name, idx, ext))

    return names
Exemplo n.º 4
0
def gen_video_stream_name(file_name, stream_index):
    '''
    Create name to video stream from given filename and index of stream
    :param file_name: str
    :param stream_index: int
    :return: str
    '''
    return "{}_stream_{}_{}{}".format(get_file_name(file_name), stream_index,
                                      rand_str(5), get_file_ext(file_name))
Exemplo n.º 5
0
    def _get_free_name(exist_check_fn, name):
        res_title = name
        suffix = 1

        name_without_ext = get_file_name(name)
        ext = get_file_ext(name)

        while exist_check_fn(res_title):
            res_title = '{}_{:03d}{}'.format(name_without_ext, suffix, ext)
            suffix += 1
        return res_title
Exemplo n.º 6
0
def generate_free_name(used_names, possible_name, with_ext=False):
    res_name = possible_name
    new_suffix = 1
    while res_name in set(used_names):
        if with_ext is True:
            res_name = '{}_{:02d}{}'.format(
                sly_fs.get_file_name(possible_name), new_suffix,
                sly_fs.get_file_ext(possible_name))
        else:
            res_name = '{}_{:02d}'.format(possible_name, new_suffix)
        new_suffix += 1
    return res_name
Exemplo n.º 7
0
def load_font(font_file_name: str, font_size: int = 12) -> ImageFont.FreeTypeFont:
    """
    Set global font true-type for drawing.
    Args:
        font_file_name: name of font file (example: 'DejaVuSansMono.ttf')
        font_size: selected font size
    Returns:
        loaded from file font
    """
    if get_file_ext(font_file_name) == FONT_EXTENSION:
        font_path = _get_font_path_by_name(font_file_name)
        if (font_path is not None) and file_exists(font_path):
            return ImageFont.truetype(font_path, font_size, encoding='utf-8')
        else:
            raise ValueError(
                'Font file "{}" not found in system paths. Try to set another font.'.format(font_file_name))
    else:
        raise ValueError('Supported only TrueType fonts!')
Exemplo n.º 8
0
    def get_free_name(self, team_id, path):
        directory = Path(path).parent
        name = get_file_name(path)
        ext = get_file_ext(path)
        res_name = name
        suffix = 0

        def _combine(suffix: int = None):
            res = "{}/{}".format(directory, res_name)
            if suffix is not None:
                res += "_{:03d}".format(suffix)
            if ext:
                res += "{}".format(ext)
            return res

        res_path = _combine()
        while self.exists(team_id, res_path):
            res_path = _combine(suffix)
            suffix += 1
        return res_path
Exemplo n.º 9
0
def create_img_infos(project_fs):
    tag_id_map = {
        tag["name"]: tag["id"]
        for tag in project_fs.meta.tag_metas.to_json()
    }
    images_infos = []
    for dataset_fs in project_fs:
        img_info_dir = os.path.join(dataset_fs.directory, "img_info")
        mkdir(img_info_dir)
        for idx, item_name in enumerate(os.listdir(dataset_fs.item_dir)):
            item_ext = get_file_ext(item_name).lstrip(".")
            item_path = os.path.join(dataset_fs.item_dir, item_name)
            item = sly.image.read(item_path)
            h, w = item.shape[:2]
            item_size = os.path.getsize(item_path)
            created_at = datetime.fromtimestamp(
                os.stat(item_path).st_ctime,
                tz=timezone.utc).strftime("%d-%m-%Y %H:%M:%S")
            modified_at = datetime.fromtimestamp(
                os.stat(item_path).st_mtime,
                tz=timezone.utc).strftime("%d-%m-%Y %H:%M:%S")

            item_ann_path = os.path.join(dataset_fs.ann_dir,
                                         f"{item_name}.json")
            ann_json = load_json_file(item_ann_path)
            ann = sly.Annotation.from_json(ann_json, project_fs.meta)
            tags = ann.img_tags
            tags_json = tags.to_json()
            labels_count = len(ann.labels)

            tags_img_info = []
            for tag in tags_json:
                tag_info = {
                    "entityId": None,
                    "tagId": tag_id_map[tag["name"]],
                    "id": None,
                    "labelerLogin": tag["labelerLogin"],
                    "createdAt": tag["createdAt"],
                    "updatedAt": tag["updatedAt"],
                    "name": tag["name"]
                }
                tags_img_info.append(tag_info)

            item_img_info = {
                "id": idx,
                "name": item_name,
                "link": "",
                "hash": "",
                "mime": f"image/{item_ext}",
                "ext": item_ext,
                "size": item_size,
                "width": w,
                "height": h,
                "labels_count": labels_count,
                "dataset_id": dataset_fs.name,
                "created_at": created_at,
                "updated_at": modified_at,
                "meta": {},
                "path_original": "",
                "full_storage_url": "",
                "tags": tags_img_info
            }
            save_path = os.path.join(img_info_dir, f"{item_name}.json")
            dump_json_file(item_img_info, save_path)
            images_infos.append(item_img_info)
    return images_infos
Exemplo n.º 10
0
    parser = argparse.ArgumentParser(description='Inference REST client for standalone Supervisely models.')
    parser.add_argument('--server-url', required=True)
    parser.add_argument('--request-type', required=True, choices=SUPPORTED_REQUEST_TYPES)
    parser.add_argument('--in-image', default='')
    parser.add_argument('--out-json', default='')
    args = parser.parse_args()

    request_url = args.server_url + '/' + MODEL + '/' + args.request_type

    response_json = None
    if args.request_type == GET_OUTPUT_META:
        response = requests.post(request_url)
    elif args.request_type == INFERENCE:
        with open(args.in_image, 'rb') as fin:
            img_bytes = fin.read()
        img_ext = sly_fs.get_file_ext(args.in_image)
        encoder = MultipartEncoder({IMAGE: (args.in_image, io.BytesIO(img_bytes), 'application/octet-stream')})
        response = requests.post(request_url, data=encoder, headers={'Content-Type': encoder.content_type})
    else:
        raise ValueError(
            'Unknown model request type: {!r}. Only the following request types are supported: {!r}.'.format(
                args.request_type, SUPPORTED_REQUEST_TYPES))

    response.raise_for_status()
    response_str = json.dumps(response.json(), indent=4, sort_keys=True)

    if args.out_json:
        with open(args.out_json, 'w') as fout:
            fout.write(response_str)
    else:
        print(response_str)
Exemplo n.º 11
0
 def img_to_hash(item):
     img, name = item[0], item[1]
     return sly_image.get_hash(img, get_file_ext(name))
Exemplo n.º 12
0
 def img_to_bytes_stream(item):
     img, name = item[0], item[1]
     img_bytes = sly_image.write_bytes(img, get_file_ext(name))
     return io.BytesIO(img_bytes)
Exemplo n.º 13
0
 def _get_suffix(self, path):
     return sly_fs.get_file_ext(path)