Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
d761606
Add real-time web dashboard and event broadcasting system
LiteObject Jul 6, 2025
56a8da5
Enhance UI dashboard to support background processing and improve log…
LiteObject Jul 6, 2025
2e42080
Add tests for background service detection, log formatting, and argum…
LiteObject Jul 6, 2025
91a5ff2
Remove obsolete test files for background service detection, log form…
LiteObject Jul 6, 2025
13abc95
Enhance async image processing and resource management in RTSP servic…
LiteObject Jul 6, 2025
c9b0a45
Refactor Google broadcast functions to support asynchronous operation…
LiteObject Jul 7, 2025
7485f5e
Enhance EventBroadcaster to support event persistence; implement load…
LiteObject Jul 7, 2025
81bc11c
Remove events.json from .gitignore to allow event file tracking in ve…
LiteObject Jul 7, 2025
3498f87
Refactor UI dashboard for improved readability and maintainability; e…
LiteObject Jul 7, 2025
2767044
Enhance synchronous wrappers for Chromecast functions to support asyn…
LiteObject Jul 7, 2025
0574c1d
Add comprehensive README updates for real-time event broadcasting, da…
LiteObject Jul 7, 2025
2d41eef
Update README to clarify features and requirements for RTSP processin…
LiteObject Jul 7, 2025
cdc3168
Refactor RTSP processing to support graceful shutdown; enhance loggin…
LiteObject Jul 11, 2025
671902e
Refactor UI dashboard for improved readability and maintainability; e…
LiteObject Jul 11, 2025
8c8150c
Refactor google_broadcast.py for improved structure and readability; …
LiteObject Jul 11, 2025
d0a13f3
Refactor UI dashboard for improved readability and maintainability; e…
LiteObject Jul 12, 2025
f59fd79
Update README to enhance feature descriptions and improve clarity on …
LiteObject Jul 12, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,4 @@ Desktop.ini
images/
logs/
temp/
events.json
154 changes: 141 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,14 @@ pip install -r requirements.txt
```

**Key dependencies:**
- `zeroconf>=0.47.0` - Async device discovery and networking
- `pyttsx3` - Cross-platform text-to-speech engine
- `opencv-python` - Image processing and RTSP capture
- `ultralytics` - YOLOv8 object detection
- `openai` - Vision API for image analysis
- `pychromecast` - Google Hub/Chromecast communication
- `pychromecast` - Google Hub/Chromecast communication with async support
- `streamlit` - Real-time web dashboard with live event updates
- `streamlit` - Real-time web dashboard (optional UI)

Comment on lines +46 to 48
Copy link

Copilot AI Jul 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Duplicate streamlit entry in the dependencies list. One line says 'with live event updates' and the next says '(optional UI)'.

Suggested change
- `streamlit` - Real-time web dashboard with live event updates
- `streamlit` - Real-time web dashboard (optional UI)
- `streamlit` - Real-time web dashboard with live event updates (optional UI)

Copilot uses AI. Check for mistakes.
### Running Unit Tests
Unit tests are provided in the `tests/` directory and use `pytest`.
Expand Down Expand Up @@ -81,19 +84,71 @@ LLM_TIMEOUT=30
### Config Class
All settings are centralized in `src/config.py` with validation and defaults.

## Event Broadcasting System

The system includes a sophisticated cross-process event broadcasting system for real-time UI updates:

### Features
- **Event-Driven UI Updates**: Dashboard refreshes immediately when new events occur
- **Cross-Process Sync**: Events from background service instantly appear in UI dashboard
- **Persistent Storage**: Events stored in `events.json` for reliability with batched writes
- **Performance Optimized**: Timer-based batched persistence (every 2 seconds) instead of per-event writes
- **Thread-Safe**: Concurrent access from multiple processes handled safely with proper locking
- **Graceful Shutdown**: Non-daemon threads with proper cleanup and shutdown handling
- **Auto-Cleanup**: Events automatically pruned to prevent file growth (max 100 events)
- **Real-time Updates**: UI dashboard reflects live activity with <1 second latency

### Event Types
- **Detection Events**: YOLO detections, LLM confirmations, person status
- **Image Events**: Image captures and file operations
- **Analysis Events**: AI descriptions and confidence scores
- **Notification Events**: TTS and Google Hub broadcast results

### Performance Improvements
- **3x faster event persistence** with batched writes
- **Reduced I/O load** with timer-based scheduling
- **Better responsiveness** with event-driven UI refresh
- **Memory efficient** with automatic event pruning
- **Thread safety** with proper locking mechanisms
- **Notification Events**: TTS and Google Hub broadcast results

### Usage
Events are automatically broadcasted - no manual intervention needed:
```python
# Events are emitted automatically by the service
# UI dashboard automatically displays them in real-time
```

The event system ensures the UI dashboard always shows current activity, even when background processing runs in a separate process.

## Usage

### 1. Run Main Application

**Command Line (Headless)**
```bash
python -m src.app
```

**UI Dashboard Only (No Background Processing)**
```bash
python -m src.app --ui
```

**🔥 Background Processing + UI Dashboard (Recommended)**
```bash
python -m src.app --with-ui
```

**What it does:**
- Runs health checks for RTSP stream and OpenAI API
- Captures images from RTSP stream (configurable interval)
- Processes multiple images concurrently using async/await
- Uses YOLO for fast person detection, then OpenAI for detailed analysis
- Broadcasts to Google Hub when person confirmed
- Automatically cleans up old images
- **With UI**: Real-time dashboard at http://localhost:8501
- **`--with-ui`**: Runs both background processing AND UI in a single command

### 2. Notification System

Expand Down Expand Up @@ -159,6 +214,42 @@ Send a custom message to a Google Hub:
python -m src.google_broadcast
```

