openscopelabs's picture
fix(conflict): readme conflict
8653aff
|
Raw History Blame Contribute Delete
7.11 kB
---
title: Cakra Intelligence — CCTV YOLO Analytics
emoji: "🚦"
colorFrom: red
colorTo: gray
sdk: docker
app_port: 7860
pinned: false
license: other
short_description: Realtime CCTV analytics — YOLO + ByteTrack + bbox-baked HLS
---
# Cakra Intelligence — CCTV YOLO Analytics
Realtime multi-camera CCTV analytics that ingests upstream HLS, runs
YOLO + ByteTrack on every stream, re-encodes an annotated HLS playlist
with bounding boxes baked into the pixels, and pushes geo-tagged
detection events over WebSocket.
The output HLS is **embed-ready** — any client drops
`<video src=playlist.m3u8>` (or hls.js) and gets a smooth 25 fps
stream with the YOLO boxes already in the video. No canvas overlay,
no WebSocket coupling required.
## Architecture (per camera)
```
upstream HLS ──► StreamReader (paced cap.read at HLS_FPS)
│
▼
ring buffer (drop-oldest, tagged with capture_ts)
│
▼
pacer thread (drift-free 25 fps + 2 ms busy-wait tail)
│ pop ──► time-align detections ──► annotate
▼
HLSEncoder writer queue (non-blocking)
│
▼
ffmpeg -c:v libx264|videotoolbox -fps_mode cfr -r 25
│
▼
.ts segments + playlist.m3u8
inference thread (own pace, TARGET_INFER_FPS)
↳ reads StreamReader.read_with_ts()
↳ runs YOLO + ByteTrack
↳ publishes (capture_ts, detections) to a short history deque
↳ never blocks anything
```
Four independent threads, lock-protected slots between them. Reader
paces the source at native fps so a 50-frame HLS-segment burst doesn't
drain ffmpeg's demuxer in one tight read; the ring stays small and no
source frames are dropped. The pacer pulls frames at exactly HLS_FPS
using an absolute monotonic schedule with a sub-millisecond busy-wait
window so `time.sleep()` overshoots can't drift the output rate. The
encoder runs in true CFR mode so segment durations land on exact
second boundaries.
## Endpoints
| Method | Path | Notes |
| ------ | --------------------------------- | ----------------------------------------------------------- |
| `GET` | `/cameras` | paginated catalog from `cctv_streams` (lean projection) |
| `GET` | `/cameras/{id}` | detail + embed.hls + metrics.websocket URLs |
| `POST` | `/cameras/{id}/connect` | spawn YOLO worker (idempotent) |
| `POST` | `/cameras/{id}/disconnect` | stop worker |
| `GET` | `/cameras/{id}/hls/playlist.m3u8` | annotated HLS playlist (bbox baked in) |
| `GET` | `/cameras/{id}/hls/{seg}.ts` | MPEG-TS segments referenced by the playlist |
| `WS` | `/cameras/{id}/metrics/ws` | realtime metrics + geo-tagged detections + coverage polygon |
Full machine-readable spec in `docs/apispec.json`; importable Postman
v2.1 collection in `docs/Cakra Intelligence - YOLO Rest.postman_collection.json`.
## Database
One persistent table: `cctv_session_metrics`. Single row per camera
that's UPSERTed on every inference tick. `started_at` is set on first
insert and never touched again so lifetime aggregates survive
reconnects.
```sql
cctv_session_metrics (
camera_id PRIMARY KEY,
location, started_at, last_captured_at,
samples_count, sum_vehicles, sum_people,
max_vehicles, max_people,
last_vehicles, last_people, last_traffic, last_crowd
)
```
The catalog itself (`cctv_streams`) is owned externally — this service
only reads it.
## Local development
Requires Python 3.12, ffmpeg, and a Postgres instance that hosts the
`cctv_streams` catalog.
```bash
.venv/bin/python -m pip install -r requirements.txt
# Bake the YOLO-World model with 17 Indonesian-traffic classes:
# s = fastest / lowest accuracy
# m = recommended balance
# l = better accuracy / slower
# x = best accuracy / very slow
PYTHONPATH=. .venv/bin/python -m scripts.setup_yolo_world m
cp .env.example .env
# edit DATABASE_URL etc., then:
./run.sh
```
Then open `docs/viewer.html` (the full debug viewer with side panel +
map) or `docs/embed.html` (minimal `<video>` + hls.js, useful as a
production embed reference).
## Quick deploy — Hugging Face Spaces
This repo is ready for a Hugging Face **Docker space**. The YAML
frontmatter at the top of this README is what the Spaces platform
reads.
1. Create a new Space at <https://huggingface.co/new-space>:
- **SDK:** Docker
- **Hardware:** CPU basic works (4 vCPU recommended); upgrade if
you need YOLO-World at the _x_ size or many cameras
2. Push this repository to the Space's git remote.
3. In **Settings → Variables and secrets**, add:
| Key | Required | Example |
| ----------------------- | -------- | -------------------------------------------- |
| `DATABASE_URL` | yes | `postgresql://user:pass@host:5432/db` |
| `STREAMS_STATUS_FILTER` | no | `online` |
| `MAX_STREAMS` | no | `8` (Spaces is shared infra) |
| `INFERENCE_SIZE` | no | `640` (lower = faster on CPU) |
| `TARGET_INFER_FPS` | no | `4` |
| `HLS_FPS` | no | `25` (match source) |
| `HLS_ENCODER` | no | `libx264` (the only one available on Spaces) |
Anything from `.env.example` is honoured.
4. The Space will build the Docker image — first build takes ~5 min
because it downloads YOLO-World base weights and bakes the
17-class model. Subsequent builds reuse the layer.
5. Once the Space is running, the API is at
`https://<user>-<space>.hf.space/api/v1/cameras` and the WebSocket
at `wss://<user>-<space>.hf.space/api/v1/cameras/{id}/metrics/ws`.
Notes on Spaces constraints:
- **No GPU on the free tier** — inference falls back to CPU. Use
smaller model sizes (`s` or `m`) and lower `INFERENCE_SIZE` to keep
proc fps usable.
- **Ephemeral disk** — HLS segments live in `/tmp/cakra-hls` and are
recycled at the playlist boundary, so no persistence is needed.
- **No host-process control** — the entrypoint binds to `${PORT}`
(default 7860), which Spaces routes for you.
- **Postgres isn't provided** — point `DATABASE_URL` at any managed
Postgres (Supabase, Neon, AWS RDS, etc.).
## Configuration reference
See [.env.example](.env.example) for every tunable knob with comments.
## License
Proprietary — © Openscope Labs / Cakra Intelligence.