A Python snippets extension for VS Code to assist with development using the Ultralytics package. These snippets will help you code with Ultralytics faster and help provide some boilerplate examples to test out. Open an Issue or a Pull Request to have your snippet added! 🚀 Also works with neovim
!
- 2024-10-03 :: Ultralytics YOLO11 added! YOLO11 will now be the default model.
- 2024-08-26 :: Ultralytics VS Code Snippets integration documentation page.
- 2024-08-14 :: Extension size is now ~100x smaller.
- 2024-08-03 :: Tracking example and KWARGs snippets added.
- 2024-07-30 :: SAM2 examples included.
- 2024-07-24 :: Lots of new snippets and updates.
- 2024-07-17 :: Adds YOLO-world snippet with custom prompts, updates reference links, and adds keyword argument.
- 2024-07-12 :: Model export snippet added and adds keyword argument.
- Visit the VS Code Extension Marketplace by going to https://marketplace.visualstudio.com/VSCode and search for
Ultralytics-Snippets
.
-
Follow this link to visit the extension page directly.
-
Click the "Install" button and allow your browser to launch a VS Code session.
You can also install the latest version of the Ultralytics-Snippets
VS Code extension using the following command.
code --install-extension ultralytics.ultralytics-snippets
All snippets use the format:
{PREFIX}.{ROOT}*-{DESCRIPTOR}
-
{PREFIX}
is alwaysultra
-
{ROOT}
denotes a common "root" verb or noun such asimport
orresults
. There will always be at least one root common with other snippets, but it's possible there could be more than one, such asresult-boxes
. -
{DESCRIPTOR}
will be related to the snippet functionality and all words will be separated with hyphens-
the snippet alias.
Import snippets are for common objects that would be imported from the Ultralytics library.
Alias | Description |
---|---|
ultra.import-model |
Add line to import Ultralytics YOLO |
ultra.import-assets |
Import Ultralytics ASSETS directory constant. |
ultra.import-results |
Import Ultralytics Results class (usually for type hinting). |
ultra.import-annotator |
Import Ultralytics auto_annotate function. |
ultra.import-coco2yolo |
Import Ultralytics function to convert annotations from COCO to YOLO format. |
ultra.import-bbox2seg |
Import Ultralytics function to convert horizontal bounding boxes to segmentation contours. |
ultra.import-seg2bbox |
Import Ultralytics function to convert segmentation contours into horizontal bounding boxes. |
ultra.import-box-convert |
Import Ultralytics function for converting bounding box coordinates. |
ultra.import-formats |
Import Ultralytics supported file formats constant. |
ultra.import-task-result |
Import task-based results class (generally helpful for type hinting). |
ultra.import-model
Snippet
Drop-down select available for Model
class to import.
Model = "YOLO"
from ultraltyics import f"{Model}" # not intended to represent valid code
These snippets will provide shortcuts for working with ultralytics.engine.results.Results
objects returned from model inference. See the Working with Results of the documentation and the Results class reference page for more information.
Alias | Description |
---|---|
ultra.result-class-str |
Convert class indices to class string names for a single image result. |
ultra.result-data |
Get result data array of detections from a single image result. |
ultra.result-loop |
Iterate prediction results from an Ultralytics model. |
ultra.result-box-xyxy |
Get pixel-value (x1, y1, x2, y2) bounding box coordinates from a single image result. |
ultra.result-box-xywh |
Get pixel-value (x-center, y-center, w, h) bounding box coordinates from a single image result. |
ultra.result-mask-binary |
Get binary segmentation masks from a single image result. NOTE: [N, H, W] shape, with inference H, W dimensions. |
ultra.result-mask-contours |
Get segmentation contours with pixel value xy or normalized xyn coordinates. |
ultra.result-obb-xywhr |
Get OBB rotated bounding boxes in pixel value [x, y, w, h, r] coordinates as torch.Tensor array. |
ultra.result-orig-image |
Get original image from a single image result. |
ultra.result-filter-class |
Filter prediction results by class ID. Using classes keyword argument for prediction should be preferred. |
ultra.result-loop
Snippet
for result in results:
result.boxes.data # torch.Tensor array
NOTE: results
is a placeholder and can be modified to match existing naming schema.
Shortcuts for initializing pretrained Ultralytics models, like YOLOv8.
Alias | Description | Reference |
---|---|---|
ultra.yolo-model |
Shortcut to initialize YOLO model. | YOLOv5, YOLOv8, YOLOv9, YOLOv10, YOLO-World |
ultra.yolo-export |
Shortcut to export YOLO model weights. | Model Export |
ultra.sam-model |
Shortcut to initialize SAM. | SAM |
ultra.mobileam-model |
Shortcut to initialize MobileSAM. | Mobile SAM |
ultra.fastam-model |
Shortcut to initialize FastSAM. | FastSAM |
ultra.nas-model |
Shortcut to initialize YOLO-NAS model. | YOLO-NAS |
ultra.rtdetr-model |
Shortcut to initialize RTDETR model. | RTDETR |
ultra.yolo-world-model |
Shortcut to initialize YOLO-world model, with class prompts. | YOLO-World |
ultra.sam2-bboxes |
Shortcut to initialize YOLO-World model with text prompts. | SAM2 |
ultra.sam2-points |
Shortcut to initialize YOLO-World model with text prompts. | SAM2 |
ultra.yolo-model
Snippet
Drop-down select available for version
, scale
, and task
, equivalent Python code shown below
version = 8
scale = "s"
task = "." # detect
model = YOLO(f"yolov{version}{scale}{task}pt")
version = 9
scale = "e"
task = "-seg." # segment
model = YOLO(f"yolov{version}{scale}{task}pt")
NOTE: It will be possible to create combinations that aren't available, such as yolov8n-worldv2.pt
. User is responsible for creating a valid configuration per documentation.
Alias | Description | Reference |
---|---|---|
ultra.util-auto-annotate |
Use Ultralytics auto_annotate function to generate annotations. | auto_annotator fucntion |
ultra.util-annotator |
Use Ultralytics Annotator class to draw box annotations | Annotator class |
ultra.util-make-divisible |
Use Ultralytics make_divisible function to make a number divisible by another. | make_divisible function |
ultra.util-callback |
Shortcut for adding custom model callback for a defined function. | callbacks |
ultra.auto-annotate
Snippet
from ultralytics.data.annotator import auto_annotate
auto_annotate(data="", det_model="yolov8n.pt", sam_model="sam_b.pt", device="cuda", output_dir="")
NOTE: Each function argument will be a "field" that can be tabbed into and changed. The det_model
, sam_model
, and device
arguments will have options for default models, but can be cleared to input custom strings instead.
The Example snippets are more "complete" blocks of code that can be used for boilerplate demonstrations.
Prefix | Description |
---|---|
ultra.example-predict-filter-class |
Ultralytics basic YOLO object detection predict with filtered classes example. |
ultra.example-result-filter-class |
Filter prediction results by class ID. Using "classes" keyword argument for prediction should be preferred. |
ultra.example-yolo-predict |
Setup Ultralytics YOLO to perform predict (simple). |
ultra.example-yolo-val |
Setup Ultralytics YOLO to perform validation (simple). |
ultra.example-yolo-train |
Setup Ultralytics YOLO to perform training (simple). |
ultra.example-yolo-predict-kwords |
Setup Ultralytics YOLO to perform inference, show all inference keyword arguments and their default values. |
ultra.example-yolo-train-kwords |
Setup Ultralytics YOLO for training, with all keyword arguments and their default values. |
ultra.example-sam-predict |
Setup Ultralytics SAM to perform inference (simple). |
ultra.example-sam2 |
Example showing use of SAM2 with bounding box and point prompts. |
ultra.example-mobile-sam-predict |
Setup Ultralytics MobileSAM to perform inference (simple). |
ultra.example-fast-sam-predict |
Setup Ultralytics FastSAM to perform inference (simple). |
ultra.example-nas-predict |
Setup Ultralytics NAS to perform inference (simple). |
ultra.example-rtdetr-predict |
Setup Ultralytics RT-DETR to perform inference (simple). |
ultra.example-callback |
Example showing how to add a custom callback function. |
ultra.example-track-loop-persist |
Example of how to open video, loop frames, and maintain tracked object IDs. |
ultra.example-track-kwords |
Example showing all keyword arguments available for track mode. |
ultra.example-predict
Snippet
from ultralytics import YOLO, ASSETS
model = YOLO("yolov8n.pt", task="detect")
results = model(source=ASSETS / "bus.jpg")
for result in results:
print(result.boxes.data)
# result.show() # uncomment to view each result image
NOTE: Here, the only configurable option is the model scale which can be any one of: n
, s
, m
, l
, or x
.
Use these to insert the various model methods defined in modes with all keyword arguments, default values, and commented descriptions quickly into your code. Includes model
as default variable, but is an editable field accessible using tab stops.
Prefix | Description | Reference |
---|---|---|
ultra.kwargs-predict |
Snippet using model predict method, including all keyword arguments and defaults. |
predict |
ultra.kwargs-train |
Snippet using model train method, including all keyword arguments and defaults. |
train |
ultra.kwargs-track |
Snippet using model track method, including all keyword arguments and defaults. |
track |
ultra.kwargs-val |
Snippet using model val method, including all keyword arguments and defaults. |
val |
ultra.kwargs-predict
model.predict(
source=src, # (str, optional) source directory for images or videos
imgsz=640, # (int | list) input images size as int or list[w,h] for predict
conf=0.25, # (float) minimum confidence threshold
iou=0.7, # (float) intersection over union (IoU) threshold for NMS
vid_stride=1, # (int) video frame-rate stride
stream_buffer=False, # (bool) buffer all streaming frames (True) or return the most recent frame (False)
visualize=False, # (bool) visualize model features
augment=False, # (bool) apply image augmentation to prediction sources
agnostic_nms=False, # (bool) class-agnostic NMS
classes=None, # (int | list[int], optional) filter results by class, i.e. classes=0, or classes=[0,2,3]
retina_masks=False, # (bool) use high-resolution segmentation masks
embed=None, # (list[int], optional) return feature vectors/embeddings from given layers
show=False, # (bool) show predicted images and videos if environment allows
save=True, # (bool) save prediction results
save_frames=False, # (bool) save predicted individual video frames
save_txt=False, # (bool) save results as .txt file
save_conf=False, # (bool) save results with confidence scores
save_crop=False, # (bool) save cropped images with results
stream=False, # (bool) for processing long videos or numerous images with reduced memory usage by returning a generator
verbose=True, # (bool) enable/disable verbose inference logging in the terminal
)
It's possible to use VS Code snippets by installing the LuaSnip repo and then adding the following line into your configuration of LuaSnip
:
require("luasnip.loaders.from_vscode").lazy_load({ paths = { "./ultralytics-snippets/" }, include = { "python" } })
Make sure that the path "./ultralytics-snippets/"
is valid for your install location.
Note
If the snippets don't work, try removing the comment lines at the top of each JSON file. These are ignored by VS Code, but might not be ignored by neovim
or LuaSnip
.