This document details the internal working of the three services, their data structures, inputs/outputs, and timing mechanisms.
File: src/edge_service/main.py
Captures video frames, detects people using Computer Vision (YOLOv8), and pushes structured data to the backend.
- Arguments:
<location_id>(e.g., "canteen") and<camera_source>(ID0or RTSP URL). - Source: Laptop Webcam or Wi-Fi IP Camera.
- Frame Rate: Dependent on hardware (typically 30 FPS), but inference runs on every frame in the loop.
- Capture: Reads a frame from the video source.
- Inference: Runs YOLOv8 Nano model to detect objects of class
person(Class ID0). - Logic: Counts bounding boxes. Determines density level based on thresholds:
Low: 0 - 5 peopleMedium: 6 - 15 peopleHigh: > 15 people
The system leverages Computer Vision to perform real-time analysis.
- Model: YOLOv8 Nano (
yolov8n)- Why Nano? It is the smallest and fastest version, optimized for edge devices and laptops with limited CPU/GPU resources.
- Task: Object Detection.
- Target Class:
Person(Class ID:0). The model is pre-trained on the COCO dataset but filtered to ignore all other objects (cars, bags, etc.). - Confidence Threshold:
0.4(40%). Only detections with >40% confidence are counted to reduce false positives. - Inference: Performed locally on the device using the
ultralyticsPython library.
Sent via POST request to http://localhost:8000/update-crowd-data.
{
"location_id": "canteen",
"timestamp": "2026-01-04T15:30:00.123456",
"count": 12,
"density_level": "Medium",
"trend": "Stable"
}- Send Interval:
2.0 seconds(Configurable viaSEND_INTERVAL). - Execution: Runs as a continuous foreground process (Python script).
File: src/backend_service/main.py
Acts as the central state manager. Receives updates from multiple Edge services and serves the latest state to the Frontend. Persists data to crowd_data.json.
- Input: JSON payload from Edge Service (see above).
- Action: Updates the in-memory dictionary
crowd_dataand saves it tocrowd_data.json. - Response:
200 OK{"status": "success", "received": ...}
- Input: None.
- Action: Retrieves the current value of
crowd_data. - Output: JSON Response (Dictionary of locations).
{
"canteen": {
"location_id": "canteen",
"timestamp": "...",
"count": 12,
"density_level": "Medium",
"trend": "Stable"
},
"library": {
"location_id": "library",
"timestamp": "...",
"count": 5,
"density_level": "Low",
"trend": "Stable"
}
}- Fallback: If no data has been received yet, returns default "Unknown" states for configured locations.
- Server: Uvicorn (ASGI Server).
- Persistence: Writes to disk on every update (simple JSON dump).
File: src/frontend/index.html
A static HTML/JS page that visualizes the crowd data for multiple locations.
- Fetches data from
http://localhost:8000/current-status.
- Polling: JavaScript
setIntervaltriggersfetchData()function. - DOM Update: Iterates through the received JSON object and updates the corresponding HTML cards for each
location_id.
- Density Badge: Green (Low), Yellow (Medium), Red (High).
- Count: Numeric value.
- Trend: Text indicator.
- Last Updated: Timestamp converted to local time.
- Refresh Rate: Staggered updates (every 2-5 seconds) to avoid UI flickering.
- Hosting: Served via Python's
http.serveron port8081.
- T=0s: Camera captures frame.
- T+0.1s: Edge Service detects 5 people.
- T+0.1s: Edge Service checks timer. If > 2s since last send, constructs JSON.
- T+0.2s: Edge Service sends
POSTto Backend. - T+0.25s: Backend updates memory:
count=5. - T+1.0s: Frontend Timer fires (every 2s).
- T+1.1s: Frontend requests
GET /current-status. - T+1.15s: Frontend receives JSON and updates UI to Green/Low.
| Service | Technology | Port | Key Config |
|---|---|---|---|
| Edge | Python / OpenCV / YOLO | N/A | SEND_INTERVAL = 2.0 |
| Backend | FastAPI / Uvicorn | 8000 |
In-Memory Storage |
| Frontend | HTML5 / JS | 8081 |
setInterval(..., 2000) |