diff --git a/supervision/detection/core.py b/supervision/detection/core.py index 39e129e1..6ade4bbf 100644 --- a/supervision/detection/core.py +++ b/supervision/detection/core.py @@ -126,14 +126,14 @@ class Detections: Example: ```python - >>> import cv2 - >>> import torch - >>> import supervision as sv + import cv2 + import torch + import supervision as sv - >>> image = cv2.imread(SOURCE_IMAGE_PATH) - >>> model = torch.hub.load('ultralytics/yolov5', 'yolov5s') - >>> result = model(image) - >>> detections = sv.Detections.from_yolov5(result) + image = cv2.imread(SOURCE_IMAGE_PATH) + model = torch.hub.load('ultralytics/yolov5', 'yolov5s') + result = model(image) + detections = sv.Detections.from_yolov5(result) ``` """ yolov5_detections_predictions = yolov5_results.pred[0].cpu().cpu().numpy() @@ -159,14 +159,14 @@ class Detections: Example: ```python - >>> import cv2 - >>> import supervision as sv - >>> from ultralytics import YOLO + import cv2 + import supervision as sv + from ultralytics import YOLO - >>> image = cv2.imread(...) - >>> model = YOLO('yolov8s.pt') - >>> result = model(image)[0] - >>> detections = sv.Detections.from_ultralytics(result) + image = cv2.imread() + model = YOLO('yolov8s.pt') + result = model(image)[0] + detections = sv.Detections.from_ultralytics(result) ``` """ @@ -198,14 +198,14 @@ class Detections: Example: ```python - >>> import cv2 - >>> from super_gradients.training import models - >>> import supervision as sv + import cv2 + from super_gradients.training import models + import supervision as sv - >>> image = cv2.imread(SOURCE_IMAGE_PATH) - >>> model = models.get('yolo_nas_l', pretrained_weights="coco") - >>> result = list(model.predict(image, conf=0.35))[0] - >>> detections = sv.Detections.from_yolo_nas(result) + image = cv2.imread(SOURCE_IMAGE_PATH) + model = models.get('yolo_nas_l', pretrained_weights="coco") + result = list(model.predict(image, conf=0.35))[0] + detections = sv.Detections.from_yolo_nas(result) ``` """ if np.asarray(yolo_nas_results.prediction.bboxes_xyxy).shape[0] == 0: @@ -235,20 +235,16 @@ class Detections: Example: ```python - >>> import tensorflow as tf - >>> import tensorflow_hub as hub - >>> import numpy as np - >>> import cv2 + import tensorflow as tf + import tensorflow_hub as hub + import numpy as np + import cv2 - >>> module_handle = "https://tfhub.dev/tensorflow/centernet/hourglass_512x512_kpts/1" - - >>> model = hub.load(module_handle) - - >>> img = np.array(cv2.imread(SOURCE_IMAGE_PATH)) - - >>> result = model(img) - - >>> detections = sv.Detections.from_tensorflow(result) + module_handle = "https://tfhub.dev/tensorflow/centernet/hourglass_512x512_kpts/1" + model = hub.load(module_handle) + img = np.array(cv2.imread(SOURCE_IMAGE_PATH)) + result = model(img) + detections = sv.Detections.from_tensorflow(result) ``` """ # noqa: E501 // docs @@ -278,15 +274,15 @@ class Detections: Example: ```python - >>> import supervision as sv - >>> from deepsparse import Pipeline + import supervision as sv + from deepsparse import Pipeline - >>> yolo_pipeline = Pipeline.create( - ... task="yolo", - ... model_path = "zoo:cv/detection/yolov5-l/pytorch/ultralytics/coco/pruned80_quant-none" - ... ) - >>> result = yolo_pipeline() - >>> detections = sv.Detections.from_deepsparse(result) + yolo_pipeline = Pipeline.create( + task="yolo", + model_path = "zoo:cv/detection/yolov5-l/pytorch/ultralytics/coco/pruned80_quant-none" + ) + result = yolo_pipeline() + detections = sv.Detections.from_deepsparse(result) ``` """ # noqa: E501 // docs @@ -315,14 +311,14 @@ class Detections: Example: ```python - >>> import cv2 - >>> import supervision as sv - >>> from mmdet.apis import DetInferencer + import cv2 + import supervision as sv + from mmdet.apis import DetInferencer - >>> inferencer = DetInferencer(model_name, checkpoint, device) - >>> mmdet_result = inferencer(SOURCE_IMAGE_PATH, out_dir='./output', - ... return_datasamples=True)["predictions"][0] - >>> detections = sv.Detections.from_mmdetection(mmdet_result) + inferencer = DetInferencer(model_name, checkpoint, device) + mmdet_result = inferencer(SOURCE_IMAGE_PATH, out_dir='./output', + return_datasamples=True)["predictions"][0] + detections = sv.Detections.from_mmdetection(mmdet_result) ``` """ @@ -364,18 +360,18 @@ class Detections: Example: ```python - >>> import cv2 - >>> from detectron2.engine import DefaultPredictor - >>> from detectron2.config import get_cfg - >>> import supervision as sv + import cv2 + from detectron2.engine import DefaultPredictor + from detectron2.config import get_cfg + import supervision as sv - >>> image = cv2.imread(SOURCE_IMAGE_PATH) - >>> cfg = get_cfg() - >>> cfg.merge_from_file("path/to/config.yaml") - >>> cfg.MODEL.WEIGHTS = "path/to/model_weights.pth" - >>> predictor = DefaultPredictor(cfg) - >>> result = predictor(image) - >>> detections = sv.Detections.from_detectron2(result) + image = cv2.imread(SOURCE_IMAGE_PATH) + cfg = get_cfg() + cfg.merge_from_file("path/to/config.yaml") + cfg.MODEL.WEIGHTS = "path/to/model_weights.pth" + predictor = DefaultPredictor(cfg) + result = predictor(image) + detections = sv.Detections.from_detectron2(result) ``` """ @@ -412,14 +408,14 @@ class Detections: Example: ```python - >>> import cv2 - >>> import supervision as sv - >>> from inference.models.utils import get_roboflow_model + import cv2 + import supervision as sv + from inference.models.utils import get_roboflow_model - >>> image = cv2.imread(...) - >>> model = get_roboflow_model(model_id="yolov8s-640") - >>> result = model.infer(image)[0] - >>> detections = sv.Detections.from_inference(result) + image = cv2.imread() + model = get_roboflow_model(model_id="yolov8s-640") + result = model.infer(image)[0] + detections = sv.Detections.from_inference(result) ``` """ with suppress(AttributeError): @@ -461,14 +457,14 @@ class Detections: Example: ```python - >>> import cv2 - >>> import supervision as sv - >>> from inference.models.utils import get_roboflow_model + import cv2 + import supervision as sv + from inference.models.utils import get_roboflow_model - >>> image = cv2.imread(...) - >>> model = get_roboflow_model(model_id="yolov8s-640") - >>> result = model.infer(image)[0] - >>> detections = sv.Detections.from_roboflow(result) + image = cv2.imread() + model = get_roboflow_model(model_id="yolov8s-640") + result = model.infer(image)[0] + detections = sv.Detections.from_roboflow(result) ``` """ return cls.from_inference(roboflow_result) @@ -488,17 +484,17 @@ class Detections: Example: ```python - >>> import supervision as sv - >>> from segment_anything import ( - ... sam_model_registry, - ... SamAutomaticMaskGenerator - ... ) + import supervision as sv + from segment_anything import ( + sam_model_registry, + SamAutomaticMaskGenerator + ) - >>> sam_model_reg = sam_model_registry[MODEL_TYPE] - >>> sam = sam_model_reg(checkpoint=CHECKPOINT_PATH).to(device=DEVICE) - >>> mask_generator = SamAutomaticMaskGenerator(sam) - >>> sam_result = mask_generator.generate(IMAGE) - >>> detections = sv.Detections.from_sam(sam_result=sam_result) + sam_model_reg = sam_model_registry[MODEL_TYPE] + sam = sam_model_reg(checkpoint=CHECKPOINT_PATH).to(device=DEVICE) + mask_generator = SamAutomaticMaskGenerator(sam) + sam_result = mask_generator.generate(IMAGE) + detections = sv.Detections.from_sam(sam_result=sam_result) ``` """ @@ -535,25 +531,25 @@ class Detections: Example: ```python - >>> import requests - >>> import supervision as sv + import requests + import supervision as sv - >>> image = open(input, "rb").read() + image = open(input, "rb").read() - >>> endpoint = "https://.cognitiveservices.azure.com/" - >>> subscription_key = "..." + endpoint = "https://.cognitiveservices.azure.com/" + subscription_key = "" - >>> headers = { - ... "Content-Type": "application/octet-stream", - ... "Ocp-Apim-Subscription-Key": subscription_key - ... } + headers = { + "Content-Type": "application/octet-stream", + "Ocp-Apim-Subscription-Key": subscription_key + } - >>> response = requests.post(endpoint, - ... headers=self.headers, - ... data=image - ... ).json() + response = requests.post(endpoint, + headers=self.headers, + data=image + ).json() - >>> detections = sv.Detections.from_azure_analyze_image(response) + detections = sv.Detections.from_azure_analyze_image(response) ``` """ if "error" in azure_result: @@ -617,21 +613,21 @@ class Detections: Example: ```python - >>> import supervision as sv - >>> import paddle - >>> from ppdet.engine import Trainer - >>> from ppdet.core.workspace import load_config + import supervision as sv + import paddle + from ppdet.engine import Trainer + from ppdet.core.workspace import load_config - >>> weights = (...) - >>> config = (...) + weights = () + config = () - >>> cfg = load_config(config) - >>> trainer = Trainer(cfg, mode='test') - >>> trainer.load_weights(weights) + cfg = load_config(config) + trainer = Trainer(cfg, mode='test') + trainer.load_weights(weights) - >>> paddledet_result = trainer.predict([images])[0] + paddledet_result = trainer.predict([images])[0] - >>> detections = sv.Detections.from_paddledet(paddledet_result) + detections = sv.Detections.from_paddledet(paddledet_result) ``` """ @@ -655,9 +651,9 @@ class Detections: Example: ```python - >>> from supervision import Detections + from supervision import Detections - >>> empty_detections = Detections.empty() + empty_detections = Detections.empty() ``` """ return cls( @@ -689,29 +685,29 @@ class Detections: import numpy as np import supervision as sv - >>> detections_1 = sv.Detections( - ... xyxy=np.array([[15, 15, 100, 100], [200, 200, 300, 300]]), - ... class_id=np.array([1, 2]), - ... data={'feature_vector': np.array([0.1, 0.2)])} - ... ) + detections_1 = sv.Detections( + xyxy=np.array([[15, 15, 100, 100], [200, 200, 300, 300]]), + class_id=np.array([1, 2]), + data={'feature_vector': np.array([0.1, 0.2)])} + ) - >>> detections_2 = sv.Detections( - ... xyxy=np.array([[30, 30, 120, 120]]), - ... class_id=np.array([1]), - ... data={'feature_vector': [np.array([0.3])]} - ... ) + detections_2 = sv.Detections( + xyxy=np.array([[30, 30, 120, 120]]), + class_id=np.array([1]), + data={'feature_vector': [np.array([0.3])]} + ) - >>> merged_detections = Detections.merge([detections_1, detections_2]) + merged_detections = Detections.merge([detections_1, detections_2]) - >>> merged_detections.xyxy + merged_detections.xyxy array([[ 15, 15, 100, 100], [200, 200, 300, 300], [ 30, 30, 120, 120]]) - >>> merged_detections.class_id + merged_detections.class_id array([1, 2, 1]) - >>> merged_detections.data['feature_vector'] + merged_detections.data['feature_vector'] array([0.1, 0.2, 0.3]) ``` """ @@ -844,17 +840,17 @@ class Detections: Example: ```python - >>> import supervision as sv + import supervision as sv - >>> detections = sv.Detections(...) + detections = sv.Detections() - >>> first_detection = detections[0] - >>> first_10_detections = detections[0:10] - >>> some_detections = detections[[0, 2, 4]] - >>> class_0_detections = detections[detections.class_id == 0] - >>> high_confidence_detections = detections[detections.confidence > 0.5] + first_detection = detections[0] + first_10_detections = detections[0:10] + some_detections = detections[[0, 2, 4]] + class_0_detections = detections[detections.class_id == 0] + high_confidence_detections = detections[detections.confidence > 0.5] - >>> feature_vector = detections['feature_vector'] + feature_vector = detections['feature_vector'] ``` """ if isinstance(index, str): @@ -880,22 +876,22 @@ class Detections: Example: ```python - >>> import cv2 - >>> from ultralytics import YOLO - >>> import supervision as sv + import cv2 + from ultralytics import YOLO + import supervision as sv - >>> model = YOLO('yolov8s.pt') + model = YOLO('yolov8s.pt') - >>> image = cv2.imread(SOURCE_IMAGE_PATH) + image = cv2.imread(SOURCE_IMAGE_PATH) - >>> result = model(image)[0] - >>> detections = sv.Detections.from_ultralytics(result) + result = model(image)[0] + detections = sv.Detections.from_ultralytics(result) - >>> detections['names'] = [ - ... model.model.names[class_id] - ... for class_id - ... in detections.class_id - ... ] + detections['names'] = [ + model.model.names[class_id] + for class_id + in detections.class_id + ] ``` """ if not isinstance(value, (np.ndarray, list)): @@ -915,7 +911,7 @@ class Detections: Returns: np.ndarray: An array of floats containing the area of each detection - in the format of `(area_1, area_2, ..., area_n)`, + in the format of `(area_1, area_2, , area_n)`, where n is the number of detections. """ if self.mask is not None: @@ -930,7 +926,7 @@ class Detections: Returns: np.ndarray: An array of floats containing the area of each bounding - box in the format of `(area_1, area_2, ..., area_n)`, + box in the format of `(area_1, area_2, , area_n)`, where n is the number of detections. """ return (self.xyxy[:, 3] - self.xyxy[:, 1]) * (self.xyxy[:, 2] - self.xyxy[:, 0])