Skip to content

Commit e157469

Browse files
add parse mask to fastlabel point
1 parent a29f8c5 commit e157469

File tree

6 files changed

+343
-21
lines changed

6 files changed

+343
-21
lines changed

README.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
- [YOLO To FastLabel](#yolo-to-fastlabel)
3434
- [Pascal VOC To FastLabel](#pascal-voc-to-fastlabel)
3535
- [labelme To FastLabel](#labelme-to-fastlabel)
36+
- [Mask To FastLabel Segmentation Points](#mask-to-fastlabel-segmentation-points)
3637
- [Model](#model)
3738
- [API Docs](#api-docs)
3839

@@ -589,7 +590,6 @@ task = client.create_multi_image_classification_task(
589590

590591
##### Limitation
591592

592-
593593
- You can upload up to a size of 20 MB.
594594
- You can upload up to a total size of 2 GB.
595595
- You can upload up to 6 files in total.
@@ -2667,7 +2667,7 @@ dataset_object = client.create_dataset_object(
26672667
tags=["dog"], # max 5 tags per dataset object.
26682668
licenses=["MIT", "my-license"], # max 10 licenses per dataset object
26692669
annotations=[
2670-
{
2670+
{
26712671
"type": "classification",
26722672
"value": "classification",
26732673
"points": [],
@@ -2824,6 +2824,7 @@ client.download_dataset_objects(
28242824
```
28252825

28262826
### Update Dataset Object
2827+
28272828
```python
28282829
dataset_object = client.update_dataset_object(
28292830
dataset_id="YOUR_DATASET_ID",
@@ -3210,6 +3211,16 @@ for image_file_path in glob.iglob(os.path.join(input_dataset_path, "**/**.jpg"),
32103211

32113212
> Please check const.COLOR_PALLETE for index colors.
32123213
3214+
### Mask To FastLabel Segmentation Points
3215+
3216+
Convert mask image to FastLabel's segmentation coordinate format.
3217+
3218+
```python
3219+
points = client.mask_to_fastlabel_segmentation_points(
3220+
mask_image = binary_image_path (or binary_image_array)
3221+
)
3222+
```
3223+
32133224
## Model
32143225

32153226
### Get training jobs

fastlabel/__init__.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,14 @@ def find_image_classification_task(self, task_id: str) -> dict:
7070
"""
7171
endpoint = "tasks/image/classification/" + task_id
7272
return self.api.get_request(endpoint)
73-
73+
7474
def find_multi_image_classification_task(self, task_id: str) -> dict:
7575
"""
7676
Find a single multi image classification task.
7777
"""
7878
endpoint = "tasks/multi-image/classification/" + task_id
7979
return self.api.get_request(endpoint)
80-
80+
8181
def find_multi_image_classification_task_by_name(
8282
self, project: str, task_name: str
8383
) -> dict:
@@ -432,9 +432,9 @@ def get_image_classification_tasks(
432432
if limit:
433433
params["limit"] = limit
434434
return self.api.get_request(endpoint, params=params)
435-
435+
436436
def get_multi_image_classification_tasks(
437-
self,
437+
self,
438438
project: str,
439439
status: str = None,
440440
external_status: str = None,
@@ -1197,7 +1197,7 @@ def create_integrated_image_classification_task(
11971197
self.__fill_assign_users(payload, **kwargs)
11981198

11991199
return self.api.post_request(endpoint, payload=payload)
1200-
1200+
12011201
def create_multi_image_classification_task(
12021202
self,
12031203
project: str,
@@ -2135,7 +2135,7 @@ def update_image_classification_task(
21352135
self.__fill_assign_users(payload, **kwargs)
21362136

21372137
return self.api.put_request(endpoint, payload=payload)
2138-
2138+
21392139
def update_multi_image_classification_task(
21402140
self,
21412141
task_id: str,
@@ -4300,7 +4300,7 @@ def download_dataset_objects(
43004300
"annotations": obj["annotations"],
43014301
"customMetadata": obj["customMetadata"],
43024302
"tags": obj["tags"],
4303-
"object_type": obj["type"]
4303+
"object_type": obj["type"],
43044304
}
43054305
for obj in objects
43064306
]
@@ -4718,6 +4718,14 @@ def get_histories(
47184718

47194719
return self.api.get_request(endpoint, params=params)
47204720

4721+
def mask_to_fastlabel_segmentation_points(
4722+
self, mask_image: Union[str, np.ndarray]
4723+
) -> List[List[List[int]]]:
4724+
return [
4725+
[converters.get_pixel_coordinates(p) for p in point]
4726+
for point in utils.mask_to_segmentation(mask_image)
4727+
]
4728+
47214729

47224730
def delete_extra_annotations_parameter(annotations: list) -> list:
47234731
for annotation in annotations:

fastlabel/api.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77

88

99
class Api:
10-
base_url = "https://api.fastlabel.ai/v1/"
10+
# base_url = "https://api.fastlabel.ai/v1/"
11+
base_url = "http://localhost:4000/v1/"
12+
1113
access_token = None
1214

1315
def __init__(self):

fastlabel/converters.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from operator import itemgetter
99
from pathlib import Path
1010
from tempfile import NamedTemporaryFile
11-
from typing import List, Dict, Optional
11+
from typing import Dict, List, Optional
1212

1313
import cv2
1414
import geojson
@@ -239,7 +239,7 @@ def __to_coco_annotation(data: dict) -> dict:
239239
image_id,
240240
annotation_type,
241241
annotation_attributes,
242-
rotation
242+
rotation,
243243
)
244244

245245

@@ -254,7 +254,11 @@ def __get_coco_category_by_name(categories: list, name: str) -> Optional[dict]:
254254

255255
def __get_coco_annotation_keypoints(keypoints: list, category_keypoints: list) -> list:
256256
coco_annotation_keypoints = []
257-
keypoint_values = {keypoint["key"]: keypoint["value"] for keypoint in keypoints if keypoint["value"]}
257+
keypoint_values = {
258+
keypoint["key"]: keypoint["value"]
259+
for keypoint in keypoints
260+
if keypoint["value"]
261+
}
258262
for category_key in category_keypoints:
259263
value = keypoint_values.get(category_key, [0, 0, 0])
260264
# Adjust fastlabel data definition to coco format
@@ -271,20 +275,22 @@ def __get_coco_annotation(
271275
image_id: str,
272276
annotation_type: str,
273277
annotation_attributes: Dict[str, AttributeValue],
274-
rotation: int
278+
rotation: int,
275279
) -> dict:
276280
annotation = {}
277281
annotation["num_keypoints"] = len(keypoints) if keypoints else 0
278282
annotation["keypoints"] = (
279-
__get_coco_annotation_keypoints(keypoints, category["keypoints"]) if keypoints else []
283+
__get_coco_annotation_keypoints(keypoints, category["keypoints"])
284+
if keypoints
285+
else []
280286
)
281287
annotation["segmentation"] = __to_coco_segmentation(annotation_type, points)
282288
annotation["iscrowd"] = 0
283289
annotation["area"] = __to_area(annotation_type, points)
284290
annotation["image_id"] = image_id
285291
annotation["bbox"] = (
286-
__get_coco_bbox(points, rotation)
287-
if annotation_type == AnnotationType.bbox
292+
__get_coco_bbox(points, rotation)
293+
if annotation_type == AnnotationType.bbox
288294
else __to_bbox(annotation_type, points)
289295
)
290296
annotation["rotation"] = rotation
@@ -329,6 +335,7 @@ def __get_rotated_rectangle_coordinates(
329335

330336
return rotated_corners
331337

338+
332339
def __get_coco_bbox(
333340
points: list,
334341
rotation: int,
@@ -788,12 +795,12 @@ def to_pixel_coordinates(tasks: list) -> list:
788795
for region in annotation["points"]:
789796
new_region = []
790797
for points in region:
791-
new_points = __get_pixel_coordinates(points)
798+
new_points = get_pixel_coordinates(points)
792799
new_region.append(new_points)
793800
new_regions.append(new_region)
794801
annotation["points"] = new_regions
795802
elif annotation["type"] == AnnotationType.polygon.value:
796-
new_points = __get_pixel_coordinates(annotation["points"])
803+
new_points = get_pixel_coordinates(annotation["points"])
797804
annotation["points"] = new_points
798805
elif annotation["type"] == AnnotationType.bbox.value:
799806
points = annotation["points"]
@@ -871,14 +878,15 @@ def __remove_duplicated_coordinates(points: List[int]) -> List[int]:
871878
return new_points
872879

873880

874-
def __get_pixel_coordinates(points: List[int or float]) -> List[int]:
881+
def get_pixel_coordinates(points: List[int | float]) -> List[int]:
875882
"""
876883
Remove diagonal coordinates and return pixel outline coordinates.
877884
"""
878885
if len(points) == 0:
879886
return []
880887

881888
new_points = []
889+
print(points)
882890
new_points.append(int(points[0]))
883891
new_points.append(int(points[1]))
884892
for i in range(int(len(points) / 2)):
@@ -1209,4 +1217,3 @@ def _get_coco_annotation_attributes(annotation: dict) -> Dict[str, AttributeValu
12091217
for attribute in attributes:
12101218
coco_attributes[attribute["key"]] = attribute["value"]
12111219
return coco_attributes
1212-

fastlabel/utils.py renamed to fastlabel/utils/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
from fastlabel import const
1212

13+
from .mask_image_util import mask_to_segmentation # noqa: F401
14+
1315

1416
def base64_encode(file_path: str) -> str:
1517
with open(file_path, "rb") as f:

0 commit comments

Comments
 (0)