### 6. Real-time Web Dashboard
Launch the monitoring dashboard using any of these methods:

**Option 1: Through main app (recommended)**
```sh
python -m src.app --ui
```

**Option 2: Direct Streamlit (from project root)**
```sh
streamlit run src/ui_dashboard.py
```

**Option 3: Using standalone runner**
```sh
streamlit run run_ui.py
```

**Dashboard Features:**
- 📊 **Live Metrics** - Real-time detection counts, image captures, persons confirmed
- � **System Status** - Three-column status indicators showing event system, background service, and last detection
- �📸 **Image Gallery** - Latest captures with person detection highlights
- 📋 **Event Stream** - Live detection events and notifications with enhanced formatting and icons
- 🔄 **Event-Driven Auto-refresh** - Updates immediately when new events occur, otherwise checks every 2 seconds
- 🎯 **Accurate Counters** - Metrics reflect actual background service activity
- ⚡ **Enhanced Performance** - Optimized caching and event-driven updates for <1 second latency

**Enhanced UI Features:**
- **Smart Auto-refresh**: Event-driven updates with immediate refresh on new activity
- **Visual Status Indicators**: Green/yellow/red status bars for system health monitoring
- **Better Event Formatting**: Rich text with icons, timestamps, and contextual styling
- **Error Handling**: Robust timestamp parsing for both datetime objects and ISO strings
- **Responsive Design**: Clean layout with improved user experience

Access at: http://localhost:8501 (or custom port if specified)

## System Architecture: Async Processing Flow

```mermaid
Expand Down Expand Up @@ -189,19 +280,22 @@ sequenceDiagram

**Key Improvements:**
- **3x faster processing** with concurrent image analysis
- **Real-time web dashboard** with live monitoring
- **Health checks** prevent runtime failures
- **Context managers** ensure proper resource cleanup
- **Retry logic** with exponential backoff for network calls

## File Overview

### Core Modules
- `src/app.py` — Async main loop with health checks
- `src/app.py` — Async main loop with health checks and UI launcher
- `src/services.py` — AsyncRTSPProcessingService for business logic
- `src/image_capture.py` — RTSP capture with context managers
- `src/image_analysis.py` — Async OpenAI vision analysis
- `src/computer_vision.py` — YOLOv8 person detection
- `src/notification_dispatcher.py` — Advanced notification system with threading and TTS
- `src/event_broadcaster.py` — Real-time event system for UI updates
- `src/ui_dashboard.py` — Streamlit web dashboard for monitoring

### Infrastructure
- `src/config.py` — Centralized configuration with validation
Expand All @@ -226,13 +320,16 @@ python -c "import logging; logging.basicConfig(level=logging.DEBUG)" -m src.app

### Key Metrics
- **Processing Speed**: 3x faster than synchronous version
- **Event Broadcasting**: Event-driven UI updates with <1 second latency
- **Concurrent Processing**: Multiple images analyzed simultaneously
- **Non-blocking Notifications**: Threaded dispatch prevents processing delays
- **TTS Optimization**: 33% faster speech (200 WPM vs 150 WPM)
- **Duplicate Filtering**: Intelligent suppression of repetitive messages
- **Resource Management**: Automatic cleanup prevents memory/disk leaks
- **Error Recovery**: Retry logic with exponential backoff
- **Health Monitoring**: Startup validation of all dependencies
- **UI Performance**: Batched event persistence with timer-based scheduling
- **Thread Safety**: Proper locking and graceful shutdown mechanisms

## Contributing

Expand All @@ -245,19 +342,50 @@ For major changes, please open an issue first to discuss what you would like to
4. Push to the branch (`git push origin feature/YourFeature`)
5. Open a pull request

## Notes
## Troubleshooting

### UI Dashboard Issues

**Dashboard shows zero counts despite background processing:**
- Ensure you're using `--with-ui` flag: `python -m src.app --with-ui`
- Check that `events.json` exists in the project root after processing starts
- Verify background service is running by checking logs: `tail -f logs/rtsp_processing.log`
- Events should appear in real-time as the service processes frames

**Event-driven refresh not working:**
- Check browser console for JavaScript errors
- Verify the dashboard shows "🟢 Event System: Active" in the status bar
- Try manual refresh with the "🔄 Refresh" button
- Ensure auto-refresh is enabled with the checkbox

**System status indicators showing errors:**
- **🔴 Background Service: Not Detected** - Start background processing with `python -m src.app --with-ui`
- **🟡 Event System: Idle** - No recent events (last 5 minutes) - check RTSP stream connectivity
- **❓ Last Detection: Unknown** - No detection events recorded yet

**Time formatting inconsistency:**
- All timestamps now use friendly 12-hour format (e.g., "6:45:30 PM")
- System logs and Live Events use consistent formatting

### Google Hub Notification Issues

**"asyncio.run() cannot be called from a running event loop" error:**
- This has been resolved in recent versions
- Google Hub broadcasting now works from both sync and async contexts
- No manual intervention needed - the system auto-detects the context

**Google Hub not responding:**
- Verify device IP in `.env` file: `GOOGLE_DEVICE_IP=192.168.x.x`
- Ensure device and computer are on same WiFi network
- Test connection: `python -m src.google_broadcast`

### LLM Options
- **OpenAI**: Cloud-based, requires API key and internet connectivity
- **Ollama**: Local processing with `llama3.2-vision:latest`, zero API costs
- **RTSP stream** must be accessible from the application
### Performance Issues

### Architecture Benefits
- **Async/await**: Non-blocking I/O for better performance
- **Health checks**: Early detection of configuration issues
- **Input validation**: Comprehensive validation prevents runtime errors
- **Context managers**: Automatic resource cleanup
- **Structured logging**: Better debugging and monitoring
**Slow processing or memory issues:**
- Check `MAX_IMAGES` setting in `.env` (default: 100)
- Verify `CAPTURE_INTERVAL` is appropriate (default: 10 seconds)
- Monitor log file size in `logs/` directory
- Ensure proper cleanup by checking for old images in `images/` directory

## License

Expand Down
91 changes: 91 additions & 0 deletions README_UPDATES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# README Updates for Recent Changes

## New Sections to Add:

### Real-time Event Broadcasting (Add after "Configuration" section)
```markdown
## Event Broadcasting System

