Reference for ultralytics/solutions/object_cropper.py
Note
This file is available at https://github.com/ultralytics/ultralytics/blob/main/ultralytics/solutions/object_cropper.py. If you spot a problem please help fix it by contributing a Pull Request 🛠️. Thank you 🙏!
ultralytics.solutions.object_cropper.ObjectCropper
Bases: BaseSolution
A class to manage the cropping of detected objects in a real-time video stream or images.
This class extends the BaseSolution class and provides functionality for cropping objects based on detected bounding boxes. The cropped images are saved to a specified directory for further analysis or usage.
Attributes:
Name | Type | Description |
---|---|---|
crop_dir |
str
|
Directory where cropped object images are stored. |
crop_idx |
int
|
Counter for the total number of cropped objects. |
iou |
float
|
IoU (Intersection over Union) threshold for non-maximum suppression. |
conf |
float
|
Confidence threshold for filtering detections. |
Methods:
Name | Description |
---|---|
process |
Crops detected objects from the input image and saves them to the output directory. |
Examples:
>>> cropper = ObjectCropper()
>>> frame = cv2.imread("frame.jpg")
>>> processed_results = cropper.process(frame)
>>> print(f"Total cropped objects: {cropper.crop_idx}")
Parameters:
Name | Type | Description | Default |
---|---|---|---|
**kwargs
|
Any
|
Keyword arguments passed to the parent class and used for configuration. crop_dir (str): Path to the directory for saving cropped object images. |
{}
|
Source code in ultralytics/solutions/object_cropper.py
process
Crop detected objects from the input image and save them as separate images.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
im0
|
ndarray
|
The input image containing detected objects. |
required |
Returns:
Type | Description |
---|---|
SolutionResults
|
A SolutionResults object containing the total number of cropped objects and processed image. |
Examples:
>>> cropper = ObjectCropper()
>>> frame = cv2.imread("image.jpg")
>>> results = cropper.process(frame)
>>> print(f"Total cropped objects: {results.total_crop_objects}")