builder
data.crop.builder
CROPPER_IMPLEMENTATION
module-attribute
CROPPER_IMPLEMENTATION = {'ocr': OcrCropper, 'polygon': PolygonCropper, 'edge': EdgeCropper}
BaseCropper
Bases: ABC
get_n_patch
abstractmethod
PolygonCropper
PolygonCropper(mode: str = 'center', crop_w: int = 512, crop_h: int = 512, patch_per_polygon: int = 1, random_patch_per_img: int = 1, max_patch_per_img: Optional[int] = None, force_random_patch_normal: bool = False, strict_inner_patch: bool = False, fixed_polygon_order: bool = False)
Bases: BaseCropper
Crop image patches using polygons
Attributes:
-
mode(str) –cropping mode. ["center", "defectrandom"] available
-
crop_w(int) –cropping width for image patch.
-
crop_h(int) –cropping height for image patch.
-
half_w(int) –half of cropping width.
-
half_h(int) –half of cropping height.
-
patch_per_polygon(int) –number of repeat for cropping patch around each polygon.
-
random_patch_per_img(int) –number of random patch cropping per image.
-
force_random_patch_normal(bool) –whether random patch should not contain polygon area.
-
strict_inner_patch(bool) –whether not allowing outer area of image.
-
fixed_polygon_order(bool) –whether order of polygons is fixed during cropping.
All input is directly set to class attribute.
Parameters:
-
mode(str, default:'center') –Defaults to "center".
-
crop_w(int, default:512) –Defaults to 512.
-
crop_h(int, default:512) –Defaults to 512.
-
patch_per_polygon(int, default:1) –Defaults to 1.
-
random_patch_per_img(int, default:1) –Defaults to 1.
-
force_random_patch_normal(bool, default:False) –Defaults to False.
-
strict_inner_patch(bool, default:False) –Defaults to False.
-
fixed_polygon_order(bool, default:False) –Defaults to False.
Source code in SaigeToolkit/data/crop/polygon_cropper.py
get_n_patch
PolygonCropper crops all polygon areas {self.patch_per_polygon} times per polygons, and also crops random position of image {self.random_patch_per_img} times per images. Therefore, resultant number of patches is weighted-sum result as coded below.
Parameters:
-
polygons(List[ndarray]) –polygons of single image in dataset.
Returns:
-
int(int) –number of patches to be cropped in single image
Source code in SaigeToolkit/data/crop/polygon_cropper.py
pick_point
picking cropping center point.
Parameters:
-
polygon(ndarray) –original polygon data
Raises:
-
NotImplementedError–only two modes ["center", "defectrandom"] available
Returns:
-
Tuple[int, int]–Tuple[int, int]: picked point, (h_center, w_center)
Source code in SaigeToolkit/data/crop/polygon_cropper.py
get_coordinates_from_center
get_coordinates_from_center(h_center: int, w_center: int, h: int, w: int) -> Tuple[int, int, int, int]
get left/right top/bottom coordinates from picked center point.
Parameters:
-
h_center(int) –picked center point h-coordinate
-
w_center(int) –picked center point w-coordinate
-
h(int) –image size height
-
w(int) –image size width
Returns:
-
Tuple[int, int, int, int]–Tuple[int, int, int, int]: (crop_left, crop_top, crop_right, crop_bottom)
Source code in SaigeToolkit/data/crop/polygon_cropper.py
__call__
__call__(image: Union[Image, ndarray], mask: Union[Image, ndarray], polygons: Optional[List[ndarray]] = None, **kwargs) -> List[dict]
crop image into image patches.
Parameters:
-
image(Union[Image, ndarray]) –original image
-
polygons(List[ndarray], default:None) –polygon data (# of polygons, (4, 2)) polygon should have 4 points
-
mask(Union[Image, ndarray]) –segmentation mask image
Returns:
-
List[dict]–List[dict]: list of cropped image data dict
Source code in SaigeToolkit/data/crop/polygon_cropper.py
OcrCropper
OcrCropper(chr_height: int = 32, crop_jitter: Optional[float] = None, crop_jitter_rate: Optional[List[float]] = None, save_polygons: bool = False)
Cropper class for OCR task.
Attributes:
-
chr_height(int) –cropped image patch is resized to be the same height as chr_height.
-
crop_jitter(Optional[float]) –if not None, cropping box is jittered horizontally with ratio crop_jitter.
-
crop_jitter_rate(Optional[List[float]]) –if not None, cropping box points are jittered to any direction with ratio crop_jitter_rate.
-
save_polygons(bool) –whether polygon label is preserved after cropping.
initializing OcrCropper. all initializing input is set to class attribute.
Parameters:
-
chr_height(int, default:32) –Defaults to 32.
-
crop_jitter(Optional[float], default:None) –Defaults to None.
-
crop_jitter_rate(Optional[List[float]], default:None) –Defaults to None.
-
save_polygons(bool, default:False) –Defaults to False.
Source code in SaigeToolkit/data/crop/ocr_cropper.py
get_n_patch
OcrCropper only crops all polygon areas, unlike SegCropper. Therefore, resultant number of patches is simply equals to number of polygons.
Parameters:
-
polygons(List[List[ndarray]]) –polygons of all images in dataset.
Returns:
-
int(int) –total number of patches to be cropped
Source code in SaigeToolkit/data/crop/ocr_cropper.py
__call__
__call__(image: Union[Image, ndarray], polygons: List[ndarray], strings: List[str], ignore: List[bool], is_vertical: Optional[List[bool]] = None, **kwargs) -> List[dict]
crop image into image patches. polygon, string, ignore data should be same length.
Parameters:
-
image(Union[Image, ndarray]) –original image
-
polygons(List[ndarray]) –polygon data (# of polygons, (4, 2)) polygon should have 4 points
-
strings(List[str]) –string data
-
ignore(List[bool]) –ignore notation data
-
is_vertical(List[bool], default:None) –is_vertical notation data
Returns:
-
List[dict]–List[dict]: list of cropped image data dict
Source code in SaigeToolkit/data/crop/ocr_cropper.py
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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | |
calculate_width_height
calculate width and heights for every four polygon points (2 tops and 2 bots). Assuming that polygon is composed of top-points and bot-points, and every top and bot points are paired.
Parameters:
-
tops(ndarray) –top points of polygon
-
bots(ndarray) –bot points of polygon
Returns:
-
Tuple[List[ndarray], List[ndarray]]–Tuple[List[np.ndarray]]: width and height list for every four points.
Source code in SaigeToolkit/data/crop/ocr_cropper.py
jitter_polygon
jittering polygon points and split into tops and bots points
Parameters:
-
polygon(ndarray) –original polygon data
Returns:
-
Tuple[ndarray, ndarray]–Tuple[np.ndarray, np.ndarray]: jittered tops, bots points
Source code in SaigeToolkit/data/crop/ocr_cropper.py
EdgeCropper
EdgeCropper(crop_w: int = 512, crop_h: int = 512, random_sample: bool = False, patch_per_polygon: int = 1, strict_inner_patch: bool = True, ok_patch_prob: float = 0.01)
Bases: BaseCropper
Crop image patches along the contour of mask (or polygon)
Attributes:
-
window_manager(WindowManager) –WindowManager
-
random_sample(bool) –whether randomly select the first point of each polygon
-
patch_per_polygon(bool) –number of repeat for cropping patches around each polygon.
Source code in SaigeToolkit/data/crop/edge_cropper.py
window_manager
instance-attribute
window_manager = WindowManager(crop_w, crop_h, strict_inner_patch)
init_centers
get_n_patch
PolygonCropper crops all polygon areas {self.patch_per_polygon} times per polygons, and also crops random position of image {self.random_patch_per_img} times per images. Therefore, resultant number of patches is weighted-sum result as coded below.
Parameters:
-
polygons(List[ndarray]) –polygons of single image in dataset.
Returns:
-
int(int) –number of patches to be cropped in single image
Source code in SaigeToolkit/data/crop/edge_cropper.py
__call__
__call__(image: Union[Image, ndarray], mask: Union[Image, ndarray], polygons: Optional[List[ndarray]] = None, **kwargs) -> List[dict]
crop image into image patches.
Parameters:
-
image(Union[Image, ndarray]) –original image
-
polygons(List[ndarray], default:None) –polygon data (# of polygons, (4, 2)) polygon should have 4 points
-
mask(Union[Image, ndarray]) –segmentation mask image
Returns:
-
List[dict]–List[dict]: list of cropped image data dict
Source code in SaigeToolkit/data/crop/edge_cropper.py
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 | |
get_data_dict_from_center_point
get_data_dict_from_center_point(image: Union[Image, ndarray], mask: Union[Image, ndarray], center, **kwargs)
Source code in SaigeToolkit/data/crop/edge_cropper.py
pick_centers_from_polygon
Source code in SaigeToolkit/data/crop/edge_cropper.py
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 | |
get_proper_point
get_proper_point(state_as_satellite: Union[Dict[str, Any], List[Dict[str, Any]]], must_contain_coordinate: List[Iterable[int]], point_in: Iterable[int], point_out: Iterable[int]) -> Optional[Iterable[int]]
get proper next center point that contains every must_contain_coordinate
Parameters:
-
state_as_satellite(Union[Dict[str, Any], List[Dict[str, Any]]]) –relation between center point(s) and target point.
-
must_contain_coordinate(List[Iterable[int]]) –points should be in next window
-
point_in(Iterable[int]) –point inside window from previous boundary
-
point_out(Iterable[int]) –point outside window from previous boundary
Returns:
-
Optional[Iterable[int]]–Optional[Iterable[int]]: proper next center (or None)
Source code in SaigeToolkit/data/crop/edge_cropper.py
get_destination_point
get_destination_point(relation: WindowState, point_ref: Iterable[int], point_in: Iterable[int], point_out: Iterable[int]) -> ndarray
calculate point a-window-away from reference point
Parameters:
-
relation(WindowState) –relation between reference point and next center
-
point_ref(Iterable[int]) –reference point to calculate next center
-
point_in(Iterable[int]) –point inside window from previous boundary
-
point_out(Iterable[int]) –point outside window from previous boundary
Returns:
-
ndarray–np.ndarray: result destination point
Source code in SaigeToolkit/data/crop/edge_cropper.py
remove_inner_points_and_get_outer_points
remove_inner_points_and_get_outer_points(center: Iterable[int], points: List[Iterable[int]]) -> Tuple[List[Iterable[int]], Iterable[int], Iterable[int]]
once center decided, check other points and remove insiders.
Parameters:
-
center(Iterable[int]) –next center coordinate
-
points(List[Iterable[int]]) –current points on polygon
Returns:
-
Tuple[List[Iterable[int]], Iterable[int], Iterable[int]]–Tuple[List[Iterable[int]], Iterable[int], Iterable[int]]: - List[Iterable[int]]: clean-up'd polygon points - Iterable[int]: boundary point (positive direction) - Iterable[int]: boundary point (negative direction)
Source code in SaigeToolkit/data/crop/edge_cropper.py
get_crop_fn
get_crop_fn(_target_: Optional[str] = None, **cfg_crop: dict) -> Optional[BaseCropper]
getting crop function
Parameters:
-
cfg_crop(dict, default:{}) –config dict for building cropper
Returns:
-
Optional[BaseCropper]–Optional[BaseCropper]: saige cropper object. if cfg_crop is None, return None
Source code in SaigeToolkit/data/crop/builder.py
get_crop_class
get_crop_class(cfg_crop_name: str) -> Type[BaseCropper]
getting crop class from name
Parameters:
-
cfg_crop_name(str) –cropper name
Returns:
-
Type[BaseCropper]–Type[BaseCropper]: cropper class