Hridambiswas/traffic

GitHub: Hridambiswas/traffic

利用实时交通摄像头与 YOLO11n 检测车辆密度,动态优化信号灯配时以缓解拥堵。

Stars: 0 | Forks: 0

# 🚦 实时交通信号决策系统 **实时公共交通摄像头 → YOLO11n 车辆检测 → 基于密度的信号控制** [![Python](https://img.shields.io/badge/Python-3.10+-blue?logo=python&logoColor=white)](https://www.python.org/) [![YOLO](https://img.shields.io/badge/YOLO11n-Ultralytics-FF6B35?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAyNCAyNCI+PC9zdmc+)](https://github.com/ultralytics/ultralytics) [![OpenCV](https://img.shields.io/badge/OpenCV-4.11+-5C3EE8?logo=opencv&logoColor=white)](https://opencv.org/) [![yt-dlp](https://img.shields.io/badge/yt--dlp-public_streams-FF0000?logo=youtube&logoColor=white)](https://github.com/yt-dlp/yt-dlp) [![License](https://img.shields.io/badge/License-MIT-lightgrey)](LICENSE) *Hridam Biswas · KIIT University* ## 问题 大多数城市的交通信号灯使用固定时间控制——一辆车停在**空无一人的路上等红灯两分钟**,而另一边无人过街。这种系统读取实时交通摄像头,计算道路被车辆覆盖的密度,并做出实时信号决策。**没有固定计时器,没有时间浪费。** ## 处理流程 ``` Public YouTube Traffic Camera (yt-dlp) │ ▼ ┌──────────────────────┐ │ Frame Capture │ OpenCV VideoCapture → raw BGR frame └──────────┬───────────┘ │ ▼ ┌──────────────────────┐ │ YOLO11n Detection │ Detects: car · truck · bus · motorcycle │ (COCO classes) │ Outputs: bounding boxes + class IDs └──────────┬───────────┘ │ ▼ ┌──────────────────────────────────────────┐ │ Density Calculation │ │ │ │ density = Σ(bbox areas) / frame_area │ │ ─────────────────────────── │ │ × 100 % │ └──────────┬───────────────────────────────┘ │ ▼ ┌──────────────────────┐ │ Rolling Average │ Last N frames smoothed → removes flicker │ Smoothing (N=8) │ └──────────┬───────────┘ │ ▼ ┌──────────────────────────────────────────┐ │ Signal Decision │ │ │ │ density < 10% → 🟢 GREEN │ │ density < 30% → 🟡 YELLOW │ │ density ≥ 30% → 🔴 RED │ └──────────┬───────────────────────────────┘ │ ▼ ┌──────────────────────┐ │ Live Display │ Bounding boxes · signal badge │ (OpenCV window) │ density bar · vehicle count · FPS └──────────────────────┘ ``` ## 信号逻辑 | 平滑密度 | 信号 | 含义 | |:---:|:---:|---| | `< 10 %` | 🟢 **绿灯** | 道路畅通 — 立即放行 | | `10 % – 30 %` | 🟡 **黄灯** | 中等车流 — 准备停车或通行 | | `≥ 30 %` | 🔴 **红灯** | 严重拥堵 — 禁止所有交叉通行 | ## 实时窗口布局 ``` ┌────────────────────────────────────────────────────┐ │ Vehicles: 7 ┌──────────┐ │ │ FPS: 24.3 │ GREEN │ │ │ └──────────┘ │ │ │ │ [ live camera feed + bounding boxes ] │ │ │ │ Density: 8.4% ████░░░░░░░░░░░░░░░░░░░░░░░░░░░░ │ └────────────────────────────────────────────────────┘ ``` | 元素 | 描述 | |---|---| | **边界框** | 每个检测到的车辆周围橙色矩形框 | | **信号徽章** | 右上角颜色块 — 绿色 / 黄色 / 红色 | | **密度条** | 左下角进度条;颜色与信号状态匹配 | | **车辆计数** | 当前帧中检测到的车辆总数 | | **FPS** | 实时推理与显示帧率 | ## 快速启动 ``` git clone https://github.com/Hridambiswas/traffic.git cd traffic pip install -r requirements.txt # 从配置中随机公共摄像头 python main.py # 特定 YouTube 直播 python main.py --source "https://www.youtube.com/watch?v=ByED80IKdIU" # 更严格的检测 + 更宽的平滑窗口 python main.py --conf 0.5 --window 12 # 更低分辨率的流(更快) python main.py --height 360 ``` 按 **Q** 退出。 ## CLI 参考 | 参数 | 默认值 | 描述 | |---|---|---| | `--source` | 从 `config.py` 随机选择 | YouTube 直播 URL 或直接流地址 | | `--conf` | `0.40` | YOLO 置信度阈值(越高 → 误检越少) | | `--window` | `8` | 滚动平均窗口大小(帧数) | | `--height` | `480` | 期望流分辨率高度 | ## 配置 所有可调参数位于 **`config.py`** 中: ``` VEHICLE_CLASSES = {2, 3, 5, 7} # car, motorcycle, bus, truck (COCO IDs) GREEN_THRESHOLD = 10.0 # % density below which signal is GREEN YELLOW_THRESHOLD = 30.0 # % density below which signal is YELLOW SMOOTH_WINDOW = 8 # rolling average frame count CONF_THRESHOLD = 0.40 # YOLO detection confidence DISPLAY_WIDTH = 960 # output window width DISPLAY_HEIGHT = 540 # output window height ``` | 参数 | 较低值 | 较高值 | |---|---|---| | `GREEN_THRESHOLD` | 更可能保持黄/红灯 | 更容易获得绿灯 | | `YELLOW_THRESHOLD` | 红灯触发更早 | 在变红前有更多容忍度 | | `SMOOTH_WINDOW` | 反应更快,但闪烁更多 | 信号更稳定,响应更慢 | | `CONF_THRESHOLD` | 检测到更多车辆 | 仅高置信度检测 | ## 摄像头来源 公共实时交通摄像头配置在 `config.py` 中: | 摄像头 | 位置 | URL | |---|---|---| | 摄像头 1 | 纽约时代广场 | `youtube.com/watch?v=ByED80IKdIU` | | 摄像头 2 | 芝加哥交通 | `youtube.com/watch?v=AdUw5RdyZxI` | | 摄像头 3 | 洛杉矶高速 | `youtube.com/watch?v=1EiC9bvVGnk` | ## YOLO 车辆类别 | COCO 类别 ID | 标签 | 包含 | |:---:|---|:---:| | 2 | car | ✅ | | 3 | motorcycle | ✅ | | 5 | bus | ✅ | | 7 | truck | ✅ | | 0 | person | ❌ | | 9 | traffic light | ❌ | 只有车辆类别计入密度。行人与静态物体被忽略。 ## 项目结构 ``` traffic/ │ ├── main.py ← entry point · CLI args · stream reconnect loop ├── config.py ← all thresholds, paths, camera URLs │ ├── stream.py ← yt-dlp handler · random source with fallback ├── detector.py ← YOLO11n detection · bbox-area density calc ├── signal.py ← rolling-average smoothed signal decision ├── display.py ← OpenCV overlay renderer │ └── requirements.txt ``` ## 依赖 | 包 | 版本 | 用途 | |---|---|---| | `ultralytics` | ≥ 8.4.0 | YOLO11n 模型 + 推理 | | `opencv-python` | ≥ 4.11.0 | 帧捕获、渲染、实时窗口 | | `numpy` | ≥ 1.26.0 | 密度计算所需的数组操作 | | `yt-dlp` | ≥ 2026.1.0 | 提取公开 YouTube 直播 URL |
*由 Hridam Biswas · KIIT University 构建*
标签:COCO数据集, OpenCV, Python, YOLO11n, 交通优化, 交通摄像头, 交通流量监测, 信号灯控制算法, 公共交通数据, 实时交通信号控制, 实时视频分析, 密度分析, 无后门, 无固定定时器, 智能交通, 机器学习模型部署, 深度学习推理, 目标检测, 自适应信号控制, 视频流处理, 车辆检测, 逆向工具