File size: 6,795 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
# Person Detection Feature Guide

## Overview

The NETRA system now tracks and stores person detection data in the live camera feed. This feature automatically remembers person detection counts across camera sessions using SQLite database.

## What's New

### 1. **Live Camera Statistics Display**

Two new statistics fields have been added to the live camera monitoring interface:

- **πŸ‘€ Person Detected**: Total count of unique persons detected in the current session
- **πŸ‘οΈ Person Present**: Real-time indicator showing if a person is currently in the frame (βœ… Yes / ❌ No)

### 2. **Bounding Box Visualization** (v2.0)

Green bounding boxes now appear around detected persons:
- Real-time green rectangles around each person
- Confidence score displayed above box
- Coordinates stored in database with detection
- Enables precise person location tracking

### 3. **SQLite Database Storage**

A new `PersonDetection` model stores all person detection records with:

- User ID (tracks which user made the detection)
- Person count (number of people detected)
- Current presence status
- Detection confidence score
- **Bounding box coordinates** (x1, y1, x2, y2)
- Timestamp of detection
- Session date (for daily grouping)

### 4. **Data Persistence**

Person detection data is automatically saved to the database and persists across camera sessions, allowing you to:

- View detection history for the current day
- Track person detection patterns
- Review confidence scores and bounding boxes
- Analyze detection timeline with precise locations

## Database Schema

### PersonDetection Table

```sql

CREATE TABLE person_detection (

    id INTEGER PRIMARY KEY,

    user_id INTEGER NOT NULL FOREIGN KEY,

    person_count INTEGER DEFAULT 0,

    is_present BOOLEAN DEFAULT FALSE,

    confidence FLOAT DEFAULT 0.0,

    detection_details TEXT,

    detected_at DATETIME DEFAULT CURRENT_TIMESTAMP,

    session_date DATE DEFAULT CURRENT_DATE

);

```

## API Endpoints

### 1. Save Person Detection

**Endpoint**: `/api/save-person-detection`
**Method**: `POST`
**Authentication**: Required (session must exist)

**Request Body**:

```json

{

  "person_count": 2,

  "is_present": true,

  "confidence": 0.95,

  "detection_details": "{...additional data...}"

}

```

**Response**:

```json

{

  "success": true,

  "message": "Person detection saved",

  "detection_id": 123

}

```

### 2. Get Person Detection History

**Endpoint**: `/api/person-detection-history`
**Method**: `GET`
**Authentication**: Required (session must exist)

**Response**:

```json

{

    "success": true,

    "data": {

        "total_detections": 5,

        "currently_present": true,

        "latest_confidence": 0.95,

        "detection_count": 12,

        "detection_records": [

            {

                "id": 1,

                "person_count": 1,

                "is_present": true,

                "confidence": 0.92,

                "detected_at": "2026-05-02T14:30:45.123456"

            },

            ...

        ]

    }

}

```

## How It Works

### Initialization

1. When camera starts, the system loads previous person detection data
2. `loadPersonDetectionHistory()` fetches today's detection records
3. Stats are populated from the database

### Detection Recording

1. As the camera processes frames, person detections are identified
2. When a person is detected, `recordPersonDetection()` is called
3. The detection count is incremented
4. Data is saved to database via `savePersonDetection()`

### Real-time Display

- **Person Detected**: Shows cumulative count of detections (increases with each new person)
- **Person Present**: Shows current status (updates in real-time based on latest detection)

## Integration with Existing Models

The person detection feature integrates with:

- **YOLO Object Detection**: Detects 'person' class objects
- **Pose Detection**: Uses person keypoints for presence validation
- **Weapon Detection**: Tracks persons carrying weapons

## JavaScript Functions

### Core Functions

- `loadPersonDetectionHistory()` - Loads previous detection data
- `savePersonDetection(count, isPresent, confidence)` - Saves new detection
- `updatePersonDetectionUI()` - Updates displayed statistics
- `recordPersonDetection(detectionData)` - Processes detection results
- `initializePersonDetection()` - Initializes system on camera start

### Usage Example

```javascript

// Load history on camera start

await initializePersonDetection();



// Record a person detection

recordPersonDetection({

  objects: [

    { label: "person", confidence: 0.95 },

    { label: "person", confidence: 0.92 },

  ],

});



// Update stats

updatePersonDetectionUI();

```

## Feature Benefits

βœ… **Data Persistence**: Detection data survives browser refresh
βœ… **Historical Tracking**: See all detections for the current day
βœ… **Real-time Updates**: Live person presence indicator
βœ… **Confidence Scoring**: Know how confident each detection is
βœ… **User-Specific**: Each user has their own detection history
βœ… **Date-Based Organization**: Data grouped by session date

## Future Enhancements

Possible improvements for this feature:

- Export detection history to CSV/PDF reports
- Visualize person detection trends over time
- Alert notifications when persons are detected
- Confidence threshold settings
- Multi-person tracking with ID persistence
- Heat mapping of person locations in frame

## Troubleshooting

### Data Not Saving

- Ensure user is logged in (authentication required)
- Check browser console for API errors
- Verify database connection in Flask app

### History Not Loading

- Data only loads for current day (session_date = today)

- Check if database has records for current user

- Clear browser cache and reload



### Stats Not Updating



- Verify `recordPersonDetection()` is being called

- Check that detection objects have 'person' label

- Ensure YOLO or other person detection model is selected



## Database Maintenance



### View All Person Detections



```python

from app import db, PersonDetection

detections = PersonDetection.query.all()

for d in detections:

    print(f"User {d.user_id}: {d.person_count} people at {d.detected_at}")
```



### Clear Old Records



```python

from datetime import datetime, timedelta

from app import db, PersonDetection



# Delete records older than 30 days

cutoff = datetime.utcnow() - timedelta(days=30)

PersonDetection.query.filter(PersonDetection.detected_at < cutoff).delete()

db.session.commit()

```

---

**Last Updated**: May 2, 2026
**Feature Version**: 1.0
**Status**: Production Ready