def test_open(self): with TemporaryDirectory() as dataset_dir: dataset = DirectoryROCODataset(Path(dataset_dir), "fake") annotation = ROCOAnnotation("frame_1", objects=[], has_rune=False, w=160, h=90) image = asarray([[[250, 0, 0], [250, 0, 0]], [[250, 0, 0], [250, 0, 0]]]).astype(float32) dataset.annotations_dir.mkdir() dataset.images_dir.mkdir() (dataset.annotations_dir / "frame_1.xml").write_text( annotation.to_xml()) save_image(image, dataset.images_dir / "frame_1.jpg") image_dataset = dataset.open() self.assertEqual([annotation], list(image_dataset.targets)) images = list(image_dataset.examples) self.assertEqual(1, len(images)) assert_array_almost_equal(image / 256, images[0] / 256, decimal=2) # jpeg precision
def improve_dji_roco_dataset_by_zooming_and_perturbating( dset: LazyROCODataset, zoomer: Zoomer, perturbator: ImagePerturbator): image_dir, annotation_dir = _prepare_empty_zoomed_dir( DJI_ROCO_ZOOMED_DSET_DIR / camel2snake(dset.name).lower()) for img, annotation, name in smart_tqdm(dset, desc=f"Processing {dset}", unit="image"): for zoomed_image, zoomed_annotation, zoomed_name in zoomer.zoom( img, annotation, name): zoomed_image = perturbator.perturbate(zoomed_image) save_image(zoomed_image, image_dir / f"{zoomed_name}.jpg") (annotation_dir / f"{zoomed_name}.xml").write_text( zoomed_annotation.to_xml())
def test_image(self): with TemporaryDirectory() as dataset_dir: dataset_dir = Path(dataset_dir) annotation = ROCOAnnotation(objects=[], has_rune=False, w=160, h=90) image = asarray([[[250, 0, 0], [250, 0, 0]], [[250, 0, 0], [250, 0, 0]]]).astype(float32) images_dir, annotations_dir = self._setup_dir(dataset_dir) (annotations_dir / "frame_1.xml").write_text(annotation.to_xml()) save_image(image, images_dir / "frame_1.jpg") dataset = ROCODatasetBuilder(dataset_dir, "fake").to_images().build() self.assertEqual([annotation], list(dataset.targets)) images = list(dataset.examples) self.assertEqual(1, len(images)) assert_array_almost_equal(image / 256, images[0] / 256, decimal=2) # jpeg precision
def save(self, directory_path: Path, name: str): save_image(self.image, directory_path / "image" / f"{name}.jpg") self._save_annotation(directory_path / "image_annotation" / f"{name}.xml")
def _save_one(self, img: Image, target: T, name: str): save_image(img, self.cache_dir / f"{name}-{str(target)}.jpg")
def save_with_image(self, directory: Path, image: Image, name: str): self.save_in_directory(directory / "image_annotation", name) save_image(image, (directory / "image" / name).with_suffix(".jpg"))