Ultralytics YOLO27: Join the waitlist.
License YOLO
Ultralytics YOLO

Running Ultralytics object detection and segmentation models in a few lines of code

Learn how to run Ultralytics YOLOv8, RT-DETR, and FastSAM object detection and segmentation models in Python with just a few lines of code.

Nuvola LadiNuvola Ladi4 min read

Listen to this article

Learn how to run Ultralytics YOLOv8, RT-DETR, and FastSAM object detection and segmentation models in Python with just a few lines of code.

In this post, we'll look at the capabilities of Ultralytics' YOLOv5 and YOLOv8 models when it comes to object detection and segmentation. We'll explore how to integrate these easy-to-use models into your projects with just a few lines of code. Whether you're a beginner or an experienced developer, you'll see how Ultralytics supports various models and architectures, including different YOLO versions and transformer-based models.

In his video, Nicolai Nielsen walks through the process of setting up and using various models within the Ultralytics framework. Let's break it down step by step and see how you can get started with these tools.

Getting started with Ultralytics models#

Ultralytics offers a comprehensive framework that supports multiple object detection and segmentation models. This includes the popular YOLO models, ranging from YOLOv3 to YOLOv8 (since succeeded by YOLO26), as well as YOLO-NAS and SAM models. These models are designed to handle a variety of tasks such as real-time detection, segmentation, and pose estimation.

To start, visit the Ultralytics documentation page. Here, you can find detailed information about each model, including their key features, architectures, and how to use them in your Python scripts.

Setting Up Your Environment#

First, ensure you have Ultralytics installed. You can do this by running:

pip install ultralytics

Once this is done, you can start using these models in your projects. Let's start with the Ultralytics YOLOv8 model as an example.

Key features of Ultralytics YOLOv8#

Ultralytics YOLOv8 comes with several enhancements over its predecessors. It’s designed to be faster and more accurate, making it well suited for real-time applications. Some key features include:

  • Enhanced speed and accuracy
  • Pre-trained weights for multiple tasks
  • Support for object detection, segmentation, and classification
  • Improved model architecture for better performance

Running Ultralytics YOLOv8 in Python#

Here's how you can get started with Ultralytics YOLOv8 in just a few lines of code:

from ultralytics import YOLO

# Load a pretrained YOLOv8 model
model = YOLO("yolov8n.pt")

# Run inference on an image
results = model("path/to/image.jpg")

That's it! You’ve just run inference with a YOLOv8 model on an image. This simplicity is what makes Ultralytics models so user-friendly.

Live webcam detection#

Want to see Ultralytics YOLOv8 in action on a live webcam feed? Here's how you can do it:

from ultralytics import YOLO

# Load a pretrained YOLOv8 model
model = YOLO("yolov8n.pt")

# Run inference on the webcam feed (source=0) and display the results
results = model.predict(source=0, show=True)

This script will open your webcam and apply the Ultralytics YOLOv8 model to detect objects in real time.

Nicolai Nielsen outlining how to run Ultralytics object detection and segmentation models

Fig 1. Nicolai Nielsen outlining how to run Ultralytics object detection and segmentation models.

Exploring other models#

Ultralytics doesn't stop at YOLOv8. It also supports various other models like YOLOv5, YOLO-NAS, and transformer-based models for real-time detection. Each model has its own strengths and use cases.

Transformer models and how to run them#

The RT-DETR model, developed by Baidu and supported by Ultralytics, is a state-of-the-art, end-to-end object detector that offers real-time performance and high accuracy. It uses a conv-based backbone and an efficient hybrid encoder for real-time speed, excelling on CUDA with TensorRT, and supports flexible inference speed adjustment.

Here's how you can run an RT-DETR model:

from ultralytics import RTDETR

# Load a COCO-pretrained RT-DETR-l model
model = RTDETR("rtdetr-l.pt")

# Run inference on an image
results = model("path/to/image.jpg")

Segment Anything Models#

Ultralytics also offers models for segmentation tasks, such as MobileSAM and FastSAM. These models are designed to segment out everything in an image, providing detailed insights into the scene.

Running FastSAM#

FastSAM is optimized for real-time segmentation, and here's how you can run it:

from ultralytics import FastSAM

