Skip to content

Refactor: Modularize EcoWatch AI into package structure - #1

Open
Super-cloud0 wants to merge 1 commit into
mainfrom
claude/fire-watch-rewrite-slkm4f
Open

Super-cloud0 wants to merge 1 commit into
mainfrom
claude/fire-watch-rewrite-slkm4f

Conversation

@Super-cloud0

Copy link
Copy Markdown
Owner

Summary

This is a major refactoring that transforms EcoWatch AI from a monolithic 873-line Streamlit script into a well-organized Python package with clear separation of concerns. The application logic is now split into focused modules, enabling better testability, reusability, and maintainability.

Key Changes

Package Structure

  • Created ecowatch/ package with modular components:
    • config.py — Settings, detection parameters, environment loading
    • detector.py — YOLO model wrapper (unified interface for classify/detect)
    • hazards.py — Hazard catalog with metadata (icons, colors, severity)
    • tracker.py — Temporal smoothing and hysteresis (frames → incidents)
    • sources.py — Frame sources (webcam, RTSP, file, screen capture)
    • storage.py — SQLite-based incident journal (replaces JSON)
    • recorder.py — Video clip recording with pre-roll buffer
    • notifier.py — Async Telegram notifications
    • overlay.py — Frame annotation with Cyrillic support
    • monitor.py — Background monitoring thread orchestrating the pipeline
    • ui.py — Streamlit web interface (extracted from main.py)
    • cli.py — Console mode for headless operation

Entry Points

  • main.py — Now a minimal entry point that imports and runs the UI
  • ecowatch/__main__.py — Enables python -m ecowatch for CLI mode
  • run_app.bat — Updated Windows launcher

Testing & Quality

  • Added tests/ directory with unit tests for core modules:
    • test_detector.py — YOLO result parsing
    • test_tracker.py — Decision logic and hysteresis
    • test_storage.py — Incident persistence
    • test_notifier.py — Telegram formatting
    • test_recorder.py — Clip recording with pre-roll
    • test_config_sources.py — Configuration and source parsing
  • Added .github/workflows/tests.yml for CI
  • Added pyproject.toml with ruff linting config
  • Added requirements-dev.txt for development dependencies

Configuration & Deployment

  • .env.example — Template for secrets (token, chat ID, coordinates)
  • .streamlit/config.toml — Streamlit theme and server settings
  • Updated .gitignore to exclude data, cache, and secrets
  • Updated requirements.txt with pinned versions

Documentation

  • Expanded README.md with feature highlights, installation, and usage examples
  • Updated train.py with clearer docstring and refactored dataset validation

Notable Implementation Details

  • Background Monitoring: Monitor runs in a daemon thread, keeping the Streamlit UI responsive and allowing monitoring to persist across page reloads
  • Incident Tracking: Temporal smoothing via exponential moving average + hysteresis prevents false alarms from single frames
  • Async Notifications: Telegram sends happen in a background queue, never blocking video processing
  • Pre-roll Recording: ClipRecorder buffers frames before incident detection, capturing context before the alert
  • Unified Model Support: Detector wrapper handles both classify and detect YOLO models with a common interface
  • Legacy Migration: EventStore can import old JSON detection history into SQLite on first run
  • Cyrillic Text: Overlay uses PIL for proper Cyrillic rendering on video frames

Migration Notes

  • Old detection_history.json is automatically imported into data/incidents.db on first run
  • Telegram credentials now read from .env file or environment variables (never hardcoded)
  • Console mode (python -m ecowatch 0) enables 24/7 headless operation on servers

https://claude.ai/code/session_01PKYmgFdxkcjGu11gXfcToj

- Split the 870-line main.py into the ecowatch/ package: detector,
  tracker, monitor, sources, storage, notifier, recorder, overlay, ui, cli
- Remove the hardcoded Telegram bot token; secrets now come from .env
- Temporal smoothing (EMA) plus hysteresis turns frames into incidents
  with onset and end, escalating from smoke to fire within one incident
- Monitoring runs in a background thread; the UI polls it from a
  Streamlit fragment, so switching tabs or reloading the page doesn't
  stop it
- Live cameras use a latest-frame grabber that reconnects on drops
- SQLite incident journal with snapshots and clips; the legacy JSON
  history is imported automatically
- Telegram sending moved to a background queue with cooldown
- Incident clips include pre-roll frames from before the alert
- Headless CLI mode: python -m ecowatch <source>
- Map shows real camera coordinates instead of random points
- train.py: argparse, classify and detect modes, backs up the old model
- Tests (pytest), ruff config and a GitHub Actions workflow

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PKYmgFdxkcjGu11gXfcToj
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants