YOLO ?????????????????? Automation ?????????????????????
YOLO (You Only Look Once) ???????????? real-time object detection algorithm ?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? YOLOv8 ????????? Ultralytics ???????????? version ????????????????????????????????????????????? detection, segmentation, classification, pose estimation ????????? tracking
Automation Script ????????????????????? ?????????????????? YOLO ?????????????????????????????????????????? ???????????? ??????????????????????????????????????????????????????????????????????????? (quality control), ??????????????????????????????????????????????????? CCTV (traffic monitoring), ????????????????????? PPE (??????????????????????????????, ??????????????????????????????????????????????????????) ?????? construction sites, ????????????????????? anomaly ?????? manufacturing, ???????????? parking ??????????????????????????? (????????????????????????????????????)
???????????????????????? YOLO ?????????????????? automation ????????????????????? (real-time 30+ FPS), accuracy ????????? (mAP 50-70%+), ?????????????????????????????? deploy (Python, ONNX, TensorRT), ?????????????????? edge devices (Jetson, Raspberry Pi), Custom training ???????????? data ???????????????????????????
????????????????????? YOLOv8 ????????? Setup Environment
Setup environment ?????????????????? YOLO automation
# === YOLOv8 Setup ===
# 1. Install dependencies
pip install ultralytics opencv-python-headless numpy
# Verify installation
yolo version
# 2. Docker Setup
cat > Dockerfile << 'EOF'
FROM ultralytics/ultralytics:latest-python
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
CMD ["python", "automation.py"]
EOF
cat > docker-compose.yml << 'EOF'
version: '3.8'
services:
yolo-detector:
build: .
volumes:
- ./models:/app/models
- ./data:/app/data
- ./output:/app/output
deploy:
resources:
reservations:
devices:
- driver: nvidia
count: 1
capabilities: [gpu]
environment:
- CONFIDENCE_THRESHOLD=0.5
- MODEL_PATH=/app/models/yolov8n.pt
- INPUT_SOURCE=rtsp://camera:554/stream
restart: unless-stopped
monitoring:
image: grafana/grafana:10.4.0
ports:
- "3000:3000"
volumes:
- grafana-data:/var/lib/grafana
volumes:
grafana-data:
EOF
# 3. Download pre-trained models
cat > download_models.sh << 'BASH'
#!/bin/bash
# Download YOLOv8 models
python3 -c "
from ultralytics import YOLO
models = ['yolov8n.pt', 'yolov8s.pt', 'yolov8m.pt']
for m in models:
model = YOLO(m)
print(f'Downloaded: {m}')
"
# Export to ONNX for edge deployment
python3 -c "
from ultralytics import YOLO
model = YOLO('yolov8n.pt')
model.export(format='onnx', imgsz=640, half=True)
print('Exported to ONNX')
"
BASH
chmod +x download_models.sh
echo "YOLOv8 environment ready"
??????????????? Automation Script ???????????? YOLO
Python automation scripts ?????????????????? tasks ???????????????
#!/usr/bin/env python3
# yolo_automation.py ??? YOLO Automation Scripts
import json
import logging
import time
import os
from typing import Dict, List, Optional, Tuple
from datetime import datetime
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger("yolo")
class YOLOAutomation:
"""YOLO-based automation for various detection tasks"""
def __init__(self, model_path="yolov8n.pt", confidence=0.5):
self.model_path = model_path
self.confidence = confidence
self.detection_log = []
self.stats = {"frames": 0, "detections": 0, "alerts": 0}
# In production: self.model = YOLO(model_path)
def detect_objects(self, image_path):
"""Detect objects in image"""
self.stats["frames"] += 1
# Simulated detection results
detections = [
{"class": "person", "confidence": 0.92, "bbox": [100, 50, 300, 400]},
{"class": "car", "confidence": 0.87, "bbox": [400, 200, 600, 350]},
{"class": "truck", "confidence": 0.78, "bbox": [50, 180, 200, 320]},
]
self.stats["detections"] += len(detections)
return detections
def count_vehicles(self, detections):
"""Count vehicles by type"""
vehicle_classes = {"car", "truck", "bus", "motorcycle", "bicycle"}
counts = {}
for det in detections:
if det["class"] in vehicle_classes:
counts[det["class"]] = counts.get(det["class"], 0) + 1
return counts
def check_safety_equipment(self, detections):
"""Check PPE compliance (helmet, vest)"""
persons = [d for d in detections if d["class"] == "person"]
helmets = [d for d in detections if d["class"] == "helmet"]
vests = [d for d in detections if d["class"] == "safety-vest"]
violations = []
for person in persons:
has_helmet = any(self._is_near(person["bbox"], h["bbox"]) for h in helmets)
has_vest = any(self._is_near(person["bbox"], v["bbox"]) for v in vests)
if not has_helmet:
violations.append({"type": "no_helmet", "location": person["bbox"]})
if not has_vest:
violations.append({"type": "no_vest", "location": person["bbox"]})
return {
"persons": len(persons),
"helmets": len(helmets),
"vests": len(vests),
"violations": violations,
"compliant": len(violations) == 0,
}
def _is_near(self, bbox1, bbox2, threshold=100):
"""Check if two bboxes are near each other"""
cx1 = (bbox1[0] + bbox1[2]) / 2
cy1 = (bbox1[1] + bbox1[3]) / 2
cx2 = (bbox2[0] + bbox2[2]) / 2
cy2 = (bbox2[1] + bbox2[3]) / 2
distance = ((cx1 - cx2) ** 2 + (cy1 - cy2) ** 2) ** 0.5
return distance < threshold
def quality_control(self, detections, expected_classes):
"""Manufacturing quality control"""
detected_classes = {d["class"] for d in detections}
missing = set(expected_classes) - detected_classes
unexpected = detected_classes - set(expected_classes)
return {
"status": "PASS" if not missing and not unexpected else "FAIL",
"expected": list(expected_classes),
"detected": list(detected_classes),
"missing": list(missing),
"unexpected": list(unexpected),
}
def generate_alert(self, alert_type, details):
"""Generate automation alert"""
alert = {
"type": alert_type,
"timestamp": datetime.now().isoformat(),
"details": details,
}
self.stats["alerts"] += 1
self.detection_log.append(alert)
logger.warning(f"ALERT [{alert_type}]: {details}")
return alert
# Demo
auto = YOLOAutomation(confidence=0.5)
# 1. Vehicle counting
detections = auto.detect_objects("traffic_cam_001.jpg")
vehicles = auto.count_vehicles(detections)
print(f"Vehicle Count: {vehicles}")
# 2. Safety check
safety = auto.check_safety_equipment([
{"class": "person", "confidence": 0.9, "bbox": [100, 50, 200, 300]},
{"class": "person", "confidence": 0.85, "bbox": [300, 60, 400, 310]},
{"class": "helmet", "confidence": 0.8, "bbox": [120, 30, 180, 80]},
])
print(f"\nSafety Check: {'PASS' if safety['compliant'] else 'FAIL'}")
print(f" Persons: {safety['persons']}, Violations: {len(safety['violations'])}")
# 3. Quality control
qc = auto.quality_control(
[{"class": "screw", "confidence": 0.9, "bbox": [10, 10, 50, 50]},
{"class": "washer", "confidence": 0.85, "bbox": [60, 10, 100, 50]}],
expected_classes=["screw", "washer", "nut"],
)
print(f"\nQC: {qc['status']} ??? Missing: {qc['missing']}")
print(f"\nStats: {auto.stats}")
Real-Time Object Detection Pipeline
Pipeline ?????????????????? real-time detection ????????????????????????
# === Real-Time Detection Pipeline ===
cat > realtime_pipeline.py << 'PYEOF'
#!/usr/bin/env python3
"""Real-time YOLO detection pipeline"""
import json
import logging
import time
from typing import Dict, List
from datetime import datetime
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger("pipeline")
class RealtimePipeline:
"""Real-time object detection pipeline"""
def __init__(self, source, model="yolov8n.pt"):
self.source = source
self.model_name = model
self.running = False
self.frame_count = 0
self.fps = 0
self.detection_buffer = []
def start(self, callback=None, max_frames=None):
"""Start detection pipeline"""
self.running = True
logger.info(f"Starting pipeline: {self.source}")
# In production:
# model = YOLO(self.model_name)
# cap = cv2.VideoCapture(self.source)
start_time = time.time()
while self.running:
self.frame_count += 1
# Simulate frame processing
# results = model(frame, conf=0.5)
detections = self._simulate_detection()
self.detection_buffer.append({
"frame": self.frame_count,
"timestamp": datetime.now().isoformat(),
"detections": detections,
})
# Keep buffer manageable
if len(self.detection_buffer) > 1000:
self.detection_buffer = self.detection_buffer[-500:]
if callback:
callback(detections)
elapsed = time.time() - start_time
self.fps = self.frame_count / elapsed if elapsed > 0 else 0
if max_frames and self.frame_count >= max_frames:
break
time.sleep(0.033) # ~30 FPS
return self.get_summary()
def _simulate_detection(self):
"""Simulate YOLO detection"""
import random
classes = ["person", "car", "truck", "bicycle"]
n = random.randint(0, 5)
return [
{
"class": random.choice(classes),
"confidence": round(random.uniform(0.5, 0.99), 2),
"bbox": [random.randint(0, 500), random.randint(0, 300),
random.randint(100, 640), random.randint(100, 480)],
}
for _ in range(n)
]
def stop(self):
self.running = False
def get_summary(self):
total_detections = sum(len(d["detections"]) for d in self.detection_buffer)
return {
"source": self.source,
"model": self.model_name,
"frames_processed": self.frame_count,
"avg_fps": round(self.fps, 1),
"total_detections": total_detections,
"avg_detections_per_frame": round(total_detections / max(self.frame_count, 1), 1),
}
# Multi-camera setup
cameras = {
"entrance": "rtsp://cam1:554/stream",
"parking": "rtsp://cam2:554/stream",
"warehouse": "rtsp://cam3:554/stream",
}
pipeline = RealtimePipeline("rtsp://cam1:554/stream", model="yolov8s.pt")
summary = pipeline.start(max_frames=100)
print(f"Pipeline Summary:")
print(f" Frames: {summary['frames_processed']}")
print(f" FPS: {summary['avg_fps']}")
print(f" Detections: {summary['total_detections']}")
PYEOF
echo "Real-time pipeline ready"
Custom Model Training
Train YOLO model ???????????? custom data
# === Custom YOLO Training ===
# 1. Dataset structure
cat > dataset_structure.yaml << 'EOF'
# YOLOv8 dataset format
# dataset/
# train/
# images/
# img001.jpg
# labels/
# img001.txt (class x_center y_center width height)
# val/
# images/
# labels/
# dataset.yaml
path: /data/custom_dataset
train: train/images
val: val/images
names:
0: helmet
1: no-helmet
2: safety-vest
3: no-vest
4: person
# Label format (YOLO): class x_center y_center width height
# Example: 0 0.5 0.3 0.1 0.15
EOF
# 2. Training script
cat > train.py << 'PYEOF'
#!/usr/bin/env python3
"""Train custom YOLOv8 model"""
# from ultralytics import YOLO
# Load pre-trained model
# model = YOLO('yolov8s.pt')
# Train
# results = model.train(
# data='dataset.yaml',
# epochs=100,
# imgsz=640,
# batch=16,
# name='safety-detection',
# patience=20,
# lr0=0.01,
# lrf=0.01,
# augment=True,
# mosaic=1.0,
# mixup=0.1,
# device=0, # GPU 0
# )
# Validate
# metrics = model.val()
# print(f"mAP50: {metrics.box.map50}")
# print(f"mAP50-95: {metrics.box.map}")
# Export
# model.export(format='onnx', imgsz=640)
# model.export(format='engine', imgsz=640) # TensorRT
print("Training pipeline configured")
print("Steps: Prepare data ??? Label (Roboflow/CVAT) ??? Train ??? Validate ??? Export ??? Deploy")
PYEOF
# 3. Data augmentation config
cat > augmentation.yaml << 'EOF'
augmentation:
training:
hsv_h: 0.015 # Hue shift
hsv_s: 0.7 # Saturation
hsv_v: 0.4 # Value/brightness
degrees: 10 # Rotation
translate: 0.1 # Translation
scale: 0.5 # Scale
shear: 5 # Shear
perspective: 0.0 # Perspective
flipud: 0.0 # Vertical flip
fliplr: 0.5 # Horizontal flip
mosaic: 1.0 # Mosaic augmentation
mixup: 0.1 # Mixup augmentation
tips:
- "Use at least 1000 images per class"
- "Balance classes (similar number of images)"
- "Include various lighting conditions"
- "Include different angles and distances"
- "Use Roboflow for easy labeling and augmentation"
EOF
echo "Custom training configured"
Deployment ????????? Monitoring
Deploy ????????? monitor YOLO automation
#!/usr/bin/env python3
# deployment_monitor.py ??? YOLO Deployment Monitoring
import json
import logging
from typing import Dict, List
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger("deploy")
class YOLODeploymentMonitor:
def __init__(self):
pass
def deployment_options(self):
return {
"cloud_api": {
"description": "FastAPI/Flask REST API on cloud GPU",
"latency": "50-200ms per image",
"cost": "$0.50-2/hour (GPU instance)",
"scaling": "Kubernetes HPA",
"best_for": "Web applications, batch processing",
},
"edge_jetson": {
"description": "NVIDIA Jetson Nano/Orin",
"latency": "30-100ms per frame",
"cost": "$150-500 one-time",
"format": "TensorRT engine",
"best_for": "On-site cameras, IoT, robotics",
},
"edge_raspberry_pi": {
"description": "Raspberry Pi with ONNX Runtime",
"latency": "200-500ms per frame",
"cost": "$50-100 one-time",
"format": "ONNX (INT8 quantized)",
"best_for": "Low-cost edge, simple detection",
},
"browser": {
"description": "ONNX.js in web browser",
"latency": "100-300ms per frame",
"cost": "Free (client-side)",
"format": "ONNX",
"best_for": "Web demos, privacy-sensitive",
},
}
def dashboard(self):
return {
"system_status": {
"cameras_active": 5,
"cameras_total": 6,
"offline": ["cam-warehouse-02"],
"model": "yolov8s-custom.pt",
"avg_fps": 28.5,
"gpu_utilization": "65%",
},
"detection_stats_24h": {
"total_frames": 2460000,
"total_detections": 8500000,
"unique_objects": {
"person": 12500,
"car": 8200,
"truck": 1500,
"helmet": 11000,
"no-helmet": 450,
},
"alerts_triggered": 45,
"false_positive_rate": "2.1%",
},
"alerts_today": [
{"time": "08:15", "type": "no_helmet", "camera": "cam-site-01", "action": "Notified supervisor"},
{"time": "11:32", "type": "restricted_area", "camera": "cam-warehouse-01", "action": "Alarm triggered"},
{"time": "14:45", "type": "vehicle_count_high", "camera": "cam-parking-01", "action": "Logged"},
],
}
monitor = YOLODeploymentMonitor()
dash = monitor.dashboard()
status = dash["system_status"]
print(f"YOLO Automation Dashboard:")
print(f" Cameras: {status['cameras_active']}/{status['cameras_total']} active")
print(f" Model: {status['model']}, FPS: {status['avg_fps']}, GPU: {status['gpu_utilization']}")
stats = dash["detection_stats_24h"]
print(f"\nDetections (24h): {stats['total_detections']:,}")
print(f" Alerts: {stats['alerts_triggered']}, False Positive: {stats['false_positive_rate']}")
options = monitor.deployment_options()
print(f"\nDeployment Options:")
for name, info in options.items():
print(f" {name}: {info['description']} ({info['latency']})")
FAQ ??????????????????????????????????????????
Q: YOLOv8 ????????? YOLOv9 ????????? RT-DETR ??????????????????????????????????
A: YOLOv8 (Ultralytics) ???????????? standard ?????????????????????????????????????????????????????? ecosystem ???????????????????????? (training, export, deploy), documentation ?????????, community ???????????? ?????? 5 sizes (n, s, m, l, x) ???????????????????????? hardware YOLOv9 ???????????????????????? accuracy ????????????????????? YOLOv8 ???????????????????????? ????????? ecosystem ?????????????????? mature ???????????? RT-DETR (Real-Time Detection Transformer) ????????? Baidu ????????? Transformer ????????? CNN accuracy ??????????????????????????????????????? GPU ????????????????????? ???????????????????????? edge devices ??????????????? ??????????????????????????? YOLOv8 ?????????????????? production ????????????????????? accuracy ????????????????????????????????? GPU ????????? ????????? RT-DETR
Q: Model size ???????????????????????????????????????????????????????
A: YOLOv8n (Nano, 3.2M params) ?????????????????????????????? ??????????????? edge devices, Jetson Nano, Raspberry Pi, mobile, YOLOv8s (Small, 11.2M params) balance ?????? ??????????????? production ???????????????????????? Jetson Orin, desktop GPU, YOLOv8m (Medium, 25.9M params) accuracy ????????????????????? ??????????????? cloud API ????????????????????? quality, YOLOv8l/x (Large/XLarge) accuracy ?????????????????? ??????????????? batch processing ????????????????????? real-time ?????????????????? real-time automation ??????????????????????????? YOLOv8s ????????? FPS ????????????????????????????????? n ????????? accuracy ?????????????????????????????????????????? m
Q: ???????????? train custom model ??????????
A: ????????????????????? use case Pre-trained model (COCO 80 classes) ??????????????????????????????????????? ????????????????????? person, car, truck, bicycle ??????????????????, Traffic monitoring, People counting ???????????? custom training ??????????????? ????????????????????????????????? objects ???????????????????????????????????? COCO (???????????? PPE, ?????????????????????????????????, defects), ???????????? accuracy ??????????????????????????? domain ???????????????, ?????????????????????????????????????????????????????? training data ????????? (?????????????????????, ???????????????????????????????????????) ????????? train custom ???????????? Data ??????????????????????????? 500-1000 images per class, Labeling ????????? Roboflow, CVAT, LabelImg, Training ????????? GPU (RTX 3060+ ???????????? Google Colab), Validation ???????????????????????? data ???????????? ???????????? deploy
Q: Deploy YOLO ?????? edge device ??????????????????????
A: ????????????????????? Export model ???????????? format ?????????????????????????????? ONNX ?????????????????? cross-platform, TensorRT ?????????????????? NVIDIA (Jetson), TFLite ?????????????????? mobile/RPi Optimize ????????? quantization (FP16 ???????????? INT8) ?????? size ???????????????????????? speed, ?????? input size (640 ??? 320) ????????? accuracy ??????????????? Deploy NVIDIA Jetson ????????? TensorRT engine ????????? 30+ FPS (YOLOv8n), Raspberry Pi ????????? ONNX Runtime ????????? 2-5 FPS (YOLOv8n), Coral TPU ????????? TFLite ????????? 10-15 FPS Tips ????????? threading ????????? capture ????????? inference, ????????? tracking (ByteTrack) ????????????????????? detections ????????????????????? process, ????????? ROI (Region of Interest) detect ?????????????????????????????????????????????????????????