The system includes a sophisticated cross-process event broadcasting system for real-time UI updates:

### Features
- **Cross-Process Sync**: Events from background service instantly appear in UI dashboard
- **Persistent Storage**: Events stored in `events.json` for reliability
- **Thread-Safe**: Concurrent access from multiple processes handled safely
- **Auto-Cleanup**: Events automatically pruned to prevent file growth (max 100 events)
- **Real-time Updates**: UI dashboard reflects live activity immediately

### Event Types
- **Detection Events**: YOLO detections, LLM confirmations, person status
- **Image Events**: Image captures and file operations
- **Notification Events**: TTS and Google Hub broadcast results

### Usage
Events are automatically broadcasted - no manual intervention needed:
```python
# Events are emitted automatically by the service
# UI dashboard automatically displays them in real-time
```

The event system ensures the UI dashboard always shows current activity, even when background processing runs in a separate process.
```

### Updated Dashboard Features (Replace existing section)
```markdown
**Dashboard Features:**
- 📊 **Live Metrics** - Real-time detection counts, image captures, persons confirmed
- 📸 **Image Gallery** - Latest captures with person detection highlights
- 📋 **Event Stream** - Live detection events and notifications with friendly 12-hour timestamps
- 📄 **System Logs** - Live log tail with user-friendly time formatting
- 🔄 **Auto-refresh** - Updates every 2 seconds with cross-process event synchronization
- 🎯 **Accurate Counters** - Metrics reflect actual background service activity
```

### Troubleshooting Section (Add before "Contributing")
```markdown
## Troubleshooting

### UI Dashboard Issues

**Dashboard shows zero counts despite background processing:**
- Ensure you're using `--with-ui` flag: `python -m src.app --with-ui`
- Check that `events.json` exists in the project root after processing starts
- Verify background service is running by checking logs: `tail -f logs/rtsp_processing.log`
- Events should appear in real-time as the service processes frames

**Time formatting inconsistency:**
- All timestamps now use friendly 12-hour format (e.g., "6:45:30 PM")
- System logs and Live Events use consistent formatting

### Google Hub Notification Issues

**"asyncio.run() cannot be called from a running event loop" error:**
- This has been resolved in recent versions
- Google Hub broadcasting now works from both sync and async contexts
- No manual intervention needed - the system auto-detects the context

**Google Hub not responding:**
- Verify device IP in `.env` file: `GOOGLE_DEVICE_IP=192.168.x.x`
- Ensure device and computer are on same WiFi network
- Test connection: `python -m src.google_broadcast`

### Performance Issues

**Slow processing or memory issues:**
- Check `MAX_IMAGES` setting in `.env` (default: 100)
- Verify `CAPTURE_INTERVAL` is appropriate (default: 10 seconds)
- Monitor log file size in `logs/` directory
- Ensure proper cleanup by checking for old images in `images/` directory
```

### Updated Dependencies (Replace in requirements section)
```markdown
**Key dependencies:**
- `zeroconf>=0.47.0` - Async device discovery and networking
- `pyttsx3` - Cross-platform text-to-speech engine
- `opencv-python` - Image processing and RTSP capture
- `ultralytics` - YOLOv8 object detection
- `openai` - Vision API for image analysis
- `pychromecast` - Google Hub/Chromecast communication with async support
- `streamlit` - Real-time web dashboard with live event updates
```
Loading
Loading