Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Welcome to the SuperAnnotate Python Software Development Kit (SDK), which enable
Resources
---------------

- API Reference and User Guide available on `Read the Docs <https://superannotate.readthedocs.io/en/stable/superannotate.sdk.html>`__
- API Reference and User Guide available on `Read the Docs <https://superannotate.readthedocs.io/en/stable/index.html>`__
- `Platform documentation <https://doc.superannotate.com/>`__


Expand Down
3 changes: 2 additions & 1 deletion requirements_extra.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ pytest==7.3.1
pytest-xdist==3.2.1
pytest-parallel==0.1.1
pytest-cov==4.0.0
pytest-rerunfailures==11.1.2
pytest-rerunfailures==11.1.2
jsoncomparison==1.1.0
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def coco_panoptic_segmentation_to_sa_pixel(coco_path, output_dir):
cv2.imwrite(str(output_dir / ("%s___save.png" % annot["file_name"])), img)

images_converted.append(annot["file_name"])
file_name = "%s___pixel.json" % annot["file_name"]
file_name = f"{annot['file_name']}.json"
sa_metadata = {
"name": annot_name,
"width": img_id_to_shape[str(annot["image_id"])]["width"],
Expand Down Expand Up @@ -138,7 +138,7 @@ def coco_instance_segmentation_to_sa_pixel(coco_path, output_dir):
logger.info("Converting to SuperAnnotate JSON format")
tqdm_thread.start()
for id_, annotations in images_dict.items():
file_name = "%s___pixel.json" % annotations["file_name"]
file_name = f"{annotations['file_name']}.json"
hexcolors = blue_color_generator(len(annotations["annotations"]))
mask = np.zeros(annotations["shape"])
H, W, _ = mask.shape
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def save_sa_jsons(coco_json, img_id_to_annot, output_dir):
sa_instances = []
else:
sa_instances = img_id_to_annot[str(img["id"])]
file_name = "%s___objects.json" % image_path
file_name = f"{image_path}.json"

sa_metadata = {
"name": image_path,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,11 @@ def dataloop_to_sa(input_dir, task, output_dir):
sa_tags.append(ann["label"])

if "name" in sa_metadata:
file_name = "%s___objects.json" % sa_metadata["name"]
file_name = f"{sa_metadata['name']}.json"
else:
file_name = "%s___objects.json" % dl_data["filename"][1:]
file_name = f"{dl_data['filename'][1:]}.json"

images_converted.append(file_name.replace("___objects.json ", ""))
images_converted.append(file_name.replace(".json", ""))
json_template = _create_sa_json(sa_instances, sa_metadata, sa_tags, sa_comments)
write_to_json(output_dir / file_name, json_template)
finish_event.set()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def googlecloud_to_sa_vector(path, output_dir):
images_not_converted.append(file_name)
continue

sa_file_name = "%s___objects.json" % Path(file_name).name
sa_file_name = f"{Path(file_name).name}.json"

points = (row[3] * W, row[4] * H, row[5] * W, row[8] * H)
sa_instances = _create_vector_instance("bbox", points, {}, [], row[2])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def labelbox_instance_segmentation_to_sa_pixel(json_data, output_dir, input_dir)
logger.info("Converting to SuperAnnotate JSON format")
tqdm_thread.start()
for data in json_data:
file_name = data["External ID"] + "___pixel.json"
file_name = data["External ID"] + ".json"
mask_name = data["External ID"] + "___save.png"
sa_metadata = {"name": data["External ID"]}
if "objects" not in data["Label"].keys():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def labelbox_to_sa(json_data, output_dir, task):
tqdm_thread.start()
for data in json_data:
if "objects" not in data["Label"].keys():
file_name = data["External ID"] + "___objects.json"
file_name = data["External ID"] + ".json"
write_to_json(
output_dir / file_name,
{"metadata": {}, "instances": [], "tags": [], "comments": []},
Expand Down Expand Up @@ -87,7 +87,7 @@ def labelbox_to_sa(json_data, output_dir, task):
sa_instances.append(sa_obj)

images_converted.append(data["External ID"])
file_name = "%s___objects.json" % data["External ID"]
file_name = f"{data['External ID']}.json"
try:
img = cv2.imread(str(output_dir / data["External ID"]))
H, W, _ = img.shape
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def sagemaker_instance_segmentation_to_sa_pixel(data_path, output_dir):

blue_colors = blue_color_generator(num_of_contours)
idx = 0
file_name = "%s___pixel.json" % (img_mapping[mask_name])
file_name = f"{img_mapping[mask_name]}.json"
sa_metadata = {"name": img_mapping[mask_name], "width": W, "height": H}
sa_instances = []
sa_mask = np.zeros((H, W, 4))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def sagemaker_object_detection_to_sa_vector(data_path, main_key, output_dir):
raise Exception

manifest = dataset_manifest[int(img["datasetObjectId"])]
file_name = "%s___objects.json" % Path(manifest["source-ref"]).name
file_name = f"{Path(manifest['source-ref']).name}.json"

classes = img["consolidatedAnnotation"]["content"][main_key + "-metadata"][
"class-map"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def supervisely_instance_segmentation_to_sa_pixel(json_files, class_id_map, outp
tqdm_thread.start()

for json_file in json_files:
file_name = "%s___pixel.json" % Path(json_file).stem
file_name = f"{Path(json_file).stem}.json"

json_data = json.load(open(json_file))
sa_instances = []
Expand Down Expand Up @@ -76,10 +76,7 @@ def supervisely_instance_segmentation_to_sa_pixel(json_files, class_id_map, outp
parts.append({"color": hex_colors[index]})
index += 1
cv2.imwrite(
str(
output_dir
/ file_name.replace("___pixel.json", "___save.png")
),
str(output_dir / file_name.replace(".json", "___save.png")),
mask,
)
sa_obj = _create_pixel_instance(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def supervisely_to_sa(json_files, class_id_map, task, output_dir):
tqdm_thread.start()
for json_file in json_files:
json_data = json.load(open(json_file))
file_name = "%s___objects.json" % Path(json_file).stem
file_name = f"{Path(json_file).stem}.json"
sa_metadata = {
"name": Path(json_file).stem,
"width": json_data["size"]["width"],
Expand Down Expand Up @@ -140,7 +140,7 @@ def supervisely_keypoint_detection_to_sa_vector(
logger.info("Converting to SuperAnnotate JSON format")
tqdm_thread.start()
for json_file in json_files:
file_name = "%s___objects.json" % (Path(json_file).stem)
file_name = f"{Path(json_file).stem}.json"
json_data = json.load(open(json_file))
sa_metadata = {
"name": Path(json_file).stem,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def vgg_to_sa(json_data, task, output_dir):
H = 0
W = 0

file_name = "%s___objects.json" % img["filename"]
file_name = f"{img['filename']}.json"
sa_metadata = {"name": img["filename"], "width": W, "height": H}
sa_instances = []
instances = img["regions"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def voc_instance_segmentation_to_sa_pixel(voc_root, output_dir):
sa_instances.append(sa_obj)

images_converted.append(filename)
file_name = "%s.jpg___pixel.json" % (filename.stem)
file_name = f"{filename.stem}.json"
height, width = _get_image_shape_from_xml(annotation_dir / filename.name)
sa_metadata = {"name": filename.stem, "height": height, "width": width}
sa_json = _create_sa_json(sa_instances, sa_metadata)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def voc_instance_segmentation_to_sa_vector(voc_root, output_dir):

images_converted.append(filename)
file_name, height, width = _get_image_metadata(annotation_dir / filename.name)
file_path = f"{file_name}___objects.json"
file_path = f"{file_name}.json"
sa_metadata = {"name": str(filename), "height": height, "width": width}
sa_json = _create_sa_json(sa_instances, sa_metadata)
write_to_json(output_dir / file_path, sa_json)
Expand Down Expand Up @@ -162,7 +162,7 @@ def voc_object_detection_to_sa_vector(voc_root, output_dir):

images_converted.append(filename)
file_name, height, width = _get_image_metadata(annotation_dir / filename.name)
file_path = f"{file_name}___objects.json"
file_path = f"{file_name}.json"
sa_metadata = {"name": str(filename), "height": height, "width": width}
sa_json = _create_sa_json(sa_instances, sa_metadata)
write_to_json(output_dir / file_path, sa_json)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def vott_to_sa(file_list, task, output_dir):
tqdm_thread.start()
for json_file in file_list:
json_data = json.load(open(json_file))
file_name = "%s___objects.json" % json_data["asset"]["name"]
file_name = f"{json_data['asset']['name']}.json"
sa_metadata = {
"name": json_data["asset"]["name"],
"width": json_data["asset"]["size"]["width"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def yolo_object_detection_to_sa_vector(data_path, output_dir):
sa_instances.append(sa_obj.copy())

images_converted.append(annotation.name)
file_name = "%s___objects.json" % Path(file_name).name
file_name = f"{Path(file_name).name}.json"
sa_metadata = {"name": Path(file_name).name, "width": W, "height": H}
sa_json = _create_sa_json(sa_instances, sa_metadata)
write_to_json(output_dir / file_name, sa_json)
Expand Down
95 changes: 2 additions & 93 deletions src/superannotate/lib/core/usecases/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,14 +231,6 @@ def default_annotation(self):
"annotation_mask_filename": None,
}

@property
def annotation_json_name(self):
if self._from_project.type == constances.ProjectType.VECTOR.value:
return f"{self._from_image.name}___objects.json"
elif self._from_project.type == constances.ProjectType.PIXEL.value:
return f"{self._from_image.name}___pixel.json"
return f"{self._from_image.name}.json"

@property
def download_auth_data(self):
return self._service_provider.get_download_token(
Expand Down Expand Up @@ -426,10 +418,7 @@ def annotations(self):
image_path = (
f"{Path(self._image_path).parent}/{Path(self._image_path).name}"
)
if self._project_type.upper() == constances.ProjectType.PIXEL.name.upper():
self._annotations = json.load(open(f"{image_path}___pixel.json"))
else:
self._annotations = json.load(open(f"{image_path}___objects.json"))
self._annotations = json.load(open(f"{image_path}.json"))
return self._annotations

@property
Expand Down Expand Up @@ -1487,10 +1476,6 @@ def execute(self):
credentials = token["annotations"]["MAIN"][0]

annotation_json_creds = credentials["annotation_json_path"]
if self._project.type == constances.ProjectType.VECTOR.value:
file_postfix = "___objects.json"
else:
file_postfix = "___pixel.json"

response = requests.get(
url=annotation_json_creds["url"],
Expand All @@ -1502,7 +1487,7 @@ def execute(self):
self._response.data = (None, None)
return self._response
data["annotation_json"] = response.json()
data["annotation_json_filename"] = f"{self._image_name}{file_postfix}"
data["annotation_json_filename"] = f"{self._image_name}.json"
mask_path = None
if self._project.type == constances.ProjectType.PIXEL.value:
annotation_blue_map_creds = credentials["annotation_bluemap_path"]
Expand Down Expand Up @@ -1530,82 +1515,6 @@ def execute(self):
return self._response


class GetImageAnnotationsUseCase(BaseReportableUseCase):
def __init__(
self,
reporter: Reporter,
service_provider: BaseServiceProvider,
project: ProjectEntity,
folder: FolderEntity,
image_name: str,
):
super().__init__(reporter)
self._service_provider = service_provider
self._project = project
self._folder = folder
self._image_name = image_name

@property
def image_use_case(self):
use_case = GetImageUseCase(
project=self._project,
folder=self._folder,
image_name=self._image_name,
service_provider=self._service_provider,
)
return use_case

def validate_project_type(self):
if self._project.type in constances.LIMITED_FUNCTIONS:
raise AppValidationException(
constances.LIMITED_FUNCTIONS[self._project.type]
)

def execute(self):
if self.is_valid():
data = {
"annotation_json": None,
"annotation_json_filename": None,
"annotation_mask": None,
"annotation_mask_filename": None,
}
image_response = self.image_use_case.execute()
token = self._service_provider.get_download_token(
project=self._project,
folder=self._folder,
image_id=image_response.data.id,
).data
credentials = token["annotations"]["MAIN"][0]
if self._project.type == constances.ProjectType.VECTOR.value:
file_postfix = "___objects.json"
else:
file_postfix = "___pixel.json"
data["annotation_mask_filename"] = f"{self._image_name}___save.png"
data["annotation_json_filename"] = f"{self._image_name}{file_postfix}"

response = requests.get(
url=credentials["annotation_json_path"]["url"],
headers=credentials["annotation_json_path"]["headers"],
)
if not response.ok:
self.reporter.log_warning("Couldn't load annotations.")
self._response.data = data
return self._response
data["annotation_json"] = response.json()
data["annotation_json_filename"] = f"{self._image_name}{file_postfix}"
if self._project.type == constances.ProjectType.PIXEL.value:
annotation_blue_map_creds = credentials["annotation_bluemap_path"]
response = requests.get(
url=annotation_blue_map_creds["url"],
headers=annotation_blue_map_creds["headers"],
)
data["annotation_mask"] = io.BytesIO(response.content)

self._response.data = data

return self._response


class UnAssignFolderUseCase(BaseUseCase):
def __init__(
self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
10
],
"area": 100,
"category_id": -1
"category_id": 1611842
},
{
"id": 3,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[
{
"name": "Personal vehicle",
"color": "#1b066e",
"attribute_groups": []
},
{
"name": "Large vehicle",
"color": "#2e5ce0",
"attribute_groups": []
},
{
"name": "Human",
"color": "#4949cc",
"attribute_groups": []
},
{
"name": "Plant",
"color": "#460f3e",
"attribute_groups": []
}
]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading