crop
Module diagram
classDiagram
class crop {
}
class base_cropper {
}
class builder {
}
class edge_cropper {
}
class ocr_cropper {
}
class polygon_cropper {
}
crop --> builder
builder --> base_cropper
builder --> edge_cropper
builder --> ocr_cropper
builder --> polygon_cropper
edge_cropper --> base_cropper
polygon_cropper --> base_cropper
data.crop
데이터 전처리를 위한 cropper 클래스를 제공합니다.
모든 cropper 클래스는 BaseCropper 클래스를 상속받아 구현되어 있으며, get_n_patch와 call 메소드를 구현해야 합니다.
- get_n_patch 메소드는 이미지를 잘라낼 때 몇 개의 패치로 나눌지를 결정합니다.
- call 메소드는 이미지를 입력받아 패치로 나누어 반환합니다.
사용 방식에 대해서는 dataset/base_dataset.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
base_cropper
Cropper 클래스의 추상화를 위한 BaseCropper 클래스를 제공합니다.
모든 Cropper 클래스는 BaseCropper 클래스를 상속받아 구현되어야 하며, get_n_patch와 call 메소드를 구현해야 합니다.
builder
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
Source code in SaigeToolkit/data/crop/builder.py
edge_cropper
Crop image patches along the contour of mask (or polygon)
Imported from DefectGeneration repository. generation/data/crop/edge_cropper.py
WindowState
Bases: Enum
relation state enum between center point and target point
WindowManager
Manager for cropping window size, relation and window coordinates.
Attributes:
-
width(int) –window width for image patch.
-
half_width(int) –half of window width for image patch.
-
height(int) –window height for image patch.
-
half_height(int) –half of window height for image patch.
-
strict_inner_patch(bool) –whether not allowing outer area of image.
Source code in SaigeToolkit/data/crop/edge_cropper.py
check_window
check_window(point: Iterable[int], center: Iterable[int]) -> WindowState
check whether target point is inside of window, returns WindowState
Parameters:
-
point(Iterable[int]) –target point to be checked
-
center(Iterable[int]) –reference center point
Returns:
-
WindowState(WindowState) –relation state enum
Source code in SaigeToolkit/data/crop/edge_cropper.py
is_in_window
is target point inside of window
Parameters:
-
point(Iterable[int]) –target point to be checked
-
center(Iterable[int]) –reference center point
Returns:
-
bool(bool) –boolean result
Source code in SaigeToolkit/data/crop/edge_cropper.py
is_in_any_window
is target point inside of any of windows
Parameters:
-
point(Iterable[int]) –target point to be checked
-
center(Iterable[int]) –list of reference center points
Returns:
-
bool(bool) –boolean result
Source code in SaigeToolkit/data/crop/edge_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/edge_cropper.py
CenterWithSatellite
dataclass
CenterWithSatellite(point: Iterable[int], window_manager: WindowManager, satellite: Optional[Iterable[int]] = None)
Center point with 'satellites'. 'Satellites' is a point within center point's window, but not the center point. Also intra distance between 'satellites' should shorter than window size.
Attributes:
-
coordinate(Iterable[int]) –center point coordinate
-
window_manager(WindowManager) –WindowManager
-
max_width_distance(int) –max width-wise positive distance of satellites
-
max_height_distance(int) –max height-wise positive distance of satellites
-
min_width_distance(int) –max width-wise negative distance of satellites
-
min_height_distance(int) –max height-wise negative distance of satellites
-
satellite_points(List[Dict[str, any]]) –satellite_points data
Source code in SaigeToolkit/data/crop/edge_cropper.py
calculate_relation_as_satellite
calculate_relation_as_satellite(point: Iterable[int]) -> Union[Dict[str, Any], List[Dict[str, Any]]]
calculate relation between target point and center point. case 1: If target point is outside of center point's window, target point should be considered as next center point candidate.
case 2
Even when target point is in the window, check whether distance between satellites is larger than window size. If so, target point should be considered as next center point candidate.
Parameters:
-
point(Iterable[int]) –target point to be checked
Returns:
-
Union[Dict[str, Any], List[Dict[str, Any]]]–Union[Dict[str, Any], List[Dict[str, Any]]]: Dict[str, Any]: simple relation between center point and target point List[Dict[str, Any]]: reltations between satellite points and target point
Source code in SaigeToolkit/data/crop/edge_cropper.py
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 233 234 235 236 237 238 239 240 241 242 243 244 245 246 | |
_compare_point_and_calculate_error
Refactored from the original code from Defect Generation (Function extracted).
Source code in SaigeToolkit/data/crop/edge_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
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_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
subdivide_points_into_min_resolution
subdivide_points_into_min_resolution(points: List[Iterable[int]], width_resolution: int = 32, height_resolution: int = 32) -> List[Iterable[int]]
refine cv2.findContours result. long straight edge in mask results in long distance between two points.
Parameters:
-
points(List[Iterable[int]]) –cv2.findContours result
-
width_resolution(int, default:32) –maximum distance width-wise. Defaults to 32.
-
height_resolution(int, default:32) –maximum distance height-wise. Defaults to 32.
Returns:
-
List[Iterable[int]]–List[Iterable[int]]: refined polygon points
Source code in SaigeToolkit/data/crop/edge_cropper.py
get_mid_point
In case failed to get next center point, get mid point from remaining polygon points.
Parameters:
-
points(List[Iterable[int]]) –remaining polygon points
Returns:
-
Iterable[int]–Iterable[int]: next center point (mid)
Source code in SaigeToolkit/data/crop/edge_cropper.py
ocr_cropper
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
polygon_cropper
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