Tutorial Lengkap Ultralytics: Object Detection dengan YOLO
Ultralytics adalah framework Python yang menyediakan implementasi state-of-the-art untuk YOLO (You Only Look Once), salah satu algoritma object detection paling populer dan efisien. Dalam tutorial ini, kita akan mempelajari cara menggunakan Ultralytics dari instalasi hingga deployment model custom.
Apa itu Ultralytics dan YOLO?
YOLO adalah algoritma real-time object detection yang dapat mendeteksi multiple objects dalam satu forward pass neural network. Ultralytics menyediakan implementasi YOLOv8, YOLOv9, YOLOv10, dan YOLO11 yang mudah digunakan dengan performa tinggi.
Keunggulan Ultralytics:- API yang sangat sederhana dan intuitif
- Support multiple tasks: detection, segmentation, classification, pose estimation
- Pretrained models untuk berbagai use case
- Easy training dan finetuning
- Export ke berbagai format (ONNX, TensorRT, CoreML, dll)
Instalasi
1. Setup Environment
# Buat virtual environment
python -m venv ultralyticsenv
source ultralyticsenv/bin/activate # Linux/Mac
ultralyticsenv\Scripts\activate # Windows
Install Ultralytics
pip install ultralytics
Untuk GPU support (CUDA)
pip install torch torchvision --index-url https://download.pytorch.org/whl/cu118
2. Verifikasi Instalasi
import ultralytics
from ultralytics import YOLO
Check version
print(f"Ultralytics version: {ultralytics.version}")
Check CUDA availability
import torch
print(f"CUDA available: {torch.cuda.isavailable()}")
if torch.cuda.isavailable():
print(f"GPU: {torch.cuda.getdevicename(0)}")
Quick Start: Object Detection
1. Load Pretrained Model
from ultralytics import YOLO
Load model YOLOv8 (tersedia: yolov8n, yolov8s, yolov8m, yolov8l, yolov8x)
n=nano, s=small, m=medium, l=large, x=extra large
model = YOLO('yolov8n.pt') # Model otomatis di-download
Atau load YOLO11 (terbaru)
model = YOLO('yolo11n.pt')
Pilihan Model:
| Model | Size | mAP | Speed (ms) | Use Case |
|-------|------|-----|------------|----------|
| yolov8n | 3.2M | 37.3 | 1.2 | Edge devices, real-time |
| yolov8s | 11.2M | 44.9 | 1.9 | Balance speed/accuracy |
| yolov8m | 25.9M | 50.2 | 4.1 | General purpose |
| yolov8l | 43.7M | 52.9 | 6.8 | High accuracy |
| yolov8x | 68.2M | 53.9 | 10.8 | Maximum accuracy |
2. Inference pada Gambar
from ultralytics import YOLO
Load model
model = YOLO('yolov8n.pt')
Inference pada single image
results = model('path/to/image.jpg')
Inference pada multiple images
results = model(['image1.jpg', 'image2.jpg', 'image3.jpg'])
Inference dari URL
results = model('https://example.com/image.jpg')
Process results
for result in results:
# Bounding boxes
boxes = result.boxes
print(f"Detected {len(boxes)} objects")
for box in boxes:
# Koordinat box (x1, y1, x2, y2)
xyxy = box.xyxy[0].tolist()
# Confidence score
confidence = box.conf[0].item()
# Class ID dan nama
classid = int(box.cls[0].item())
classname = model.names[classid]
print(f" {classname}: {confidence:.2f} at {xyxy}")
3. Inference pada Video
from ultralytics import YOLO
import cv2
model = YOLO('yolov8n.pt')
Inference pada video file
results = model('path/to/video.mp4', stream=True)
for result in results:
# Process setiap frame
annotatedframe = result.plot()
# Display (optional)
cv2.imshow('Detection', annotatedframe)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cv2.destroyAllWindows()
4. Inference Real-time dari Webcam
from ultralytics import YOLO
import cv2
model = YOLO('yolov8n.pt')
Open webcam
cap = cv2.VideoCapture(0)
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
# Run inference
results = model(frame, verbose=False)