def write(path, img, remove_alpha_channel=True): ''' The function write saves the image to the specified file. It create directory from path if the directory for this path does not exist. It generate exception error(UnsupportedImageFormat) if file extention is not in list of supported images extensions('.jpg', '.jpeg', '.mpo', '.bmp', '.png', '.webp'). :param path: the path to the output file :param img: image in RGB format(numpy matrix) ''' ensure_base_path(path) validate_ext(path) res_img = img.copy() if len(img.shape) == 2: res_img = np.expand_dims(img, 2) cnt_channels = res_img.shape[2] if cnt_channels == 4: if remove_alpha_channel is True: res_img = cv2.cvtColor(res_img.astype(np.uint8), cv2.COLOR_RGBA2BGR) else: res_img = cv2.cvtColor(res_img.astype(np.uint8), cv2.COLOR_RGBA2BGRA) elif cnt_channels == 3: res_img = cv2.cvtColor(res_img.astype(np.uint8), cv2.COLOR_RGB2BGR) elif cnt_channels == 1: res_img = cv2.cvtColor(res_img.astype(np.uint8), cv2.COLOR_GRAY2BGR) return cv2.imwrite(path, res_img)
def download_import_file(self, id, file_path, save_path): response = self._api.post('tasks.import.download_file', {ApiField.ID: id, ApiField.FILENAME: file_path}, stream=True) ensure_base_path(save_path) with open(save_path, 'wb') as fd: for chunk in response.iter_content(chunk_size=1024 * 1024): fd.write(chunk)
def download_git_archive(self, ecosystem_item_id, app_id, version, save_path, log_progress=True, ext_logger=None): payload = { ApiField.ECOSYSTEM_ITEM_ID: ecosystem_item_id, ApiField.VERSION: version, "isArchive": True } if app_id is not None: payload[ApiField.APP_ID] = app_id response = self._api.post('ecosystem.file.download', payload, stream=True) if log_progress: if ext_logger is None: ext_logger = logger length = -1 # Content-Length if "Content-Length" in response.headers: length = int(response.headers['Content-Length']) progress = Progress("Downloading: ", length, ext_logger=ext_logger, is_size=True) mb1 = 1024 * 1024 ensure_base_path(save_path) with open(save_path, 'wb') as fd: log_size = 0 for chunk in response.iter_content(chunk_size=mb1): fd.write(chunk) log_size += len(chunk) if log_progress and log_size > mb1: progress.iters_done_report(log_size) log_size = 0
def download_related_image(self, id, path): response = self._api.post('point-clouds.images.download', {ApiField.ID: id}, stream=True) ensure_base_path(path) with open(path, 'wb') as fd: for chunk in response.iter_content(chunk_size=1024 * 1024): fd.write(chunk) return response
def download(self, team_id, remote_path, local_save_path): response = self._api.post('file-storage.download', { ApiField.TEAM_ID: team_id, ApiField.PATH: remote_path }, stream=True) ensure_base_path(local_save_path) with open(local_save_path, 'wb') as fd: for chunk in response.iter_content(chunk_size=1024 * 1024): fd.write(chunk)
def download_to_tar(self, workspace_id, name, tar_path, progress_cb=None): model = self.get_info_by_name(workspace_id, name) response = self.download(model.id) ensure_base_path(tar_path) with open(tar_path, 'wb') as fd: for chunk in response.iter_content(chunk_size=1024 * 1024): fd.write(chunk) if progress_cb is not None: read_mb = len(chunk) / 1024.0 / 1024.0 progress_cb(read_mb)
def _download(self, team_id, remote_path, local_save_path, progress_cb=None): # TODO: progress bar response = self._api.post('file-storage.download', {ApiField.TEAM_ID: team_id, ApiField.PATH: remote_path}, stream=True) #print(response.headers) #print(response.headers['Content-Length']) ensure_base_path(local_save_path) with open(local_save_path, 'wb') as fd: for chunk in response.iter_content(chunk_size=1024 * 1024): fd.write(chunk) if progress_cb is not None: progress_cb(len(chunk))
def download_path(self, id, path): ''' Download image with given id and saves it for a given path :param id: int :param path: str ''' response = self._download(id, is_stream=True) ensure_base_path(path) with open(path, 'wb') as fd: for chunk in response.iter_content(chunk_size=1024 * 1024): fd.write(chunk)
def download_path(self, remote_path, save_path, progress_cb=None): ensure_base_path(save_path) response = self._api.post('remote-storage.download', {ApiField.LINK: remote_path}, stream=True) # if "Content-Length" in response.headers: # length = int(response.headers['Content-Length']) with open(save_path, 'wb') as fd: for chunk in response.iter_content(chunk_size=1024 * 1024): fd.write(chunk) if progress_cb is not None: progress_cb(len(chunk))
def download_path(self, video_id, frame_index, path): ''' :param video_id: int :param frame_index: int :param path: str :return: write image on the given path for frame with given index from given video id ''' response = self._download(video_id, frame_index) ensure_base_path(path) with open(path, 'wb') as fd: for chunk in response.iter_content(chunk_size=1024 * 1024): fd.write(chunk)
def write(path, img): ''' The function write saves the image to the specified file. It create directory from path if the directory for this path does not exist. It generate exception error(UnsupportedImageFormat) if file extention is not in list of supported images extensions('.jpg', '.jpeg', '.mpo', '.bmp', '.png', '.webp'). :param path: the path to the output file :param img: image in RGB format(numpy matrix) ''' ensure_base_path(path) validate_ext(path) img = cv2.cvtColor(img.astype(np.uint8), cv2.COLOR_RGB2BGR) return cv2.imwrite(path, img) # why return there?
def download_paths_by_hashes(self, hashes, paths, progress_cb=None): if len(hashes) == 0: return if len(hashes) != len(paths): raise RuntimeError("Can not match \"hashes\" and \"paths\" lists, len(hashes) != len(paths)") h_to_path = {h: path for h, path in zip(hashes, paths)} for h, resp_part in self._download_batch_by_hashes(list(set(hashes))): ensure_base_path(h_to_path[h]) with open(h_to_path[h], 'wb') as w: w.write(resp_part.content) if progress_cb is not None: progress_cb(1)
def download_tar(github_url, tar_path, github_token=None, version="master", log_progress=True): headers = {} if github_token is not None: headers = {"Authorization": "token {}".format(github_token)} ensure_base_path(tar_path) if ".git" not in github_url: github_url += ".git" tar_url = github_url.replace(".git", "/archive/{}.tar.gz".format(version)) r = requests.get(tar_url, headers=headers, stream=True) if r.status_code != requests.codes.ok: Api._raise_for_status(r) progress = Progress("Downloading (KB)", len(r.content) / 1024) with open(tar_path, 'wb') as f: for chunk in r.iter_content(chunk_size=8192): f.write(chunk) progress.iters_done_report(len(chunk) / 1024)
def download_path(self, id, path): response = self._download(id) ensure_base_path(path) with open(path, 'wb') as fd: for chunk in response.iter_content(chunk_size=1024 * 1024): fd.write(chunk)
def _read_obj_impl(self, st_path, dst_path): sly_fs.ensure_base_path(dst_path) sly_fs.copy_file(st_path, dst_path)
def write(path, img): ensure_base_path(path) validate_ext(path) img = cv2.cvtColor(img.astype(np.uint8), cv2.COLOR_RGB2BGR) return cv2.imwrite(path, img)
def _write_obj_impl(self, src_path, st_path): sly_fs.ensure_base_path(st_path) self._copy_file_concurr(src_path, st_path)