Green check
Link copied to clipboard

Running Ultralytics Object Detection and Segmentation Models in a Few Lines of Code

A step-by-step guide on how to run Ultralytics object detection and segmentation models in a few lines of code.

Welcome to another blogpost where we'll dive into 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 us 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 incredible 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 the latest YOLOv8, 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:

bash

Copy code


pip install ultralytics

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

Key Features of YOLOv8

YOLOv8 comes with several enhancements over its predecessors. It’s designed to be faster and more accurate, making it perfect 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 YOLOv8 in Python

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

Copy code


# Copy code
import ultralytics
model = ultralytics.YOLO('yolov8')
results = model.predict('path/to/your/image.jpg')
results.show()

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

Live Webcam Detection

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

python

Copy code


# Copy code
import ultralytics
model = ultralytics.YOLO('yolov8')
# Open a live webcam feed
cap = cv2.VideoCapture(0)
while True:
	ret, frame = cap.read()
	if not ret:
		break
	results = model.predict(frame)
	results.show()
	if cv2.waitKey(1) & 0xFF == ord('q'):
		break
cap.release()
cv2.destroyAllWindows()

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

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

Exploring Other Models

Ultralytics doesn't just stop at YOLOv8. They also support 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 a RT-DETR model:

Copy code


from Ultralytics import YOLO
# Load a pretrained YOLOv8n model
model = YOLO("rtdetr-l.pt")

# Run inference on 'bus.jpg' with arguments
model.predict("bus.jpg", save=True, imgsz=320, conf=0.5)

"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:

Copy code


import ultralytics
model = ultralytics.SAM("FastSAM-s.pt")
results = model.predict('path/to/your/image.jpg')
results.show()

This model is perfect for applications that require quick and accurate segmentation.

Performance and Comparisons

One of the great features of the Ultralytics framework is the ability to compare different models side by side. You can easily 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 incredibly 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!

Facebook logoTwitter logoLinkedIn logoCopy-link symbol

Read more in this category

Let’s build the future
of AI together!

Begin your journey with the future of machine learning