Function for performing prediction for given image using given detection_model. Args: image: str or np.ndarray Location of image or numpy image matrix to slice detection_model: model.DetectionMode shift_amount: List To shift the box and mask predi
(
image: str | np.ndarray | Image.Image,
detection_model: DetectionModel,
shift_amount: list[int] | None = None,
full_shape: list[int] | None = None,
postprocess: PostprocessPredictions | None = None,
verbose: int = 0,
exclude_classes_by_name: list[str] | None = None,
exclude_classes_by_id: list[int] | None = None,
confidence_threshold: float | None = None,
)
| 75 | |
| 76 | |
| 77 | def get_prediction( |
| 78 | image: str | np.ndarray | Image.Image, |
| 79 | detection_model: DetectionModel, |
| 80 | shift_amount: list[int] | None = None, |
| 81 | full_shape: list[int] | None = None, |
| 82 | postprocess: PostprocessPredictions | None = None, |
| 83 | verbose: int = 0, |
| 84 | exclude_classes_by_name: list[str] | None = None, |
| 85 | exclude_classes_by_id: list[int] | None = None, |
| 86 | confidence_threshold: float | None = None, |
| 87 | ) -> PredictionResult: |
| 88 | """Function for performing prediction for given image using given detection_model. |
| 89 | |
| 90 | Args: |
| 91 | image: str or np.ndarray |
| 92 | Location of image or numpy image matrix to slice |
| 93 | detection_model: model.DetectionMode |
| 94 | shift_amount: List |
| 95 | To shift the box and mask predictions from sliced image to full |
| 96 | sized image, should be in the form of [shift_x, shift_y] |
| 97 | full_shape: List |
| 98 | Size of the full image, should be in the form of [height, width] |
| 99 | postprocess: sahi.postprocess.combine.PostprocessPredictions |
| 100 | verbose: int |
| 101 | 0: no print (default) |
| 102 | 1: print prediction duration |
| 103 | exclude_classes_by_name: Optional[List[str]] |
| 104 | None: if no classes are excluded |
| 105 | List[str]: set of classes to exclude using its/their class label name/s |
| 106 | exclude_classes_by_id: Optional[List[int]] |
| 107 | None: if no classes are excluded |
| 108 | List[int]: set of classes to exclude using one or more IDs |
| 109 | confidence_threshold: float, optional |
| 110 | Override the model's confidence threshold for this call only. |
| 111 | The model's original threshold is restored after the call. |
| 112 | |
| 113 | Example: |
| 114 | >>> from sahi import AutoDetectionModel |
| 115 | >>> from sahi.predict import get_prediction |
| 116 | >>> model = AutoDetectionModel.from_pretrained( |
| 117 | ... model_type="ultralytics", |
| 118 | ... model_path="yolo11n.pt", |
| 119 | ... confidence_threshold=0.3, |
| 120 | ... ) |
| 121 | >>> # run with a different threshold without changing the model |
| 122 | >>> result = get_prediction("image.jpg", model, confidence_threshold=0.7) |
| 123 | >>> print(model.confidence_threshold) # still 0.3 |
| 124 | |
| 125 | Returns: |
| 126 | A dict with fields: |
| 127 | object_prediction_list: a list of ObjectPrediction |
| 128 | durations_in_seconds: a dict containing elapsed times for profiling |
| 129 | """ |
| 130 | original_confidence_threshold = detection_model.confidence_threshold |
| 131 | if confidence_threshold is not None: |
| 132 | detection_model.confidence_threshold = confidence_threshold |
| 133 | |
| 134 | try: |