def _add_img_np(self, item_name, img): self._check_add_item_name(item_name) dst_img_path = os.path.join(self.img_dir, item_name) sly_image.write(dst_img_path, img) # This is a new-style annotation name, so if there was no image with this name yet, there should not have been # an annotation either. self._item_to_ann[item_name] = item_name + ANN_EXT
def get_image_and_ann(): mkdir(image_dir_path) mkdir(ann_dir) image_path = os.path.join(image_dir_path, image_name) api.image.download_path(image_id, image_path) image_ext_to_png(image_path) mask_color, mask_label, poly_json = from_ann_to_cityscapes_mask( ann, name2id, app_logger, train_val_flag) # dump_json_file(poly_json, # os.path.join(ann_dir, get_file_name(base_image_name) + cityscapes_polygons_suffix)) # write( # os.path.join(ann_dir, # get_file_name(base_image_name) + cityscapes_color_suffix), mask_color) # write( # os.path.join(ann_dir, # get_file_name(base_image_name) + cityscapes_labels_suffix), mask_label) dump_json_file( poly_json, os.path.join( ann_dir, get_file_name(base_image_name).replace('_leftImg8bit', '') + cityscapes_polygons_suffix)) write( os.path.join( ann_dir, get_file_name(base_image_name).replace('_leftImg8bit', '') + cityscapes_color_suffix), mask_color) write( os.path.join( ann_dir, get_file_name(base_image_name).replace('_leftImg8bit', '') + cityscapes_labels_suffix), mask_label)
def save_project_as_pascal_voc_detection(save_path, project: Project): # Create root pascal 'datasets' folders for dataset in project.datasets: pascal_dataset_path = os.path.join(save_path, dataset.name) pascal_dataset_relative_path = os.path.relpath(pascal_dataset_path, save_path) images_dir = os.path.join(pascal_dataset_path, 'JPEGImages') anns_dir = os.path.join(pascal_dataset_path, 'Annotations') lists_dir = os.path.join(pascal_dataset_path, 'ImageSets/Layout') fs_utils.mkdir(pascal_dataset_path) for subdir in [ 'ImageSets', # Train list, Val list, etc. 'ImageSets/Layout', 'Annotations', 'JPEGImages' ]: fs_utils.mkdir(os.path.join(pascal_dataset_path, subdir)) samples_by_tags = defaultdict(list) # TRAIN: [img_1, img2, ..] for item_name in dataset: img_path, ann_path = dataset.get_item_paths(item_name) no_ext_name = fs_utils.get_file_name(item_name) pascal_img_path = os.path.join(images_dir, no_ext_name + OUT_IMG_EXT) pascal_ann_path = os.path.join(anns_dir, no_ext_name + XML_EXT) if item_name.endswith(OUT_IMG_EXT): fs_utils.copy_file(img_path, pascal_img_path) else: img = image_utils.read(img_path) image_utils.write(pascal_img_path, img) ann = Annotation.load_json_file(ann_path, project_meta=project.meta) # Read tags for images lists generation for tag in ann.img_tags: samples_by_tags[tag.name].append( (no_ext_name, len(ann.labels))) writer = pascal_voc_writer.Writer( path=pascal_dataset_relative_path, width=ann.img_size[1], height=ann.img_size[0]) for label in ann.labels: obj_class = label.obj_class rect: Rectangle = label.geometry.to_bbox() writer.addObject(name=obj_class.name, xmin=rect.left, ymin=rect.top, xmax=rect.right, ymax=rect.bottom) writer.save(pascal_ann_path) save_images_lists(lists_dir, samples_by_tags)
def _add_img_np(self, item_name, img): ''' Write given image(RGB format(numpy matrix)) to dataset items directory. Generate exception error if item_name already exists in dataset or item name has unsupported extension :param item_name: str :param img: image in RGB format(numpy matrix) ''' self._check_add_item_name(item_name) dst_img_path = os.path.join(self.item_dir, item_name) sly_image.write(dst_img_path, img)
def draw_pretty(self, bitmap: np.ndarray, color: List[int, int, int] = None, thickness: int = 1, opacity: float = 0.5, draw_tags: bool = False, output_path: str = None) -> None: height, width = bitmap.shape[:2] vis_filled = np.zeros((height, width, 3), np.uint8) self.draw(vis_filled, color=color, thickness=thickness, draw_tags=draw_tags) vis = cv2.addWeighted(bitmap, 1, vis_filled, opacity, 0) np.copyto(bitmap, vis) self.draw_contour(bitmap, color=color, thickness=thickness, draw_tags=draw_tags) if output_path: sly_image.write(output_path, bitmap)
def _add_img_np(self, item_name, img): self._check_add_item_name(item_name) dst_img_path = os.path.join(self.img_dir, item_name) sly_image.write(dst_img_path, img)
def _add_img_np(self, item_name, img, img_ext): dst_img_path = self.deprecated_make_img_path(item_name, img_ext) image.write(dst_img_path, img) self._items_exts[item_name] = img_ext