# Create a FastSAM model
model = FastSAM("FastSAM-s.pt")  # or FastSAM-x.pt

# Run inference on an image
results = model("path/to/image.jpg", device="cpu", retina_masks=True, imgsz=1024, conf=0.4, iou=0.9)

This model is well suited to applications that require quick and accurate segmentation.

Performance and comparisons#

A useful feature of the Ultralytics ecosystem is the ability to compare different models side by side. You can determine which model works best for your specific application by looking at performance metrics such as inference speed and mean average precision (mAP).

Main takeaways#

Ultralytics makes it easy to run object detection and segmentation models with just a few lines of code. Whether you’re working on real-time applications or need high-precision models, Ultralytics has a solution for you. Make sure to check out Nicolai Nielsen's full tutorial on the Ultralytics YouTube channel for more in-depth information and examples.

Stay tuned for more tutorials and updates from the Ultralytics community!

Written by

Nuvola Ladi
Nuvola Ladi

Digital Content Manager · Ultralytics

Nuvola Ladi is a Digital Content Manager at Ultralytics, where she creates content covering vision AI and Ultralytics YOLO and represents the company at industry events such as the PyTorch Conference 2025. Based in Spain, she holds a Master's degree in Visual and Digital Media from IE University. Before joining Ultralytics, she held roles at organizations including XVA Gallery & Hotel and Rolex.

Comments2

Ask a question, share your results, or help someone else.

  1. Mariani Rosa

    Make a São José do Rio Preto parking alpr

    1. Paula DerrengerUltralytics

      For a parking ALPR project in São José do Rio Preto, start with a YOLO model trained to detect license plates, then use OCR to read their characters, as outlined in our ANPR guide. (ultralytics.com) Are you looking to log vehicle entry and exit, control a gate, or monitor parking occupancy, and will you use recorded video or a live camera?

Explore solutions

Computer vision in sports

Computer vision in sports

Discover how Ultralytics YOLO powers sports vision with player and ball tracking, performance analytics, coaching insights, and athlete rehabilitation.
Learn more
Computer vision for aerial imagery

Computer vision for aerial imagery

Turn drone and aerial imagery into real-time insights for agriculture, aquaculture, conservation, and geospatial analysis with Ultralytics YOLO.
Learn more
Computer vision in aerospace

Computer vision in aerospace

Apply computer vision in aerospace with Ultralytics YOLO for aircraft inspection, runway FOD detection, airport ground operations, and composite defect detection.
Learn more
Computer vision in security

Computer vision in security

Protect every site with Ultralytics YOLO models. Vision AI powers perimeter monitoring, threat detection, license-plate recognition, and workplace safety.
Learn more
Computer vision in robotics

Computer vision in robotics

Power smarter machines with Ultralytics YOLO models. Vision AI in robotics drives autonomous navigation, perception, object tracking, and real-time control.
Learn more
Computer vision in logistics

Computer vision in logistics

Streamline logistics with Ultralytics YOLO models. Vision AI enables package inspection, sorting, vehicle tracking, and real-time warehouse safety monitoring.
Learn more
Computer vision in retail

Computer vision in retail

Reimagine retail with Ultralytics YOLO models. Vision AI powers inventory tracking, shelf monitoring, queue management, and smarter customer insights.
Learn more
Computer vision in healthcare

Computer vision in healthcare

Build healthcare solutions with Ultralytics YOLO models. Vision AI in healthcare powers faster medical imaging, smarter diagnostics, and patient monitoring.
Learn more
Computer vision in manufacturing

Computer vision in manufacturing

Optimize manufacturing with Ultralytics YOLO models. Vision AI drives quality control, defect detection, PPE compliance, and assembly line automation.
Learn more
Computer vision in automotive

Computer vision in automotive

Apply computer vision in automotive with Ultralytics YOLO models. Vision AI elevates road safety, driver assistance, and vehicle automation for smarter roads.
Learn more
Computer vision in agriculture

Computer vision in agriculture

Bring vision AI to smart agriculture with Ultralytics YOLO models. Power crop monitoring, livestock tracking, and precision farming for higher, smarter yields.
Learn more

Let's build the future of AI together!

Begin your journey with the future of machine learning