diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 4c89f60f..cc534e66 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -49,8 +49,9 @@ repos: - id: mdformat additional_dependencies: - "mdformat-mkdocs[recommended]>=2.1.0" + - "mdformat-ruff" args: ["--number"] - exclude: ^docs/ + exclude: ^(docs/changelog\.md|docs/deprecated\.md)$ - repo: https://github.com/pre-commit/mirrors-mypy rev: v1.19.1 diff --git a/docs/datasets/core.md b/docs/datasets/core.md index 4e04fada..73931515 100644 --- a/docs/datasets/core.md +++ b/docs/datasets/core.md @@ -5,6 +5,7 @@ comments: true # Datasets !!! warning + Dataset API is still fluid and may change. If you use Dataset API in your project until further notice, freeze the `supervision` version in your `requirements.txt` or `setup.py`. diff --git a/docs/detection/annotators.md b/docs/detection/annotators.md index 2c0afa1d..e6dd21bf 100644 --- a/docs/detection/annotators.md +++ b/docs/detection/annotators.md @@ -17,7 +17,7 @@ Annotators accept detections and apply box or mask visualizations to the detecti box_annotator = sv.BoxAnnotator() annotated_frame = box_annotator.annotate( scene=image.copy(), - detections=detections + detections=detections, ) ``` @@ -38,7 +38,7 @@ Annotators accept detections and apply box or mask visualizations to the detecti round_box_annotator = sv.RoundBoxAnnotator() annotated_frame = round_box_annotator.annotate( scene=image.copy(), - detections=detections + detections=detections, ) ``` @@ -59,7 +59,7 @@ Annotators accept detections and apply box or mask visualizations to the detecti corner_annotator = sv.BoxCornerAnnotator() annotated_frame = corner_annotator.annotate( scene=image.copy(), - detections=detections + detections=detections, ) ``` @@ -80,7 +80,7 @@ Annotators accept detections and apply box or mask visualizations to the detecti color_annotator = sv.ColorAnnotator() annotated_frame = color_annotator.annotate( scene=image.copy(), - detections=detections + detections=detections, ) ``` @@ -101,7 +101,7 @@ Annotators accept detections and apply box or mask visualizations to the detecti circle_annotator = sv.CircleAnnotator() annotated_frame = circle_annotator.annotate( scene=image.copy(), - detections=detections + detections=detections, ) ``` @@ -122,7 +122,7 @@ Annotators accept detections and apply box or mask visualizations to the detecti dot_annotator = sv.DotAnnotator() annotated_frame = dot_annotator.annotate( scene=image.copy(), - detections=detections + detections=detections, ) ``` @@ -143,7 +143,7 @@ Annotators accept detections and apply box or mask visualizations to the detecti triangle_annotator = sv.TriangleAnnotator() annotated_frame = triangle_annotator.annotate( scene=image.copy(), - detections=detections + detections=detections, ) ``` @@ -164,7 +164,7 @@ Annotators accept detections and apply box or mask visualizations to the detecti ellipse_annotator = sv.EllipseAnnotator() annotated_frame = ellipse_annotator.annotate( scene=image.copy(), - detections=detections + detections=detections, ) ``` @@ -185,7 +185,7 @@ Annotators accept detections and apply box or mask visualizations to the detecti halo_annotator = sv.HaloAnnotator() annotated_frame = halo_annotator.annotate( scene=image.copy(), - detections=detections + detections=detections, ) ``` @@ -206,7 +206,7 @@ Annotators accept detections and apply box or mask visualizations to the detecti percentage_bar_annotator = sv.PercentageBarAnnotator() annotated_frame = percentage_bar_annotator.annotate( scene=image.copy(), - detections=detections + detections=detections, ) ``` @@ -227,7 +227,7 @@ Annotators accept detections and apply box or mask visualizations to the detecti mask_annotator = sv.MaskAnnotator() annotated_frame = mask_annotator.annotate( scene=image.copy(), - detections=detections + detections=detections, ) ``` @@ -248,7 +248,7 @@ Annotators accept detections and apply box or mask visualizations to the detecti polygon_annotator = sv.PolygonAnnotator() annotated_frame = polygon_annotator.annotate( scene=image.copy(), - detections=detections + detections=detections, ) ``` @@ -268,15 +268,15 @@ Annotators accept detections and apply box or mask visualizations to the detecti labels = [ f"{class_name} {confidence:.2f}" - for class_name, confidence - in zip(detections['class_name'], detections.confidence) + for class_name, confidence in zip( + detections["class_name"], + detections.confidence, + ) ] label_annotator = sv.LabelAnnotator(text_position=sv.Position.CENTER) annotated_frame = label_annotator.annotate( - scene=image.copy(), - detections=detections, - labels=labels + scene=image.copy(), detections=detections, labels=labels ) ``` @@ -296,18 +296,20 @@ Annotators accept detections and apply box or mask visualizations to the detecti labels = [ f"{class_name} {confidence:.2f}" - for class_name, confidence - in zip(detections['class_name'], detections.confidence) + for class_name, confidence in zip( + detections["class_name"], + detections.confidence, + ) ] rich_label_annotator = sv.RichLabelAnnotator( - font_path="", - text_position=sv.Position.CENTER + font_path="TTF_FONT_PATH", + text_position=sv.Position.CENTER, ) annotated_frame = rich_label_annotator.annotate( scene=image.copy(), detections=detections, - labels=labels + labels=labels, ) ``` @@ -325,16 +327,13 @@ Annotators accept detections and apply box or mask visualizations to the detecti image = ... detections = sv.Detections(...) - icon_paths = [ - "" - for _ in detections - ] + icon_paths = ["" for _ in detections] icon_annotator = sv.IconAnnotator() annotated_frame = icon_annotator.annotate( scene=image.copy(), detections=detections, - icon_path=icon_paths + icon_path=icon_paths, ) ``` @@ -346,23 +345,25 @@ Annotators accept detections and apply box or mask visualizations to the detecti === "Blur" @@ -374,10 +375,7 @@ Annotators accept detections and apply box or mask visualizations to the detecti detections = sv.Detections(...) blur_annotator = sv.BlurAnnotator() - annotated_frame = blur_annotator.annotate( - scene=image.copy(), - detections=detections - ) + annotated_frame = (blur_annotator.annotate(scene=image.copy(), detections=detections),) ```
@@ -397,7 +395,7 @@ Annotators accept detections and apply box or mask visualizations to the detecti pixelate_annotator = sv.PixelateAnnotator() annotated_frame = pixelate_annotator.annotate( scene=image.copy(), - detections=detections + detections=detections, ) ``` @@ -413,22 +411,23 @@ Annotators accept detections and apply box or mask visualizations to the detecti import supervision as sv from ultralytics import YOLO - model = YOLO('yolov8x.pt') + model = YOLO("yolov8x.pt") trace_annotator = sv.TraceAnnotator() - video_info = sv.VideoInfo.from_video_path(video_path='...') - frames_generator = sv.get_video_frames_generator(source_path='...') + video_info = sv.VideoInfo.from_video_path(video_path="...") + frames_generator = sv.get_video_frames_generator(source_path="...") tracker = sv.ByteTrack() - with sv.VideoSink(target_path='...', video_info=video_info) as sink: + with sv.VideoSink(target_path="...", video_info=video_info) as sink: for frame in frames_generator: result = model(frame)[0] detections = sv.Detections.from_ultralytics(result) detections = tracker.update_with_detections(detections) annotated_frame = trace_annotator.annotate( scene=frame.copy(), - detections=detections) + detections=detections, + ) sink.write_frame(frame=annotated_frame) ``` @@ -444,20 +443,21 @@ Annotators accept detections and apply box or mask visualizations to the detecti import supervision as sv from ultralytics import YOLO - model = YOLO('yolov8x.pt') + model = YOLO("yolov8x.pt") heat_map_annotator = sv.HeatMapAnnotator() - video_info = sv.VideoInfo.from_video_path(video_path='...') - frames_generator = sv.get_video_frames_generator(source_path='...') + video_info = sv.VideoInfo.from_video_path(video_path="...") + frames_generator = sv.get_video_frames_generator(source_path="...") - with sv.VideoSink(target_path='...', video_info=video_info) as sink: + with sv.VideoSink(target_path="...", video_info=video_info) as sink: for frame in frames_generator: result = model(frame)[0] detections = sv.Detections.from_ultralytics(result) annotated_frame = heat_map_annotator.annotate( scene=frame.copy(), - detections=detections) + detections=detections, + ) sink.write_frame(frame=annotated_frame) ``` @@ -478,7 +478,7 @@ Annotators accept detections and apply box or mask visualizations to the detecti background_overlay_annotator = sv.BackgroundOverlayAnnotator() annotated_frame = background_overlay_annotator.annotate( scene=image.copy(), - detections=detections + detections=detections, ) ``` @@ -501,7 +501,7 @@ Annotators accept detections and apply box or mask visualizations to the detecti annotated_frame = comparison_annotator.annotate( scene=image.copy(), detections_1=detections_1, - detections_2=detections_2 + detections_2=detections_2, ) ``` diff --git a/docs/how_to/benchmark_a_model.md b/docs/how_to/benchmark_a_model.md index aa707fa7..04b4f8a2 100644 --- a/docs/how_to/benchmark_a_model.md +++ b/docs/how_to/benchmark_a_model.md @@ -51,7 +51,7 @@ from roboflow import Roboflow rf = Roboflow(api_key="") project = rf.workspace("").project("") -dataset = project.version().download("") +dataset = project.version("").download("") ``` If your dataset is from Universe, go to `Dataset` > `Download Dataset` > select the format (e.g. `YOLOv11`) > `Show download code`. @@ -148,7 +148,7 @@ We'll use `supervision` to create a dataset iterator, and then run the model on test_set = sv.DetectionDataset.from_yolo( images_directory_path=f"{dataset.location}/test/images", annotations_directory_path=f"{dataset.location}/test/labels", - data_yaml_path=f"{dataset.location}/data.yaml" + data_yaml_path=f"{dataset.location}/data.yaml", ) image_paths = [] @@ -172,7 +172,7 @@ We'll use `supervision` to create a dataset iterator, and then run the model on test_set = sv.DetectionDataset.from_yolo( images_directory_path=f"{dataset.location}/test/images", annotations_directory_path=f"{dataset.location}/test/labels", - data_yaml_path=f"{dataset.location}/data.yaml" + data_yaml_path=f"{dataset.location}/data.yaml", ) image_paths = [] @@ -199,14 +199,16 @@ We need to remap them to match the dataset classes. Here's how to do it: def remap_classes( detections: sv.Detections, class_ids_from_to: dict[int, int], - class_names_from_to: dict[str, str] + class_names_from_to: dict[str, str], ) -> None: new_class_ids = [ - class_ids_from_to.get(class_id, class_id) for class_id in detections.class_id] + class_ids_from_to.get(class_id, class_id) for class_id in detections.class_id + ] detections.class_id = np.array(new_class_ids) new_class_names = [ - class_names_from_to.get(name, name) for name in detections["class_name"]] + class_names_from_to.get(name, name) for name in detections["class_name"] + ] predictions["class_name"] = np.array(new_class_names) ``` @@ -222,7 +224,7 @@ Let's also remove the predictions that are not in the dataset classes. test_set = sv.DetectionDataset.from_yolo( images_directory_path=f"{dataset.location}/test/images", annotations_directory_path=f"{dataset.location}/test/labels", - data_yaml_path=f"{dataset.location}/data.yaml" + data_yaml_path=f"{dataset.location}/data.yaml", ) image_paths = [] @@ -236,11 +238,9 @@ Let's also remove the predictions that are not in the dataset classes. remap_classes( detections=predictions, class_ids_from_to={16: 0}, - class_names_from_to={"dog": "Corgi"} + class_names_from_to={"dog": "Corgi"}, ) - predictions = predictions[ - np.isin(predictions["class_name"], test_set.classes) - ] + predictions = predictions[np.isin(predictions["class_name"], test_set.classes),] image_paths.append(image_path) predictions_list.append(predictions) @@ -260,7 +260,7 @@ Let's also remove the predictions that are not in the dataset classes. test_set = sv.DetectionDataset.from_yolo( images_directory_path=f"{dataset.location}/test/images", annotations_directory_path=f"{dataset.location}/test/labels", - data_yaml_path=f"{dataset.location}/data.yaml" + data_yaml_path=f"{dataset.location}/data.yaml", ) image_paths = [] @@ -274,11 +274,9 @@ Let's also remove the predictions that are not in the dataset classes. remap_classes( detections=predictions, class_ids_from_to={16: 0}, - class_names_from_to={"dog": "Corgi"} + class_names_from_to={"dog": "Corgi"}, ) - predictions = predictions[ - np.isin(predictions["class_name"], test_set.classes) - ] + predictions = predictions[np.isin(predictions["class_name"], test_set.classes),] image_paths.append(image_path) predictions_list.append(predictions) @@ -297,16 +295,22 @@ N = 9 GRID_SIZE = (3, 3) target_annotator = sv.PolygonAnnotator(color=sv.Color.from_hex("#8315f9"), thickness=8) -prediction_annotator = sv.PolygonAnnotator(color=sv.Color.from_hex("#00cfc6"), thickness=6) +prediction_annotator = sv.PolygonAnnotator( + color=sv.Color.from_hex("#00cfc6"), thickness=6 +) annotated_images = [] for image_path, predictions, targets in zip( - image_paths[:N], predictions_list[:N], targets_list[:N] + image_paths[:N], predictions_list[:N], targets_list[:N] ): annotated_image = cv2.imread(image_path) - annotated_image = target_annotator.annotate(scene=annotated_image, detections=targets) - annotated_image = prediction_annotator.annotate(scene=annotated_image, detections=prediction) + annotated_image = target_annotator.annotate( + scene=annotated_image, detections=targets + ) + annotated_image = prediction_annotator.annotate( + scene=annotated_image, detections=prediction + ) annotated_images.append(annotated_image) sv.plot_images_grid(images=annotated_images, grid_size=GRID_SIZE) diff --git a/docs/how_to/detect_and_annotate.md b/docs/how_to/detect_and_annotate.md index 235c5e38..c1c48f3d 100644 --- a/docs/how_to/detect_and_annotate.md +++ b/docs/how_to/detect_and_annotate.md @@ -20,26 +20,29 @@ First, you'll need to obtain predictions from your object detection or segmentat model. === "Inference" + ```python import cv2 from inference import get_model model = get_model(model_id="yolov8n-640") - image = cv2.imread() + image = cv2.imread("") results = model.infer(image)[0] ``` === "Ultralytics" + ```python import cv2 from ultralytics import YOLO model = YOLO("yolov8n.pt") - image = cv2.imread() + image = cv2.imread("") results = model(image)[0] ``` === "Transformers" + ```python import torch from PIL import Image @@ -48,7 +51,7 @@ model. processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50") model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50") - image = Image.open() + image = Image.open("") inputs = processor(images=image, return_tensors="pt") with torch.no_grad(): @@ -57,7 +60,8 @@ model. width, height = image.size target_size = torch.tensor([[height, width]]) results = processor.post_process_object_detection( - outputs=outputs, target_sizes=target_size)[0] + outputs=outputs, target_sizes=target_size + )[0] ``` ## Load Predictions into Supervision @@ -65,6 +69,7 @@ model. Now that we have predictions from a model, we can load them into Supervision. === "Inference" + We can do so using the [`sv.Detections.from_inference`](https://supervision.roboflow.com/latest/detection/core/#supervision.detection.core.Detections.from_inference) method, which accepts model results from both detection and segmentation models. ```{ .py hl_lines="2 8" } @@ -73,12 +78,13 @@ Now that we have predictions from a model, we can load them into Supervision. from inference import get_model model = get_model(model_id="yolov8n-640") - image = cv2.imread() + image = cv2.imread("") results = model.infer(image)[0] detections = sv.Detections.from_inference(results) ``` === "Ultralytics" + We can do so using the [`sv.Detections.from_ultralytics`](https://supervision.roboflow.com/latest/detection/core/#supervision.detection.core.Detections.from_ultralytics) method, which accepts model results from both detection and segmentation models. ```{ .py hl_lines="2 8" } @@ -87,12 +93,13 @@ Now that we have predictions from a model, we can load them into Supervision. from ultralytics import YOLO model = YOLO("yolov8n.pt") - image = cv2.imread() + image = cv2.imread("") results = model(image)[0] detections = sv.Detections.from_ultralytics(results) ``` === "Transformers" + We can do so using the [`sv.Detections.from_transformers`](https://supervision.roboflow.com/latest/detection/core/#supervision.detection.core.Detections.from_transformers) method, which accepts model results from both detection and segmentation models. ```{ .py hl_lines="2 19-21" } @@ -104,7 +111,7 @@ Now that we have predictions from a model, we can load them into Supervision. processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50") model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50") - image = Image.open() + image = Image.open("") inputs = processor(images=image, return_tensors="pt") with torch.no_grad(): @@ -132,13 +139,14 @@ You can load predictions from other computer vision frameworks and libraries usi Finally, we can annotate the image with the predictions. Since we are working with an object detection model, we will use the [`sv.BoxAnnotator`](https://supervision.roboflow.com/latest/detection/annotators/#supervision.annotators.core.BoxAnnotator) and [`sv.LabelAnnotator`](https://supervision.roboflow.com/latest/detection/annotators/#supervision.annotators.core.LabelAnnotator) classes. === "Inference" + ```{ .py hl_lines="10-16" } import cv2 import supervision as sv from inference import get_model model = get_model(model_id="yolov8n-640") - image = cv2.imread() + image = cv2.imread("") results = model.infer(image)[0] detections = sv.Detections.from_inference(results) @@ -152,13 +160,14 @@ Finally, we can annotate the image with the predictions. Since we are working wi ``` === "Ultralytics" + ```{ .py hl_lines="10-16" } import cv2 import supervision as sv from ultralytics import YOLO model = YOLO("yolov8n.pt") - image = cv2.imread() + image = cv2.imread("") results = model(image)[0] detections = sv.Detections.from_ultralytics(results) @@ -172,6 +181,7 @@ Finally, we can annotate the image with the predictions. Since we are working wi ``` === "Transformers" + ```{ .py hl_lines="23-30" } import torch import supervision as sv @@ -181,7 +191,7 @@ Finally, we can annotate the image with the predictions. Since we are working wi processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50") model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50") - image = Image.open() + image = Image.open("") inputs = processor(images=image, return_tensors="pt") with torch.no_grad(): @@ -213,13 +223,14 @@ will label each detection with its `class_name` (if possible) or `class_id`. You override this behavior by passing a list of custom `labels` to the `annotate` method. === "Inference" + ```{ .py hl_lines="13-17 22" } import cv2 import supervision as sv from inference import get_model model = get_model(model_id="yolov8n-640") - image = cv2.imread() + image = cv2.imread("") results = model.infer(image)[0] detections = sv.Detections.from_inference(results) @@ -239,13 +250,14 @@ override this behavior by passing a list of custom `labels` to the `annotate` me ``` === "Ultralytics" + ```{ .py hl_lines="13-17 22" } import cv2 import supervision as sv from ultralytics import YOLO model = YOLO("yolov8n.pt") - image = cv2.imread() + image = cv2.imread("") results = model(image)[0] detections = sv.Detections.from_ultralytics(results) @@ -265,6 +277,7 @@ override this behavior by passing a list of custom `labels` to the `annotate` me ``` === "Transformers" + ```{ .py hl_lines="26-30 35" } import torch import supervision as sv @@ -274,7 +287,7 @@ override this behavior by passing a list of custom `labels` to the `annotate` me processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50") model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50") - image = Image.open() + image = Image.open("") inputs = processor(images=image, return_tensors="pt") with torch.no_grad(): @@ -314,13 +327,14 @@ is a drop-in replacement for that will allow you to draw masks instead of boxes. === "Inference" + ```python import cv2 import supervision as sv from inference import get_model model = get_model(model_id="yolov8n-seg-640") - image = cv2.imread() + image = cv2.imread("") results = model.infer(image)[0] detections = sv.Detections.from_inference(results) @@ -328,19 +342,24 @@ that will allow you to draw masks instead of boxes. label_annotator = sv.LabelAnnotator(text_position=sv.Position.CENTER_OF_MASS) annotated_image = mask_annotator.annotate( - scene=image, detections=detections) + scene=image, + detections=detections, + ) annotated_image = label_annotator.annotate( - scene=annotated_image, detections=detections) + scene=annotated_image, + detections=detections, + ) ``` === "Ultralytics" + ```python import cv2 import supervision as sv from ultralytics import YOLO model = YOLO("yolov8n-seg.pt") - image = cv2.imread() + image = cv2.imread("") results = model(image)[0] detections = sv.Detections.from_ultralytics(results) @@ -348,12 +367,17 @@ that will allow you to draw masks instead of boxes. label_annotator = sv.LabelAnnotator(text_position=sv.Position.CENTER_OF_MASS) annotated_image = mask_annotator.annotate( - scene=image, detections=detections) + scene=image, + detections=detections, + ) annotated_image = label_annotator.annotate( - scene=annotated_image, detections=detections) + scene=annotated_image, + detections=detections, + ) ``` === "Transformers" + ```python import torch import supervision as sv @@ -363,7 +387,7 @@ that will allow you to draw masks instead of boxes. processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50-panoptic") model = DetrForSegmentation.from_pretrained("facebook/detr-resnet-50-panoptic") - image = Image.open() + image = Image.open("") inputs = processor(images=image, return_tensors="pt") with torch.no_grad(): @@ -372,24 +396,27 @@ that will allow you to draw masks instead of boxes. width, height = image.size target_size = torch.tensor([[height, width]]) results = processor.post_process_segmentation( - outputs=outputs, target_sizes=target_size)[0] + outputs=outputs, target_sizes=target_size + )[0] detections = sv.Detections.from_transformers( - transformers_results=results, - id2label=model.config.id2label) + transformers_results=results, id2label=model.config.id2label + ) mask_annotator = sv.MaskAnnotator() label_annotator = sv.LabelAnnotator(text_position=sv.Position.CENTER_OF_MASS) labels = [ f"{class_name} {confidence:.2f}" - for class_name, confidence - in zip(detections['class_name'], detections.confidence) + for class_name, confidence in zip( + detections["class_name"], + detections.confidence, + ) ] - annotated_image = mask_annotator.annotate( - scene=image, detections=detections) + annotated_image = mask_annotator.annotate(scene=image, detections=detections) annotated_image = label_annotator.annotate( - scene=annotated_image, detections=detections, labels=labels) + scene=annotated_image, detections=detections, labels=labels + ) ``` ![segmentation-annotation](https://media.roboflow.com/supervision_detect_and_annotate_example_3.png) diff --git a/docs/how_to/detect_small_objects.md b/docs/how_to/detect_small_objects.md index 10ef95bf..e4cb7a32 100644 --- a/docs/how_to/detect_small_objects.md +++ b/docs/how_to/detect_small_objects.md @@ -20,13 +20,14 @@ Small object detection in high-resolution images presents challenges due to the size relative to the image resolution. === "Inference" + ```python import cv2 import supervision as sv from inference import get_model model = get_model(model_id="yolov8x-640") - image = cv2.imread() + image = cv2.imread("") results = model.infer(image)[0] detections = sv.Detections.from_inference(results) @@ -34,19 +35,24 @@ size relative to the image resolution. label_annotator = sv.LabelAnnotator() annotated_image = box_annotator.annotate( - scene=image, detections=detections) + scene=image, + detections=detections, + ) annotated_image = label_annotator.annotate( - scene=annotated_image, detections=detections) + scene=annotated_image, + detections=detections, + ) ``` === "Ultralytics" + ```python import cv2 import supervision as sv from ultralytics import YOLO model = YOLO("yolov8x.pt") - image = cv2.imread() + image = cv2.imread("") results = model(image)[0] detections = sv.Detections.from_ultralytics(results) @@ -54,12 +60,17 @@ size relative to the image resolution. label_annotator = sv.LabelAnnotator() annotated_image = box_annotator.annotate( - scene=image, detections=detections) + scene=image, + detections=detections, + ) annotated_image = label_annotator.annotate( - scene=annotated_image, detections=detections) + scene=annotated_image, + detections=detections, + ) ``` === "Transformers" + ```python import torch import supervision as sv @@ -69,7 +80,7 @@ size relative to the image resolution. processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50") model = DetrForSegmentation.from_pretrained("facebook/detr-resnet-50") - image = Image.open() + image = Image.open("") inputs = processor(images=image, return_tensors="pt") with torch.no_grad(): @@ -78,22 +89,19 @@ size relative to the image resolution. width, height = image_slice.size target_size = torch.tensor([[width, height]]) results = processor.post_process_object_detection( - outputs=outputs, target_sizes=target_size)[0] + outputs=outputs, target_sizes=target_size + )[0] detections = sv.Detections.from_transformers(results) box_annotator = sv.BoxAnnotator() label_annotator = sv.LabelAnnotator() - labels = [ - model.config.id2label[class_id] - for class_id - in detections.class_id - ] + labels = [model.config.id2label[class_id] for class_id in detections.class_id] - annotated_image = box_annotator.annotate( - scene=image, detections=detections) + annotated_image = box_annotator.annotate(scene=image, detections=detections) annotated_image = label_annotator.annotate( - scene=annotated_image, detections=detections, labels=labels) + scene=annotated_image, detections=detections, labels=labels + ) ``` ![basic-detection](https://media.roboflow.com/supervision_detect_small_objects_example_1.png) @@ -105,13 +113,14 @@ identification at the cost of processing speed and increased memory usage. This is less effective for ultra-high-resolution images (4K and above). === "Inference" + ```{ .py hl_lines="5" } import cv2 import supervision as sv from inference import get_model model = get_model(model_id="yolov8x-1280") - image = cv2.imread() + image = cv2.imread("") results = model.infer(image)[0] detections = sv.Detections.from_inference(results) @@ -125,13 +134,14 @@ is less effective for ultra-high-resolution images (4K and above). ``` === "Ultralytics" + ```{ .py hl_lines="7" } import cv2 import supervision as sv from ultralytics import YOLO model = YOLO("yolov8x.pt") - image = cv2.imread() + image = cv2.imread("") results = model(image, imgsz=1280)[0] detections = sv.Detections.from_ultralytics(results) @@ -157,6 +167,7 @@ objects within each, and aggregating the results. === "Inference" + ```{ .py hl_lines="9-14" } import cv2 import numpy as np @@ -164,7 +175,7 @@ objects within each, and aggregating the results. from inference import get_model model = get_model(model_id="yolov8x-640") - image = cv2.imread() + image = cv2.imread("") def callback(image_slice: np.ndarray) -> sv.Detections: results = model.infer(image_slice)[0] @@ -183,6 +194,7 @@ objects within each, and aggregating the results. ``` === "Ultralytics" + ```{ .py hl_lines="9-14" } import cv2 import numpy as np @@ -190,7 +202,7 @@ objects within each, and aggregating the results. from ultralytics import YOLO model = YOLO("yolov8x.pt") - image = cv2.imread() + image = cv2.imread("") def callback(image_slice: np.ndarray) -> sv.Detections: result = model(image_slice)[0] @@ -209,6 +221,7 @@ objects within each, and aggregating the results. ``` === "Transformers" + ```{ .py hl_lines="13-28" } import cv2 import torch @@ -220,7 +233,7 @@ objects within each, and aggregating the results. processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50") model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50") - image = cv2.imread() + image = cv2.imread("") def callback(image_slice: np.ndarray) -> sv.Detections: image_slice = cv2.cvtColor(image_slice, cv2.COLOR_BGR2RGB) @@ -261,6 +274,7 @@ objects within each, and aggregating the results. [`InferenceSlicer`](https://supervision.roboflow.com/latest/detection/tools/inference_slicer/#supervision.detection.tools.inference_slicer.InferenceSlicer) can perform segmentation tasks too. === "Inference" + ```{ .py hl_lines="6 16 19-20" } import cv2 import numpy as np @@ -268,7 +282,7 @@ objects within each, and aggregating the results. from inference import get_model model = get_model(model_id="yolov8x-seg-640") - image = cv2.imread() + image = cv2.imread("") def callback(image_slice: np.ndarray) -> sv.Detections: results = model.infer(image_slice)[0] @@ -287,6 +301,7 @@ objects within each, and aggregating the results. ``` === "Ultralytics" + ```{ .py hl_lines="6 16 19-20" } import cv2 import numpy as np @@ -294,7 +309,7 @@ objects within each, and aggregating the results. from ultralytics import YOLO model = YOLO("yolov8x-seg.pt") - image = cv2.imread() + image = cv2.imread("") def callback(image_slice: np.ndarray) -> sv.Detections: result = model(image_slice)[0] diff --git a/docs/how_to/filter_detections.md b/docs/how_to/filter_detections.md index 1624d815..64e26618 100644 --- a/docs/how_to/filter_detections.md +++ b/docs/how_to/filter_detections.md @@ -15,6 +15,7 @@ the filters in their applications. Allows you to select detections that belong only to one selected class. === "After" + ```python import supervision as sv @@ -29,6 +30,7 @@ Allows you to select detections that belong only to one selected class.
=== "Before" + ```python import supervision as sv @@ -47,6 +49,7 @@ Allows you to select detections that belong only to one selected class. Allows you to select detections that belong only to selected set of classes. === "After" + ```python import numpy as np import supervision as sv @@ -63,6 +66,7 @@ Allows you to select detections that belong only to selected set of classes. === "Before" + ```python import numpy as np import supervision as sv @@ -83,6 +87,7 @@ Allows you to select detections that belong only to selected set of classes. Allows you to select detections with specific confidence value, for example higher than selected threshold. === "After" + ```python import supervision as sv @@ -97,6 +102,7 @@ Allows you to select detections with specific confidence value, for example high === "Before" + ```python import supervision as sv @@ -116,6 +122,7 @@ Allows you to select detections based on their size. We define the area as the n detection in the image. In the example below, we have sifted out the detections that are too small. === "After" + ```python import supervision as sv @@ -130,6 +137,7 @@ detection in the image. In the example below, we have sifted out the detections === "Before" + ```python import supervision as sv @@ -151,6 +159,7 @@ but small on a 3840x2160 image. In such cases, we can filter out detections base occupied by them. In the example below, we remove too large detections. === "After" + ```python import supervision as sv @@ -169,6 +178,7 @@ occupied by them. In the example below, we remove too large detections. === "Before" + ```python import supervision as sv @@ -193,6 +203,7 @@ can be criteria for rejecting detection. Implementing such filtering requires a simple and fast. === "After" + ```python import supervision as sv @@ -209,6 +220,7 @@ simple and fast. === "Before" + ```python import supervision as sv @@ -230,6 +242,7 @@ Allows you to use `Detections` in combination with `PolygonZone` to weed out bou zone. In the example below you can see how to filter out all detections located in the lower part of the image. === "After" + ```python import supervision as sv @@ -246,6 +259,7 @@ zone. In the example below you can see how to filter out all detections located === "Before" + ```python import supervision as sv @@ -266,6 +280,7 @@ zone. In the example below you can see how to filter out all detections located `Detections`' greatest strength, however, is that you can build arbitrarily complex logical conditions by simply combining separate conditions using `&` or `|`. === "After" + ```python import supervision as sv @@ -282,6 +297,7 @@ zone. In the example below you can see how to filter out all detections located === "Before" + ```python import supervision as sv diff --git a/docs/how_to/process_datasets.md b/docs/how_to/process_datasets.md index ec5083f6..e00b8053 100644 --- a/docs/how_to/process_datasets.md +++ b/docs/how_to/process_datasets.md @@ -32,8 +32,8 @@ your workspace ID, project ID, and version number. roboflow.login() rf = roboflow.Roboflow() - project = rf.workspace('').project('') - dataset = project.version('').download("coco") + project = rf.workspace("").project("") + dataset = project.version("").download("coco") ``` === "YOLO" @@ -44,8 +44,8 @@ your workspace ID, project ID, and version number. roboflow.login() rf = roboflow.Roboflow() - project = rf.workspace('').project('') - dataset = project.version('').download("yolov8") + project = rf.workspace("").project("") + dataset = project.version("").download("yolov8") ``` === "Pascal VOC" @@ -56,8 +56,8 @@ your workspace ID, project ID, and version number. roboflow.login() rf = roboflow.Roboflow() - project = rf.workspace('').project('') - dataset = project.version('').download("voc") + project = rf.workspace("").project("") + dataset = project.version("").download("voc") ``` ## Load Dataset @@ -75,16 +75,16 @@ instances. import supervision as sv ds_train = sv.DetectionDataset.from_coco( - images_directory_path=f'{dataset.location}/train', - annotations_path=f'{dataset.location}/train/_annotations.coco.json', + images_directory_path=f"{dataset.location}/train", + annotations_path=f"{dataset.location}/train/_annotations.coco.json", ) ds_valid = sv.DetectionDataset.from_coco( - images_directory_path=f'{dataset.location}/valid', - annotations_path=f'{dataset.location}/valid/_annotations.coco.json', + images_directory_path=f"{dataset.location}/valid", + annotations_path=f"{dataset.location}/valid/_annotations.coco.json", ) ds_test = sv.DetectionDataset.from_coco( - images_directory_path=f'{dataset.location}/test', - annotations_path=f'{dataset.location}/test/_annotations.coco.json', + images_directory_path=f"{dataset.location}/test", + annotations_path=f"{dataset.location}/test/_annotations.coco.json", ) ds_train.classes @@ -102,19 +102,19 @@ instances. import supervision as sv ds_train = sv.DetectionDataset.from_yolo( - images_directory_path=f'{dataset.location}/train/images', - annotations_directory_path=f'{dataset.location}/train/labels', - data_yaml_path=f'{dataset.location}/data.yaml' + images_directory_path=f"{dataset.location}/train/images", + annotations_directory_path=f"{dataset.location}/train/labels", + data_yaml_path=f"{dataset.location}/data.yaml", ) ds_valid = sv.DetectionDataset.from_yolo( - images_directory_path=f'{dataset.location}/valid/images', - annotations_directory_path=f'{dataset.location}/valid/labels', - data_yaml_path=f'{dataset.location}/data.yaml' + images_directory_path=f"{dataset.location}/valid/images", + annotations_directory_path=f"{dataset.location}/valid/labels", + data_yaml_path=f"{dataset.location}/data.yaml", ) ds_test = sv.DetectionDataset.from_yolo( - images_directory_path=f'{dataset.location}/test/images', - annotations_directory_path=f'{dataset.location}/test/labels', - data_yaml_path=f'{dataset.location}/data.yaml' + images_directory_path=f"{dataset.location}/test/images", + annotations_directory_path=f"{dataset.location}/test/labels", + data_yaml_path=f"{dataset.location}/data.yaml", ) ds_train.classes @@ -132,16 +132,16 @@ instances. import supervision as sv ds_train = sv.DetectionDataset.from_pascal_voc( - images_directory_path=f'{dataset.location}/train/images', - annotations_directory_path=f'{dataset.location}/train/labels' + images_directory_path=f"{dataset.location}/train/images", + annotations_directory_path=f"{dataset.location}/train/labels", ) ds_valid = sv.DetectionDataset.from_pascal_voc( - images_directory_path=f'{dataset.location}/valid/images', - annotations_directory_path=f'{dataset.location}/valid/labels' + images_directory_path=f"{dataset.location}/valid/images", + annotations_directory_path=f"{dataset.location}/valid/labels", ) ds_test = sv.DetectionDataset.from_pascal_voc( - images_directory_path=f'{dataset.location}/test/images', - annotations_directory_path=f'{dataset.location}/test/labels' + images_directory_path=f"{dataset.location}/test/images", + annotations_directory_path=f"{dataset.location}/test/labels", ) ds_train.classes @@ -294,12 +294,12 @@ ds = sv.DetectionDataset(...) # Option 1 for image_path, image, annotations in ds: - ... # Process each image and its annotations + ... # Process each image and its annotations # Option 2 for idx in range(len(ds)): image_path, image, annotations = ds[idx] - ... # Process the image and annotations at index `idx` + ... # Process the image and annotations at index `idx` ``` ## Visualize Dataset @@ -351,8 +351,8 @@ sv.plot_images_grid( ds = sv.DetectionDataset(...) ds.as_coco( - images_directory_path='', - annotations_path='' + images_directory_path="", + annotations_path="", ) ``` @@ -366,9 +366,9 @@ sv.plot_images_grid( ds = sv.DetectionDataset(...) ds.as_yolo( - images_directory_path='', - annotations_directory_path='', - data_yaml_path='' + images_directory_path="", + annotations_directory_path="", + data_yaml_path="", ) ``` @@ -382,8 +382,8 @@ sv.plot_images_grid( ds = sv.DetectionDataset(...) ds.as_pascal_voc( - images_directory_path='', - annotations_directory_path='' + images_directory_path="", + annotations_directory_path="", ) ``` @@ -413,11 +413,11 @@ augmentation = A.Compose( transforms=[ A.Perspective(p=0.1), A.HorizontalFlip(p=0.5), - A.RandomBrightnessContrast(p=0.5) + A.RandomBrightnessContrast(p=0.5), ], bbox_params=A.BboxParams( - format='pascal_voc', - label_fields=['category'] + format="pascal_voc", + label_fields=["category"], ), ) ``` @@ -437,14 +437,14 @@ _, original_image, original_annotations = ds[0] output = augmentation( image=original_image, bboxes=original_annotations.xyxy, - category=original_annotations.class_id + category=original_annotations.class_id, ) -augmented_image = output['image'] +augmented_image = output["image"] augmented_annotations = replace( original_annotations, - xyxy=np.array(output['bboxes']), - class_id=np.array(output['category']) + xyxy=np.array(output["bboxes"]), + class_id=np.array(output["category"]), ) ``` diff --git a/docs/how_to/save_detections.md b/docs/how_to/save_detections.md index feec7ad8..bda4790f 100644 --- a/docs/how_to/save_detections.md +++ b/docs/how_to/save_detections.md @@ -19,34 +19,35 @@ model. You can learn more on this topic in our [How to Detect and Annotate](https://supervision.roboflow.com/latest/how_to/detect_and_annotate/) guide. === "Inference" + ```python import supervision as sv from inference import get_model model = get_model(model_id="yolov8n-640") - frames_generator = sv.get_video_frames_generator() + frames_generator = sv.get_video_frames_generator("") for frame in frames_generator: - results = model.infer(image)[0] detections = sv.Detections.from_inference(results) ``` === "Ultralytics" + ```python import supervision as sv from ultralytics import YOLO model = YOLO("yolov8n.pt") - frames_generator = sv.get_video_frames_generator() + frames_generator = sv.get_video_frames_generator("") for frame in frames_generator: - results = model(frame)[0] detections = sv.Detections.from_ultralytics(results) ``` === "Transformers" + ```python import torch import supervision as sv @@ -54,10 +55,9 @@ model. You can learn more on this topic in our processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50") model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50") - frames_generator = sv.get_video_frames_generator() + frames_generator = sv.get_video_frames_generator("") for frame in frames_generator: - frame = sv.cv2_to_pillow(frame) inputs = processor(images=frame, return_tensors="pt") @@ -67,7 +67,8 @@ model. You can learn more on this topic in our width, height = frame.size target_size = torch.tensor([[height, width]]) results = processor.post_process_object_detection( - outputs=outputs, target_sizes=target_size)[0] + outputs=outputs, target_sizes=target_size + )[0] detections = sv.Detections.from_transformers(results) ``` @@ -80,14 +81,15 @@ and then pass the object resulting from the inference to it. Its fields are parsed and saved on disk. === "Inference" + ```{ .py hl_lines="7 12" } import supervision as sv from inference import get_model model = get_model(model_id="yolov8n-640") - frames_generator = sv.get_video_frames_generator() + frames_generator = sv.get_video_frames_generator("") - with sv.CSVSink() as sink: + with sv.CSVSink("") as sink: for frame in frames_generator: results = model.infer(image)[0] @@ -96,14 +98,15 @@ object resulting from the inference to it. Its fields are parsed and saved on di ``` === "Ultralytics" + ```{ .py hl_lines="7 12" } import supervision as sv from ultralytics import YOLO model = YOLO("yolov8n.pt") - frames_generator = sv.get_video_frames_generator() + frames_generator = sv.get_video_frames_generator("") - with sv.CSVSink() as sink: + with sv.CSVSink("") as sink: for frame in frames_generator: results = model(frame)[0] @@ -112,6 +115,7 @@ object resulting from the inference to it. Its fields are parsed and saved on di ``` === "Transformers" + ```{ .py hl_lines="9 23" } import torch import supervision as sv @@ -119,9 +123,9 @@ object resulting from the inference to it. Its fields are parsed and saved on di processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50") model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50") - frames_generator = sv.get_video_frames_generator() + frames_generator = sv.get_video_frames_generator("") - with sv.CSVSink() as sink: + with sv.CSVSink("") as sink: for frame in frames_generator: frame = sv.cv2_to_pillow(frame) @@ -154,14 +158,15 @@ also allows you to add custom information to each row, which can be passed via t frame index from which the detections originate. === "Inference" + ```{ .py hl_lines="8 12" } import supervision as sv from inference import get_model model = get_model(model_id="yolov8n-640") - frames_generator = sv.get_video_frames_generator() + frames_generator = sv.get_video_frames_generator("") - with sv.CSVSink() as sink: + with sv.CSVSink("") as sink: for frame_index, frame in enumerate(frames_generator): results = model.infer(image)[0] @@ -170,14 +175,15 @@ frame index from which the detections originate. ``` === "Ultralytics" + ```{ .py hl_lines="8 12" } import supervision as sv from ultralytics import YOLO model = YOLO("yolov8n.pt") - frames_generator = sv.get_video_frames_generator() + frames_generator = sv.get_video_frames_generator("") - with sv.CSVSink() as sink: + with sv.CSVSink("") as sink: for frame_index, frame in enumerate(frames_generator): results = model(frame)[0] @@ -186,6 +192,7 @@ frame index from which the detections originate. ``` === "Transformers" + ```{ .py hl_lines="10 23" } import torch import supervision as sv @@ -193,9 +200,9 @@ frame index from which the detections originate. processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50") model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50") - frames_generator = sv.get_video_frames_generator() + frames_generator = sv.get_video_frames_generator("") - with sv.CSVSink() as sink: + with sv.CSVSink("") as sink: for frame_index, frame in enumerate(frames_generator): frame = sv.cv2_to_pillow(frame) @@ -227,14 +234,15 @@ with [`sv.JSONSink`](https://supervision.roboflow.com/latest/detection/tools/save_detections/#supervision.detection.tools.csv_sink.JSONSink). === "Inference" + ```{ .py hl_lines="7" } import supervision as sv from inference import get_model model = get_model(model_id="yolov8n-640") - frames_generator = sv.get_video_frames_generator() + frames_generator = sv.get_video_frames_generator("") - with sv.JSONSink() as sink: + with sv.JSONSink("") as sink: for frame_index, frame in enumerate(frames_generator): results = model.infer(image)[0] @@ -243,14 +251,15 @@ with ``` === "Ultralytics" + ```{ .py hl_lines="7" } import supervision as sv from ultralytics import YOLO model = YOLO("yolov8n.pt") - frames_generator = sv.get_video_frames_generator() + frames_generator = sv.get_video_frames_generator("") - with sv.JSONSink() as sink: + with sv.JSONSink("") as sink: for frame_index, frame in enumerate(frames_generator): results = model(frame)[0] @@ -259,6 +268,7 @@ with ``` === "Transformers" + ```{ .py hl_lines="9" } import torch import supervision as sv @@ -266,9 +276,9 @@ with processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50") model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50") - frames_generator = sv.get_video_frames_generator() + frames_generator = sv.get_video_frames_generator("") - with sv.JSONSink() as sink: + with sv.JSONSink("") as sink: for frame_index, frame in enumerate(frames_generator): frame = sv.cv2_to_pillow(frame) diff --git a/docs/how_to/track_objects.md b/docs/how_to/track_objects.md index f021d5ba..8cf9eae0 100644 --- a/docs/how_to/track_objects.md +++ b/docs/how_to/track_objects.md @@ -75,7 +75,7 @@ it will be modified to include tracking, labeling, and trace annotations. import supervision as sv from inference.models.utils import get_roboflow_model - model = get_roboflow_model(model_id="yolov8n-640", api_key=) + model = get_roboflow_model(model_id="yolov8n-640", api_key="") box_annotator = sv.BoxAnnotator() def callback(frame: np.ndarray, _: int) -> np.ndarray: @@ -133,7 +133,7 @@ enabling the continuous following of the object's motion path across different f import supervision as sv from inference.models.utils import get_roboflow_model - model = get_roboflow_model(model_id="yolov8n-640", api_key=) + model = get_roboflow_model(model_id="yolov8n-640", api_key="") tracker = sv.ByteTrack() box_annotator = sv.BoxAnnotator() @@ -200,7 +200,7 @@ offering a clear visual representation of each object's class and unique identif import supervision as sv from inference.models.utils import get_roboflow_model - model = get_roboflow_model(model_id="yolov8n-640", api_key=) + model = get_roboflow_model(model_id="yolov8n-640", api_key="") tracker = sv.ByteTrack() box_annotator = sv.BoxAnnotator() label_annotator = sv.LabelAnnotator() @@ -285,7 +285,7 @@ movement patterns and interactions between objects in the video. import supervision as sv from inference.models.utils import get_roboflow_model - model = get_roboflow_model(model_id="yolov8n-640", api_key=) + model = get_roboflow_model(model_id="yolov8n-640", api_key="") tracker = sv.ByteTrack() box_annotator = sv.BoxAnnotator() label_annotator = sv.LabelAnnotator() @@ -381,7 +381,7 @@ Let's immediately visualize the results with our [`EdgeAnnotator`](https://super from inference.models.utils import get_roboflow_model model = get_roboflow_model( - model_id="yolov8m-pose-640", api_key=) + model_id="yolov8m-pose-640", api_key="") edge_annotator = sv.EdgeAnnotator() vertex_annotator = sv.VertexAnnotator() @@ -454,7 +454,7 @@ Let's convert to detections and visualize the results with our [`BoxAnnotator`]( from inference.models.utils import get_roboflow_model model = get_roboflow_model( - model_id="yolov8m-pose-640", api_key=) + model_id="yolov8m-pose-640", api_key="") edge_annotator = sv.EdgeAnnotator() vertex_annotator = sv.VertexAnnotator() box_annotator = sv.BoxAnnotator() @@ -531,7 +531,7 @@ Now that we have a `Detections` object, we can track it throughout the video. Ut from inference.models.utils import get_roboflow_model model = get_roboflow_model( - model_id="yolov8m-pose-640", api_key=) + model_id="yolov8m-pose-640", api_key="") edge_annotator = sv.EdgeAnnotator() vertex_annotator = sv.VertexAnnotator() box_annotator = sv.BoxAnnotator() @@ -616,7 +616,7 @@ We could stop here as we have successfully tracked the object detected by the ke from inference.models.utils import get_roboflow_model model = get_roboflow_model( - model_id="yolov8m-pose-640", api_key=) + model_id="yolov8m-pose-640", api_key="") edge_annotator = sv.EdgeAnnotator() vertex_annotator = sv.VertexAnnotator() box_annotator = sv.BoxAnnotator() diff --git a/docs/index.md b/docs/index.md index 1f524639..251ec767 100644 --- a/docs/index.md +++ b/docs/index.md @@ -42,6 +42,7 @@ You can install `supervision` in a !!! example "Installation" === "pip (recommended)" + [![version](https://badge.fury.io/py/supervision.svg)](https://badge.fury.io/py/supervision) [![downloads](https://img.shields.io/pypi/dm/supervision)](https://pypistats.org/packages/supervision) [![license](https://img.shields.io/pypi/l/supervision)](../LICENSE.md) @@ -52,6 +53,7 @@ You can install `supervision` in a ``` === "poetry" + [![version](https://badge.fury.io/py/supervision.svg)](https://badge.fury.io/py/supervision) [![downloads](https://img.shields.io/pypi/dm/supervision)](https://pypistats.org/packages/supervision) [![license](https://img.shields.io/pypi/l/supervision)](../LICENSE.md) @@ -62,6 +64,7 @@ You can install `supervision` in a ``` === "uv" + [![version](https://badge.fury.io/py/supervision.svg)](https://badge.fury.io/py/supervision) [![downloads](https://img.shields.io/pypi/dm/supervision)](https://pypistats.org/packages/supervision) [![license](https://img.shields.io/pypi/l/supervision)](../LICENSE.md) @@ -78,6 +81,7 @@ You can install `supervision` in a ``` === "rye" + [![version](https://badge.fury.io/py/supervision.svg)](https://badge.fury.io/py/supervision) [![downloads](https://img.shields.io/pypi/dm/supervision)](https://pypistats.org/packages/supervision) [![license](https://img.shields.io/pypi/l/supervision)](../LICENSE.md) @@ -87,9 +91,10 @@ You can install `supervision` in a rye add supervision ``` - !!! example "conda/mamba install" + === "conda" + [![conda-recipe](https://img.shields.io/badge/recipe-supervision-green.svg)](https://anaconda.org/conda-forge/supervision) [![conda-downloads](https://img.shields.io/conda/dn/conda-forge/supervision.svg)](https://anaconda.org/conda-forge/supervision) [![conda-version](https://img.shields.io/conda/vn/conda-forge/supervision.svg)](https://anaconda.org/conda-forge/supervision) [![conda-platforms](https://img.shields.io/conda/pn/conda-forge/supervision.svg)](https://anaconda.org/conda-forge/supervision) ```bash @@ -97,6 +102,7 @@ You can install `supervision` in a ``` === "mamba" + [![mamba-recipe](https://img.shields.io/badge/recipe-supervision-green.svg)](https://anaconda.org/conda-forge/supervision) [![mamba-downloads](https://img.shields.io/conda/dn/conda-forge/supervision.svg)](https://anaconda.org/conda-forge/supervision) [![mamba-version](https://img.shields.io/conda/vn/conda-forge/supervision.svg)](https://anaconda.org/conda-forge/supervision) [![mamba-platforms](https://img.shields.io/conda/pn/conda-forge/supervision.svg)](https://anaconda.org/conda-forge/supervision) ```bash @@ -104,7 +110,9 @@ You can install `supervision` in a ``` !!! example "git clone (for development)" + === "virtualenv" + ```bash # clone repository and navigate to root directory git clone --depth 1 -b develop https://github.com/roboflow/supervision.git @@ -120,6 +128,7 @@ You can install `supervision` in a ``` === "uv" + ```bash # clone repository and navigate to root directory git clone --depth 1 -b develop https://github.com/roboflow/supervision.git diff --git a/docs/keypoint/annotators.md b/docs/keypoint/annotators.md index 92c7ceba..a3060e2c 100644 --- a/docs/keypoint/annotators.md +++ b/docs/keypoint/annotators.md @@ -14,11 +14,11 @@ comments: true vertex_annotator = sv.VertexAnnotator( color=sv.Color.GREEN, - radius=10 + radius=10, ) annotated_frame = vertex_annotator.annotate( scene=image.copy(), - key_points=key_points + key_points=key_points, ) ``` @@ -38,11 +38,11 @@ comments: true edge_annotator = sv.EdgeAnnotator( color=sv.Color.GREEN, - thickness=5 + thickness=5, ) annotated_frame = edge_annotator.annotate( scene=image.copy(), - key_points=key_points + key_points=key_points, ) ``` @@ -63,11 +63,11 @@ comments: true vertex_label_annotator = sv.VertexLabelAnnotator( color=sv.Color.GREEN, text_color=sv.Color.BLACK, - border_radius=5 + border_radius=5, ) annotated_frame = vertex_label_annotator.annotate( scene=image.copy(), - key_points=key_points + key_points=key_points, ) ``` diff --git a/supervision/annotators/core.py b/supervision/annotators/core.py index a1334e3a..38c2957e 100644 --- a/supervision/annotators/core.py +++ b/supervision/annotators/core.py @@ -291,7 +291,7 @@ class OrientedBoxAnnotator(BaseAnnotator): import supervision as sv from ultralytics import YOLO - image = cv2.imread() + image = cv2.imread("") model = YOLO("yolov8n-obb.pt") result = model(image)[0] diff --git a/supervision/detection/core.py b/supervision/detection/core.py index a5af44bc..f3305fc1 100644 --- a/supervision/detection/core.py +++ b/supervision/detection/core.py @@ -74,7 +74,7 @@ class Detections: from inference import get_model model = get_model(model_id="yolov8n-640") - image = cv2.imread() + image = cv2.imread("") results = model.infer(image)[0] detections = sv.Detections.from_inference(results) ``` @@ -90,7 +90,7 @@ class Detections: from ultralytics import YOLO model = YOLO("yolov8n.pt") - image = cv2.imread() + image = cv2.imread("") results = model(image)[0] detections = sv.Detections.from_ultralytics(results) ``` @@ -109,7 +109,7 @@ class Detections: processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50") model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50") - image = Image.open() + image = Image.open("") inputs = processor(images=image, return_tensors="pt") with torch.no_grad(): @@ -225,7 +225,7 @@ class Detections: import torch import supervision as sv - image = cv2.imread() + image = cv2.imread("") model = torch.hub.load('ultralytics/yolov5', 'yolov5s') result = model(image) detections = sv.Detections.from_yolov5(result) @@ -265,7 +265,7 @@ class Detections: import supervision as sv from ultralytics import YOLO - image = cv2.imread() + image = cv2.imread("") model = YOLO('yolov8s.pt') results = model(image)[0] detections = sv.Detections.from_ultralytics(results) @@ -338,7 +338,7 @@ class Detections: from super_gradients.training import models import supervision as sv - image = cv2.imread() + image = cv2.imread("") model = models.get('yolo_nas_l', pretrained_weights="coco") result = list(model.predict(image, conf=0.35))[0] @@ -452,8 +452,8 @@ class Detections: import supervision as sv from mmdet.apis import init_detector, inference_detector - image = cv2.imread() - model = init_detector(, , device=) + image = cv2.imread("") + model = init_detector("", "", device="") result = inference_detector(model, image) detections = sv.Detections.from_mmdetection(result) @@ -501,7 +501,7 @@ class Detections: processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50") model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50") - image = Image.open() + image = Image.open("") inputs = processor(images=image, return_tensors="pt") with torch.no_grad(): @@ -570,10 +570,10 @@ class Detections: from detectron2.config import get_cfg - image = cv2.imread() + image = cv2.imread("") cfg = get_cfg() - cfg.merge_from_file() - cfg.MODEL.WEIGHTS = + cfg.merge_from_file("") + cfg.MODEL.WEIGHTS = "" predictor = DefaultPredictor(cfg) result = predictor(image) @@ -616,7 +616,7 @@ class Detections: import supervision as sv from inference import get_model - image = cv2.imread() + image = cv2.imread("") model = get_model(model_id="yolov8s-640") result = model.infer(image)[0] @@ -1689,7 +1689,7 @@ class Detections: import easyocr reader = easyocr.Reader(['en']) - results = reader.readtext() + results = reader.readtext("") detections = sv.Detections.from_easyocr(results) detected_text = detections["class_name"] ``` @@ -1734,7 +1734,7 @@ class Detections: from ncnn.model_zoo import get_model import supervision as sv - image = cv2.imread() + image = cv2.imread("") model = get_model( "yolov8s", target_size=640 @@ -2037,7 +2037,7 @@ class Detections: import supervision as sv from ultralytics import YOLO - image = cv2.imread() + image = cv2.imread("") model = YOLO('yolov8s.pt') result = model(image)[0] @@ -2300,7 +2300,7 @@ def merge_inner_detection_object_pair( import supervision as sv from inference import get_model - image = cv2.imread() + image = cv2.imread("") model = get_model(model_id="yolov8s-640") result = model.infer(image)[0] diff --git a/supervision/detection/line_zone.py b/supervision/detection/line_zone.py index c7008cfa..574860f3 100644 --- a/supervision/detection/line_zone.py +++ b/supervision/detection/line_zone.py @@ -55,9 +55,9 @@ class LineZone: import supervision as sv from ultralytics import YOLO - model = YOLO() + model = YOLO("") tracker = sv.ByteTrack() - frames_generator = sv.get_video_frames_generator() + frames_generator = sv.get_video_frames_generator("") start, end = sv.Point(x=0, y=1080), sv.Point(x=3840, y=1080) line_zone = sv.LineZone(start=start, end=end) diff --git a/supervision/detection/tools/json_sink.py b/supervision/detection/tools/json_sink.py index 42fb187c..a19eb33e 100644 --- a/supervision/detection/tools/json_sink.py +++ b/supervision/detection/tools/json_sink.py @@ -28,15 +28,15 @@ class JSONSink: import supervision as sv from ultralytics import YOLO - model = YOLO() + model = YOLO("") json_sink = sv.JSONSink() - frames_generator = sv.get_video_frames_generator() + frames_generator = sv.get_video_frames_generator("") with json_sink as sink: for frame in frames_generator: result = model(frame)[0] detections = sv.Detections.from_ultralytics(result) - sink.append(detections, custom_data={'':''}) + sink.append(detections, custom_data={"":""}) ``` """ diff --git a/supervision/detection/tools/polygon_zone.py b/supervision/detection/tools/polygon_zone.py index 952da514..3319c178 100644 --- a/supervision/detection/tools/polygon_zone.py +++ b/supervision/detection/tools/polygon_zone.py @@ -43,7 +43,7 @@ class PolygonZone: import numpy as np import cv2 - image = cv2.imread() + image = cv2.imread("") model = YOLO("yolo11s") tracker = sv.ByteTrack() diff --git a/supervision/detection/tools/smoother.py b/supervision/detection/tools/smoother.py index 2c1137c0..cf4734ce 100644 --- a/supervision/detection/tools/smoother.py +++ b/supervision/detection/tools/smoother.py @@ -35,16 +35,17 @@ class DetectionsSmoother: from ultralytics import YOLO - video_info = sv.VideoInfo.from_video_path(video_path=) - frame_generator = sv.get_video_frames_generator(source_path=) + video_info = sv.VideoInfo.from_video_path(video_path="") + frame_generator = sv.get_video_frames_generator( + source_path="") - model = YOLO() + model = YOLO("") tracker = sv.ByteTrack(frame_rate=video_info.fps) smoother = sv.DetectionsSmoother() box_annotator = sv.BoxAnnotator() - with sv.VideoSink(, video_info=video_info) as sink: + with sv.VideoSink("", video_info=video_info) as sink: for frame in frame_generator: result = model(frame)[0] detections = sv.Detections.from_ultralytics(result) diff --git a/supervision/key_points/core.py b/supervision/key_points/core.py index 4e2da3e7..f9a7720f 100644 --- a/supervision/key_points/core.py +++ b/supervision/key_points/core.py @@ -32,7 +32,7 @@ class KeyPoints: import supervision as sv from ultralytics import YOLO - image = cv2.imread() + image = cv2.imread("") model = YOLO('yolo11s-pose.pt') result = model(image)[0] @@ -49,8 +49,8 @@ class KeyPoints: import supervision as sv from inference import get_model - image = cv2.imread() - model = get_model(model_id=, api_key=) + image = cv2.imread("") + model = get_model(model_id="", api_key="") result = model.infer(image)[0] key_points = sv.KeyPoints.from_inference(result) @@ -68,7 +68,7 @@ class KeyPoints: import mediapipe as mp import supervision as sv - image = cv2.imread() + image = cv2.imread("") image_height, image_width, _ = image.shape mediapipe_image = mp.Image( image_format=mp.ImageFormat.SRGB, @@ -106,7 +106,7 @@ class KeyPoints: ) device = "cuda" if torch.cuda.is_available() else "cpu" - image = Image.open() + image = Image.open("") DETECTION_MODEL_ID = "PekingU/rtdetr_r50vd_coco_o365" @@ -228,8 +228,8 @@ class KeyPoints: import supervision as sv from inference import get_model - image = cv2.imread() - model = get_model(model_id=, api_key=) + image = cv2.imread("") + model = get_model(model_id="", api_key="") result = model.infer(image)[0] key_points = sv.KeyPoints.from_inference(result) @@ -240,13 +240,13 @@ class KeyPoints: import supervision as sv from inference_sdk import InferenceHTTPClient - image = cv2.imread() + image = cv2.imread("") client = InferenceHTTPClient( api_url="https://detect.roboflow.com", - api_key= + api_key="" ) - result = client.infer(image, model_id=) + result = client.infer(image, model_id="") key_points = sv.KeyPoints.from_inference(result) ``` """ @@ -320,7 +320,7 @@ class KeyPoints: import mediapipe as mp import supervision as sv - image = cv2.imread() + image = cv2.imread("") image_height, image_width, _ = image.shape mediapipe_image = mp.Image( image_format=mp.ImageFormat.SRGB, @@ -346,7 +346,7 @@ class KeyPoints: import mediapipe as mp import supervision as sv - image = cv2.imread() + image = cv2.imread("") image_height, image_width, _ = image.shape mediapipe_image = mp.Image( image_format=mp.ImageFormat.SRGB, @@ -436,7 +436,7 @@ class KeyPoints: import supervision as sv from ultralytics import YOLO - image = cv2.imread() + image = cv2.imread("") model = YOLO('yolov8s-pose.pt') result = model(image)[0] @@ -475,7 +475,7 @@ class KeyPoints: import supervision as sv import super_gradients - image = cv2.imread() + image = cv2.imread("") device = "cuda" if torch.cuda.is_available() else "cpu" model = super_gradients.training.models.get( @@ -535,10 +535,10 @@ class KeyPoints: from detectron2.config import get_cfg - image = cv2.imread() + image = cv2.imread("") cfg = get_cfg() - cfg.merge_from_file() - cfg.MODEL.WEIGHTS = + cfg.merge_from_file("") + cfg.MODEL.WEIGHTS = "" predictor = DefaultPredictor(cfg) result = predictor(image) @@ -591,7 +591,7 @@ class KeyPoints: ) device = "cuda" if torch.cuda.is_available() else "cpu" - image = Image.open() + image = Image.open("") DETECTION_MODEL_ID = "PekingU/rtdetr_r50vd_coco_o365" @@ -728,7 +728,7 @@ class KeyPoints: import supervision as sv from ultralytics import YOLO - image = cv2.imread() + image = cv2.imread("") model = YOLO('yolov8s.pt') result = model(image)[0] diff --git a/supervision/tracker/byte_tracker/core.py b/supervision/tracker/byte_tracker/core.py index 482d33e3..52bdef0e 100644 --- a/supervision/tracker/byte_tracker/core.py +++ b/supervision/tracker/byte_tracker/core.py @@ -75,7 +75,7 @@ class ByteTrack: import supervision as sv from ultralytics import YOLO - model = YOLO() + model = YOLO("") tracker = sv.ByteTrack() box_annotator = sv.BoxAnnotator() @@ -95,8 +95,8 @@ class ByteTrack: return annotated_frame sv.process_video( - source_path=, - target_path=, + source_path="", + target_path="", callback=callback ) ``` diff --git a/supervision/utils/video.py b/supervision/utils/video.py index c4e33da6..d3b7776f 100644 --- a/supervision/utils/video.py +++ b/supervision/utils/video.py @@ -31,7 +31,7 @@ class VideoInfo: ```python import supervision as sv - video_info = sv.VideoInfo.from_video_path(video_path=) + video_info = sv.VideoInfo.from_video_path(video_path="") video_info # VideoInfo(width=3840, height=2160, fps=25, total_frames=538) @@ -78,10 +78,10 @@ class VideoSink: ```python import supervision as sv - video_info = sv.VideoInfo.from_video_path() - frames_generator = sv.get_video_frames_generator() + video_info = sv.VideoInfo.from_video_path("") + frames_generator = sv.get_video_frames_generator("") - with sv.VideoSink(target_path=, video_info=video_info) as sink: + with sv.VideoSink(target_path="", video_info=video_info) as sink: for frame in frames_generator: sink.write_frame(frame=frame) ``` @@ -182,7 +182,7 @@ def get_video_frames_generator( ```python import supervision as sv - for frame in sv.get_video_frames_generator(source_path=): + for frame in sv.get_video_frames_generator(source_path=""): ... ``` """ @@ -379,7 +379,8 @@ class FPSMonitor: ```python import supervision as sv - frames_generator = sv.get_video_frames_generator(source_path=) + frames_generator = sv.get_video_frames_generator( + source_path="") fps_monitor = sv.FPSMonitor() for frame in frames_generator: @@ -387,7 +388,7 @@ class FPSMonitor: fps_monitor.tick() fps = fps_monitor.fps ``` - """ # noqa: E501 // docs + """ self.all_timestamps: deque[float] = deque(maxlen=sample_size) @property