summaryrefslogtreecommitdiff
path: root/README.md
blob: 4a0f610ed88d3cbc65cfc91705985a133aec2cc0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# Garage Door Monitoring Project

This project uses a deep learning model to monitor the state of a garage door (open or closed) via an MJPEG camera stream.
It sends email alerts when the door is open and continues sending reminders at a configurable interval until the door is closed.

## Directory Structure

```
garage-door/
├── data/                       # Dataset folder
│   ├── open/                   # All "open" images
│   └── closed/                 # All "closed" images
├── app/                        # Application folder
│   ├── model/                  # Trained model
│   ├── scripts/                # Monitoring script and config
│   └── requirements.txt        # Dependencies for monitoring
│   └── Dockerfile              # Dockerfile for the container
├── train.py                    # Training script
├── build.py                    # Build script
├── start.sh                    # Start container script
├── requirements.txt            # Dependencies
└── README.md                   # Documentation
```

---

## Prerequisites

- Python 3.10+
- Docker
- SMTP server for email alerts

---

## Usage

### 1. Train the Model

#### Dataset Setup:
Place your dataset in the `data/` folder with the following structure:
```
data/
├── open/    # Images of the door open
├── closed/  # Images of the door closed
```

#### Train the Model:
Run the training script to preprocess the data and train the model:
```bash
pip install -r requirements.txt
python train.py
```

The trained model will be saved to `app/model/garage_door_classifier.h5`.

---

### 2. Configure `config.json`

Before building the monitoring container, configure the `app/scripts/config.json` file with your desired settings.

Below is an example configuration:

```json
{
    "stream_url": "http://your-video-server:8080/video",
    "alert_interval": 300,
    "retry_limit": 20,
    "retry_delay": 3,
    "stats_interval": 60,
    "min_open_frames": 3,
    "frame_window": 10,
    "max_buffer_size_mb": 2,
    "smtp_server": "your-smtp-server",
    "smtp_port": 25,
    "email_from": "[email protected]",
    "email_to": "[email protected];[email protected]",
    "subject": "Garage Door Alert",
    "body": "The garage door is OPEN."
}
```

#### Configuration Parameters:
- **`stream_url`**: URL of the live MJPEG camera stream.
- **`alert_interval`**: Interval (in seconds) between repeated alerts while the door is open.
- **`retry_limit`**: Maximum number of retries for fetching the camera stream.
- **`retry_delay`**: Delay (in seconds) between retry attempts.
- **`stats_interval`**: Interval (in seconds) for logging system stats.
- **`min_open_frames`**: Minimum consecutive frames to confirm the door is open.
- **`frame_window`**: Number of frames analyzed per cycle.
- **`max_buffer_size_mb`**: Maximum memory buffer for frames before cleanup.
- **`smtp_server`**: SMTP server address for sending emails.
- **`smtp_port`**: SMTP server port.
- **`email_from`**: Sender's email address.
- **`email_to`**: Recipients' email addresses separated by semicolons.
- **`subject`**: Email subject line.
- **`body`**: Email body text.

---

### 3. Build the Docker Container

Run the build script to ensure the model is trained and the container is created:
```bash
python build.py
```

---

### 4. Run the Monitoring App

Run the monitoring system using the `start.sh` script:
```bash
./start.sh
```

---

### 5. Monitor the Logs

You can check the logs to verify the monitoring system is working:
```bash
docker logs -f garagedoor
```

---

### 6. Stop the Monitoring System

To stop the monitoring system:
```bash
docker stop garagedoor
```

---

## Notes

- The `monitor.py` script streams the live MJPEG stream URL and analyzes frames using the trained model.
- Alerts are sent immediately upon detecting an open door and repeated at configurable intervals while the door remains open.
- System stats, such as retry attempts and buffer usage, are logged periodically.

---

## FAQ

1. **How do I test the system locally?**
   You can directly run the monitoring script:
   ```bash
   python app/scripts/monitor.py
   ```

2. **What happens if the camera stream is unreachable?**
   The system retries fetching the stream according to `retry_limit` and `retry_delay`. If the stream remains unreachable, it logs the issue.

3. **Can I add more email recipients?**
   Yes, update the `email_to` field in `config.json` with additional addresses separated by semicolons.

4. **How do I retrain the model?**
   Delete the existing model file at `app/model/garage_door_classifier.h5` and rerun:
   ```bash
   python train.py
   ```

---

## License
This project is licensed under the MIT License.