File size: 7,161 Bytes
cb3c674
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# Anomaly Detection Model Integration Guide

## Overview

The `best.bin` anomaly detection model has been successfully integrated into the NETRA webapp. This model detects unusual patterns and behaviors in video feeds in real-time.

## Installation & Setup

### 1. Prerequisites

Ensure your environment has:

```bash

# Install required packages

pip install -r requirements.txt

```

Key dependencies:

- PyTorch 2.0+ (for model loading)
- TensorFlow 2.15+ (fallback)
- OpenCV with contrib modules
- NumPy

### 2. Model Location

Place the `best.bin` model file in one of these locations:

- `ai_models/best.bin` βœ… (Recommended)
- `ai_models/anomaly_detection/best.bin`
- `NETRA/model/anomaly_detection.bin`

The webapp will auto-detect and load the model from the first available location.

## How It Works

### Detection Flow

1. **Frame Buffering**: Collects 16 consecutive frames for temporal analysis
2. **Preprocessing**: Resizes to 224x224, normalizes to 0-1 range
3. **Inference**: Runs model on buffered frames
4. **Scoring**: Generates anomaly confidence score
5. **Alert Generation**: Creates alert if score exceeds threshold (default: 0.5)

### Confidence Levels

- **SAFE** (0.0-0.5): Normal behavior, no alert
- **LOW RISK** (0.5-0.6): Minor anomaly detected
- **MEDIUM RISK** (0.6-0.8): Moderate anomaly detected
- **HIGH RISK** (0.8-1.0): Significant anomaly detected

## Configuration

### Adjusting Detection Sensitivity

Edit `webapp/app.py` in the `load_models()` method:

```python

# Line ~155 - Modify threshold (0.0-1.0)

anomaly_model_path = next((p for p in anomaly_model_candidates if p.exists()), None)

if anomaly_model_path:

    # Lower threshold = more sensitive, more false positives

    # Higher threshold = less sensitive, fewer false positives

    self.models['anomaly'] = AnomalyDetector(

        model_path=str(anomaly_model_path),

        anomaly_threshold=0.5  # ← Adjust here (default: 0.5)

    )

```

### Using GPU Acceleration

Enable GPU in `NETRA/anomaly_detector.py`:

```python

# Change device parameter

anomaly_model = AnomalyDetector(

    model_path=str(anomaly_model_path),

    device='cuda'  # or 'cpu' for CPU-only

)

```

## Monitoring & Debugging

### Check if Model is Loaded

The Flask app startup will show:

```

[OK] Anomaly detection model loaded from: ai_models/best.bin

```

If not loaded:

```

[WARN] Anomaly detection model not loaded: missing model file

```

### Real-time Monitoring

Open the webapp dashboard to see:

- Anomaly detection alerts in real-time
- Confidence scores per frame
- Alert history with timestamps

### Logs

Check terminal output for:

- Model loading status
- Inference errors (if any)
- Frame processing details

## API Reference

### AnomalyDetection Result Object

```python

{

    'is_anomaly': bool,           # True if anomaly detected

    'confidence': float,          # 0.0-1.0 confidence score

    'anomaly_score': float,       # Raw model output

    'alert_level': str,           # SAFE/LOW_RISK/MEDIUM_RISK/HIGH_RISK

    'description': str            # Human-readable message

}

```

### Alert Format (from process_frame)



```python

{

    'type': 'anomaly_detected',
    'severity': str,              # HIGH_RISK/MEDIUM_RISK/LOW_RISK

    'confidence': float,          # 0.0-1.0

    'message': str,               # Description

    'anomaly_score': float        # Raw score

}

```


## Testing the Model

### 1. Test with Live Camera

```python

# Start webapp

python webapp/app.py



# Navigate to http://localhost:5000/live-camera

# Watch for anomaly detection alerts

```

### 2. Test with Video File

```python

# Upload a video file in webapp

# Go to http://localhost:5000/video-analysis

# Analyze the video

# Check for anomaly detection results

```

### 3. Direct Python Testing

```python

from NETRA.anomaly_detector import AnomalyDetector

import cv2



# Load model

detector = AnomalyDetector('ai_models/best.bin')



# Process video

cap = cv2.VideoCapture('test_video.mp4')

while True:

    ret, frame = cap.read()

    if not ret:

        break



    result = detector.predict_frame(frame)

    if result:

        print(f"Anomaly: {result.is_anomaly}, Score: {result.anomaly_score}")



cap.release()

```

## Performance Optimization

### Reduce Processing Time

1. **Skip frames**: Process every Nth frame instead of all frames
2. **Lower resolution**: Use 168x168 instead of 224x224
3. **GPU usage**: Enable CUDA for faster inference
4. **Batch processing**: Process multiple frames simultaneously

Example modification in `process_frame()`:

```python

# Process every 2nd frame

if self.frame_index % 2 == 0:

    anomaly_result = anomaly_model.predict_frame(frame)

```

## Troubleshooting

| Issue             | Solution                                 |
| ----------------- | ---------------------------------------- |
| Model not loading | Check file path, ensure .bin file exists |
| Out of memory     | Use CPU mode or reduce buffer size       |
| Slow inference    | Enable GPU acceleration or skip frames   |
| False positives   | Increase anomaly_threshold (0.5 β†’ 0.7)   |

| Missed detections | Decrease anomaly_threshold (0.5 β†’ 0.3)   |

## File Structure

```

NETRA_Project/

β”œβ”€β”€ ai_models/

β”‚   β”œβ”€β”€ best.bin                    ← Anomaly model

β”‚   β”œβ”€β”€ object_detection/

β”‚   β”œβ”€β”€ pose_detection/

β”‚   └── wepan_detection/

β”œβ”€β”€ NETRA/

β”‚   β”œβ”€β”€ anomaly_detector.py         ← New detector class

β”‚   β”œβ”€β”€ violence_detector.py

β”‚   β”œβ”€β”€ yolo_detector.py

β”‚   └── weapon_person_detector.py

└── webapp/

    β”œβ”€β”€ app.py                      ← Updated with integration

    β”œβ”€β”€ requirements.txt            ← Updated with torch

    └── ...

```

## Integration Details

### What Changed

1. βœ… New `AnomalyDetector` class in NETRA module
2. βœ… Model loading in `ModelManager.load_models()`
3. βœ… Frame processing in `VideoProcessor.process_frame()`
4. βœ… State reset in `reset_session_state()`
5. βœ… PyTorch dependency added to requirements

### Backward Compatibility

- All existing detections continue to work
- Anomaly detection is additive (doesn't affect other models)
- Graceful fallback if model file is missing
- No breaking changes to API

## Support & Extensions

### Custom Anomaly Thresholds

You can modify the threshold per-session:

```python

anomaly_model = model_manager.get_model('anomaly')

if anomaly_model:

    anomaly_model.anomaly_threshold = 0.6  # Adjust sensitivity

```

### Custom Model Format

The AnomalyDetector supports:

- βœ… PyTorch (.bin, .pth)
- βœ… TensorFlow SavedModel format
- Can be extended for other formats

## References

- PyTorch Model Saving: https://pytorch.org/docs/stable/generated/torch.save.html
- TensorFlow SavedModel: https://www.tensorflow.org/guide/saved_model

- Anomaly Detection Concepts: https://en.wikipedia.org/wiki/Anomaly_detection