@kubohiroya/turbowarp-camera-calibration 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,452 @@
1
+ # TurboWarp-Camera-Calibration
2
+
3
+ [English](README.md) | [日本語](README.ja.md)
4
+
5
+ A TurboWarp extension that calibrates a camera shared through
6
+ [TurboWarp-Camera-Source](https://github.com/kubohiroya/turbowarp-camera-source)
7
+ and publishes the resulting intrinsic calibration profile.
8
+
9
+ **User guide:** [English](https://kubohiroya.github.io/turbowarp-camera-calibration/)
10
+
11
+ > [!NOTE]
12
+ > The calibration path ships behind a startup feature flag that defaults to OFF.
13
+ > See [Enabling calibration](#enabling-calibration).
14
+
15
+ ## What it does
16
+
17
+ - Runs a chessboard calibration session against a camera leased from Camera Source, at the camera's real capture resolution.
18
+ - Solves the camera intrinsic matrix and distortion coefficients, and reports the RMS reprojection error.
19
+ - Publishes an intrinsic calibration profile that AR, photogrammetry, and motion capture extensions can share.
20
+
21
+ Intrinsic calibration and external pose stay separate. A profile describes one
22
+ camera's optics; it never carries a world pose, and a pose that was not measured
23
+ is never filled in with identity.
24
+
25
+ ## Why calibration lives in its own extension
26
+
27
+ The OpenCV build required to detect a chessboard and solve `calibrateCamera` is
28
+ roughly 10 MB. TurboWarp extensions ship as a single standalone bundle, so a
29
+ dynamic `import()` does not split that weight out: it is inlined into the same
30
+ file. Keeping the solver here lets Camera Source stay a small, dependency-free
31
+ capability that every camera consumer can afford to load. A startup flag cannot
32
+ substitute for this split, because a flag gates execution, not bytes.
33
+
34
+ The responsibilities split as follows.
35
+
36
+ | Package | Owns |
37
+ |---|---|
38
+ | `@kubohiroya/turbowarp-camera-source` | Camera acquisition, lease sharing, preview, and the calibration profile contract (schema, validation, applicability) |
39
+ | `@kubohiroya/turbowarp-camera-calibration` | The calibration procedure: chessboard sampling, solving, and profile publication |
40
+
41
+ ## Documentation and block icon
42
+
43
+ This extension publishes its English user documentation with GitHub Pages.
44
+
45
+ - `docsURI` points to `https://kubohiroya.github.io/turbowarp-camera-calibration/`.
46
+ - `blockIconURI` is a self-contained chessboard SVG encoded as `data:image/svg+xml;base64,...`.
47
+
48
+ ## Requirements and safety
49
+
50
+ - TurboWarp Desktop, Web, and Packager.
51
+ - `@kubohiroya/turbowarp-camera-source` must be loaded first; this extension never opens a camera itself.
52
+ - A printed chessboard calibration target, flat and unglossy. The board arguments count **inner corners**, not printed squares: a 10×7 square board is `9` by `6`.
53
+ - Must run without the sandbox.
54
+
55
+ > [!IMPORTANT]
56
+ > This extension must run without the sandbox because it reaches the Camera
57
+ > Source capability through the TurboWarp runtime. Load unsandboxed extension
58
+ > code only from a source you trust.
59
+
60
+ Camera frames are read only to detect the chessboard. Nothing is uploaded, and a
61
+ profile that carries a pairing credential is rejected rather than stored.
62
+
63
+ ### Capture conditions for a usable result
64
+
65
+ | Condition | Why |
66
+ |---|---|
67
+ | At least 8 accepted samples, at most 40 | Fewer views leave the distortion terms under-determined |
68
+ | Vary the board angle by roughly 20°–45° across samples | Views that are all fronto-parallel cannot separate focal length from distance |
69
+ | Vary the distance, and fill different parts of the frame, including the corners | Distortion is strongest away from the image center |
70
+ | Keep the whole board inside the frame and in focus | A partially visible or blurred board is rejected as `board-not-found` or `sample-low-quality` |
71
+ | Do not change resolution, camera device, mirroring, focus, or zoom during a session | The session is fixed to the resolution it started with and rejects changed conditions |
72
+
73
+ Verify a finished calibration against images that were not part of the solve. A
74
+ low reprojection error on the calibration samples alone does not prove the
75
+ result generalizes.
76
+
77
+ ## Enabling calibration
78
+
79
+ The calibration path is fixed at startup and defaults to OFF, so loading this
80
+ extension changes nothing until it is switched on. With the flag OFF, only the
81
+ `camera calibration state` reporter is published and it answers `idle`; no
82
+ camera lease is taken and the OpenCV runtime is never initialized.
83
+
84
+ Set the override before the extension is registered:
85
+
86
+ ```js
87
+ globalThis.__TWCC_FEATURE_FLAGS__ = {cameraCalibrationV1: true};
88
+ ```
89
+
90
+ The flags are read once and frozen. Changing the value afterwards has no effect,
91
+ which keeps a running project from switching contracts mid-session.
92
+
93
+ ## Installation
94
+
95
+ ### Ready-to-use JavaScript
96
+
97
+ 1. Download [`dist/camera-calibration.js`](dist/camera-calibration.js?raw=1).
98
+ 2. Open **Extensions** in TurboWarp.
99
+ 3. Choose **Custom Extension** and load the file.
100
+ 4. Enable **Run extension without sandbox**.
101
+
102
+ The reviewed JavaScript build is committed to this repository, so users do not
103
+ need Node.js to install the extension. The build inlines OpenCV.js and is
104
+ therefore about 11 MB.
105
+
106
+ ### npm package
107
+
108
+ Install an exact version that you have reviewed:
109
+
110
+ ```bash
111
+ pnpm add --save-exact @kubohiroya/turbowarp-camera-calibration@0.1.0
112
+ ```
113
+
114
+ Load the standalone bundle from:
115
+
116
+ ```text
117
+ node_modules/@kubohiroya/turbowarp-camera-calibration/dist/camera-calibration.js
118
+ ```
119
+
120
+ A version-pinned CDN URL is:
121
+
122
+ ```text
123
+ https://cdn.jsdelivr.net/npm/@kubohiroya/turbowarp-camera-calibration@0.1.0/dist/camera-calibration.js
124
+ ```
125
+
126
+ ## Quick start
127
+
128
+ 1. Load Camera Source and start the shared camera you want to calibrate.
129
+ 2. Load this extension with the calibration flag enabled.
130
+ 3. Start a session, capture views of the board from different angles and distances, then solve.
131
+
132
+ ```text
133
+ start shared camera [default]
134
+ start camera [default] calibration [calibration-1] board [9] by [6] square [0.025] m max error [1.5] px
135
+ repeat until <camera [default] calibration sample count = 12>
136
+ add calibration sample for camera [default]
137
+ solve calibration for camera [default]
138
+ publish calibration profile for camera [default]
139
+ ```
140
+
141
+ `add calibration sample` reports why a view was refused, so a project can show
142
+ `camera [default] calibration error code` and ask the user to move the board.
143
+
144
+ ## Calibration profile
145
+
146
+ The solved profile is intrinsics only:
147
+
148
+ ```json
149
+ {
150
+ "schema": "camerasource/camera-intrinsics",
151
+ "version": 1,
152
+ "calibrationId": "calibration-1",
153
+ "cameraId": "camera-1",
154
+ "cameraModel": "pinhole",
155
+ "imageWidth": 800,
156
+ "imageHeight": 600,
157
+ "imageState": "raw",
158
+ "intrinsicMatrix": [700, 0, 400, 0, 700, 300, 0, 0, 1],
159
+ "distortionModel": "opencv-plumb-bob",
160
+ "distortionCoefficients": [0.01, -0.02, 0, 0, 0],
161
+ "quality": {"sampleCount": 8, "reprojectionErrorPx": 0.75},
162
+ "calibratedAt": "2026-09-13T12:00:00.000Z"
163
+ }
164
+ ```
165
+
166
+ - `intrinsicMatrix` is row-major `[fx, 0, cx, 0, fy, cy, 0, 0, 1]`, in pixels of `imageWidth` × `imageHeight`.
167
+ - `imageState` is `raw`: the coefficients still have to be applied. `undistorted` states that the image is already corrected and they must not be applied again.
168
+ - `distortionCoefficients` are in OpenCV order and their count must match `distortionModel`.
169
+ - `quality` is absent when the producer did not measure it.
170
+ - Preview mirroring is a display concern. Profile coordinates are always the unmirrored capture coordinates.
171
+
172
+ [`schemas/camera-intrinsics-v1.schema.json`](schemas/camera-intrinsics-v1.schema.json)
173
+ is the published schema. Camera Source owns this contract
174
+ ([kubohiroya/turbowarp-camera-source#14](https://github.com/kubohiroya/turbowarp-camera-source/issues/14));
175
+ until it ships, the schema is published from this repository, and `schema` and
176
+ the schema `$id` are the only fields that move when it does.
177
+
178
+ Profiles written by `@kubohiroya/turbowarp-realtime-motion-capture`
179
+ (`twrmc/camera-calibration` v1) are accepted on import. Their
180
+ `worldFromCameraMatrix` is dropped rather than reinterpreted, because a world
181
+ pose is not part of an intrinsic profile.
182
+
183
+ ## Block reference
184
+
185
+ The block reference is generated from
186
+ [`src/block-definitions.json`](src/block-definitions.json). Do not edit the
187
+ generated section manually.
188
+
189
+ <!-- BEGIN GENERATED BLOCKS -->
190
+
191
+ ### `start camera [CAMERA_ID] calibration [CALIBRATION_ID] board [COLUMNS] by [ROWS] square [SQUARE_METERS] m max error [MAX_ERROR_PX] px`
192
+
193
+ Leases one shared Camera Source camera and fixes its real capture resolution for a chessboard calibration session. The board is measured in inner corners, not printed squares.
194
+
195
+ | Property | Value |
196
+ |---|---|
197
+ | Type | Command |
198
+ | Opcode | `startCameraCalibration` |
199
+ | `CAMERA_ID` | String, default: `default` |
200
+ | `CALIBRATION_ID` | String, default: `calibration-1` |
201
+ | `COLUMNS` | Number, default: `9` |
202
+ | `ROWS` | Number, default: `6` |
203
+ | `SQUARE_METERS` | Number, default: `0.025` |
204
+ | `MAX_ERROR_PX` | Number, default: `1.5` |
205
+
206
+ ### `add calibration sample for camera [CAMERA_ID]`
207
+
208
+ Detects the complete board in the current shared frame and retains it when its quality and its novelty against the retained samples both pass.
209
+
210
+ | Property | Value |
211
+ |---|---|
212
+ | Type | Command |
213
+ | Opcode | `addCameraCalibrationSample` |
214
+ | `CAMERA_ID` | String, default: `default` |
215
+
216
+ ### `solve calibration for camera [CAMERA_ID]`
217
+
218
+ Solves the intrinsic matrix and distortion coefficients from at least eight accepted samples, then releases the camera lease. No external pose is produced.
219
+
220
+ | Property | Value |
221
+ |---|---|
222
+ | Type | Command |
223
+ | Opcode | `solveCameraCalibration` |
224
+ | `CAMERA_ID` | String, default: `default` |
225
+
226
+ ### `cancel calibration for camera [CAMERA_ID]`
227
+
228
+ Releases the camera lease and the retained samples while preserving the last validated profile.
229
+
230
+ | Property | Value |
231
+ |---|---|
232
+ | Type | Command |
233
+ | Opcode | `cancelCameraCalibration` |
234
+ | `CAMERA_ID` | String, default: `default` |
235
+
236
+ ### `cleanup calibration for camera [CAMERA_ID]`
237
+
238
+ Releases the session and also clears the in-memory calibration profile for that camera.
239
+
240
+ | Property | Value |
241
+ |---|---|
242
+ | Type | Command |
243
+ | Opcode | `cleanupCameraCalibration` |
244
+ | `CAMERA_ID` | String, default: `default` |
245
+
246
+ ### `publish calibration profile for camera [CAMERA_ID]`
247
+
248
+ Registers the solved profile with Camera Source, which owns the profile contract. Reports an explicit error when Camera Source is absent, speaks another contract version, or has no profile to publish.
249
+
250
+ | Property | Value |
251
+ |---|---|
252
+ | Type | Command |
253
+ | Opcode | `publishCameraCalibration` |
254
+ | `CAMERA_ID` | String, default: `default` |
255
+
256
+ ### `import calibration profile [JSON] for camera [CAMERA_ID]`
257
+
258
+ Imports a calibration profile after schema, credential, and applicability checks. Pre-migration profiles are accepted and their world pose is dropped rather than reinterpreted.
259
+
260
+ | Property | Value |
261
+ |---|---|
262
+ | Type | Command |
263
+ | Opcode | `importCameraCalibration` |
264
+ | `JSON` | String, default: `{}` |
265
+ | `CAMERA_ID` | String, default: `default` |
266
+
267
+ ### `calibration profile [JSON] valid for camera [CAMERA_ID]?`
268
+
269
+ Validates a profile and records why it failed, without replacing the stored profile.
270
+
271
+ | Property | Value |
272
+ |---|---|
273
+ | Type | Boolean |
274
+ | Opcode | `cameraCalibrationJsonValid` |
275
+ | `JSON` | String, default: `{}` |
276
+ | `CAMERA_ID` | String, default: `default` |
277
+
278
+ ### `camera [CAMERA_ID] calibration ready?`
279
+
280
+ Reports whether a fixed-resolution session for that camera can accept a sample or solve.
281
+
282
+ | Property | Value |
283
+ |---|---|
284
+ | Type | Boolean |
285
+ | Opcode | `cameraCalibrationReady` |
286
+ | `CAMERA_ID` | String, default: `default` |
287
+
288
+ ### `camera calibration state [CAMERA_ID]`
289
+
290
+ Returns the calibration session state for one shared Camera Source camera: idle, acquiring-camera, sampling, ready, solving, solved, cancelling, or error. A camera that has never been calibrated reports idle.
291
+
292
+ | Property | Value |
293
+ |---|---|
294
+ | Type | Reporter |
295
+ | Opcode | `cameraCalibrationState` |
296
+ | `CAMERA_ID` | String, default: `default` |
297
+
298
+ ### `camera calibration backend`
299
+
300
+ Returns the pinned solver identifier. Reading it does not load the solver.
301
+
302
+ | Property | Value |
303
+ |---|---|
304
+ | Type | Reporter |
305
+ | Opcode | `cameraCalibrationBackend` |
306
+
307
+ ### `camera [CAMERA_ID] calibration sample count`
308
+
309
+ Returns the accepted sample count for the current or last solved session.
310
+
311
+ | Property | Value |
312
+ |---|---|
313
+ | Type | Reporter |
314
+ | Opcode | `cameraCalibrationSampleCount` |
315
+ | `CAMERA_ID` | String, default: `default` |
316
+
317
+ ### `camera [CAMERA_ID] calibration sample quality`
318
+
319
+ Returns the latest accepted sample's combined board coverage and sharpness score, from zero to one.
320
+
321
+ | Property | Value |
322
+ |---|---|
323
+ | Type | Reporter |
324
+ | Opcode | `cameraCalibrationSampleQuality` |
325
+ | `CAMERA_ID` | String, default: `default` |
326
+
327
+ ### `camera [CAMERA_ID] calibration reprojection error px`
328
+
329
+ Returns the RMS reprojection error in pixels for the latest solve or imported profile.
330
+
331
+ | Property | Value |
332
+ |---|---|
333
+ | Type | Reporter |
334
+ | Opcode | `cameraCalibrationReprojectionError` |
335
+ | `CAMERA_ID` | String, default: `default` |
336
+
337
+ ### `camera [CAMERA_ID] calibration error code`
338
+
339
+ Returns a stable code for dependency, board, camera, sample, solve, reprojection, profile, or publication errors, or an empty string.
340
+
341
+ | Property | Value |
342
+ |---|---|
343
+ | Type | Reporter |
344
+ | Opcode | `cameraCalibrationErrorCode` |
345
+ | `CAMERA_ID` | String, default: `default` |
346
+
347
+ ### `camera [CAMERA_ID] calibration error`
348
+
349
+ Returns the detailed calibration diagnostic for that camera.
350
+
351
+ | Property | Value |
352
+ |---|---|
353
+ | Type | Reporter |
354
+ | Opcode | `cameraCalibrationError` |
355
+ | `CAMERA_ID` | String, default: `default` |
356
+
357
+ ### `camera [CAMERA_ID] calibration profile JSON`
358
+
359
+ Exports the stored intrinsic profile for that camera, or an empty string when none exists.
360
+
361
+ | Property | Value |
362
+ |---|---|
363
+ | Type | Reporter |
364
+ | Opcode | `cameraCalibrationJson` |
365
+ | `CAMERA_ID` | String, default: `default` |
366
+
367
+ <!-- END GENERATED BLOCKS -->
368
+
369
+ ## Important behavior
370
+
371
+ | Situation | Behavior |
372
+ |---|---|
373
+ | Calibration flag is OFF | Only the state reporter is published and it returns `idle`; commands refuse with an explicit error |
374
+ | Camera Source is not loaded | `dependency-missing`; the extension never opens its own camera |
375
+ | Camera Source has no profile registry, or another contract version | `api-version-mismatch` on publish; never a silent success, and the solved profile is kept |
376
+ | Camera calibration has not run | The state reporter returns `idle` and the profile reporter returns an empty string |
377
+ | Board not found, blurred, or too similar to a kept view | The sample is refused with its own error code and the session stays ready |
378
+ | Resolution, device, or mirroring changes mid-session | `resolution-mismatch` or `capture-condition-mismatch` |
379
+ | Reprojection error exceeds the session limit | `reprojection-too-high`; no profile is stored and more samples can be added |
380
+ | Profile belongs to another camera or another capture size | `calibration-not-applicable`, rejected before any state changes |
381
+ | Solve succeeds | The camera lease is released immediately |
382
+ | Project stop, project reload, runtime disposal | Every session is cancelled and every camera lease is released |
383
+ | Invalid input | Rejected before any session state changes, including a restart with a mistyped board |
384
+
385
+ Each shared camera has its own session, profile, and diagnostics. Cancelling one
386
+ camera never releases another camera's lease or stops another consumer of the
387
+ same shared camera.
388
+
389
+ ## Integration
390
+
391
+ | Consumer | Relationship |
392
+ |---|---|
393
+ | `@kubohiroya/turbowarp-camera-source` | Required provider: supplies the camera lease and owns the profile contract |
394
+ | `@kubohiroya/turbowarp-realtime-motion-capture` | Optional consumer of the published calibration profile |
395
+ | `@kubohiroya/turbowarp-ar` | Optional consumer of the published calibration profile |
396
+ | `@kubohiroya/turbowarp-photogrammetry` | Optional consumer of the published calibration profile |
397
+
398
+ Dependencies point from the consumer to the provider. Do not document
399
+ private-field access as an integration API.
400
+
401
+ ## Compatibility
402
+
403
+ | Identifier | Value | Stability |
404
+ |---|---|---|
405
+ | Product name | `TurboWarp-Camera-Calibration` | Human-facing |
406
+ | Repository | `kubohiroya/turbowarp-camera-calibration` | Current source location |
407
+ | npm package | `@kubohiroya/turbowarp-camera-calibration` | Public package contract |
408
+ | Extension ID | `kubohiroyacameracalibration` | Stored in SB3; migration required to change |
409
+ | Profile schema | `camerasource/camera-intrinsics` v1 | Moves to Camera Source with the contract |
410
+ | Solve backend | `opencv-js-wasm-4.12.0` | Pinned; reported by a block |
411
+
412
+ Document schema-aware migrations when an extension ID or opcode changes. Never
413
+ instruct users to replace JavaScript alone when stored identifiers have changed.
414
+
415
+ ## Development
416
+
417
+ Use Node.js `>=22.18.0` and the pnpm version declared by `packageManager`.
418
+
419
+ ```bash
420
+ corepack enable
421
+ pnpm install --frozen-lockfile
422
+ pnpm check
423
+ ```
424
+
425
+ Useful commands:
426
+
427
+ | Command | Purpose |
428
+ |---|---|
429
+ | `pnpm dev` | Rebuild while source files change |
430
+ | `pnpm test` | Run tests |
431
+ | `pnpm docs` | Regenerate block documentation |
432
+ | `pnpm check:dist` | Verify committed build artifacts |
433
+ | `pnpm pack:check` | Inspect the npm package |
434
+ | `pnpm release:check` | Dry-run the release |
435
+
436
+ ## Release
437
+
438
+ 1. Update `CHANGELOG.md` when present.
439
+ 2. Update the version in `package.json`.
440
+ 3. Run `pnpm check`.
441
+ 4. Merge the release PR.
442
+ 5. Create tag `v<version>` on the verified commit.
443
+ 6. Publish the same version to npm.
444
+ 7. Verify GitHub Release, npm tarball, GitHub Pages, `docsURI`, block icon, and CDN artifacts.
445
+
446
+ ## License
447
+
448
+ [Mozilla Public License 2.0](LICENSE) (SPDX: `MPL-2.0`).
449
+
450
+ Third-party components are listed in
451
+ [`THIRD_PARTY_NOTICES.md`](THIRD_PARTY_NOTICES.md). The bundle inlines OpenCV.js
452
+ `4.12.0-release.1`, distributed under the Apache License 2.0.
@@ -0,0 +1,29 @@
1
+ # Third-party notices
2
+
3
+ `dist/camera-calibration.js` is a standalone bundle. Everything it needs at run time is inlined,
4
+ including the components listed below.
5
+
6
+ ## OpenCV.js
7
+
8
+ The chessboard detector and the `calibrateCamera` solver come from OpenCV.js, bundled through
9
+ [`@techstark/opencv-js`](https://github.com/TechStark/opencv-js) at the exact version
10
+ `4.12.0-release.1`. The version is pinned in `package.json` and is part of the backend identifier
11
+ that the `camera calibration backend` block reports, so a project can record which build produced a
12
+ profile.
13
+
14
+ OpenCV is distributed under the Apache License 2.0:
15
+
16
+ - <https://github.com/opencv/opencv>
17
+ - <https://github.com/TechStark/opencv-js>
18
+ - <https://www.apache.org/licenses/LICENSE-2.0>
19
+
20
+ The build is about 10.4 MB, which is why the calibration procedure lives in this extension rather
21
+ than in `@kubohiroya/turbowarp-camera-source`. A TurboWarp extension ships as one standalone file,
22
+ so a dynamic `import()` is inlined into the same file instead of being split into a separate chunk.
23
+ The import is still lazy at run time: the WebAssembly runtime is initialized on the first sample or
24
+ solve, and never when the extension loads or when the backend reporter is read.
25
+
26
+ ## This extension
27
+
28
+ `@kubohiroya/turbowarp-camera-calibration` itself is distributed under the Mozilla Public License
29
+ 2.0. See [`LICENSE`](LICENSE).