Reference for ultralytics/models/yolo/detect/predict.py
Note
This file is available at https://github.com/ultralytics/ultralytics/blob/main/ultralytics/models/yolo/detect/predict.py. If you spot a problem please help fix it by contributing a Pull Request 🛠️. Thank you 🙏!
ultralytics.models.yolo.detect.predict.DetectionPredictor
DetectionPredictor(
cfg=DEFAULT_CFG,
overrides: Optional[Dict[str, Any]] = None,
_callbacks: Optional[Dict[str, List[callable]]] = None,
)
Bases: BasePredictor
A class extending the BasePredictor class for prediction based on a detection model.
This predictor specializes in object detection tasks, processing model outputs into meaningful detection results with bounding boxes and class predictions.
Attributes:
Name | Type | Description |
---|---|---|
args |
namespace
|
Configuration arguments for the predictor. |
model |
Module
|
The detection model used for inference. |
batch |
list
|
Batch of images and metadata for processing. |
Methods:
Name | Description |
---|---|
postprocess |
Process raw model predictions into detection results. |
construct_results |
Build Results objects from processed predictions. |
construct_result |
Create a single Result object from a prediction. |
get_obj_feats |
Extract object features from the feature maps. |
Examples:
>>> from ultralytics.utils import ASSETS
>>> from ultralytics.models.yolo.detect import DetectionPredictor
>>> args = dict(model="yolo11n.pt", source=ASSETS)
>>> predictor = DetectionPredictor(overrides=args)
>>> predictor.predict_cli()
Source code in ultralytics/engine/predictor.py
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 |
|
construct_result
construct_result(pred, img, orig_img, img_path)
Construct a single Results object from one image prediction.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pred
|
Tensor
|
Predicted boxes and scores with shape (N, 6) where N is the number of detections. |
required |
img
|
Tensor
|
Preprocessed image tensor used for inference. |
required |
orig_img
|
ndarray
|
Original image before preprocessing. |
required |
img_path
|
str
|
Path to the original image file. |
required |
Returns:
Type | Description |
---|---|
Results
|
Results object containing the original image, image path, class names, and scaled bounding boxes. |
Source code in ultralytics/models/yolo/detect/predict.py
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
|
construct_results
construct_results(preds, img, orig_imgs)
Construct a list of Results objects from model predictions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
preds
|
List[Tensor]
|
List of predicted bounding boxes and scores for each image. |
required |
img
|
Tensor
|
Batch of preprocessed images used for inference. |
required |
orig_imgs
|
List[ndarray]
|
List of original images before preprocessing. |
required |
Returns:
Type | Description |
---|---|
List[Results]
|
List of Results objects containing detection information for each image. |
Source code in ultralytics/models/yolo/detect/predict.py
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
|
get_obj_feats
get_obj_feats(feat_maps, idxs)
Extract object features from the feature maps.
Source code in ultralytics/models/yolo/detect/predict.py
84 85 86 87 88 89 90 91 92 |
|
postprocess
postprocess(preds, img, orig_imgs, **kwargs)
Post-process predictions and return a list of Results objects.
This method applies non-maximum suppression to raw model predictions and prepares them for visualization and further analysis.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
preds
|
Tensor
|
Raw predictions from the model. |
required |
img
|
Tensor
|
Processed input image tensor in model input format. |
required |
orig_imgs
|
Tensor | list
|
Original input images before preprocessing. |
required |
**kwargs
|
Any
|
Additional keyword arguments. |
{}
|
Returns:
Type | Description |
---|---|
list
|
List of Results objects containing the post-processed predictions. |
Examples:
>>> predictor = DetectionPredictor(overrides=dict(model="yolo11n.pt"))
>>> results = predictor.predict("path/to/image.jpg")
>>> processed_results = predictor.postprocess(preds, img, orig_imgs)
Source code in ultralytics/models/yolo/detect/predict.py
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
|