### Getting Started with YOLO (You Only Look Once)
YOLO is a state-of-the-art, real-time object detection system. It is popular due to its speed and accuracy. Here is a step-by-step guide on how to get started with using YOLO for object detection.
### 1. Install Necessary Libraries
To use YOLO, you need to have Python and several libraries installed. You can install the necessary libraries using pip. For example, if you are using YOLOv5:
```bash
pip install torch torchvision torchaudio
pip install opencv-python-headless
```
For YOLOv8 or later versions, the installation might include additional dependencies:
```bash
pip install ultralytics
```
### 2. Download the YOLO Model
You need to download a pre-trained YOLO model. For YOLOv5, you can download the models directly from the Ultralytics repository:
```bash
git clone https://github.com/ultralytics/yolov5
cd yolov5
pip install -r requirements.txt
```
For YOLOv8, you can download the model and examples from the official repository:
```bash
pip install ultralytics
```
### 3. Prepare Your Environment
Set up your working directory and ensure you have your input images or video files ready. YOLO works with various input formats including images, video, and streams.
### 4. Perform Object Detection
Here is a basic example of how to run YOLOv5 for object detection on an image:
```python
import torch
# Load YOLOv5 model
model = torch.hub.load('ultralytics/yolov5', 'yolov5s')
# Load an image
img = 'path/to/your/image.jpg'
# Perform inference
results = model(img)
# Display results
results.show() # or .save()
```
For YOLOv8, the usage is slightly different:
```python
from ultralytics import YOLO
# Load YOLOv8 model
model = YOLO('yolov8s.pt')
# Load an image
img = 'path/to/your/image.jpg'
# Perform inference
results = model(img)
# Display results
results.show() # or .save()
```
### 5. Training Your Own Model
If you need to train YOLO on your own dataset, you will need to prepare your dataset in the correct format (e.g., COCO format for YOLOv5) and then modify the configuration files. Here's an example for YOLOv5:
```bash
# Create a custom YAML file for your dataset
# custom_data.yaml
train: /path/to/your/train/dataset
val: /path/to/your/val/dataset
nc: 20 # number of classes
names: ['class1', 'class2', 'class3', ...] # class names
# Train the model
python train.py --img 640 --batch 16 --epochs 50 --data custom_data.yaml --weights yolov5s.pt
```
For YOLOv8, the process is similar but with slight variations:
```bash
# Create a custom YAML file for your dataset
# custom_data.yaml
train: /path/to/your/train/dataset
val: /path/to/your/val/dataset
nc: 20 # number of classes
names: ['class1', 'class2', 'class3', ...] # class names
# Train the model
yolo train data=custom_data.yaml model=yolov8s.pt epochs=50 imgsz=640
```
### 6. Evaluate and Fine-tune
After training, you can evaluate your model using the validation set to see how well it performs and fine-tune it as necessary.
### Additional Resources
- [Ultralytics YOLOv5 GitHub](https://github.com/ultralytics/yolov5)
- [Ultralytics YOLOv8 GitHub](https://github.com/ultralytics/ultralytics)
- [YOLOv5 Documentation](https://docs.ultralytics.com)
- [YOLOv8 Documentation](https://docs.ultralytics.com/v8)
These resources include detailed guides, tutorials, and example code to help you get started and troubleshoot any issues you might encounter.