@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.
@@ -0,0 +1,200 @@
1
+ {
2
+ "formatVersion": 1,
3
+ "id": "kubohiroyacameracalibration",
4
+ "blocks": [
5
+ {
6
+ "opcode": "addCameraCalibrationSample",
7
+ "blockType": "COMMAND",
8
+ "arguments": [
9
+ {
10
+ "id": "CAMERA_ID",
11
+ "type": "STRING"
12
+ }
13
+ ]
14
+ },
15
+ {
16
+ "opcode": "cameraCalibrationBackend",
17
+ "blockType": "REPORTER",
18
+ "arguments": []
19
+ },
20
+ {
21
+ "opcode": "cameraCalibrationError",
22
+ "blockType": "REPORTER",
23
+ "arguments": [
24
+ {
25
+ "id": "CAMERA_ID",
26
+ "type": "STRING"
27
+ }
28
+ ]
29
+ },
30
+ {
31
+ "opcode": "cameraCalibrationErrorCode",
32
+ "blockType": "REPORTER",
33
+ "arguments": [
34
+ {
35
+ "id": "CAMERA_ID",
36
+ "type": "STRING"
37
+ }
38
+ ]
39
+ },
40
+ {
41
+ "opcode": "cameraCalibrationJson",
42
+ "blockType": "REPORTER",
43
+ "arguments": [
44
+ {
45
+ "id": "CAMERA_ID",
46
+ "type": "STRING"
47
+ }
48
+ ]
49
+ },
50
+ {
51
+ "opcode": "cameraCalibrationJsonValid",
52
+ "blockType": "BOOLEAN",
53
+ "arguments": [
54
+ {
55
+ "id": "CAMERA_ID",
56
+ "type": "STRING"
57
+ },
58
+ {
59
+ "id": "JSON",
60
+ "type": "STRING"
61
+ }
62
+ ]
63
+ },
64
+ {
65
+ "opcode": "cameraCalibrationReady",
66
+ "blockType": "BOOLEAN",
67
+ "arguments": [
68
+ {
69
+ "id": "CAMERA_ID",
70
+ "type": "STRING"
71
+ }
72
+ ]
73
+ },
74
+ {
75
+ "opcode": "cameraCalibrationReprojectionError",
76
+ "blockType": "REPORTER",
77
+ "arguments": [
78
+ {
79
+ "id": "CAMERA_ID",
80
+ "type": "STRING"
81
+ }
82
+ ]
83
+ },
84
+ {
85
+ "opcode": "cameraCalibrationSampleCount",
86
+ "blockType": "REPORTER",
87
+ "arguments": [
88
+ {
89
+ "id": "CAMERA_ID",
90
+ "type": "STRING"
91
+ }
92
+ ]
93
+ },
94
+ {
95
+ "opcode": "cameraCalibrationSampleQuality",
96
+ "blockType": "REPORTER",
97
+ "arguments": [
98
+ {
99
+ "id": "CAMERA_ID",
100
+ "type": "STRING"
101
+ }
102
+ ]
103
+ },
104
+ {
105
+ "opcode": "cameraCalibrationState",
106
+ "blockType": "REPORTER",
107
+ "arguments": [
108
+ {
109
+ "id": "CAMERA_ID",
110
+ "type": "STRING"
111
+ }
112
+ ]
113
+ },
114
+ {
115
+ "opcode": "cancelCameraCalibration",
116
+ "blockType": "COMMAND",
117
+ "arguments": [
118
+ {
119
+ "id": "CAMERA_ID",
120
+ "type": "STRING"
121
+ }
122
+ ]
123
+ },
124
+ {
125
+ "opcode": "cleanupCameraCalibration",
126
+ "blockType": "COMMAND",
127
+ "arguments": [
128
+ {
129
+ "id": "CAMERA_ID",
130
+ "type": "STRING"
131
+ }
132
+ ]
133
+ },
134
+ {
135
+ "opcode": "importCameraCalibration",
136
+ "blockType": "COMMAND",
137
+ "arguments": [
138
+ {
139
+ "id": "CAMERA_ID",
140
+ "type": "STRING"
141
+ },
142
+ {
143
+ "id": "JSON",
144
+ "type": "STRING"
145
+ }
146
+ ]
147
+ },
148
+ {
149
+ "opcode": "publishCameraCalibration",
150
+ "blockType": "COMMAND",
151
+ "arguments": [
152
+ {
153
+ "id": "CAMERA_ID",
154
+ "type": "STRING"
155
+ }
156
+ ]
157
+ },
158
+ {
159
+ "opcode": "solveCameraCalibration",
160
+ "blockType": "COMMAND",
161
+ "arguments": [
162
+ {
163
+ "id": "CAMERA_ID",
164
+ "type": "STRING"
165
+ }
166
+ ]
167
+ },
168
+ {
169
+ "opcode": "startCameraCalibration",
170
+ "blockType": "COMMAND",
171
+ "arguments": [
172
+ {
173
+ "id": "CALIBRATION_ID",
174
+ "type": "STRING"
175
+ },
176
+ {
177
+ "id": "CAMERA_ID",
178
+ "type": "STRING"
179
+ },
180
+ {
181
+ "id": "COLUMNS",
182
+ "type": "NUMBER"
183
+ },
184
+ {
185
+ "id": "MAX_ERROR_PX",
186
+ "type": "NUMBER"
187
+ },
188
+ {
189
+ "id": "ROWS",
190
+ "type": "NUMBER"
191
+ },
192
+ {
193
+ "id": "SQUARE_METERS",
194
+ "type": "NUMBER"
195
+ }
196
+ ]
197
+ }
198
+ ],
199
+ "menus": []
200
+ }
package/docs/.nojekyll ADDED
File without changes
@@ -0,0 +1,153 @@
1
+ # アーキテクチャ
2
+
3
+ [English](architecture.md)
4
+
5
+ ## packageの境界
6
+
7
+ この機能拡張はカメラ校正の**手順**を所有します。校正**プロファイルの契約**、
8
+ すなわちschema、検証、現在の撮影条件に対する適合性判定は、
9
+ [TurboWarp-Camera-Source](https://github.com/kubohiroya/turbowarp-camera-source)
10
+ が所有します。
11
+
12
+ ```text
13
+ turbowarp-camera-source カメラ取得、lease、preview、プロファイル契約
14
+ ^ ^
15
+ | lease | プロファイル提供
16
+ | |
17
+ turbowarp-camera-calibration チェスボードのサンプル収集、solve、再投影誤差
18
+ ```
19
+
20
+ この位置に境界を置いたのは実測に基づく判断です。`findChessboardCorners`と
21
+ `calibrateCamera`に必要なOpenCVのbuildは約10 MBあり、TurboWarp機能拡張は
22
+ 単一のstandalone bundleとして配布されます。動的`import()`は別チャンクにならず
23
+ 同じfileへインライン展開されるため、solverをCamera Source内に置くと、校正を
24
+ 一度も行わない利用者を含めたすべてのカメラ利用者がその重量を負担します。
25
+ 起動時のfeature flagでは回避できません。flagが制御するのは実行であって、
26
+ 配信されるバイト数ではないからです。
27
+
28
+ ## build成果物
29
+
30
+ 同一のcommit済みsource定義から、実行時の振る舞いと互換性metadataを分けて
31
+ 生成します。
32
+
33
+ ```text
34
+ src/index.ts + src/extension.ts
35
+ -> vite-plugin-turbowarp-extension
36
+ -> dist/camera-calibration.js
37
+
38
+ src/config.ts + src/block-definitions.json
39
+ -> extension-api-manifest Vite plugin
40
+ -> dist/extension-manifest.json
41
+ ```
42
+
43
+ manifest pluginはViteのpost-build phaseで動作します。これによりJavaScript plugin
44
+ の単一出力検証を保ったまま、TurboWarp bundleの完成後にmanifestだけを追加します。
45
+
46
+ ## Extension API manifest v1
47
+
48
+ `schemas/extension-manifest.schema.json` が正規のJSON Schemaです。
49
+ `formatVersion`は`1`で、互換性のないmanifest形状を導入する際に変更します。
50
+
51
+ v1の契約に含むもの:
52
+
53
+ - TurboWarpのextension ID
54
+ - 各blockのopcodeとblock type
55
+ - 各argumentのID、type、任意のmenu参照
56
+ - 各menuのIDとreporter受入れ可否
57
+
58
+ block、argument、menuはserialize前に識別子で整列します。text、description、
59
+ 既定値、静的menu項目は、保存済みprojectのAPI参照を identify しないため意図的に
60
+ 除外します。
61
+
62
+ ## moduleの構成
63
+
64
+ ```text
65
+ config/feature-flags.ts 起動時固定のflag。校正は既定OFF
66
+ src/calibration/types.ts board、sample、solve結果、backendの継ぎ目
67
+ src/calibration/profile.ts 内部校正プロファイル、その検証、旧形式のadapter
68
+ src/calibration/camera-source.ts Camera Source capabilityのclient
69
+ src/calibration/controller.ts 共有カメラ1台ごとのセッション
70
+ src/calibration/opencv-backend.ts 固定versionのOpenCV solver。初回利用時に生成
71
+ src/extension.ts blockの結線とruntimeのライフサイクル
72
+ ```
73
+
74
+ OpenCVに触れるのは`opencv-backend.ts`だけで、controllerは
75
+ `CalibrationBackendFactory`越しにのみ到達します。testはmock backendで手順全体を
76
+ 動かすため、solverは必要な場所だけで動きます。
77
+
78
+ ## 校正の流れ
79
+
80
+ 1. 1つの`cameraId`についてCamera Sourceからleaseを取得し、その時点の解像度・device・左右反転にセッションを固定する。
81
+ 2. チェスボードのサンプルを収集し、品質の低い視点と類似しすぎる視点を拒否する。保持するのは8枚以上40枚以下。
82
+ 3. 内部行列と歪み係数を求め、RMS再投影誤差をセッションの上限と比較する。
83
+ 4. 得られた内部プロファイルをCamera Sourceのプロファイル契約経由で提供する。
84
+ 5. solve、cancel、cleanup、device喪失、project停止、project再読込、runtime破棄でleaseを解放する。
85
+
86
+ 内部校正と外部姿勢は分離したままにします。この機能拡張はworld姿勢を提供せず、
87
+ 提供されていない姿勢をidentityで代用することもありません。`calibrateCamera`は
88
+ 視点ごとの回転と並進も返しますが、それはboardの位置であってカメラの位置では
89
+ ないため、姿勢として提供せず破棄します。
90
+
91
+ ## 共有カメラ1台ごとのセッション
92
+
93
+ `CameraCalibrationController`は`cameraId`ごとに`CameraCalibration`を保持します。
94
+ 各instanceが自分のlease、サンプル、プロファイル、診断を所有します。あるカメラの
95
+ cancelやcleanupが別のカメラのleaseを解放することはなく、Camera Sourceは元の
96
+ カメラを他の利用者と共有し続けます。
97
+
98
+ 非同期の各段階はセッションの操作世代を持ち回ります。cancelはこれを進めるため、
99
+ その後に解決したサンプルやsolveは、状態を書かずに、またcancelが引き取った
100
+ leaseを二重に解放せずに終了します。遅い応答が取り消し済みセッションを復活させたり、
101
+ 共有leaseを壊したりしないのはこのためです。
102
+
103
+ ## プロファイル契約
104
+
105
+ 公開するプロファイルは
106
+ `schemas/camera-intrinsics-v1.schema.json`が記述します。この契約はCamera Sourceの
107
+ 責務であり
108
+ ([kubohiroya/turbowarp-camera-source#14](https://github.com/kubohiroya/turbowarp-camera-source/issues/14))、
109
+ それが出るまでは本repositoryから公開します。移行時に変わるのは`schema`と
110
+ schemaの`$id`だけです。
111
+
112
+ 検証はschema libraryではなく`src/calibration/profile.ts`の手書き実装で行い、
113
+ 公開schemaと検証実装のdriftは`tests/calibration-profile.test.ts`が検出します。
114
+ プロファイルは状態になる前に完全に検証します。不正なschema、非有限値、未知の
115
+ カメラモデルや歪みモデル、モデルと一致しない係数数、ペアリング資格情報、別カメラ
116
+ のプロファイル、別の撮影サイズのプロファイルは、固有のエラーコードで拒否し、
117
+ 何も変更しません。
118
+
119
+ 移設元の`twrmc/camera-calibration` v1のプロファイルも同じ入口で読みます。
120
+ `worldFromCameraMatrix`は捨て、記録されていなかった品質は捏造せず欠落のままに
121
+ します。
122
+
123
+ ## 依存とversionのエラー
124
+
125
+ Camera Sourceのcapabilityは必要になった時点でruntimeから参照し、読み込み時に
126
+ キャッシュしません。
127
+
128
+ | 状況 | コード |
129
+ |---|---|
130
+ | Camera Sourceが未読込み | `dependency-missing` |
131
+ | Camera Sourceにプロファイル登録APIが無い | `api-version-mismatch` |
132
+ | Camera Sourceの契約versionが異なる | `api-version-mismatch` |
133
+ | solveもimportもしていない | `not-calibrated` |
134
+ | 登録側がプロファイルを拒否した | `publish-failed` |
135
+
136
+ いずれも成功として扱わず、いずれも校正状態を書き換えません。publishはセッション
137
+ 操作ではありません。Camera Sourceが受け取れなかったsolve済みプロファイルは
138
+ solve済みのままであり、すでに失敗したセッションがpublish要求で回復することも
139
+ ありません。
140
+
141
+ ## feature flag
142
+
143
+ `config/feature-flags.ts`はmodule評価時に`cameraCalibrationV1`を凍結します。
144
+ 既定はOFFです。OFFのときは状態reporterだけを提供して`idle`を返し、校正command
145
+ はすべて明示的に拒否し、leaseを要求せず、OpenCVのruntimeも初期化しません。
146
+ これがロールバック経路です。従来の挙動はflag 1つ分の距離にあり、利用側は
147
+ この機能拡張を読み込まずに旧経路へ戻せます。
148
+
149
+ ## drift検出
150
+
151
+ `dist/`はrelease artifactとしてcommitします。`pnpm run check:dist`は両fileを
152
+ 再buildし、`dist/`配下にGitが変更・削除・未追跡を報告した時点で失敗します。
153
+ これによりmanifestとbundleのdriftをローカル検査とCIの双方で検出します。
@@ -0,0 +1,159 @@
1
+ # Architecture
2
+
3
+ [日本語](architecture.ja.md)
4
+
5
+ ## Package boundary
6
+
7
+ This extension owns the camera calibration *procedure*. The calibration
8
+ *profile contract* — schema, validation, and applicability against the current
9
+ capture conditions — belongs to
10
+ [TurboWarp-Camera-Source](https://github.com/kubohiroya/turbowarp-camera-source).
11
+
12
+ ```text
13
+ turbowarp-camera-source camera acquisition, lease, preview, profile contract
14
+ ^ ^
15
+ | lease | publish profile
16
+ | |
17
+ turbowarp-camera-calibration chessboard sampling, solve, reprojection error
18
+ ```
19
+
20
+ The boundary is placed here for a measured reason. The OpenCV build required to
21
+ run `findChessboardCorners` and `calibrateCamera` is roughly 10 MB, and a
22
+ TurboWarp extension is distributed as a single standalone bundle: a dynamic
23
+ `import()` is inlined into that same file rather than split into a separate
24
+ chunk. Placing the solver inside Camera Source would therefore charge every
25
+ camera consumer for the solver, whether or not it ever calibrates. A startup
26
+ feature flag cannot avoid that, because a flag gates execution, not bytes.
27
+
28
+ ## Build outputs
29
+
30
+ The project keeps runtime behavior and compatibility metadata separate while
31
+ generating both from the same checked-in source definitions.
32
+
33
+ ```text
34
+ src/index.ts + src/extension.ts
35
+ -> vite-plugin-turbowarp-extension
36
+ -> dist/camera-calibration.js
37
+
38
+ src/config.ts + src/block-definitions.json
39
+ -> extension-api-manifest Vite plugin
40
+ -> dist/extension-manifest.json
41
+ ```
42
+
43
+ The manifest plugin runs in Vite's post-build phase. This preserves the
44
+ JavaScript plugin's single-output validation and adds the manifest only after
45
+ the TurboWarp bundle is complete.
46
+
47
+ ## Extension API manifest v1
48
+
49
+ `schemas/extension-manifest.schema.json` is the normative JSON Schema.
50
+ `formatVersion` is `1` and must change when an incompatible manifest shape is
51
+ introduced.
52
+
53
+ The v1 contract contains:
54
+
55
+ - the TurboWarp extension ID;
56
+ - each block opcode and block type;
57
+ - each argument ID, argument type, and optional menu reference;
58
+ - each menu ID and whether it accepts reporter blocks.
59
+
60
+ Blocks, arguments, and menus are sorted by their identifiers before
61
+ serialization. Text, descriptions, default values, and static menu items are
62
+ intentionally excluded because they do not identify saved-project API
63
+ references.
64
+
65
+ ## Module layout
66
+
67
+ ```text
68
+ config/feature-flags.ts startup-fixed flags, calibration OFF by default
69
+ src/calibration/types.ts board, sample, solve result, backend seam
70
+ src/calibration/profile.ts the intrinsic profile, its validator, legacy adapter
71
+ src/calibration/camera-source.ts the Camera Source capability client
72
+ src/calibration/controller.ts one session per shared camera
73
+ src/calibration/opencv-backend.ts the pinned OpenCV solver, created on first use
74
+ src/extension.ts block wiring and runtime lifecycle
75
+ ```
76
+
77
+ Only `opencv-backend.ts` touches OpenCV, and the controller reaches it through
78
+ `CalibrationBackendFactory`. Tests drive the whole procedure against a mock
79
+ backend, so the solver is exercised where it matters and nowhere else.
80
+
81
+ ## Calibration flow
82
+
83
+ 1. Acquire a lease for one `cameraId` from Camera Source and fix the session to the resolution, device, and mirroring the camera reports at that moment.
84
+ 2. Collect chessboard samples, rejecting low-quality and near-duplicate views. Between 8 and 40 samples are retained.
85
+ 3. Solve for the intrinsic matrix and distortion coefficients, and check the RMS reprojection error against the session limit.
86
+ 4. Publish the resulting intrinsic profile through the Camera Source profile contract.
87
+ 5. Release the lease on solve, cancel, cleanup, device loss, project stop, project reload, and runtime disposal.
88
+
89
+ Intrinsic calibration and external pose stay separate. This extension never
90
+ supplies a world pose, and never substitutes identity for one that is missing.
91
+ OpenCV also returns a per-view rotation and translation from `calibrateCamera`;
92
+ those describe where the board sat, not where the camera stands, so they are
93
+ discarded rather than published as a pose.
94
+
95
+ ## One session per shared camera
96
+
97
+ `CameraCalibrationController` holds a `CameraCalibration` per `cameraId`. Each
98
+ one owns its lease, its samples, its profile, and its diagnostics. Cancelling or
99
+ cleaning up one camera never releases another camera's lease, and Camera Source
100
+ keeps sharing the underlying camera with other consumers.
101
+
102
+ Every asynchronous step carries the session's operation counter. A cancel
103
+ increments it, so a sample or a solve that resolves afterwards returns without
104
+ writing state and without releasing a lease that cancel already took over. This
105
+ is what keeps a slow response from resurrecting a cancelled session or
106
+ double-releasing a shared lease.
107
+
108
+ ## Profile contract
109
+
110
+ `schemas/camera-intrinsics-v1.schema.json` describes the published profile.
111
+ Camera Source owns this contract
112
+ ([kubohiroya/turbowarp-camera-source#14](https://github.com/kubohiroya/turbowarp-camera-source/issues/14));
113
+ this repository publishes it until that lands, and `schema` plus the schema
114
+ `$id` are the only fields that move when it does.
115
+
116
+ Validation is hand-written in `src/calibration/profile.ts` rather than taken
117
+ from a schema library, and `tests/calibration-profile.test.ts` fails when the
118
+ published schema and the validator drift apart. A profile is fully validated
119
+ before it becomes state: an invalid schema, a non-finite value, an unknown
120
+ camera or distortion model, a coefficient count that does not match its model, a
121
+ pairing credential, a profile for another camera, or a profile for another
122
+ capture size is rejected with its own error code and changes nothing.
123
+
124
+ The pre-migration `twrmc/camera-calibration` v1 profile is read through the same
125
+ entry point. Its `worldFromCameraMatrix` is dropped, and the quality it never
126
+ recorded stays absent rather than being invented.
127
+
128
+ ## Dependency and version errors
129
+
130
+ The Camera Source capability is looked up on the runtime at the moment it is
131
+ needed, never cached at load time.
132
+
133
+ | Situation | Code |
134
+ |---|---|
135
+ | Camera Source is not loaded | `dependency-missing` |
136
+ | Camera Source has no profile registry | `api-version-mismatch` |
137
+ | Camera Source publishes another contract version | `api-version-mismatch` |
138
+ | No profile has been solved or imported | `not-calibrated` |
139
+ | The registry rejected the profile | `publish-failed` |
140
+
141
+ None of these is treated as success, and none of them rewrites the calibration
142
+ state. Publishing is not a session operation: a solved profile that Camera
143
+ Source could not accept is still solved, and a session that already failed is
144
+ not repaired by asking to publish.
145
+
146
+ ## Feature flag
147
+
148
+ `config/feature-flags.ts` freezes `cameraCalibrationV1` at module evaluation,
149
+ default OFF. With the flag OFF the extension publishes only the state reporter,
150
+ which answers `idle`; every calibration command refuses explicitly; no lease is
151
+ requested and the OpenCV runtime is never initialized. This is the rollback
152
+ path: the previous behavior is one flag away, and the consumer side can fall
153
+ back to its own calibration without loading this extension at all.
154
+
155
+ ## Drift detection
156
+
157
+ `dist/` is committed as a release artifact. `pnpm run check:dist` rebuilds both
158
+ files and fails when Git reports any modified, deleted, or untracked file below
159
+ `dist/`. This catches manifest and bundle drift in local checks and CI.
@@ -0,0 +1,124 @@
1
+ <!doctype html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="utf-8">
5
+ <meta name="viewport" content="width=device-width,initial-scale=1">
6
+ <title>TurboWarp-Camera-Calibration documentation</title>
7
+ <style>
8
+ body{font-family:system-ui,-apple-system,BlinkMacSystemFont,"Segoe UI",sans-serif;max-width:900px;margin:0 auto;padding:2rem;line-height:1.6;color:#222}
9
+ code{background:#f3f3f3;padding:.1em .3em;border-radius:.25em}
10
+ pre{background:#f6f8fa;padding:1rem;overflow:auto;border-radius:.5rem}
11
+ table{border-collapse:collapse}
12
+ th,td{border:1px solid #ddd;padding:.4rem .6rem;text-align:left}
13
+ </style>
14
+ </head>
15
+ <body>
16
+ <h1>TurboWarp-Camera-Calibration</h1>
17
+ <p>Calibrate a camera shared through TurboWarp-Camera-Source and publish its intrinsic calibration profile.</p>
18
+
19
+ <p><strong>Status:</strong> the calibration path ships behind a startup feature flag that defaults to OFF. With the flag OFF the extension publishes only the state reporter, which answers <code>idle</code>.</p>
20
+
21
+ <h2>Requirements</h2>
22
+ <ul>
23
+ <li>TurboWarp Desktop, Web, or Packager.</li>
24
+ <li><code>@kubohiroya/turbowarp-camera-source</code> loaded first. This extension never opens a camera itself.</li>
25
+ <li>A printed chessboard calibration target, flat and unglossy. The block arguments count <strong>inner corners</strong>, not printed squares: a 10&times;7 square board is <code>9</code> by <code>6</code>.</li>
26
+ <li>The extension must run without the sandbox.</li>
27
+ </ul>
28
+
29
+ <h2>Enabling calibration</h2>
30
+ <p>Set the override before the extension is registered. The flags are read once and frozen.</p>
31
+ <pre><code>globalThis.__TWCC_FEATURE_FLAGS__ = {cameraCalibrationV1: true};</code></pre>
32
+
33
+ <h2>Calibrating a camera</h2>
34
+ <pre><code>start shared camera [default]
35
+ start camera [default] calibration [calibration-1] board [9] by [6] square [0.025] m max error [1.5] px
36
+ repeat until &lt;camera [default] calibration sample count = 12&gt;
37
+ add calibration sample for camera [default]
38
+ solve calibration for camera [default]
39
+ publish calibration profile for camera [default]</code></pre>
40
+ <p>Each shared camera has its own session, profile, and diagnostics. Cancelling one camera never stops another consumer of the same shared camera.</p>
41
+
42
+ <h3>Getting a usable result</h3>
43
+ <table>
44
+ <tr><th>Do this</th><th>Why</th></tr>
45
+ <tr><td>Capture at least 8 accepted samples, at most 40</td><td>Fewer views leave the distortion terms under-determined</td></tr>
46
+ <tr><td>Vary the board angle by roughly 20&deg;&ndash;45&deg; across samples</td><td>Views that are all fronto-parallel cannot separate focal length from distance</td></tr>
47
+ <tr><td>Vary the distance and fill different parts of the frame, including the corners</td><td>Distortion is strongest away from the image center</td></tr>
48
+ <tr><td>Keep the whole board inside the frame and in focus</td><td>A partially visible or blurred board is refused</td></tr>
49
+ <tr><td>Do not change resolution, camera device, mirroring, focus, or zoom during a session</td><td>The session is fixed to the resolution it started with</td></tr>
50
+ </table>
51
+ <p>Verify a finished calibration against images that were not part of the solve. A low reprojection error on the calibration samples alone does not prove the result generalizes.</p>
52
+
53
+ <h2>Blocks</h2>
54
+ <table>
55
+ <tr><th>Block</th><th>Type</th><th>Does</th></tr>
56
+ <tr><td><code>start camera [CAMERA_ID] calibration [CALIBRATION_ID] board [COLUMNS] by [ROWS] square [SQUARE_METERS] m max error [MAX_ERROR_PX] px</code></td><td>Command</td><td>Leases one shared camera and fixes its real capture resolution for a session</td></tr>
57
+ <tr><td><code>add calibration sample for camera [CAMERA_ID]</code></td><td>Command</td><td>Detects the complete board in the current frame and keeps it when quality and novelty pass</td></tr>
58
+ <tr><td><code>solve calibration for camera [CAMERA_ID]</code></td><td>Command</td><td>Solves the intrinsics from at least eight samples and releases the lease</td></tr>
59
+ <tr><td><code>cancel calibration for camera [CAMERA_ID]</code></td><td>Command</td><td>Releases the lease and the samples, keeping the last validated profile</td></tr>
60
+ <tr><td><code>cleanup calibration for camera [CAMERA_ID]</code></td><td>Command</td><td>Also clears the in-memory profile for that camera</td></tr>
61
+ <tr><td><code>publish calibration profile for camera [CAMERA_ID]</code></td><td>Command</td><td>Registers the profile with Camera Source, which owns the contract</td></tr>
62
+ <tr><td><code>import calibration profile [JSON] for camera [CAMERA_ID]</code></td><td>Command</td><td>Imports a profile after schema, credential, and applicability checks</td></tr>
63
+ <tr><td><code>calibration profile [JSON] valid for camera [CAMERA_ID]?</code></td><td>Boolean</td><td>Validates a profile without replacing the stored one</td></tr>
64
+ <tr><td><code>camera [CAMERA_ID] calibration ready?</code></td><td>Boolean</td><td>Whether the session can accept a sample or a solve</td></tr>
65
+ <tr><td><code>camera calibration state [CAMERA_ID]</code></td><td>Reporter</td><td><code>idle</code>, <code>acquiring-camera</code>, <code>sampling</code>, <code>ready</code>, <code>solving</code>, <code>solved</code>, <code>cancelling</code>, or <code>error</code></td></tr>
66
+ <tr><td><code>camera calibration backend</code></td><td>Reporter</td><td>The pinned solver identifier</td></tr>
67
+ <tr><td><code>camera [CAMERA_ID] calibration sample count</code></td><td>Reporter</td><td>Accepted samples in the current or last solved session</td></tr>
68
+ <tr><td><code>camera [CAMERA_ID] calibration sample quality</code></td><td>Reporter</td><td>The latest accepted sample's score, from zero to one</td></tr>
69
+ <tr><td><code>camera [CAMERA_ID] calibration reprojection error px</code></td><td>Reporter</td><td>RMS reprojection error of the latest solve or imported profile</td></tr>
70
+ <tr><td><code>camera [CAMERA_ID] calibration error code</code></td><td>Reporter</td><td>A stable code, or an empty string</td></tr>
71
+ <tr><td><code>camera [CAMERA_ID] calibration error</code></td><td>Reporter</td><td>The detailed diagnostic</td></tr>
72
+ <tr><td><code>camera [CAMERA_ID] calibration profile JSON</code></td><td>Reporter</td><td>The stored intrinsic profile, or an empty string</td></tr>
73
+ </table>
74
+
75
+ <h2>Error codes</h2>
76
+ <table>
77
+ <tr><th>Code</th><th>Meaning</th></tr>
78
+ <tr><td><code>dependency-missing</code></td><td>Camera Source is not loaded</td></tr>
79
+ <tr><td><code>api-version-mismatch</code></td><td>Camera Source has no profile registry, or speaks another contract version</td></tr>
80
+ <tr><td><code>invalid-board</code></td><td>The board size, square size, or error limit is out of range</td></tr>
81
+ <tr><td><code>camera-unavailable</code></td><td>Camera Source refused the lease</td></tr>
82
+ <tr><td><code>camera-ended</code></td><td>The camera stopped or the device disappeared</td></tr>
83
+ <tr><td><code>resolution-mismatch</code></td><td>The capture resolution changed after the session started</td></tr>
84
+ <tr><td><code>capture-condition-mismatch</code></td><td>The camera device or mirroring changed after the session started</td></tr>
85
+ <tr><td><code>board-not-found</code></td><td>The complete chessboard was not visible</td></tr>
86
+ <tr><td><code>sample-low-quality</code></td><td>The board was too small in frame, or too blurred</td></tr>
87
+ <tr><td><code>sample-too-similar</code></td><td>The view is too close to one already kept</td></tr>
88
+ <tr><td><code>sample-limit</code></td><td>Forty samples are already retained</td></tr>
89
+ <tr><td><code>sample-insufficient</code></td><td>Fewer than eight samples have been accepted</td></tr>
90
+ <tr><td><code>sample-failed</code>, <code>solve-failed</code></td><td>The solver itself failed</td></tr>
91
+ <tr><td><code>reprojection-too-high</code></td><td>The solve exceeded the session's error limit</td></tr>
92
+ <tr><td><code>invalid-calibration</code></td><td>The profile does not match the schema</td></tr>
93
+ <tr><td><code>credential-forbidden</code></td><td>The profile carries a pairing credential</td></tr>
94
+ <tr><td><code>calibration-not-applicable</code></td><td>The profile belongs to another camera or another capture size</td></tr>
95
+ <tr><td><code>not-calibrated</code></td><td>There is no profile to publish</td></tr>
96
+ <tr><td colspan="2">A refused publication records its code without changing the calibration state: a solved profile stays solved.</td></tr>
97
+ <tr><td><code>publish-failed</code></td><td>Camera Source rejected the profile</td></tr>
98
+ </table>
99
+
100
+ <h2>The calibration profile</h2>
101
+ <p>The profile is intrinsics only: an intrinsic matrix, a distortion model and its coefficients, the calibrated image size, and when it was measured. It never carries a world pose, and a pose that was not measured is never filled in with identity. Preview mirroring is a display concern; profile coordinates are always the unmirrored capture coordinates.</p>
102
+ <p>Profiles written by <code>@kubohiroya/turbowarp-realtime-motion-capture</code> are accepted on import; their world pose is dropped rather than reinterpreted.</p>
103
+
104
+ <h2>How the packages divide the work</h2>
105
+ <table>
106
+ <tr><th>Package</th><th>Owns</th></tr>
107
+ <tr>
108
+ <td><code>@kubohiroya/turbowarp-camera-source</code></td>
109
+ <td>Camera acquisition, lease sharing, preview, and the calibration profile contract</td>
110
+ </tr>
111
+ <tr>
112
+ <td><code>@kubohiroya/turbowarp-camera-calibration</code></td>
113
+ <td>The calibration procedure: chessboard sampling, solving, and profile publication</td>
114
+ </tr>
115
+ </table>
116
+ <p>The OpenCV build needed to solve a calibration is roughly 10 MB, and a TurboWarp extension ships as a single standalone bundle. Keeping the solver in its own extension lets Camera Source stay small enough for every camera consumer to load.</p>
117
+
118
+ <h2>Safety</h2>
119
+ <p>Calibration reads camera frames only to detect the chessboard target. Nothing is uploaded. A profile that carries a pairing credential is rejected rather than stored. Load unsandboxed extension code only from a source you trust.</p>
120
+
121
+ <h2>Development</h2>
122
+ <p>See the repository README and architecture documentation for build, test, release, and compatibility details.</p>
123
+ </body>
124
+ </html>