File size: 6,716 Bytes
4ed7d03
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Migration Guide - Old to New Structure

## Overview

This guide explains how to update your code imports from the old structure to the new reorganized structure.

## Old Structure vs New Structure

### Model Imports

#### BEFORE (Old Structure)
```python
from NETRA.violence_detector import ViolenceDetector
from NETRA.yolo_detector import YOLODetector
from NETRA.weapon_person_detector import WeaponPersonDetector
from NETRA.anomaly_detector import AnomalyDetector
from NETRA.video_capture import VideoCapture
from pose_detection import PoseDetection
```

#### AFTER (New Structure)
```python
from src.detectors import (
    ViolenceDetector,
    YOLODetector,
    WeaponPersonDetector,
    PoseDetection,
    AnomalyDetector,
)
from src.pipeline import VideoCapture
```

**Or use the main src module:**
```python
from src import (
    ViolenceDetector,
    YOLODetector,
    WeaponPersonDetector,
    PoseDetection,
    AnomalyDetector,
    VideoCapture,
)
```

### Configuration Imports

#### BEFORE (Hardcoded paths in code)
```python
# Scattered throughout app.py
violence_model_path = Path(__file__).parent.parent / "NETRA" / "model" / "violence_model.h5"
yolo_model_candidates = [
    Path(__file__).parent.parent / "ai_models" / "object_detection" / "yolov8n.pt",
    ...
]
```

#### AFTER (Centralized in config)
```python
from config import MODEL_PATHS, get_model_path, DETECTION_THRESHOLDS, PROCESSING_PARAMS

# Get a specific model path
violence_model = get_model_path('violence')

# Get threshold
yolo_threshold = DETECTION_THRESHOLDS['yolo']  # 0.25

# Get processing parameters
frame_skip = PROCESSING_PARAMS['frame_skip']  # 10
```

### Settings Imports

#### BEFORE (Config spread throughout app.py)
```python
app.config['SECRET_KEY'] = 'netra-surveillance-secret-key-2024'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///netra.db'
app.config['MAX_CONTENT_LENGTH'] = 500 * 1024 * 1024
```

#### AFTER (Centralized in config)
```python
from config import SECRET_KEY, DATABASE_URI, MAX_CONTENT_LENGTH

app.config['SECRET_KEY'] = SECRET_KEY
app.config['SQLALCHEMY_DATABASE_URI'] = DATABASE_URI
app.config['MAX_CONTENT_LENGTH'] = MAX_CONTENT_LENGTH
```

## Step-by-Step webapp/app.py Update

### Step 1: Update Imports (Top of file)

**REPLACE:**
```python
from NETRA.violence_detector import ViolenceDetector
from NETRA.yolo_detector import YOLODetector
from NETRA.weapon_person_detector import WeaponPersonDetector
from NETRA.anomaly_detector import AnomalyDetector
from NETRA.video_capture import VideoCapture
from pose_detection import PoseDetection
```

**WITH:**
```python
from src import (
    ViolenceDetector,
    YOLODetector,
    WeaponPersonDetector,
    PoseDetection,
    AnomalyDetector,
    VideoCapture,
)
from config import (
    MODEL_PATHS,
    get_model_path,
    DETECTION_THRESHOLDS,
    PROCESSING_PARAMS,
    SECRET_KEY,
    DATABASE_URI,
    MAX_CONTENT_LENGTH,
    UPLOAD_FOLDER,
    PROCESSED_FOLDER,
    INSTANCE_FOLDER,
)
```

### Step 2: Update sys.path (No longer needed!)

**REMOVE THIS:**
```python
sys.path.append(str(Path(__file__).parent.parent))
sys.path.append(str(Path(__file__).parent.parent / "NETRA"))
sys.path.append(str(Path(__file__).parent.parent / "detection logic"))
```

**INSTEAD:** Just add project root if needed:
```python
sys.path.insert(0, str(Path(__file__).parent.parent))
```

### Step 3: Update Configuration

**REPLACE:**
```python
app.config['SECRET_KEY'] = 'netra-surveillance-secret-key-2024'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///netra.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['UPLOAD_FOLDER'] = str(Path(__file__).parent / 'uploads')
app.config['PROCESSED_FOLDER'] = str(Path(app.config['UPLOAD_FOLDER']) / 'processed')
app.config['MAX_CONTENT_LENGTH'] = 500 * 1024 * 1024

os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
os.makedirs(app.config['PROCESSED_FOLDER'], exist_ok=True)
```

**WITH:**
```python
app.config['SECRET_KEY'] = SECRET_KEY
app.config['SQLALCHEMY_DATABASE_URI'] = DATABASE_URI
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['UPLOAD_FOLDER'] = str(UPLOAD_FOLDER)
app.config['PROCESSED_FOLDER'] = str(PROCESSED_FOLDER)
app.config['MAX_CONTENT_LENGTH'] = MAX_CONTENT_LENGTH

# Folders already created by config/settings.py
```

### Step 4: Update Model Loading in ModelManager

**REPLACE hardcoded paths like:**
```python
violence_model_path = Path(__file__).parent.parent / "NETRA" / "model" / "violence_model.h5"
yolo_model_candidates = [...]
```

**WITH:**
```python
violence_model_path = get_model_path('violence')
yolo_model_path = get_model_path('yolo')
weapon_gun_path = get_model_path('weapon')
pose_model_path = get_model_path('pose')
anomaly_model_path = get_model_path('anomaly')
```

### Step 5: Update Detection Thresholds

**REPLACE:**
```python
detections = yolo_model.detect(frame, conf_threshold=0.25)
violence_result = violence_model.detect_violence(frame, confidence_threshold=0.6)
```

**WITH:**
```python
detections = yolo_model.detect(frame, conf_threshold=DETECTION_THRESHOLDS['yolo'])
violence_result = violence_model.detect_violence(frame, confidence_threshold=DETECTION_THRESHOLDS['violence'])
```

### Step 6: Update Video Processing Parameters

**REPLACE:**
```python
if frame_count % 10 == 0:  # Magic number
    results = video_processor.process_frame(frame)
```

**WITH:**
```python
if frame_count % PROCESSING_PARAMS['frame_skip'] == 0:
    results = video_processor.process_frame(frame)
```

## Benefits of Migration**Cleaner Imports** - All imports from organized src/ and config/
✅ **Centralized Configuration** - Single source of truth for paths and thresholds
✅ **Easier Maintenance** - Update model paths once in config/model_config.py
**Better Scalability** - Easy to add new models or modules
**Less Code Duplication** - No scattered hardcoded paths

## Testing After Migration

After updating app.py, verify:

```bash
# Test imports work
python -c "from src import YOLODetector; print('✓ Imports OK')"

# Test config loads
python -c "from config import get_all_available_models; print(get_all_available_models())"

# Test app starts
python webapp/app.py
```

## Summary of Changes

| What Changed | Was | Now |
|--------------|-----|-----|
| **Detector location** | `NETRA/` | `src/detectors/` |
| **Pipeline location** | `NETRA/` | `src/pipeline/` |
| **Model paths** | Hardcoded | `config/model_config.py` |
| **Settings** | Scattered | `config/settings.py` |
| **Models organization** | `ai_models/mixed` | `models/type/` |
| **Documentation** | `webapp/ARCHITECTURE.md` | `docs/` |

## If You Need Help

Refer to `docs/PROJECT_STRUCTURE.md` for detailed explanation of each component.