Skip to content

YOLO annotation normalization diagram and code #149

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

Merged
merged 2 commits into from
Jul 14, 2024
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
44 changes: 43 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ Sample of annotated image along with its mask and settings is show below.
```
### YOLO Format [[documentation page]](https://annotate-docs.dwaste.live/fundamentals/set-up-and-run/outputs#yolo-format)

YOLO format is also supported by A.Lab. Below is an example of annotated ripe and unripe tomatoes.The entire dataset can be found on [Kaggle](https://www.kaggle.com/datasets/sumn2u/riped-and-unriped-tomato-dataset). In this example, `0` represents ripe tomatoes and `1` represents unripe ones.
YOLO format is also supported by A.Lab. Below is an example of annotated ripe and unripe tomatoes. The entire dataset can be found on [Kaggle](https://www.kaggle.com/datasets/sumn2u/riped-and-unriped-tomato-dataset). In this example, `0` represents ripe tomatoes and `1` represents unripe ones.

![yolo_annotation_example](./sample_images/yolo_annotation_example.png)

Expand All @@ -357,6 +357,48 @@ Applying the generated labels we get following results.

![yolo_with_generated_labels](./sample_images/yolo_applied_annotation.jpg)

### Normalization process of YOLO annotations

#### Example Conversion

To convert non-normalized bounding box coordinates (x<sub style="font-size: 0.8em;">max</sub>, y<sub style="font-size: 0.8em;">max</sub>, x<sub style="font-size: 0.8em;">min</sub>, y<sub style="font-size: 0.8em;">min</sub>) to YOLO format (x<sub style="font-size: 0.8em;">center</sub>, y<sub style="font-size: 0.8em;">center</sub>, <span style="font-variant: small-caps;">width</span>, <span style="font-variant: small-caps;">height</span>):



![yolo-normalization](./sample_images/yolo-normalization.png)
Image Credit: Leandro de Oliveira
```python
# Assuming row contains your bounding box coordinates
row = {'xmax': 400, 'xmin': 200, 'ymax': 300, 'ymin': 100}
class_id = 0 # Example class id (replace with actual class id)

# Image dimensions
WIDTH = 640 # annotated image width
HEIGHT = 640 # annotated image height

# Calculate width and height of the bounding box
width = row['xmax'] - row['xmin']
height = row['ymax'] - row['ymin']

# Calculate the center of the bounding box
x_center = row['xmin'] + (width / 2)
y_center = row['ymin'] + (height / 2)

# Normalize the coordinates
normalized_x_center = x_center / WIDTH
normalized_y_center = y_center / HEIGHT
normalized_width = width / WIDTH
normalized_height = height / HEIGHT

# Create the annotation string in YOLO format
content = f"{class_id} {normalized_x_center} {normalized_y_center} {normalized_width} {normalized_height}"
print(content)
```
The above conversion will give us YOLO format string.
```txt
0 0.46875 0.3125 0.3125 0.3125
```

## Troubleshooting [[documentation page]](https://annotate-docs.dwaste.live/troubleshooting)

- Ensure that both the client and server are running.
Expand Down
Binary file added sample_images/yolo-normalization.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion server/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"taskDescription": "Updated task description",
"taskDescription": "",
"taskChoice": "image_classification",
"images": [],
"showLab": true,
Expand Down
Loading