openscopelabs commited on
Commit
8653aff
·
1 Parent(s): abc7cb8

fix(conflict): readme conflict

Browse files
Files changed (3) hide show
  1. .dockerignore +44 -0
  2. Dockerfile +71 -0
  3. README.md +164 -5
.dockerignore ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ .git
2
+ .gitignore
3
+ .venv
4
+ __pycache__
5
+ *.py[cod]
6
+ *.egg-info
7
+ .python-version
8
+
9
+ # Editor / OS
10
+ .DS_Store
11
+ .idea
12
+ .vscode
13
+ *.swp
14
+
15
+ # Local secrets — should be set via container env / HF Secrets instead
16
+ .env
17
+ .env.local
18
+
19
+ # Local dev tooling
20
+ .claude
21
+ .playwright-mcp
22
+ playwright-report
23
+ test-results
24
+
25
+ # Pre-built model artefacts — Dockerfile regenerates these via
26
+ # scripts.setup_yolo_world during build
27
+ models/*.pt
28
+ models/*.onnx
29
+ models/*.engine
30
+
31
+ # Dev screenshots / one-off captures
32
+ cakra_frame_*.jpeg
33
+ cakra_after_scale_*.jpeg
34
+ cakra_25fps_*.jpeg
35
+ cakra_final_*.jpeg
36
+ cakra_embed_*.jpeg
37
+ *.screenshot.png
38
+
39
+ # Logs
40
+ *.log
41
+ /tmp/cakra-*
42
+
43
+ # Generated client artefacts that get rebuilt by scripts
44
+ session/
Dockerfile ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # syntax=docker/dockerfile:1.6
2
+ #
3
+ # Cakra CCTV YOLO Analytics — single-stage image suited for both
4
+ # Hugging Face Spaces (sdk: docker, app_port: 7860) and a generic
5
+ # VPS. ffmpeg is the only external runtime dependency beyond the
6
+ # Python packages in requirements.txt.
7
+
8
+ FROM python:3.12-slim
9
+
10
+ # --- system deps -----------------------------------------------------
11
+ # - ffmpeg : HLS demux on the input + libx264 encode on the output
12
+ # - libgl1 : opencv-python-headless still pulls libGL.so for some
13
+ # ops on Debian slim, even though we use the headless
14
+ # build
15
+ # - libglib2.0 : opencv + supervision transitive dep
16
+ # - curl : healthcheck convenience
17
+ RUN apt-get update \
18
+ && apt-get install -y --no-install-recommends \
19
+ ffmpeg \
20
+ libgl1 \
21
+ libglib2.0-0 \
22
+ curl \
23
+ ca-certificates \
24
+ && rm -rf /var/lib/apt/lists/*
25
+
26
+ WORKDIR /app
27
+
28
+ # --- python deps -----------------------------------------------------
29
+ # Copy requirements first to maximise layer caching on code-only edits.
30
+ COPY requirements.txt ./
31
+ RUN pip install --no-cache-dir --upgrade pip \
32
+ && pip install --no-cache-dir -r requirements.txt
33
+
34
+ # --- app source ------------------------------------------------------
35
+ COPY app ./app
36
+ COPY scripts ./scripts
37
+ COPY docs ./docs
38
+ COPY run.sh ./
39
+
40
+ # Ensure run.sh is executable (git mode bits are sometimes lost during
41
+ # Hugging Face Spaces sync).
42
+ RUN chmod +x run.sh
43
+
44
+ # Bake the YOLO-World model with our 17 Indonesian-traffic classes so
45
+ # the first /connect doesn't have to wait on a 70-450 MB download. Uses
46
+ # the size declared by YOLO_BUILD_SIZE (default = "m"). Skip with
47
+ # `--build-arg YOLO_BUILD_SIZE=skip` if you want to mount a model in
48
+ # at runtime instead.
49
+ ARG YOLO_BUILD_SIZE=m
50
+ RUN if [ "$YOLO_BUILD_SIZE" != "skip" ]; then \
51
+ PYTHONPATH=. python -m scripts.setup_yolo_world "$YOLO_BUILD_SIZE"; \
52
+ fi
53
+
54
+ # HLS segments are written here at runtime; ephemeral disk is fine.
55
+ RUN mkdir -p /tmp/cakra-hls
56
+
57
+ # --- runtime ---------------------------------------------------------
58
+ # Hugging Face Spaces exposes ${PORT} (default 7860). On a generic VPS
59
+ # you can override the same variable.
60
+ ENV PORT=7860 \
61
+ HOST=0.0.0.0 \
62
+ HLS_OUTPUT_DIR=/tmp/cakra-hls \
63
+ HLS_ENCODER=libx264 \
64
+ PYTHONUNBUFFERED=1
65
+
66
+ EXPOSE 7860
67
+
68
+ HEALTHCHECK --interval=30s --timeout=5s --start-period=20s --retries=3 \
69
+ CMD curl -fs http://localhost:${PORT}/health || exit 1
70
+
71
+ CMD ["./run.sh"]
README.md CHANGED
@@ -1,11 +1,170 @@
1
  ---
2
- title: Cctv Object Detection
3
- emoji: 🏃
4
- colorFrom: yellow
5
- colorTo: indigo
6
  sdk: docker
 
7
  pinned: false
8
  license: other
 
9
  ---
10
 
11
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
+ title: Cakra Intelligence — CCTV YOLO Analytics
3
+ emoji: "🚦"
4
+ colorFrom: red
5
+ colorTo: gray
6
  sdk: docker
7
+ app_port: 7860
8
  pinned: false
9
  license: other
10
+ short_description: Realtime CCTV analytics — YOLO + ByteTrack + bbox-baked HLS
11
  ---
12
 
13
+ # Cakra Intelligence — CCTV YOLO Analytics
14
+
15
+ Realtime multi-camera CCTV analytics that ingests upstream HLS, runs
16
+ YOLO + ByteTrack on every stream, re-encodes an annotated HLS playlist
17
+ with bounding boxes baked into the pixels, and pushes geo-tagged
18
+ detection events over WebSocket.
19
+
20
+ The output HLS is **embed-ready** — any client drops
21
+ `<video src=playlist.m3u8>` (or hls.js) and gets a smooth 25 fps
22
+ stream with the YOLO boxes already in the video. No canvas overlay,
23
+ no WebSocket coupling required.
24
+
25
+ ## Architecture (per camera)
26
+
27
+ ```
28
+ upstream HLS ──► StreamReader (paced cap.read at HLS_FPS)
29
+ │
30
+ ▼
31
+ ring buffer (drop-oldest, tagged with capture_ts)
32
+ │
33
+ ▼
34
+ pacer thread (drift-free 25 fps + 2 ms busy-wait tail)
35
+ │ pop ──► time-align detections ──► annotate
36
+ ▼
37
+ HLSEncoder writer queue (non-blocking)
38
+ │
39
+ ▼
40
+ ffmpeg -c:v libx264|videotoolbox -fps_mode cfr -r 25
41
+ │
42
+ ▼
43
+ .ts segments + playlist.m3u8
44
+
45
+ inference thread (own pace, TARGET_INFER_FPS)
46
+ ↳ reads StreamReader.read_with_ts()
47
+ ↳ runs YOLO + ByteTrack
48
+ ↳ publishes (capture_ts, detections) to a short history deque
49
+ ↳ never blocks anything
50
+ ```
51
+
52
+ Four independent threads, lock-protected slots between them. Reader
53
+ paces the source at native fps so a 50-frame HLS-segment burst doesn't
54
+ drain ffmpeg's demuxer in one tight read; the ring stays small and no
55
+ source frames are dropped. The pacer pulls frames at exactly HLS_FPS
56
+ using an absolute monotonic schedule with a sub-millisecond busy-wait
57
+ window so `time.sleep()` overshoots can't drift the output rate. The
58
+ encoder runs in true CFR mode so segment durations land on exact
59
+ second boundaries.
60
+
61
+ ## Endpoints
62
+
63
+ | Method | Path | Notes |
64
+ | ------ | --------------------------------- | ----------------------------------------------------------- |
65
+ | `GET` | `/cameras` | paginated catalog from `cctv_streams` (lean projection) |
66
+ | `GET` | `/cameras/{id}` | detail + embed.hls + metrics.websocket URLs |
67
+ | `POST` | `/cameras/{id}/connect` | spawn YOLO worker (idempotent) |
68
+ | `POST` | `/cameras/{id}/disconnect` | stop worker |
69
+ | `GET` | `/cameras/{id}/hls/playlist.m3u8` | annotated HLS playlist (bbox baked in) |
70
+ | `GET` | `/cameras/{id}/hls/{seg}.ts` | MPEG-TS segments referenced by the playlist |
71
+ | `WS` | `/cameras/{id}/metrics/ws` | realtime metrics + geo-tagged detections + coverage polygon |
72
+
73
+ Full machine-readable spec in `docs/apispec.json`; importable Postman
74
+ v2.1 collection in `docs/Cakra Intelligence - YOLO Rest.postman_collection.json`.
75
+
76
+ ## Database
77
+
78
+ One persistent table: `cctv_session_metrics`. Single row per camera
79
+ that's UPSERTed on every inference tick. `started_at` is set on first
80
+ insert and never touched again so lifetime aggregates survive
81
+ reconnects.
82
+
83
+ ```sql
84
+ cctv_session_metrics (
85
+ camera_id PRIMARY KEY,
86
+ location, started_at, last_captured_at,
87
+ samples_count, sum_vehicles, sum_people,
88
+ max_vehicles, max_people,
89
+ last_vehicles, last_people, last_traffic, last_crowd
90
+ )
91
+ ```
92
+
93
+ The catalog itself (`cctv_streams`) is owned externally — this service
94
+ only reads it.
95
+
96
+ ## Local development
97
+
98
+ Requires Python 3.12, ffmpeg, and a Postgres instance that hosts the
99
+ `cctv_streams` catalog.
100
+
101
+ ```bash
102
+ .venv/bin/python -m pip install -r requirements.txt
103
+
104
+ # Bake the YOLO-World model with 17 Indonesian-traffic classes:
105
+ # s = fastest / lowest accuracy
106
+ # m = recommended balance
107
+ # l = better accuracy / slower
108
+ # x = best accuracy / very slow
109
+ PYTHONPATH=. .venv/bin/python -m scripts.setup_yolo_world m
110
+
111
+ cp .env.example .env
112
+ # edit DATABASE_URL etc., then:
113
+ ./run.sh
114
+ ```
115
+
116
+ Then open `docs/viewer.html` (the full debug viewer with side panel +
117
+ map) or `docs/embed.html` (minimal `<video>` + hls.js, useful as a
118
+ production embed reference).
119
+
120
+ ## Quick deploy — Hugging Face Spaces
121
+
122
+ This repo is ready for a Hugging Face **Docker space**. The YAML
123
+ frontmatter at the top of this README is what the Spaces platform
124
+ reads.
125
+
126
+ 1. Create a new Space at <https://huggingface.co/new-space>:
127
+ - **SDK:** Docker
128
+ - **Hardware:** CPU basic works (4 vCPU recommended); upgrade if
129
+ you need YOLO-World at the _x_ size or many cameras
130
+ 2. Push this repository to the Space's git remote.
131
+ 3. In **Settings → Variables and secrets**, add:
132
+
133
+ | Key | Required | Example |
134
+ | ----------------------- | -------- | -------------------------------------------- |
135
+ | `DATABASE_URL` | yes | `postgresql://user:pass@host:5432/db` |
136
+ | `STREAMS_STATUS_FILTER` | no | `online` |
137
+ | `MAX_STREAMS` | no | `8` (Spaces is shared infra) |
138
+ | `INFERENCE_SIZE` | no | `640` (lower = faster on CPU) |
139
+ | `TARGET_INFER_FPS` | no | `4` |
140
+ | `HLS_FPS` | no | `25` (match source) |
141
+ | `HLS_ENCODER` | no | `libx264` (the only one available on Spaces) |
142
+
143
+ Anything from `.env.example` is honoured.
144
+
145
+ 4. The Space will build the Docker image — first build takes ~5 min
146
+ because it downloads YOLO-World base weights and bakes the
147
+ 17-class model. Subsequent builds reuse the layer.
148
+ 5. Once the Space is running, the API is at
149
+ `https://<user>-<space>.hf.space/api/v1/cameras` and the WebSocket
150
+ at `wss://<user>-<space>.hf.space/api/v1/cameras/{id}/metrics/ws`.
151
+
152
+ Notes on Spaces constraints:
153
+
154
+ - **No GPU on the free tier** — inference falls back to CPU. Use
155
+ smaller model sizes (`s` or `m`) and lower `INFERENCE_SIZE` to keep
156
+ proc fps usable.
157
+ - **Ephemeral disk** — HLS segments live in `/tmp/cakra-hls` and are
158
+ recycled at the playlist boundary, so no persistence is needed.
159
+ - **No host-process control** — the entrypoint binds to `${PORT}`
160
+ (default 7860), which Spaces routes for you.
161
+ - **Postgres isn't provided** — point `DATABASE_URL` at any managed
162
+ Postgres (Supabase, Neon, AWS RDS, etc.).
163
+
164
+ ## Configuration reference
165
+
166
+ See [.env.example](.env.example) for every tunable knob with comments.
167
+
168
+ ## License
169
+
170
+ Proprietary — © Openscope Labs / Cakra Intelligence.