Coco¶
sahi.utils.coco
¶
COCO dataset format utilities and classes for handling annotations and predictions.
Classes¶
CocoCategory
¶
CocoCategory(
id: int = 0,
name: str | None = None,
supercategory: str | None = None,
)
COCO formatted category.
Initialize a COCO category.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
int
|
Category ID. |
0
|
|
str | None
|
Category name. |
None
|
|
str | None
|
Supercategory name. |
None
|
Source code in sahi/utils/coco.py
Attributes¶
Methods:¶
from_coco_category
classmethod
¶
from_coco_category(category: dict) -> _TCocoCategory
Create CocoCategory object using coco category.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
category
¶ |
dict
|
Dict {"supercategory": "person", "id": 1, "name": "person"}, |
required |
Source code in sahi/utils/coco.py
CocoAnnotation
¶
CocoAnnotation(
category_id: int,
category_name: str | None = None,
segmentation: list[list[float]]
| list[list[int]]
| None = None,
bbox: list[int] | None = None,
image_id: int | None = None,
iscrowd: int = 0,
)
COCO formatted annotation.
Create coco annotation object using bbox or segmentation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
list[list[float]] | list[list[int]] | None
|
List[List]
|
None
|
|
list[int] | None
|
List [xmin, ymin, width, height] |
None
|
|
int
|
int Category id of the annotation |
required |
|
str | None
|
str Category name of the annotation |
None
|
|
int | None
|
int Image ID of the annotation |
None
|
|
int
|
int 0 or 1 |
0
|
Source code in sahi/utils/coco.py
Attributes¶
bbox
property
¶
Returns coco formatted bbox of the annotation as [xmin, ymin, width, height].
segmentation
property
¶
Returns coco formatted segmentation of the annotation as [[1, 1, 325, 125, 250, 200, 5, 200]].
category_name
property
writable
¶
Returns category name of the annotation as str.
Methods:¶
from_coco_segmentation
classmethod
¶
from_coco_segmentation(
segmentation: list[list[float]] | list[list[int]],
category_id: int,
category_name: str,
iscrowd: int = 0,
) -> _TCocoAnnotation
Create CocoAnnotation object using coco segmentation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
segmentation
¶ |
list[list[float]] | list[list[int]]
|
List[List]
|
required |
category_id
¶ |
int
|
int Category id of the annotation |
required |
category_name
¶ |
str
|
str Category name of the annotation |
required |
iscrowd
¶ |
int
|
int 0 or 1 |
0
|
Source code in sahi/utils/coco.py
from_coco_bbox
classmethod
¶
from_coco_bbox(
bbox: list[int],
category_id: int,
category_name: str,
iscrowd: int = 0,
) -> _TCocoAnnotation
Create CocoAnnotation object using coco bbox.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bbox
¶ |
list[int]
|
List [xmin, ymin, width, height] |
required |
category_id
¶ |
int
|
int Category id of the annotation |
required |
category_name
¶ |
str
|
str Category name of the annotation |
required |
iscrowd
¶ |
int
|
int 0 or 1 |
0
|
Source code in sahi/utils/coco.py
from_coco_annotation_dict
classmethod
¶
from_coco_annotation_dict(
annotation_dict: dict, category_name: str | None = None
) -> _TCocoAnnotation
Create CocoAnnotation object from category name and COCO formatted annotation dict.
Creates object from COCO formatted annotation dict with fields "bbox", "segmentation", "category_id".
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
category_name
¶ |
str | None
|
str Category name of the annotation |
None
|
annotation_dict
¶ |
dict
|
dict COCO formatted annotation dict (with fields "bbox", "segmentation", "category_id") |
required |
Source code in sahi/utils/coco.py
from_shapely_annotation
classmethod
¶
from_shapely_annotation(
shapely_annotation: ShapelyAnnotation,
category_id: int,
category_name: str,
iscrowd: int,
) -> _TCocoAnnotation
Create CocoAnnotation object from ShapelyAnnotation object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
shapely_annotation
¶ |
ShapelyAnnotation
|
ShapelyAnnotation object to convert. |
required |
category_id
¶ |
int
|
Category id of the annotation. |
required |
category_name
¶ |
str
|
Category name of the annotation. |
required |
iscrowd
¶ |
int
|
0 or 1. |
required |
Source code in sahi/utils/coco.py
get_sliced_coco_annotation
¶
get_sliced_coco_annotation(
slice_bbox: list[int],
) -> CocoAnnotation
Get the annotation sliced by a bounding box.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
slice_bbox
¶ |
list[int]
|
Bounding box to slice with as [xmin, ymin, xmax, ymax]. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
CocoAnnotation |
CocoAnnotation
|
The sliced annotation. |
Source code in sahi/utils/coco.py
CocoPrediction
¶
CocoPrediction(
segmentation: list[list[float]]
| list[list[int]]
| None = None,
bbox: list[int] | None = None,
category_id: int = 0,
category_name: str = "",
image_id: int | None = None,
score: float | None = None,
iscrowd: int = 0,
)
Bases: CocoAnnotation
Class for handling predictions in coco format.
Initialize a COCO prediction object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
list[list[float]] | list[list[int]] | None
|
List[List]
|
None
|
|
list[int] | None
|
List [xmin, ymin, width, height] |
None
|
|
int
|
int Category id of the annotation |
0
|
|
str
|
str Category name of the annotation |
''
|
|
int | None
|
int Image ID of the annotation |
None
|
|
float | None
|
float Prediction score between 0 and 1 |
None
|
|
int
|
int 0 or 1. |
0
|
Source code in sahi/utils/coco.py
Attributes¶
Methods:¶
from_coco_segmentation
classmethod
¶
from_coco_segmentation(
segmentation: list[list[float]] | list[list[int]],
category_id: int,
category_name: str,
score: float,
iscrowd: int = 0,
image_id: int | None = None,
) -> _TCocoPrediction
Create CocoAnnotation object using coco segmentation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
segmentation
¶ |
list[list[float]] | list[list[int]]
|
List[List]
|
required |
category_id
¶ |
int
|
int Category id of the annotation |
required |
category_name
¶ |
str
|
str Category name of the annotation |
required |
score
¶ |
float
|
float Prediction score between 0 and 1 |
required |
iscrowd
¶ |
int
|
int 0 or 1 |
0
|
image_id
¶ |
int | None
|
Image ID of the prediction. |
None
|
Source code in sahi/utils/coco.py
from_coco_bbox
classmethod
¶
from_coco_bbox(
bbox: list[int],
category_id: int,
category_name: str,
score: float,
iscrowd: int = 0,
image_id: int | None = None,
) -> _TCocoPrediction
Create CocoAnnotation object using coco bbox.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bbox
¶ |
list[int]
|
List [xmin, ymin, width, height] |
required |
category_id
¶ |
int
|
int Category id of the annotation |
required |
category_name
¶ |
str
|
str Category name of the annotation |
required |
score
¶ |
float
|
float Prediction score between 0 and 1 |
required |
iscrowd
¶ |
int
|
int 0 or 1 |
0
|
image_id
¶ |
int | None
|
Image ID of the prediction. |
None
|
Source code in sahi/utils/coco.py
from_coco_annotation_dict
classmethod
¶
from_coco_annotation_dict(
category_name: str,
annotation_dict: dict,
score: float,
image_id: int | None = None,
) -> _TCocoPrediction
Create CocoAnnotation object from category name and COCO formatted annotation dict.
Creates object from COCO formatted annotation dict with fields "bbox", "segmentation", "category_id".
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
category_name
¶ |
str
|
str Category name of the annotation |
required |
annotation_dict
¶ |
dict
|
dict COCO formatted annotation dict (with fields "bbox", "segmentation", "category_id") |
required |
score
¶ |
float
|
float Prediction score between 0 and 1 |
required |
image_id
¶ |
int | None
|
Image ID of the prediction. |
None
|
Source code in sahi/utils/coco.py
CocoVidAnnotation
¶
CocoVidAnnotation(
category_id: int,
category_name: str,
bbox: list[int],
image_id: int | None = None,
instance_id: int | None = None,
iscrowd: int = 0,
id: int | None = None,
)
Bases: CocoAnnotation
COCOVid formatted annotation.
https://github.com/open-mmlab/mmtracking/blob/master/docs/tutorials/customize_dataset.md#the-cocovid-annotation-file
Initialize a COCOVid annotation object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
int
|
Category id of the annotation. |
required |
|
str
|
Category name of the annotation. |
required |
|
list[int]
|
List [xmin, ymin, width, height]. |
required |
|
int | None
|
Image ID of the annotation. |
None
|
|
int | None
|
Instance id used for tracking. |
None
|
|
int
|
0 or 1. |
0
|
|
int | None
|
Annotation id. |
None
|
Source code in sahi/utils/coco.py
CocoImage
¶
COCO formatted image.
Create CocoImage object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
int | None
|
int Image id |
None
|
|
str
|
str Image path |
required |
|
int
|
int Image height in pixels |
required |
|
int
|
int Image width in pixels |
required |
Source code in sahi/utils/coco.py
Attributes¶
Methods:¶
from_coco_image_dict
classmethod
¶
from_coco_image_dict(image_dict: dict) -> _TCocoImage
Create CocoImage object from COCO formatted image dict.
Creates object from COCO formatted image dict with fields "id", "file_name", "height" and "width".
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image_dict
¶ |
dict
|
dict COCO formatted image dict (with fields "id", "file_name", "height" and "weight") |
required |
Source code in sahi/utils/coco.py
add_annotation
¶
add_annotation(annotation: CocoAnnotation) -> None
Add annotation to this CocoImage instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
annotation
¶ |
CocoAnnotation
|
CocoAnnotation object to add. |
required |
Source code in sahi/utils/coco.py
add_prediction
¶
add_prediction(prediction: CocoPrediction) -> None
Add prediction to this CocoImage instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prediction
¶ |
CocoPrediction
|
CocoPrediction object to add. |
required |
Source code in sahi/utils/coco.py
CocoVidImage
¶
CocoVidImage(
file_name: str,
height: int,
width: int,
video_id: int | None = None,
frame_id: int | None = None,
id: int | None = None,
)
Bases: CocoImage
COCOVid formatted image.
https://github.com/open-mmlab/mmtracking/blob/master/docs/tutorials/customize_dataset.md#the-cocovid-annotation-file
Create CocoVidImage object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
int | None
|
int Image id |
None
|
|
str
|
str Image path |
required |
|
int
|
int Image height in pixels |
required |
|
int
|
int Image width in pixels |
required |
|
int | None
|
int 0-indexed frame id |
None
|
|
int | None
|
int Video id |
None
|
Source code in sahi/utils/coco.py
Attributes¶
Methods:¶
from_coco_image
classmethod
¶
from_coco_image(
coco_image: CocoImage,
video_id: int | None = None,
frame_id: int | None = None,
) -> _TCocoVidImage
Create CocoVidImage object using CocoImage object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coco_image
¶ |
CocoImage
|
CocoImage |
required |
frame_id
¶ |
int | None
|
int 0-indexed frame id |
None
|
video_id
¶ |
int | None
|
int Video id |
None
|
Source code in sahi/utils/coco.py
add_annotation
¶
add_annotation(annotation: CocoVidAnnotation) -> None
Add annotation to this CocoImage instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
annotation
¶ |
CocoVidAnnotation
|
CocoVidAnnotation object to add. |
required |
Source code in sahi/utils/coco.py
CocoVideo
¶
CocoVideo(
name: str,
id: int | None = None,
fps: float | None = None,
height: int | None = None,
width: int | None = None,
)
COCO formatted video.
https://github.com/open-mmlab/mmtracking/blob/master/docs/tutorials/customize_dataset.md#the-cocovid-annotation-file
Create CocoVideo object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
str Video name |
required |
|
int | None
|
int Video id |
None
|
|
float | None
|
float Video fps |
None
|
|
int | None
|
int Video height in pixels |
None
|
|
int | None
|
int Video width in pixels |
None
|
Source code in sahi/utils/coco.py
Attributes¶
Methods:¶
add_image
¶
add_cocovidimage
¶
add_cocovidimage(cocovidimage: CocoVidImage) -> None
Add CocoVidImage to this CocoVideo instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cocovidimage
¶ |
CocoVidImage
|
CocoVidImage. |
required |
Source code in sahi/utils/coco.py
Coco
¶
Coco(
name: str | None = None,
image_dir: str | None = None,
remapping_dict: dict[int, int] | None = None,
ignore_negative_samples: bool = False,
clip_bboxes_to_img_dims: bool = False,
image_id_setting: Literal["auto", "manual"] = "auto",
)
COCO dataset object for managing images, annotations, and predictions.
Create Coco object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str | None
|
Name of the Coco dataset, determines exported json name. |
None
|
|
str | None
|
Base file directory that contains dataset images. Required for dataset merging. |
None
|
|
dict[int, int] | None
|
Maps category ids, e.g., {1:0, 2:1} maps category id 1 to 0. |
None
|
|
bool
|
If True, ignores images without annotations. |
False
|
|
bool
|
If True, clips bounding boxes to image dimensions. |
False
|
|
Literal['auto', 'manual']
|
How to assign image ids while exporting ("auto" or "manual"). |
'auto'
|
Source code in sahi/utils/coco.py
Attributes¶
category_mapping
property
¶
Get mapping of category IDs to names.
Methods:¶
add_categories_from_coco_category_list
¶
add_categories_from_coco_category_list(
coco_category_list: list[dict],
) -> None
Create CocoCategory object using coco category list.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coco_category_list
¶ |
list[dict]
|
List[Dict] [ {"supercategory": "person", "id": 1, "name": "person"}, {"supercategory": "vehicle", "id": 2, "name": "bicycle"} ] |
required |
Source code in sahi/utils/coco.py
add_category
¶
add_category(category: CocoCategory) -> None
Add category to this Coco instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
category
¶ |
CocoCategory
|
CocoCategory |
required |
Source code in sahi/utils/coco.py
add_image
¶
update_categories
¶
update_categories(
desired_name2id: dict[str, int],
update_image_filenames: bool = False,
) -> None
Rearrange category mapping of given COCO object based on given desired_name2id.
Can also be used to filter some of the categories.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
desired_name2id
¶ |
dict[str, int]
|
dict |
required |
update_image_filenames
¶ |
bool
|
bool If True, updates coco image file_names with absolute file paths. |
False
|
Source code in sahi/utils/coco.py
merge
¶
merge(
coco: Coco,
desired_name2id: dict | None = None,
verbose: int = 1,
) -> None
Combine the images/annotations/categories of given coco object with current one.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coco
¶ |
Coco
|
sahi.utils.coco.Coco instance A COCO dataset object |
required |
desired_name2id
¶ |
dict | None
|
dict |
None
|
verbose
¶ |
int
|
bool If True, merging info is printed |
1
|
Source code in sahi/utils/coco.py
from_coco_dict_or_path
classmethod
¶
from_coco_dict_or_path(
coco_dict_or_path: dict | str,
image_dir: str | None = None,
remapping_dict: dict | None = None,
ignore_negative_samples: bool = False,
clip_bboxes_to_img_dims: bool = False,
use_threads: bool = False,
num_threads: int = 10,
) -> _TCoco
Create coco object from COCO formatted dict or COCO dataset file path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coco_dict_or_path
¶ |
dict | str
|
dict/str or List[dict/str] COCO formatted dict or COCO dataset file path List of COCO formatted dict or COCO dataset file path |
required |
image_dir
¶ |
str | None
|
str Base file directory that contains dataset images. Required for merging and yolov5 conversion. |
None
|
remapping_dict
¶ |
dict | None
|
dict {1:0, 2:1} maps category id 1 to 0 and category id 2 to 1 |
None
|
ignore_negative_samples
¶ |
bool
|
bool If True ignores images without annotations in all operations. |
False
|
clip_bboxes_to_img_dims
¶ |
bool
|
bool = False Limits bounding boxes to image dimensions. |
False
|
use_threads
¶ |
bool
|
bool = False Use threads when processing the json image list, defaults to False |
False
|
num_threads
¶ |
int
|
int = 10 Slice the image list to given number of chunks, defaults to 10 |
10
|
Properties
images: list of CocoImage category_mapping: dict
Source code in sahi/utils/coco.py
1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 | |
calculate_stats
¶
Iterate over all annotations and calculate total number of.
Source code in sahi/utils/coco.py
1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 | |
split_coco_as_train_val
¶
split_coco_as_train_val(
train_split_rate: float = 0.9, numpy_seed: int = 0
) -> dict
Split images into train-val and return as Coco objects.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
train_split_rate
¶ |
float
|
float |
0.9
|
numpy_seed
¶ |
int
|
int random seed. Actually, this doesn't use numpy, but the random package from the standard library, but it is called numpy for compatibility. |
0
|
Returns:
| Name | Type | Description |
|---|---|---|
result |
dict
|
Dict with keys "train_coco" and "val_coco". |
Source code in sahi/utils/coco.py
export_as_yolo
¶
export_as_yolo(
output_dir: str | Path,
train_split_rate: float = 1.0,
numpy_seed: int = 0,
mp: bool = False,
disable_symlink: bool = False,
) -> None
Export current COCO dataset in YOLO format.
Creates train/val folders with image symlinks and txt files and a data yaml file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
output_dir
¶ |
str | Path
|
str Export directory. |
required |
train_split_rate
¶ |
float
|
If given 1, exports as train split. If 0, as val split. If between 0-1, exports both. |
1.0
|
numpy_seed
¶ |
int
|
Random seed for splitting. |
0
|
mp
¶ |
bool
|
If True, multiprocess mode is on (should be in 'if name == "main":' block). |
False
|
disable_symlink
¶ |
bool
|
If True, images will be copied instead of creating symlinks. |
False
|
Source code in sahi/utils/coco.py
1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 | |
get_subsampled_coco
¶
get_subsampled_coco(
subsample_ratio: int = 2, category_id: int | None = None
) -> Coco
Subsample images and return as Coco object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
subsample_ratio
¶ |
int
|
int 10 means take every 10th image with its annotations |
2
|
category_id
¶ |
int | None
|
int subsample only images containing given category_id, if -1 then subsamples negative samples |
None
|
Returns: subsampled_coco: sahi.utils.coco.Coco
Source code in sahi/utils/coco.py
1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 | |
get_upsampled_coco
¶
get_upsampled_coco(
upsample_ratio: int = 2, category_id: int | None = None
) -> Coco
Upsample images and return as Coco object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
upsample_ratio
¶ |
int
|
int 10 means copy each sample 10 times |
2
|
category_id
¶ |
int | None
|
int upsample only images containing given category_id, if -1 then upsamples negative samples |
None
|
Returns: upsampled_coco: sahi.utils.coco.Coco
Source code in sahi/utils/coco.py
get_area_filtered_coco
¶
get_area_filtered_coco(
min: int = 0,
max_val: float = float("inf"),
intervals_per_category: dict | None = None,
) -> Coco
Filter annotations by area and return remaining images as Coco object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
min
¶ |
int
|
int minimum allowed area |
0
|
max_val
¶ |
float
|
int maximum allowed area |
float('inf')
|
intervals_per_category
¶ |
dict | None
|
dict of dicts { "human": {"min": 20, "max": 10000}, "vehicle": {"min": 50, "max": 15000}, } |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
area_filtered_coco |
Coco
|
sahi.utils.coco.Coco |
Source code in sahi/utils/coco.py
get_coco_with_clipped_bboxes
¶
get_coco_with_clipped_bboxes() -> Coco
Limits overflowing bounding boxes to image dimensions.
Source code in sahi/utils/coco.py
DatasetClassCounts
dataclass
¶
CocoVid
¶
CocoVid(
name: str | None = None,
remapping_dict: dict | None = None,
)
COCOVid dataset object for managing videos, images, and annotations.
Initialize a COCOVid dataset object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str | None
|
Name of the CocoVid dataset, determines exported json name. |
None
|
|
dict | None
|
Category id mapping, e.g., {1:0, 2:1} maps id 1 to 0. |
None
|
Source code in sahi/utils/coco.py
Attributes¶
category_mapping
property
¶
Get mapping of category IDs to names.
Methods:¶
add_categories_from_coco_category_list
¶
add_categories_from_coco_category_list(
coco_category_list: list[dict],
) -> None
Create CocoCategory object using coco category list.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coco_category_list
¶ |
list[dict]
|
List[Dict] [ {"supercategory": "person", "id": 1, "name": "person"}, {"supercategory": "vehicle", "id": 2, "name": "bicycle"} ] |
required |
Source code in sahi/utils/coco.py
add_category
¶
add_category(category: CocoCategory) -> None
Add category to this CocoVid instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
category
¶ |
CocoCategory
|
CocoCategory |
required |
Source code in sahi/utils/coco.py
Functions:¶
export_yolo_images_and_txts_from_coco_object
¶
export_yolo_images_and_txts_from_coco_object(
output_dir: str,
coco: Coco,
ignore_negative_samples: bool = False,
mp: bool = False,
disable_symlink: bool = False,
) -> None
Create image symlinks and annotation txts in yolo format from coco dataset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
str Export directory. |
required |
|
Coco
|
sahi.utils.coco.Coco Initialized Coco object that contains images and categories. |
required |
|
bool
|
bool If True ignores images without annotations in all operations. |
False
|
|
bool
|
bool If True, multiprocess mode is on. Should be called in 'if name == main:' block. |
False
|
|
bool
|
bool If True, symlinks are not created. Instead images are copied. |
False
|
Source code in sahi/utils/coco.py
export_single_yolo_image_and_corresponding_txt
¶
export_single_yolo_image_and_corresponding_txt(
coco_image: CocoImage,
coco_image_dir: str,
output_dir: str,
ignore_negative_samples: bool = False,
disable_symlink: bool = False,
) -> None
Generate YOLO formatted image symlink and annotation txt file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
CocoImage
|
CocoImage object. |
required |
|
str
|
Image directory path. |
required |
|
str
|
Export directory. |
required |
|
bool
|
If True, ignores images without annotations. |
False
|
|
bool
|
If True, copies images instead of creating symlinks. |
False
|
Source code in sahi/utils/coco.py
1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 | |
update_categories
¶
update_categories(
desired_name2id: dict, coco_dict: dict
) -> dict
Rearrange category mapping of COCO dictionary.
Can also be used to filter some of the categories.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
dict
|
Desired category name to id mapping, e.g. {"big_vehicle": 1, "car": 2, "human": 3}. |
required |
|
dict
|
COCO formatted dictionary. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
coco_target |
dict
|
COCO dict with updated/filtered categories. |
Source code in sahi/utils/coco.py
update_categories_from_file
¶
update_categories_from_file(
desired_name2id: dict, coco_path: str, save_path: str
) -> None
Rearrange category mapping from COCO file.
Can also be used to filter some of the categories.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
dict
|
Category name to id mapping, e.g., {"human": 1, "car": 2}. |
required |
|
str
|
Path to COCO JSON file. |
required |
|
str
|
Path where the updated COCO JSON will be saved. |
required |
Source code in sahi/utils/coco.py
merge
¶
merge(
coco_dict1: dict,
coco_dict2: dict,
desired_name2id: dict | None = None,
) -> dict
Combine 2 coco formatted annotations dicts, and returns the combined coco dict.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
dict
|
dict First coco dictionary. |
required |
|
dict
|
dict Second coco dictionary. |
required |
|
dict | None
|
dict |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
merged_coco_dict |
dict
|
Merged COCO dict. |
Source code in sahi/utils/coco.py
merge_from_list
¶
merge_from_list(
coco_dict_list: list[dict],
desired_name2id: dict | None = None,
verbose: int = 1,
) -> dict
Combine a list of coco formatted annotations dicts, and returns the combined coco dict.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
list[dict]
|
list of dict A list of coco dicts |
required |
|
dict | None
|
dict |
None
|
|
int
|
bool If True, merging info is printed |
1
|
Returns:
merged_coco_dict: dict
Merged COCO dict.
Source code in sahi/utils/coco.py
merge_from_file
¶
merge_from_file(
coco_path1: str, coco_path2: str, save_path: str
) -> None
Combine 2 coco formatted annotations files given their paths, and saves the combined file to save_path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
Path for the first coco file. |
required |
|
str
|
Path for the second coco file. |
required |
|
str
|
Path to save the merged file, e.g. "dirname/coco.json". |
required |
Source code in sahi/utils/coco.py
get_imageid2annotationlist_mapping
¶
get_imageid2annotationlist_mapping(
coco_dict: dict,
) -> dict[int, list[dict]]
Get image_id to annotationlist mapping for faster indexing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
dict
|
COCO dict with fields "images", "annotations", "categories". |
required |
Returns:
| Name | Type | Description |
|---|---|---|
image_id_to_annotation_list |
dict[int, list[dict]]
|
Mapping from image id to list of annotation dicts. |
Source code in sahi/utils/coco.py
create_coco_dict
¶
create_coco_dict(
images: list[CocoImage],
categories: list[dict],
ignore_negative_samples: bool = False,
image_id_setting: str = "auto",
) -> dict
Create COCO dict with fields "images", "annotations", "categories".
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
list[CocoImage]
|
List of CocoImage containing a list of CocoAnnotation. |
required |
|
list[dict]
|
List of Dict COCO categories. |
required |
|
bool
|
If True, images without annotations are ignored. |
False
|
|
str
|
How to assign image ids while exporting can be
auto --> will assign id from scratch ( |
'auto'
|
Returns:
| Name | Type | Description |
|---|---|---|
coco_dict |
dict
|
COCO dict with fields "images", "annotations", "categories". |
Source code in sahi/utils/coco.py
1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 | |
create_coco_prediction_array
¶
create_coco_prediction_array(
images: list[CocoImage],
ignore_negative_samples: bool = False,
image_id_setting: str = "auto",
) -> list[dict]
Create COCO prediction array which is list of predictions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
list[CocoImage]
|
List of CocoImage containing a list of CocoAnnotation. |
required |
|
bool
|
If True, images without predictions are ignored. |
False
|
|
str
|
How to assign image ids while exporting can be
auto --> will assign id from scratch ( |
'auto'
|
Returns:
| Name | Type | Description |
|---|---|---|
coco_prediction_array |
list[dict]
|
COCO predictions array. |
Source code in sahi/utils/coco.py
add_bbox_and_area_to_coco
¶
add_bbox_and_area_to_coco(
source_coco_path: str = "",
target_coco_path: str = "",
add_bbox: bool = True,
add_area: bool = True,
) -> dict
Calculate and fill bbox and area fields in COCO annotations.
Takes a COCO dataset file, calculates bbox and area fields, and exports updated dict.
Returns:
| Name | Type | Description |
|---|---|---|
coco_dict |
dict
|
Updated COCO dict. |
Source code in sahi/utils/coco.py
count_images_with_category
¶
count_images_with_category(
coco_file_path: str,
) -> DatasetClassCounts
Count images with each category in COCO dataset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
Path to COCO dataset file. |
required |
Returns:
| Type | Description |
|---|---|
DatasetClassCounts
|
DatasetClassCounts object storing counts. |
Source code in sahi/utils/coco.py
remove_invalid_coco_results
¶
remove_invalid_coco_results(
result_list_or_path: list | str,
dataset_dict_or_path: dict | str | None = None,
) -> list[dict]
Remove invalid predictions from coco result.
Removes predictions with negative bbox values or extreme bbox values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
list | str
|
path or list for coco result json |
required |
|
optional
|
path or dict for coco dataset json |
None
|
Source code in sahi/utils/coco.py
export_coco_as_yolo
¶
export_coco_as_yolo(
output_dir: str,
train_coco: Coco | None = None,
val_coco: Coco | None = None,
train_split_rate: float = 0.9,
numpy_seed: int = 0,
disable_symlink: bool = False,
) -> str
Export current COCO dataset in ultralytics/YOLO format.
Creates train val folders with image symlinks and txt files and a data yaml file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
str Export directory. |
required |
|
Coco | None
|
Coco coco object for training |
None
|
|
Coco | None
|
Coco coco object for val |
None
|
|
float
|
float train split rate between 0 and 1. will be used when val_coco is None. |
0.9
|
|
int
|
int To fix the numpy seed. |
0
|
|
bool
|
bool If True, copy images instead of creating symlinks. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
yaml_path |
str
|
str Path for the exported YOLO data.yml |
Source code in sahi/utils/coco.py
2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 | |
export_coco_as_yolo_via_yml
¶
export_coco_as_yolo_via_yml(
yml_path: str,
output_dir: str,
train_split_rate: float = 0.9,
numpy_seed: int = 0,
disable_symlink: bool = False,
) -> str
Export current COCO dataset in ultralytics/YOLO format using a YML file.
Creates train val folders with image symlinks and txt files and a data yaml file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
str file should contain these fields: train_json_path: str train_image_dir: str val_json_path: str val_image_dir: str |
required |
|
str
|
str Export directory. |
required |
|
float
|
float train split rate between 0 and 1. will be used when val_json_path is None. |
0.9
|
|
int
|
int To fix the numpy seed. |
0
|
|
bool
|
bool If True, copy images instead of creating symlinks. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
yaml_path |
str
|
str Path for the exported YOLO data.yml |
Source code in sahi/utils/coco.py
2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 | |