Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal for yolo bbox conversion #912

Closed
wants to merge 2 commits into from
Closed
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
31 changes: 20 additions & 11 deletions albumentations/augmentations/bbox_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,26 +229,30 @@ def convert_bbox_to_albumentations(bbox, source_format, rows, cols, check_validi
(x_min, y_min, width, height), tail = bbox[:4], tuple(bbox[4:])
x_max = x_min + width
y_max = y_min + height
bbox = (x_min, y_min, x_max, y_max) + tail
bbox = normalize_bbox(bbox, rows, cols)
elif source_format == "yolo":
# https://github.com/pjreddie/darknet/blob/f6d861736038da22c9eb0739dca84003c5a5e275/scripts/voc_label.py#L12
bbox, tail = bbox[:4], tuple(bbox[4:])
_bbox = np.array(bbox[:4])
if np.any((_bbox <= 0) | (_bbox > 1)):
raise ValueError("In YOLO format all labels must be float and in range (0, 1]")

x, y, width, height = denormalize_bbox(bbox, rows, cols)
x, y, width, height = bbox[:4]
w_half, h_half = width / 2, height / 2

x_min = int(x - width / 2 + 1)
x_max = int(x_min + width)
y_min = int(y - height / 2 + 1)
y_max = int(y_min + height)
x_min, x_max = x - w_half, x + w_half
y_min, y_max = y - h_half, y + h_half

bbox = (x_min, y_min, x_max, y_max) + tail
else:
(x_min, y_min, x_max, y_max), tail = bbox[:4], tuple(bbox[4:])
bbox = (x_min, y_min, x_max, y_max) + tail
bbox = normalize_bbox(bbox, rows, cols)

bbox = (x_min, y_min, x_max, y_max) + tail
bbox = normalize_bbox(bbox, rows, cols)
if check_validity:
check_bbox(bbox)

return bbox


Expand Down Expand Up @@ -280,20 +284,25 @@ def convert_bbox_from_albumentations(bbox, target_format, rows, cols, check_vali
)
if check_validity:
check_bbox(bbox)
bbox = denormalize_bbox(bbox, rows, cols)
if target_format == "coco":
bbox = denormalize_bbox(bbox, rows, cols)
(x_min, y_min, x_max, y_max), tail = bbox[:4], tuple(bbox[4:])
width = x_max - x_min
height = y_max - y_min

bbox = (x_min, y_min, width, height) + tail
elif target_format == "yolo":
# https://github.com/pjreddie/darknet/blob/f6d861736038da22c9eb0739dca84003c5a5e275/scripts/voc_label.py#L12
(x_min, y_min, x_max, y_max), tail = bbox[:4], bbox[4:]
x = int((x_min + x_max) / 2 - 1)
y = int((y_min + y_max) / 2 - 1)

width = x_max - x_min
height = y_max - y_min
bbox = normalize_bbox((x, y, width, height) + tail, rows, cols)

x_mid = x_min + width / 2
y_mid = y_min + height / 2

bbox = (x_mid, y_mid, width, height) + tail

return bbox


Expand Down