harshit-033/trafic_analysis

GitHub: harshit-033/trafic_analysis

SmartFlow 是一个基于计算机视觉和 YOLO 目标检测的实时智能交通监控系统,通过动态计算自适应绿灯时长替代固定信号配时。

Stars: 0 | Forks: 0

# SmartFlow — 智能交通监控系统 🚦

SmartFlow Dashboard

**SmartFlow** 是一个由计算机视觉驱动的实时智能交通信号系统。它在实时视频流上使用 YOLO 目标检测来统计和分类车辆,根据实际交通负载动态计算自适应绿灯时长,并通过专业的毛玻璃化仪表盘展示所有信息。该系统具有完全的自我修复能力——边缘脚本会进行自我监控,在出现故障时自动重启,并在摄像头或节点离线时发送 SMS 警报。 ## 🌟 当前状态 | 组件 | 状态 | |---|---| | YOLO 推理引擎 | ✅ 正常运行 | | FastAPI 后端 + MongoDB | ✅ 正常运行 | | 自适应信号配时算法 | ✅ 正常运行 | | React / Vite 仪表盘 | ✅ 正常运行 | | 心跳 + 看门狗边缘服务 | ✅ 正常运行 | | 通过 Twilio 发送 SMS 警报 | ✅ 正常运行 | | 每车道 ROI 检测 | 🔜 计划中 | | 多路口协同控制 | 🔜 计划中 | ## ✨ 核心功能 - **🤖 实时边缘 AI** — YOLOv8/YOLO11 在每一帧视频或 RTSP 摄像头画面中检测并分类车辆(小汽车、公交车、卡车、自行车、行人)。 - **🚦 自适应信号配时** — 算法根据车辆的尺寸/影响程度分配权重,计算每个入口方向的最佳绿灯时长,取代了固定的定时间隔计时器。 - **📊 实时仪表盘** — 深色主题、毛玻璃化的 React Web 应用,包含动态环形图、面积图和柱状图——所有数据每 5 秒轮询一次实时后端数据。 - **💓 边缘健康监控** — 心跳脚本每 10 秒向后端报告一次 CPU、内存、FPS 和摄像头状态。 - **🐕 看门狗恢复** — 自动检测崩溃的推理或心跳进程并重启它们(在发出警报前最多尝试 5 次)。 - **📲 SMS 警报** — 集成 Twilio,一旦路口离线或检测到严重故障,即刻发送 SMS。 ## 🏗️ 系统架构 ``` ┌──────────────────────────────────────────────────────────┐ │ Edge Device (Camera) │ │ ┌────────────────┐ ┌────────────┐ ┌─────────────┐ │ │ │ inference3.py │ │heartbeat.py│ │ watchdog.py │ │ │ │ (YOLO + OpenCV)│ │(Health HB) │ │(Auto-Restart│ │ │ └───────┬────────┘ └─────┬──────┘ └─────┬───────┘ │ └───────────┼──────────────────┼────────────────┼─────────┘ │ POST /detections │ POST /heartbeat │ POST /alert ▼ ▼ ▼ ┌──────────────────────────────────────────────────────────┐ │ FastAPI Backend (src/backend/main.py) │ │ • Stores detections, heartbeats, alerts in MongoDB │ │ • Runs adaptive signal timing algorithm │ │ • Serves REST API on http://localhost:8001 │ └──────────────────────────────┬───────────────────────────┘ │ Polls every 5s ▼ ┌──────────────────────────────────────────────────────────┐ │ React/Vite Dashboard (frontend/) │ │ • http://localhost:5173 │ │ • Shows live counts, timing, health, alerts, charts │ └──────────────────────────────────────────────────────────┘ ``` ## 🚀 快速入门 ### 前置条件 请确保您的机器上已安装以下工具: | 工具 | 版本 | 用途 | |---|---|---| | Git | 任意 | 克隆代码仓库 | | Python | 3.10+ | 后端与边缘脚本 | | Node.js + npm | 18+ | 前端仪表盘 | | Docker + Docker Compose | 任意 | 运行 MongoDB | ### 第 1 步 — 克隆代码仓库 ``` git clone https://github.com/your-username/SmartFlow.git cd SmartFlow ``` ### 第 2 步 — 配置环境变量 复制示例 env 文件并填入您的值: ``` cp .env.example .env ``` 使用您的配置编辑 `.env`: ``` MONGO_URI=mongodb://admin:admin123@localhost:27017/ TWILIO_SID=your_twilio_sid TWILIO_AUTH=your_twilio_auth_token TWILIO_FROM=+1xxxxxxxxxx TWILIO_TO=+91xxxxxxxxxx ``` ### 第 3 步 — 安装 Python 依赖 ``` # 创建并激活 virtual environment python -m venv venv # Windows venv\Scripts\activate # macOS / Linux source venv/bin/activate # 安装所有依赖 pip install -r requirements.txt ``` ### 第 4 步 — 安装前端依赖 ``` cd frontend npm install cd .. ``` ## 🖥️ 运行项目 在各自独立的终端中启动每个组件。在运行 Python 脚本之前,请务必激活虚拟环境 (`venv\Scripts\activate`)。 ### 终端 1 — 数据库 ``` docker compose -f infra/docker-compose.yml up -d ``` ### 终端 2 — 后端 API ``` python -m uvicorn src.backend.main:app --port 8001 --reload ``` API 文档可在 [http://localhost:8001/docs](http://localhost:8001/docs) 查看 ### 终端 3 — 前端仪表盘 ``` cd frontend npm run dev ``` 👉 在浏览器中打开 [http://localhost:5173](http://localhost:5173)。 ### 终端 4 — AI 推理 ``` python src/edge/inference3.py ``` ### 终端 5 — 心跳监控 ``` python src/edge/heartbeat.py ``` ### 终端 6 — 看门狗 ``` python src/edge/watchdog.py ``` ## 📂 项目结构 ``` SmartFlow/ ├── src/ │ ├── backend/ # FastAPI server, API routes, timing algorithm │ │ ├── main.py # Main application entry point │ │ └── sms_utils.py # Twilio SMS integration │ ├── edge/ # Hardware-side runtime scripts │ │ ├── inference3.py # YOLO inference + frame processing │ │ ├── heartbeat.py # System health reporter │ │ └── watchdog.py # Process monitor + auto-restart │ └── dashboard/ # (Legacy) Original Streamlit dashboard ├── frontend/ # React + Vite dashboard (NEW) │ └── src/ │ ├── App.jsx # Main app with routing & live data polling │ ├── api.js # Backend API client (fetch wrappers) │ └── index.css # Glassmorphic dark theme styles ├── infra/ │ └── docker-compose.yml # MongoDB container setup ├── models/ # Place your trained .pt model weights here ├── data/ # Sample video files for the inference engine ├── ai/ # Training experiments and datasets ├── .env.example # Environment variable template ├── requirements.txt # Python dependencies └── Readme.md ``` ## 🔑 运行所需的核心文件(预训练模型) 如果模型已经训练完成,运行整个系统您**只需具备以下文件**: ``` ✅ src/backend/ ✅ src/edge/ ✅ frontend/ ✅ infra/docker-compose.yml ✅ models/best.pt ← your trained YOLO model weights ✅ data/sample2.mp4 ← camera feed video ✅ .env ✅ requirements.txt ``` `ai/` 和 `experiments/` 目录仅在进行研究、重新训练或扩展模型时需要。 ## 🛠️ 技术栈 | 层级 | 技术 | |---|---| | 目标检测 | YOLO11 / YOLOv8 (Ultralytics) | | 视频处理 | OpenCV | | 后端 | FastAPI + Uvicorn | | 数据库 | MongoDB 6 (通过 Docker) | | 仪表盘 | React 18 + Vite | | 图表 | Recharts | | 图标 | Lucide React | | 警报 | Twilio SMS | | 监控 | psutil |
标签:AV绕过, FastAPI, React, Syscalls, 智慧交通, 智能交通信号灯, 目标检测, 自适应信号控制, 计算机视觉, 请求拦截, 边缘AI, 逆向工具