|
| 1 | +import json |
| 2 | +import os |
| 3 | +from typing import Tuple |
| 4 | + |
| 5 | +import PIL |
| 6 | +from datumaro.components.annotation import Bbox |
| 7 | +from datumaro.components.dataset import Dataset |
| 8 | +from datumaro.components.dataset_base import DatasetItem |
| 9 | +from datumaro.components.media import Image |
| 10 | + |
| 11 | +from clarifai_datautils.errors import AnnotationsDatasetError, AnnotationsFormatError |
| 12 | + |
| 13 | + |
| 14 | +class Clarifai_to_Datumaro(): |
| 15 | + |
| 16 | + def __init__( |
| 17 | + self, |
| 18 | + main_path: str, |
| 19 | + ): |
| 20 | + """Converts a clarifai dataset to a Datumaro dataset. |
| 21 | +
|
| 22 | + Args: |
| 23 | + path (str): The path to the clarifai dataset. |
| 24 | +
|
| 25 | + """ |
| 26 | + self.main_path = main_path |
| 27 | + self.image_list = os.listdir(os.path.join(self.main_path, 'inputs')) |
| 28 | + self.annotations_list = os.listdir(os.path.join(self.main_path, 'annotations')) |
| 29 | + self.label_map = {} |
| 30 | + |
| 31 | + def convert(self) -> Dataset: |
| 32 | + """Check folder format and creates a Datumaro Dataset. |
| 33 | +
|
| 34 | + Returns: |
| 35 | + A Datumaro dataset object. |
| 36 | + """ |
| 37 | + self.check_folder() |
| 38 | + # create a dataset |
| 39 | + dataset = Dataset.from_iterable( |
| 40 | + iterable=[self.create_item(path) for path in self.image_list], |
| 41 | + media_type=Image, |
| 42 | + categories=list(self.label_map.keys())) |
| 43 | + |
| 44 | + return dataset |
| 45 | + |
| 46 | + def create_item(self, image_path: str) -> DatasetItem: |
| 47 | + """Creates a Datumaro item from an image path.""" |
| 48 | + image_full_path = os.path.join(self.main_path, 'inputs', image_path) |
| 49 | + image_data = Image.from_file(image_full_path) |
| 50 | + width, height = PIL.Image.open(image_full_path).size |
| 51 | + try: |
| 52 | + with open( |
| 53 | + os.path.join(self.main_path, 'annotations', image_path.split('.png')[0] + '.json'), |
| 54 | + 'r') as file: |
| 55 | + item_data = json.load(file) |
| 56 | + # create annotations |
| 57 | + annotations = [] |
| 58 | + for annot in item_data: |
| 59 | + #check if the annotation has a bounding box |
| 60 | + if 'regionInfo' in annot.keys() and 'boundingBox' in annot['regionInfo'].keys(): |
| 61 | + x, y, w, h = self.clarifai_bbox_to_datumaro_bbox(annot['regionInfo']['boundingBox'], |
| 62 | + width, height) |
| 63 | + label = annot['data']['concepts'][0]['name'] |
| 64 | + value = self.label_map.get(label, len(self.label_map)) |
| 65 | + self.label_map[label] = value |
| 66 | + annotations.append(Bbox(x=x, y=y, w=w, h=h, label=value)) |
| 67 | + |
| 68 | + except FileNotFoundError: |
| 69 | + annotations = [] |
| 70 | + |
| 71 | + return DatasetItem(id=image_path.split('.png')[0], media=image_data, annotations=annotations) |
| 72 | + |
| 73 | + def clarifai_bbox_to_datumaro_bbox(self, clarifai_bbox, width, height) -> Tuple[int]: |
| 74 | + left_col = clarifai_bbox['leftCol'] * width |
| 75 | + top_row = clarifai_bbox['topRow'] * height |
| 76 | + right_col = clarifai_bbox['rightCol'] * width |
| 77 | + bottom_row = clarifai_bbox['bottomRow'] * height |
| 78 | + |
| 79 | + obj_box = (left_col, top_row, right_col - left_col, bottom_row - top_row) |
| 80 | + return obj_box |
| 81 | + |
| 82 | + def check_folder(self): |
| 83 | + """Checks the clarifai folder format.""" |
| 84 | + if not os.path.exists(self.main_path): |
| 85 | + raise AnnotationsDatasetError(f'Folder not found at {self.main_path}') |
| 86 | + |
| 87 | + if not os.path.exists(os.path.join(self.main_path, 'inputs')): |
| 88 | + raise AnnotationsFormatError( |
| 89 | + f'Folder does not contain an "inputs" folder at {self.main_path}') |
| 90 | + if not os.path.exists(os.path.join(self.main_path, 'annotations')): |
| 91 | + raise AnnotationsFormatError( |
| 92 | + f'Folder does not contain an "annotations" folder at {self.main_path}') |
| 93 | + |
| 94 | + if not all(img.endswith('.png') for img in self.image_list): |
| 95 | + raise AnnotationsFormatError(f'Folder should only contain images at {self.main_path}/inputs') |
| 96 | + if not all(img.endswith('.json') for img in self.annotations_list): |
| 97 | + raise AnnotationsFormatError( |
| 98 | + f'Folder should only contain annotations at {self.main_path}/annotations') |
0 commit comments