This repository has been archived by the owner on Aug 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 133
feat: Add YOLO_OBB #281
Closed
Closed
feat: Add YOLO_OBB #281
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,6 +57,7 @@ class Format(Enum): | |
ASR_MANIFEST = 10 | ||
YOLO = 11 | ||
CSV_OLD = 12 | ||
YOLO_OBB = 13 | ||
|
||
def __str__(self): | ||
return self.name | ||
|
@@ -121,6 +122,14 @@ class Converter(object): | |
'link': 'https://labelstud.io/guide/export.html#YOLO', | ||
'tags': ['image segmentation', 'object detection'], | ||
}, | ||
Format.YOLO_OBB: { | ||
'title': 'YOLOv8-OBB', | ||
'description': 'Popular TXT format is created for each image file. Each txt file contains annotations for ' | ||
'the corresponding image fileThe YOLO OBB format designates bounding boxes by their four corner points ' | ||
'with coordinates normalized between 0 and 1', | ||
'link': 'https://labelstud.io/guide/export.html#YOLO', | ||
'tags': ['image segmentation', 'object detection'], | ||
}, | ||
Format.BRUSH_TO_NUMPY: { | ||
'title': 'Brush labels to NumPy', | ||
'description': 'Export your brush labels as NumPy 2d arrays. Each label outputs as one image.', | ||
|
@@ -215,7 +224,7 @@ def convert(self, input_data, output_data, format, is_dir=True, **kwargs): | |
self.convert_to_coco( | ||
input_data, output_data, output_image_dir=image_dir, is_dir=is_dir | ||
) | ||
elif format == Format.YOLO: | ||
elif format == Format.YOLO or format == Format.YOLO_OBB: | ||
image_dir = kwargs.get('image_dir') | ||
label_dir = kwargs.get('label_dir') | ||
self.convert_to_yolo( | ||
|
@@ -224,6 +233,7 @@ def convert(self, input_data, output_data, format, is_dir=True, **kwargs): | |
output_image_dir=image_dir, | ||
output_label_dir=label_dir, | ||
is_dir=is_dir, | ||
is_obb=(format == Format.YOLO_OBB) | ||
) | ||
elif format == Format.VOC: | ||
image_dir = kwargs.get('image_dir') | ||
|
@@ -727,6 +737,7 @@ def convert_to_yolo( | |
output_label_dir=None, | ||
is_dir=True, | ||
split_labelers=False, | ||
is_obb=False, | ||
): | ||
"""Convert data in a specific format to the YOLO format. | ||
|
||
|
@@ -744,8 +755,13 @@ def convert_to_yolo( | |
A boolean indicating whether `input_data` is a directory (True) or a JSON file (False). | ||
split_labelers : bool, optional | ||
A boolean indicating whether to create a dedicated subfolder for each labeler in the output label directory. | ||
is_obb : bool, optional | ||
A boolean indicating whether the format is obb | ||
""" | ||
self._check_format(Format.YOLO) | ||
if is_obb: | ||
self._check_format(Format.YOLO_OBB) | ||
else: | ||
self._check_format(Format.YOLO) | ||
ensure_dir(output_dir) | ||
notes_file = os.path.join(output_dir, 'notes.json') | ||
class_file = os.path.join(output_dir, 'classes.txt') | ||
|
@@ -851,20 +867,52 @@ def convert_to_yolo( | |
or 'rectangle' in label | ||
or 'labels' in label | ||
): | ||
xywh = self.rotated_rectangle(label) | ||
if xywh is None: | ||
continue | ||
if is_obb: | ||
base_y = label["original_height"] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would replace it to call get_rotated_corner_points from my code, it's a bit more clear and computationally optimized. |
||
base_x = label["original_width"] | ||
|
||
x1=label["x"]*base_x | ||
y1=label["y"]*base_y | ||
w=label["width"]*base_x | ||
h=label["height"]*base_y | ||
beta = math.pi * ( | ||
label["rotation"] / 180 | ||
) if "rotation" in label else 0.0 | ||
|
||
# Compute the vectors between points | ||
v12 = (w * math.cos(beta), w * math.sin(beta)) | ||
v23 = (- h*math.sin(beta), h * math.cos(beta)) | ||
|
||
X = [ | ||
(x1, y1), | ||
(x1 + v12[0], y1 + v12[1]), | ||
(x1 + v12[0] + v23[0], y1 + v12[1] + v23[1]), | ||
(x1 + v23[0], y1 + v23[1]) | ||
] | ||
|
||
x, y, w, h = xywh | ||
annotations.append( | ||
[ | ||
category_id, | ||
(x + w / 2) / 100, | ||
(y + h / 2) / 100, | ||
w / 100, | ||
h / 100, | ||
X= [ | ||
(P[0]/base_x/100, P[1]/base_y/100,) for P in X | ||
] | ||
) | ||
|
||
annotations.append( | ||
[category_id] + list(sum(X, ())) | ||
) | ||
|
||
else: | ||
xywh = self.rotated_rectangle(label) | ||
if xywh is None: | ||
continue | ||
|
||
x, y, w, h = xywh | ||
annotations.append( | ||
[ | ||
category_id, | ||
(x + w / 2) / 100, | ||
(y + h / 2) / 100, | ||
w / 100, | ||
h / 100, | ||
] | ||
) | ||
elif "polygonlabels" in label or 'polygon' in label: | ||
points_abs = [(x / 100, y / 100) for x, y in label["points"]] | ||
annotations.append( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
replace the description to this:
'description': 'Popular TXT format is created for each image file. Each txt file contains annotations for '
'the corresponding image file. The YOLO OBB format designates bounding boxes by their four corner points '
'with coordinates normalized between 0 and 1, so it is possible to export rotated objects.',