Reference for ultralytics/data/converter.py
Note
Full source code for this file is available at https://github.com/ultralytics/ultralytics/blob/main/ultralytics/data/converter.py. Help us fix any issues you see by submitting a Pull Request 🛠️. Thank you 🙏!
ultralytics.data.converter.coco91_to_coco80_class()
Converts 91-index COCO class IDs to 80-index COCO class IDs.
Returns:
Type | Description |
---|---|
list
|
A list of 91 class IDs where the index represents the 80-index class ID and the value is the corresponding 91-index class ID. |
Source code in ultralytics/data/converter.py
ultralytics.data.converter.coco80_to_coco91_class()
Converts 80-index (val2014) to 91-index (paper).
For details see https://tech.amikelive.com/node-718/what-object-categories-labels-are-in-coco-dataset/.
Example:
```python
import numpy as np
a = np.loadtxt('data/coco.names', dtype='str', delimiter='
') b = np.loadtxt('data/coco_paper.names', dtype='str', delimiter=' ') x1 = [list(a[i] == b).index(True) + 1 for i in range(80)] # darknet to coco x2 = [list(b[i] == a).index(True) if any(b[i] == a) else None for i in range(91)] # coco to darknet ```
Source code in ultralytics/data/converter.py
ultralytics.data.converter.convert_coco(labels_dir='../coco/annotations/', use_segments=False, use_keypoints=False, cls91to80=True)
Converts COCO dataset annotations to a format suitable for training YOLOv5 models.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
labels_dir |
str
|
Path to directory containing COCO dataset annotation files. |
'../coco/annotations/'
|
use_segments |
bool
|
Whether to include segmentation masks in the output. |
False
|
use_keypoints |
bool
|
Whether to include keypoint annotations in the output. |
False
|
cls91to80 |
bool
|
Whether to map 91 COCO class IDs to the corresponding 80 COCO class IDs. |
True
|
Example
Output
Generates output files in the specified output directory.
Source code in ultralytics/data/converter.py
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
|
ultralytics.data.converter.convert_dota_to_yolo_obb(dota_root_path)
Converts DOTA dataset annotations to YOLO OBB (Oriented Bounding Box) format.
The function processes images in the 'train' and 'val' folders of the DOTA dataset. For each image, it reads the associated label from the original labels directory and writes new labels in YOLO OBB format to a new directory.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dota_root_path |
str
|
The root directory path of the DOTA dataset. |
required |
Example
Notes
The directory structure assumed for the DOTA dataset: - DOTA - images - train - val - labels - train_original - val_original
After the function execution, the new labels will be saved in: - DOTA - labels - train - val
Source code in ultralytics/data/converter.py
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
|
ultralytics.data.converter.min_index(arr1, arr2)
Find a pair of indexes with the shortest distance between two arrays of 2D points.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
arr1 |
array
|
A NumPy array of shape (N, 2) representing N 2D points. |
required |
arr2 |
array
|
A NumPy array of shape (M, 2) representing M 2D points. |
required |
Returns:
Type | Description |
---|---|
tuple
|
A tuple containing the indexes of the points with the shortest distance in arr1 and arr2 respectively. |
Source code in ultralytics/data/converter.py
ultralytics.data.converter.merge_multi_segment(segments)
Merge multiple segments into one list by connecting the coordinates with the minimum distance between each segment. This function connects these coordinates with a thin line to merge all segments into one.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
segments |
List[List]
|
Original segmentations in COCO's JSON file. Each element is a list of coordinates, like [segmentation1, segmentation2,...]. |
required |
Returns:
Name | Type | Description |
---|---|---|
s |
List[ndarray]
|
A list of connected segments represented as NumPy arrays. |