Update README.md
Browse files
README.md
CHANGED
|
@@ -56,6 +56,68 @@ Note that the annotations follow the YOLO instance segmentation format.
|
|
| 56 |
|
| 57 |
Please refer to [this page](https://docs.ultralytics.com/datasets/segment/) for more info.
|
| 58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
## 🙏 Acknowledgement
|
| 61 |
<style>
|
|
|
|
| 56 |
|
| 57 |
Please refer to [this page](https://docs.ultralytics.com/datasets/segment/) for more info.
|
| 58 |
|
| 59 |
+
## 🧪 How to use
|
| 60 |
+
```python
|
| 61 |
+
from datasets import load_dataset
|
| 62 |
+
from PIL import Image, ImageDraw
|
| 63 |
+
import numpy as np
|
| 64 |
+
import random
|
| 65 |
+
|
| 66 |
+
# --- Configuration ---
|
| 67 |
+
DATASET_NAME = "FBK-TeV/RaspGrade"
|
| 68 |
+
SAMPLE_INDEX = 0 # Index of the sample to visualize from the 'valid' split
|
| 69 |
+
OUTPUT_IMAGE = 'annotated_hub_image_fixed_colors.png'
|
| 70 |
+
ALPHA = 128 # Transparency level for masks
|
| 71 |
+
|
| 72 |
+
# Define a color map for different classes
|
| 73 |
+
CLASS_COLORS = {
|
| 74 |
+
0: (255, 0, 0, ALPHA), # Red
|
| 75 |
+
1: (0, 255, 0, ALPHA), # Green
|
| 76 |
+
2: (0, 0, 255, ALPHA), # Blue
|
| 77 |
+
3: (255, 255, 0, ALPHA), # Yellow
|
| 78 |
+
4: (255, 0, 255, ALPHA) # Magenta
|
| 79 |
+
# Add more colors for other class IDs if needed
|
| 80 |
+
}
|
| 81 |
+
|
| 82 |
+
def convert_normalized_polygon_to_pixels(polygon_normalized, width, height):
|
| 83 |
+
polygon_pixels = (np.array(polygon_normalized).reshape(-1, 2) * np.array([width, height])).astype(int).flatten().tolist()
|
| 84 |
+
return polygon_pixels
|
| 85 |
+
|
| 86 |
+
if __name__ == "__main__":
|
| 87 |
+
try:
|
| 88 |
+
dataset = load_dataset(DATASET_NAME)
|
| 89 |
+
if 'valid' not in dataset:
|
| 90 |
+
raise ValueError(f"Split 'valid' not found in dataset '{DATASET_NAME}'")
|
| 91 |
+
valid_dataset = dataset['valid']
|
| 92 |
+
if SAMPLE_INDEX >= len(valid_dataset):
|
| 93 |
+
raise ValueError(f"Sample index {SAMPLE_INDEX} is out of bounds for the 'valid' split (size: {len(valid_dataset)})")
|
| 94 |
+
|
| 95 |
+
sample = valid_dataset[SAMPLE_INDEX]
|
| 96 |
+
original_image = sample['image'].convert("RGBA")
|
| 97 |
+
width, height = original_image.size
|
| 98 |
+
|
| 99 |
+
mask = Image.new('RGBA', (width, height), (0, 0, 0, 0))
|
| 100 |
+
mask_draw = ImageDraw.Draw(mask, 'RGBA')
|
| 101 |
+
|
| 102 |
+
labels = sample['labels']
|
| 103 |
+
if isinstance(labels, list):
|
| 104 |
+
for annotation in labels:
|
| 105 |
+
if len(annotation) > 4: # Assuming YOLO format includes class and polygon
|
| 106 |
+
class_id = int(annotation[0])
|
| 107 |
+
polygon_normalized = np.array(annotation[1:]).astype(float).reshape(-1, 2).flatten().tolist()
|
| 108 |
+
polygon_pixels = convert_normalized_polygon_to_pixels(polygon_normalized, width, height)
|
| 109 |
+
color = CLASS_COLORS.get(class_id, (255, 255, 255, ALPHA)) # Default to white if class_id not in map
|
| 110 |
+
mask_draw.polygon(polygon_pixels, fill=color)
|
| 111 |
+
|
| 112 |
+
annotated_image = Image.alpha_composite(original_image, mask)
|
| 113 |
+
|
| 114 |
+
annotated_image.save(OUTPUT_IMAGE)
|
| 115 |
+
print(f"Annotated image with fixed colors saved as {OUTPUT_IMAGE}")
|
| 116 |
+
annotated_image.show()
|
| 117 |
+
|
| 118 |
+
except Exception as e:
|
| 119 |
+
print(f"An error occurred: {e}")
|
| 120 |
+
```
|
| 121 |
|
| 122 |
## 🙏 Acknowledgement
|
| 123 |
<style>
|