Initialize ObjectAnnotation. Args: bbox: List [minx, miny, maxx, maxy] segmentation: List[List] [ [x1, y1, x2, y2, x3, y3, ...], [x1, y1, x2, y2, x3, y3, ...], ...
(
self,
bbox: list[float] | None = None,
segmentation: np.ndarray | list[list[float]] | None = None,
category_id: int | None = None,
category_name: str | None = None,
shift_amount: list[int] | list[int | float] | None = None,
full_shape: list[int] | list[int | float] | None = None,
)
| 339 | """All about an annotation such as Mask, Category, BoundingBox.""" |
| 340 | |
| 341 | def __init__( |
| 342 | self, |
| 343 | bbox: list[float] | None = None, |
| 344 | segmentation: np.ndarray | list[list[float]] | None = None, |
| 345 | category_id: int | None = None, |
| 346 | category_name: str | None = None, |
| 347 | shift_amount: list[int] | list[int | float] | None = None, |
| 348 | full_shape: list[int] | list[int | float] | None = None, |
| 349 | ) -> None: |
| 350 | """Initialize ObjectAnnotation. |
| 351 | |
| 352 | Args: |
| 353 | bbox: List |
| 354 | [minx, miny, maxx, maxy] |
| 355 | segmentation: List[List] |
| 356 | [ |
| 357 | [x1, y1, x2, y2, x3, y3, ...], |
| 358 | [x1, y1, x2, y2, x3, y3, ...], |
| 359 | ... |
| 360 | ] |
| 361 | category_id: int |
| 362 | ID of the object category |
| 363 | category_name: str |
| 364 | Name of the object category |
| 365 | shift_amount: List |
| 366 | To shift the box and mask predictions from sliced image |
| 367 | to full sized image, should be in the form of [shift_x, shift_y] |
| 368 | full_shape: List |
| 369 | Size of the full image after shifting, should be in |
| 370 | the form of [height, width]. |
| 371 | """ |
| 372 | if not isinstance(category_id, int): |
| 373 | raise ValueError("category_id must be an integer") |
| 374 | if (bbox is None) and (segmentation is None): |
| 375 | raise ValueError("you must provide a bbox or segmentation") |
| 376 | |
| 377 | if shift_amount is None: |
| 378 | shift_amount = [0, 0] |
| 379 | |
| 380 | self.mask: Mask | None = None |
| 381 | if segmentation is not None: |
| 382 | self.mask = Mask( |
| 383 | segmentation=segmentation, |
| 384 | shift_amount=shift_amount, |
| 385 | full_shape=full_shape, |
| 386 | ) |
| 387 | # Convert to list if ndarray for get_bbox_from_coco_segmentation |
| 388 | seg_for_bbox = segmentation if not isinstance(segmentation, np.ndarray) else segmentation.tolist() |
| 389 | bbox_from_segmentation = get_bbox_from_coco_segmentation(seg_for_bbox) # type: ignore[arg-type] |
| 390 | # https://github.com/obss/sahi/issues/235 |
| 391 | if bbox_from_segmentation is not None: |
| 392 | bbox = bbox_from_segmentation |
| 393 | else: |
| 394 | raise ValueError("Invalid segmentation mask.") |
| 395 | |
| 396 | # if bbox is a numpy object, convert it to python List[float] |
| 397 | if isinstance(bbox, np.ndarray): |
| 398 | bbox = copy.deepcopy(bbox).tolist() |
nothing calls this directly
no test coverage detected