Skip to content

Instantly share code, notes, and snippets.

@stephenlb
Last active June 19, 2024 03:50
Show Gist options
  • Save stephenlb/2f965222e360a98a4cbf3cf4996ba9e4 to your computer and use it in GitHub Desktop.
Save stephenlb/2f965222e360a98a4cbf3cf4996ba9e4 to your computer and use it in GitHub Desktop.
YOLO Ultralytics Object Detection in Images

YOLO Object Detection: Image Recognition Software Guide

Introduction

YOLO (You Only Look Once) is a popular object detection algorithm well-documented by Ultralytics. For more comprehensive details, visit the Ultralytics YOLO documentation.

Docker Ultralytics

To perform object detection using YOLO in a Docker environment, you can run the following commands:

# Install
docker pull ultralytics/ultralytics

# Run YOLO prediction on a remote image
docker run -v ./runs:/usr/src/ultralytics/runs ultralytics/ultralytics yolo predict model=yolov8n.pt source=https://ultralytics.com/images/bus.jpg

# View result
open runs/detect/predict/bus.jpg

# Run YOLO prediction on a locally saved image
docker run -v ./runs:/usr/src/ultralytics/runs ultralytics/ultralytics yolo predict model=yolov8n.pt source=./runs/detect/predict/bus.jpg

# View result
open runs/detect/predict2/bus.jpg

Quick 60-Second Installation and Usage for Object Detection

Follow these steps to quickly install and use YOLO for object detection:

  1. Create a Python Virtual Environment:
python3 -m venv venv
source venv/bin/activate
  1. Install Required Dependencies:
# Option 1: Specific Torch and Torchaudio versions
pip install torch==2.0.1 torchvision==0.15.2 torchaudio==2.0.2

# Option 2: Alternative versions
pip install torch==2.2.2 torchvision==0.17.2 torchaudio==2.2.2
  1. Install Ultralytics:
pip install ultralytics
  1. Verify Torch Installation:
pip show torch
  1. Run YOLO Prediction:
yolo predict model=yolov8n.pt source='https://ultralytics.com/images/bus.jpg'
  1. Open the Resulting Image:
open runs/detect/predict/bus.jpg

Python Code Usage

You can find the Python code to perform YOLO predictions in the Ultralytics documentation.

from ultralytics import YOLO

# Load a model
model = YOLO("yolov8n.pt")  # load an official model
#### model = YOLO("path/to/best.pt")  # load a custom model

# Predict with the model
results = model("https://ultralytics.com/images/bus.jpg")  # predict on an image

Detectron2

Detectron2, another object detection tool, has a lot of promise. However it's not been kept up to date. For more information, visit Meta's AI Detectron2 page.

Here is the YouTube comments thread around the topic https://www.youtube.com/watch?v=1qWxkFhhv2s&lc=Ugw3-v9v-HrF5KJvvTZ4AaABAg


Use this guide to quickly set up and run YOLO object detection, either using Docker or a Python virtual environment. Happy coding!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment