|
| 1 | +(ImageCropBoxes)= |
| 2 | +# ImageCropBoxes |
| 3 | + |
| 4 | +## Overview |
| 5 | + |
| 6 | +`ImageCropBoxes` is a PySpark ML transformer that crops images based on provided bounding boxes. It is designed to process images in Spark pipelines, supporting batch and distributed processing. The transformer can add padding to crops, limit the number of crops per image, and handle cases where no boxes are present. |
| 7 | + |
| 8 | +## Usage Example |
| 9 | + |
| 10 | +```python |
| 11 | +from scaledp import FaceDetector, ImageCropBoxes, PipelineModel |
| 12 | + |
| 13 | +# Step 1: Detect faces in images |
| 14 | +detector = FaceDetector( |
| 15 | + inputCol="image", |
| 16 | + outputCol="boxes", |
| 17 | + keepInputData=True, |
| 18 | + scoreThreshold=0.25, |
| 19 | + padding=20, |
| 20 | +) |
| 21 | + |
| 22 | +# Step 2: Crop images using detected face boxes |
| 23 | +cropper = ImageCropBoxes( |
| 24 | + inputCols=["image", "boxes"], |
| 25 | + outputCol="cropped_image", |
| 26 | + keepInputData=True, |
| 27 | + padding=10, |
| 28 | + limit=5, |
| 29 | + noCrop=True, |
| 30 | + autoRotate=False, # Automatically rotate crops if box height > width |
| 31 | +) |
| 32 | + |
| 33 | +# Build and run the pipeline |
| 34 | +pipeline = PipelineModel(stages=[detector, cropper]) |
| 35 | +result = pipeline.transform(image_df) |
| 36 | +result.show_image("cropped_image") |
| 37 | +``` |
| 38 | + |
| 39 | + |
| 40 | + |
| 41 | +## Parameters |
| 42 | + |
| 43 | +| Parameter | Type | Description | Default | |
| 44 | +|-------------------|---------|--------------------------------------------------|-----------------| |
| 45 | +| inputCols | list | Input columns: image and boxes | ["image", "boxes"] | |
| 46 | +| outputCol | str | Output column for cropped images | "cropped_image"| |
| 47 | +| keepInputData | bool | Keep input data in output | False | |
| 48 | +| imageType | Enum | Type of image (e.g., FILE) | ImageType.FILE | |
| 49 | +| numPartitions | int | Number of partitions for Spark | 0 | |
| 50 | +| padding | int | Padding added to each crop | 0 | |
| 51 | +| pageCol | str | Page column for repartitioning | "page" | |
| 52 | +| propagateError | bool | Propagate errors | False | |
| 53 | +| noCrop | bool | Raise error if no boxes to crop | True | |
| 54 | +| limit | int | Limit number of crops per image | 0 (no limit) | |
| 55 | +| autoRotate | bool | Auto rotate crop if box height > width | True | |
| 56 | + |
| 57 | +## Notes |
| 58 | +- Crops are performed using bounding boxes from the `boxes` column. |
| 59 | +- If `noCrop` is True and no boxes are present, an error is raised. |
| 60 | +- If `limit` is set, only the first N boxes are used for cropping. |
| 61 | +- If `autoRotate` is True, crops are rotated if the bounding box height is greater than its width. |
| 62 | +- Supports distributed processing with Spark. |
| 63 | +- Errors can be propagated or handled gracefully based on `propagateError`. |
0 commit comments