@kubohiroya/turbowarp-camera-calibration 0.1.0 → 0.3.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.ja.md +47 -3
- package/README.md +48 -3
- package/THIRD_PARTY_NOTICES.md +14 -0
- package/dist/camera-calibration.js +218 -29
- package/lib/calibration/contract.d.ts +19 -0
- package/lib/calibration/contract.js +1 -0
- package/lib/calibration/types.d.ts +57 -0
- package/lib/calibration/types.js +8 -0
- package/lib/runtime-capability.d.ts +65 -0
- package/lib/runtime-capability.js +31 -0
- package/lib/runtime.d.ts +34 -0
- package/lib/runtime.js +42 -0
- package/package.json +16 -4
- package/src/calibration/camera-source.ts +113 -58
- package/src/calibration/contract.ts +51 -0
- package/src/calibration/controller.ts +41 -51
- package/src/extension.ts +47 -0
- package/src/runtime-capability.ts +106 -0
- package/src/runtime.ts +62 -0
package/README.ja.md
CHANGED
|
@@ -106,7 +106,7 @@ bundleは約11 MBあります。
|
|
|
106
106
|
検証済みのversionをexact pinします。
|
|
107
107
|
|
|
108
108
|
```bash
|
|
109
|
-
pnpm add --save-exact @kubohiroya/turbowarp-camera-calibration@0.
|
|
109
|
+
pnpm add --save-exact @kubohiroya/turbowarp-camera-calibration@0.3.0
|
|
110
110
|
```
|
|
111
111
|
|
|
112
112
|
standalone bundle:
|
|
@@ -118,7 +118,7 @@ node_modules/@kubohiroya/turbowarp-camera-calibration/dist/camera-calibration.js
|
|
|
118
118
|
version固定CDN URL:
|
|
119
119
|
|
|
120
120
|
```text
|
|
121
|
-
https://cdn.jsdelivr.net/npm/@kubohiroya/turbowarp-camera-calibration@0.
|
|
121
|
+
https://cdn.jsdelivr.net/npm/@kubohiroya/turbowarp-camera-calibration@0.3.0/dist/camera-calibration.js
|
|
122
122
|
```
|
|
123
123
|
|
|
124
124
|
## クイックスタート
|
|
@@ -226,6 +226,48 @@ block referenceは
|
|
|
226
226
|
cancelが、別のカメラのleaseを解放したり、同じ共有カメラの他の利用者を止めたり
|
|
227
227
|
することはありません。
|
|
228
228
|
|
|
229
|
+
## ほかの機能拡張から校正を実行する
|
|
230
|
+
|
|
231
|
+
ブロックはプロジェクトのためのパレットであって、機能拡張間のAPIではありません。
|
|
232
|
+
校正を実行したい機能拡張 — かつて自前で持っていて、いまは委譲する側 — は、VM
|
|
233
|
+
runtime上のversion付きcapabilityを使います。
|
|
234
|
+
|
|
235
|
+
```ts
|
|
236
|
+
import {
|
|
237
|
+
readCameraCalibrationCapability
|
|
238
|
+
} from '@kubohiroya/turbowarp-camera-calibration/runtime';
|
|
239
|
+
|
|
240
|
+
const calibration = readCameraCalibrationCapability(Scratch.vm.runtime);
|
|
241
|
+
if (!calibration) {
|
|
242
|
+
// 未ロード、または校正機能がOFF。いずれにせよ実行できる手順は無いので、
|
|
243
|
+
// 待たずにその旨を返します。
|
|
244
|
+
return;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
await calibration.requireVersion(1).start({
|
|
248
|
+
cameraId: 'stage-left',
|
|
249
|
+
calibrationId: 'session-1',
|
|
250
|
+
board: {columns: 9, rows: 6, squareSizeMeters: 0.025},
|
|
251
|
+
maximumReprojectionErrorPx: 1.5
|
|
252
|
+
});
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
このsub-entryが持つのは宣言と定数2つだけで、機能拡張本体は一切含みません。利用側
|
|
256
|
+
のbundleに増えるものは実質ありません(とくにOpenCVは入りません)。
|
|
257
|
+
|
|
258
|
+
`requireVersion` は、このビルドが実装していないversionを明示的に拒否します。これは
|
|
259
|
+
capabilityが無い場合とは別の答えです。機能拡張はロードされていて、要求されたことが
|
|
260
|
+
できない、という状態であり、操作者への説明も変わります。
|
|
261
|
+
|
|
262
|
+
capabilityが駆動するのはブロックと同じカメラ単位のセッションで、カメラの指定方法も
|
|
263
|
+
同じです。空文字や空白の `cameraId` は、ブロックが `default` と呼ぶカメラを指します。
|
|
264
|
+
委譲された校正とパレットから始めた校正はひとつのセッションであり、同じカメラについて
|
|
265
|
+
食い違う2つの見え方にはなりません。
|
|
266
|
+
|
|
267
|
+
プロファイルは別の話です。プロファイル契約を所有しているのはCamera Sourceなので、
|
|
268
|
+
プロファイルの読み書き・保管・適合判定だけが目的の利用側は
|
|
269
|
+
`@kubohiroya/turbowarp-camera-source` と話せばよく、この機能拡張は不要です。
|
|
270
|
+
|
|
229
271
|
## 連携
|
|
230
272
|
|
|
231
273
|
| 利用側 | 関係 |
|
|
@@ -245,7 +287,9 @@ cancelが、別のカメラのleaseを解放したり、同じ共有カメラの
|
|
|
245
287
|
| repository | `kubohiroya/turbowarp-camera-calibration` | current source |
|
|
246
288
|
| npm package | `@kubohiroya/turbowarp-camera-calibration` | public package contract |
|
|
247
289
|
| extension ID | `kubohiroyacameracalibration` | SB3に保存。変更にはmigrationが必要 |
|
|
248
|
-
|
|
|
290
|
+
| 保存プロファイルschema | `camerasource/camera-intrinsics` v1 | この機能拡張が読み書きします |
|
|
291
|
+
| 公開プロファイルschema | `twcs/camera-intrinsics` v1 | Camera Sourceが所有します |
|
|
292
|
+
| runtime capability | `kubohiroyaCameraCalibrationCapability` v1 | VM runtimeから読みます |
|
|
249
293
|
| solve backend | `opencv-js-wasm-4.12.0` | 固定。ブロックで報告します |
|
|
250
294
|
|
|
251
295
|
extension IDまたはopcode変更時はschema-aware migrationを説明し、保存済み識別子が
|
package/README.md
CHANGED
|
@@ -108,7 +108,7 @@ therefore about 11 MB.
|
|
|
108
108
|
Install an exact version that you have reviewed:
|
|
109
109
|
|
|
110
110
|
```bash
|
|
111
|
-
pnpm add --save-exact @kubohiroya/turbowarp-camera-calibration@0.
|
|
111
|
+
pnpm add --save-exact @kubohiroya/turbowarp-camera-calibration@0.3.0
|
|
112
112
|
```
|
|
113
113
|
|
|
114
114
|
Load the standalone bundle from:
|
|
@@ -120,7 +120,7 @@ node_modules/@kubohiroya/turbowarp-camera-calibration/dist/camera-calibration.js
|
|
|
120
120
|
A version-pinned CDN URL is:
|
|
121
121
|
|
|
122
122
|
```text
|
|
123
|
-
https://cdn.jsdelivr.net/npm/@kubohiroya/turbowarp-camera-calibration@0.
|
|
123
|
+
https://cdn.jsdelivr.net/npm/@kubohiroya/turbowarp-camera-calibration@0.3.0/dist/camera-calibration.js
|
|
124
124
|
```
|
|
125
125
|
|
|
126
126
|
## Quick start
|
|
@@ -386,6 +386,49 @@ Each shared camera has its own session, profile, and diagnostics. Cancelling one
|
|
|
386
386
|
camera never releases another camera's lease or stops another consumer of the
|
|
387
387
|
same shared camera.
|
|
388
388
|
|
|
389
|
+
## Running a calibration from another extension
|
|
390
|
+
|
|
391
|
+
Blocks are a palette for a project, not an API between extensions. An extension
|
|
392
|
+
that needs to run a calibration -- because it used to own one and now delegates
|
|
393
|
+
it -- reaches a versioned capability on the VM runtime instead.
|
|
394
|
+
|
|
395
|
+
```ts
|
|
396
|
+
import {
|
|
397
|
+
readCameraCalibrationCapability
|
|
398
|
+
} from '@kubohiroya/turbowarp-camera-calibration/runtime';
|
|
399
|
+
|
|
400
|
+
const calibration = readCameraCalibrationCapability(Scratch.vm.runtime);
|
|
401
|
+
if (!calibration) {
|
|
402
|
+
// Not loaded, or loaded with the calibration feature off. Either way there is
|
|
403
|
+
// no procedure to drive, and the caller has to say so rather than wait.
|
|
404
|
+
return;
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
await calibration.requireVersion(1).start({
|
|
408
|
+
cameraId: 'stage-left',
|
|
409
|
+
calibrationId: 'session-1',
|
|
410
|
+
board: {columns: 9, rows: 6, squareSizeMeters: 0.025},
|
|
411
|
+
maximumReprojectionErrorPx: 1.5
|
|
412
|
+
});
|
|
413
|
+
```
|
|
414
|
+
|
|
415
|
+
The sub-entry holds declarations and two constants. It pulls in none of the
|
|
416
|
+
extension, so importing it costs a consumer nothing at run time -- in
|
|
417
|
+
particular, not the OpenCV build.
|
|
418
|
+
|
|
419
|
+
`requireVersion` refuses a version this build does not implement, out loud. That
|
|
420
|
+
is a different answer from the capability being absent: the extension is loaded
|
|
421
|
+
and cannot do what was asked, which needs a different message to the operator.
|
|
422
|
+
|
|
423
|
+
The capability drives the same per-camera sessions the blocks drive, and
|
|
424
|
+
addresses cameras the same way: an empty or blank `cameraId` is the camera the
|
|
425
|
+
blocks call `default`. A delegated calibration and one started from the palette
|
|
426
|
+
are one session, not two views of one camera that disagree.
|
|
427
|
+
|
|
428
|
+
Profiles are a separate matter. Camera Source owns the profile contract, so a
|
|
429
|
+
consumer that only wants to read, store, or check a profile talks to
|
|
430
|
+
`@kubohiroya/turbowarp-camera-source` and does not need this extension at all.
|
|
431
|
+
|
|
389
432
|
## Integration
|
|
390
433
|
|
|
391
434
|
| Consumer | Relationship |
|
|
@@ -406,7 +449,9 @@ private-field access as an integration API.
|
|
|
406
449
|
| Repository | `kubohiroya/turbowarp-camera-calibration` | Current source location |
|
|
407
450
|
| npm package | `@kubohiroya/turbowarp-camera-calibration` | Public package contract |
|
|
408
451
|
| Extension ID | `kubohiroyacameracalibration` | Stored in SB3; migration required to change |
|
|
409
|
-
|
|
|
452
|
+
| Stored profile schema | `camerasource/camera-intrinsics` v1 | Read and written by this extension |
|
|
453
|
+
| Published profile schema | `twcs/camera-intrinsics` v1 | Owned by Camera Source |
|
|
454
|
+
| Runtime capability | `kubohiroyaCameraCalibrationCapability` v1 | Read from the VM runtime |
|
|
410
455
|
| Solve backend | `opencv-js-wasm-4.12.0` | Pinned; reported by a block |
|
|
411
456
|
|
|
412
457
|
Document schema-aware migrations when an extension ID or opcode changes. Never
|
package/THIRD_PARTY_NOTICES.md
CHANGED
|
@@ -23,6 +23,20 @@ so a dynamic `import()` is inlined into the same file instead of being split int
|
|
|
23
23
|
The import is still lazy at run time: the WebAssembly runtime is initialized on the first sample or
|
|
24
24
|
solve, and never when the extension loads or when the backend reporter is read.
|
|
25
25
|
|
|
26
|
+
## Camera Source declarations
|
|
27
|
+
|
|
28
|
+
The contract this extension talks to is imported from
|
|
29
|
+
[`@kubohiroya/turbowarp-camera-source`](https://github.com/kubohiroya/turbowarp-camera-source) at the
|
|
30
|
+
exact version `0.8.0`, through its `./runtime` sub-entry: the camera lease, the runtime capability,
|
|
31
|
+
and the intrinsic profile document. That entry is declarations and a few string constants, and its
|
|
32
|
+
built form imports nothing at all, so nothing of Camera Source reaches
|
|
33
|
+
`dist/camera-calibration.js` beyond those constants. It is imported rather than copied because a
|
|
34
|
+
copy is checked against nothing: this repository's own hand-written copy named a registry method
|
|
35
|
+
Camera Source does not have, and every attempt to publish a profile failed for a reason that had
|
|
36
|
+
nothing to do with what the message said.
|
|
37
|
+
|
|
38
|
+
Camera Source is distributed under the Mozilla Public License 2.0.
|
|
39
|
+
|
|
26
40
|
## This extension
|
|
27
41
|
|
|
28
42
|
`@kubohiroya/turbowarp-camera-calibration` itself is distributed under the Mozilla Public License
|
|
@@ -270,11 +270,67 @@
|
|
|
270
270
|
]
|
|
271
271
|
};
|
|
272
272
|
//#endregion
|
|
273
|
+
//#region node_modules/.pnpm/@kubohiroya+turbowarp-camera-source@0.8.0/node_modules/@kubohiroya/turbowarp-camera-source/dist/runtime.js
|
|
274
|
+
/**
|
|
275
|
+
* Where the extension instance puts itself on the VM runtime.
|
|
276
|
+
*
|
|
277
|
+
* Present as soon as the extension is registered. Absent means Camera Source is not loaded, which a
|
|
278
|
+
* consumer has to handle whatever else it does.
|
|
279
|
+
*/
|
|
280
|
+
var cameraSourceRuntimeKey = "ext_kubohiroyacamerasource";
|
|
281
|
+
/**
|
|
282
|
+
* Where the versioned capability sits, when the build publishing it has that path enabled.
|
|
283
|
+
*
|
|
284
|
+
* Separate from `cameraSourceRuntimeKey` on purpose. The extension key is present as soon as Camera
|
|
285
|
+
* Source is registered; this one appears only when the calibration profile contract is switched on.
|
|
286
|
+
* A consumer can therefore tell "not loaded" from "loaded, and not offering profiles", and neither
|
|
287
|
+
* has to be reported as the other.
|
|
288
|
+
*/
|
|
289
|
+
var cameraSourceCapabilityKey = "kubohiroyaCameraSourceCapability";
|
|
290
|
+
/** Narrows a runtime value to the Camera Source surface, so a missing extension reads as absent. */
|
|
291
|
+
function readCameraSourceRuntime(runtime) {
|
|
292
|
+
if (typeof runtime !== "object" || runtime === null) return void 0;
|
|
293
|
+
const candidate = runtime[cameraSourceRuntimeKey];
|
|
294
|
+
if (typeof candidate !== "object" || candidate === null) return void 0;
|
|
295
|
+
const { acquireCamera } = candidate;
|
|
296
|
+
return typeof acquireCamera === "function" ? candidate : void 0;
|
|
297
|
+
}
|
|
298
|
+
/**
|
|
299
|
+
* Narrows a runtime value to the profile capability, or reports it as unavailable.
|
|
300
|
+
*
|
|
301
|
+
* Undefined means one of two things a consumer usually handles the same way: Camera Source is not
|
|
302
|
+
* loaded, or it is loaded with the profile contract switched off. `readCameraSourceRuntime` is what
|
|
303
|
+
* separates them when the difference matters -- a consumer that can still share a camera but cannot
|
|
304
|
+
* ask about calibration should say that, rather than reporting the extension as missing.
|
|
305
|
+
*
|
|
306
|
+
* A version this build does not implement is a third thing again, and not an absence: the capability
|
|
307
|
+
* is returned, and `requireVersion` refuses out loud when asked for a version it cannot honour.
|
|
308
|
+
*/
|
|
309
|
+
function readCameraSourceCapability(runtime) {
|
|
310
|
+
if (typeof runtime !== "object" || runtime === null) return void 0;
|
|
311
|
+
const candidate = runtime[cameraSourceCapabilityKey];
|
|
312
|
+
if (typeof candidate !== "object" || candidate === null) return void 0;
|
|
313
|
+
const { requireVersion } = candidate;
|
|
314
|
+
return typeof requireVersion === "function" ? candidate : void 0;
|
|
315
|
+
}
|
|
316
|
+
//#endregion
|
|
317
|
+
//#region src/calibration/types.ts
|
|
318
|
+
/** Coefficient counts OpenCV emits for each supported distortion model. */
|
|
319
|
+
var DISTORTION_COEFFICIENT_COUNTS = Object.freeze({
|
|
320
|
+
"none": [0],
|
|
321
|
+
"opencv-plumb-bob": [4, 5],
|
|
322
|
+
"opencv-rational": [8],
|
|
323
|
+
"opencv-thin-prism": [12],
|
|
324
|
+
"opencv-tilted": [14]
|
|
325
|
+
});
|
|
326
|
+
//#endregion
|
|
273
327
|
//#region src/calibration/camera-source.ts
|
|
274
|
-
/** The runtime key Camera Source publishes itself under. */
|
|
275
|
-
var CAMERA_SOURCE_RUNTIME_KEY = "ext_kubohiroyacamerasource";
|
|
276
328
|
/** The lease owner recorded with Camera Source, for diagnostics on its side. */
|
|
277
329
|
var CALIBRATION_LEASE_OWNER = "turbowarp-camera-calibration";
|
|
330
|
+
/** Recorded on every published profile, so a reader knows what solved it. */
|
|
331
|
+
var CALIBRATION_PRODUCER = "turbowarp-camera-calibration";
|
|
332
|
+
/** The version of Camera Source's runtime capability this extension speaks. */
|
|
333
|
+
var SUPPORTED_CAMERA_SOURCE_CAPABILITY_VERSION = 1;
|
|
278
334
|
var CameraSourceError = class extends Error {
|
|
279
335
|
constructor(code, message) {
|
|
280
336
|
super(message);
|
|
@@ -282,39 +338,100 @@
|
|
|
282
338
|
this.name = "CameraSourceError";
|
|
283
339
|
}
|
|
284
340
|
};
|
|
285
|
-
/** Returns the Camera Source
|
|
341
|
+
/** Returns the Camera Source camera surface, or reports that it is not loaded. */
|
|
286
342
|
function requireCameraSource(runtime) {
|
|
287
|
-
const candidate = runtime
|
|
288
|
-
if (!
|
|
343
|
+
const candidate = readCameraSourceRuntime(runtime);
|
|
344
|
+
if (!candidate) throw new CameraSourceError("dependency-missing", "TurboWarp Camera Source is not loaded. Load it before calibrating; this extension never opens its own camera.");
|
|
289
345
|
return candidate;
|
|
290
346
|
}
|
|
291
347
|
/**
|
|
292
348
|
* Returns the profile registry when Camera Source publishes a version this
|
|
293
|
-
* extension speaks.
|
|
294
|
-
*
|
|
349
|
+
* extension speaks.
|
|
350
|
+
*
|
|
351
|
+
* The capability key is separate from the extension key on purpose: Camera
|
|
352
|
+
* Source withholds the capability when its own calibration feature is off, so
|
|
353
|
+
* an extension that is loaded but not offering profiles is distinguishable from
|
|
354
|
+
* one that is absent, and neither is reported as the other.
|
|
295
355
|
*/
|
|
296
356
|
function requireProfileRegistry(runtime) {
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
357
|
+
if (!readCameraSourceRuntime(runtime)) throw new CameraSourceError("dependency-missing", "TurboWarp Camera Source is not loaded, so no calibration profile registry exists.");
|
|
358
|
+
const capability = readCameraSourceCapability(runtime);
|
|
359
|
+
if (!capability) throw new CameraSourceError("api-version-mismatch", `The loaded Camera Source publishes no calibration profile registry. Version ${SUPPORTED_CAMERA_SOURCE_CAPABILITY_VERSION} of its runtime capability is required.`);
|
|
360
|
+
try {
|
|
361
|
+
capability.requireVersion(SUPPORTED_CAMERA_SOURCE_CAPABILITY_VERSION);
|
|
362
|
+
} catch (error) {
|
|
363
|
+
throw new CameraSourceError("api-version-mismatch", error instanceof Error ? error.message : String(error));
|
|
364
|
+
}
|
|
365
|
+
return capability;
|
|
304
366
|
}
|
|
305
|
-
|
|
306
|
-
|
|
367
|
+
/**
|
|
368
|
+
* Restates a solved profile as the document Camera Source validates.
|
|
369
|
+
*
|
|
370
|
+
* The two shapes are not the same and never were: this extension stores a flat
|
|
371
|
+
* profile with a packed nine-number matrix, and Camera Source names the pinhole
|
|
372
|
+
* parameters individually because an array cannot say whether it is row-major.
|
|
373
|
+
* The conversion happens here, at the boundary, so that what a project has
|
|
374
|
+
* saved and what `cameraCalibrationJson` reports keep their existing shape --
|
|
375
|
+
* an SB3 holding either format still imports.
|
|
376
|
+
*
|
|
377
|
+
* Nothing is invented on the way across. The image is stated as raw or already
|
|
378
|
+
* undistorted from `imageState` rather than assumed, and a distortion model
|
|
379
|
+
* with no counterpart is refused instead of being relabelled as the nearest
|
|
380
|
+
* one, which would leave a consumer applying the wrong coefficients to real
|
|
381
|
+
* pixels and getting a plausible wrong answer.
|
|
382
|
+
*/
|
|
383
|
+
function toCameraSourceProfile(profile) {
|
|
384
|
+
const [fx, skew, cx, , fy, cy] = profile.intrinsicMatrix;
|
|
385
|
+
const document = {
|
|
386
|
+
schema: "twcs/camera-intrinsics",
|
|
387
|
+
profileId: profile.calibrationId,
|
|
388
|
+
cameraId: profile.cameraId,
|
|
389
|
+
calibratedAt: profile.calibratedAt,
|
|
390
|
+
producer: CALIBRATION_PRODUCER,
|
|
391
|
+
cameraModel: profile.cameraModel,
|
|
392
|
+
image: {
|
|
393
|
+
width: profile.imageWidth,
|
|
394
|
+
height: profile.imageHeight,
|
|
395
|
+
undistorted: profile.imageState === "undistorted"
|
|
396
|
+
},
|
|
397
|
+
intrinsics: {
|
|
398
|
+
fx,
|
|
399
|
+
fy,
|
|
400
|
+
cx,
|
|
401
|
+
cy,
|
|
402
|
+
skew
|
|
403
|
+
},
|
|
404
|
+
distortion: {
|
|
405
|
+
model: cameraSourceDistortionModel(profile.distortionModel),
|
|
406
|
+
coefficients: [...profile.distortionCoefficients]
|
|
407
|
+
}
|
|
408
|
+
};
|
|
409
|
+
return profile.quality ? {
|
|
410
|
+
...document,
|
|
411
|
+
version: 1,
|
|
412
|
+
quality: { ...profile.quality }
|
|
413
|
+
} : {
|
|
414
|
+
...document,
|
|
415
|
+
version: 1
|
|
416
|
+
};
|
|
417
|
+
}
|
|
418
|
+
/**
|
|
419
|
+
* Names this extension's distortion model the way Camera Source names it.
|
|
420
|
+
*
|
|
421
|
+
* `opencv-plumb-bob` and `opencv-rational` are the radial-tangential family
|
|
422
|
+
* Camera Source calls `brown-conrady`, in the same coefficient order. The thin
|
|
423
|
+
* prism and tilted-sensor layouts have no counterpart in that contract, so a
|
|
424
|
+
* profile using one cannot be published rather than being sent under a name
|
|
425
|
+
* that would make a consumer read its coefficients as something else.
|
|
426
|
+
*/
|
|
427
|
+
function cameraSourceDistortionModel(model) {
|
|
428
|
+
switch (model) {
|
|
429
|
+
case "none": return "none";
|
|
430
|
+
case "opencv-plumb-bob":
|
|
431
|
+
case "opencv-rational": return "brown-conrady";
|
|
432
|
+
default: throw new CameraSourceError("api-version-mismatch", `Camera Source has no distortion model matching ${model} (${String(DISTORTION_COEFFICIENT_COUNTS[model])} coefficients), so this profile cannot be published.`);
|
|
433
|
+
}
|
|
307
434
|
}
|
|
308
|
-
//#endregion
|
|
309
|
-
//#region src/calibration/types.ts
|
|
310
|
-
/** Coefficient counts OpenCV emits for each supported distortion model. */
|
|
311
|
-
var DISTORTION_COEFFICIENT_COUNTS = Object.freeze({
|
|
312
|
-
"none": [0],
|
|
313
|
-
"opencv-plumb-bob": [4, 5],
|
|
314
|
-
"opencv-rational": [8],
|
|
315
|
-
"opencv-thin-prism": [12],
|
|
316
|
-
"opencv-tilted": [14]
|
|
317
|
-
});
|
|
318
435
|
//#endregion
|
|
319
436
|
//#region src/calibration/profile.ts
|
|
320
437
|
/**
|
|
@@ -617,7 +734,7 @@
|
|
|
617
734
|
imageWidth: frame.width,
|
|
618
735
|
imageHeight: frame.height,
|
|
619
736
|
deviceId: frame.deviceId,
|
|
620
|
-
|
|
737
|
+
previewFlip: frame.previewFlip
|
|
621
738
|
};
|
|
622
739
|
this.lease = lease;
|
|
623
740
|
this.samples = [];
|
|
@@ -709,11 +826,19 @@
|
|
|
709
826
|
} catch (error) {
|
|
710
827
|
this.refuseWith(errorCodeFor(error), error);
|
|
711
828
|
}
|
|
829
|
+
let document;
|
|
830
|
+
try {
|
|
831
|
+
document = toCameraSourceProfile(profile);
|
|
832
|
+
} catch (error) {
|
|
833
|
+
this.refuseWith(errorCodeFor(error), error);
|
|
834
|
+
}
|
|
835
|
+
let result;
|
|
712
836
|
try {
|
|
713
|
-
|
|
837
|
+
result = registry.registerProfile(document);
|
|
714
838
|
} catch (error) {
|
|
715
839
|
this.refuseWith("publish-failed", error);
|
|
716
840
|
}
|
|
841
|
+
if (!result.ok) this.refuseWith("publish-failed", /* @__PURE__ */ new Error(`Camera Source refused the profile: ${result.error?.code ?? "unknown"} at ${result.error?.path || "/"} -- ${result.error?.message ?? "no detail reported"}`));
|
|
717
842
|
this.clearError();
|
|
718
843
|
}
|
|
719
844
|
state() {
|
|
@@ -754,7 +879,7 @@
|
|
|
754
879
|
this.fail("camera-ended", error);
|
|
755
880
|
}
|
|
756
881
|
if (frame.width !== session.imageWidth || frame.height !== session.imageHeight) this.reject("resolution-mismatch", `Expected ${session.imageWidth}x${session.imageHeight}, received ${frame.width}x${frame.height}.`);
|
|
757
|
-
if (frame.deviceId !== session.deviceId || frame.
|
|
882
|
+
if (frame.deviceId !== session.deviceId || frame.previewFlip !== session.previewFlip) this.reject("capture-condition-mismatch", `The capture conditions changed after the session started. Restart the calibration for camera ${this.cameraId}.`);
|
|
758
883
|
let sample;
|
|
759
884
|
try {
|
|
760
885
|
sample = await (await this.resolveBackend()).captureSample({
|
|
@@ -8542,6 +8667,36 @@
|
|
|
8542
8667
|
return Math.min(1, Math.max(0, value));
|
|
8543
8668
|
}
|
|
8544
8669
|
//#endregion
|
|
8670
|
+
//#region src/runtime-capability.ts
|
|
8671
|
+
var runtimeCapabilityKey = "kubohiroyaCameraCalibrationCapability";
|
|
8672
|
+
function createRuntimeCapability(host) {
|
|
8673
|
+
const capability = {
|
|
8674
|
+
version: 1,
|
|
8675
|
+
requireVersion(version) {
|
|
8676
|
+
if (version !== 1) throw new Error(`Unsupported Camera Calibration runtime capability version: ${version}; this build provides 1.`);
|
|
8677
|
+
return capability;
|
|
8678
|
+
},
|
|
8679
|
+
start: (options) => host.start(options),
|
|
8680
|
+
addSample: (cameraId) => host.addSample(cameraId),
|
|
8681
|
+
solve: (cameraId) => host.solve(cameraId),
|
|
8682
|
+
publish: (cameraId) => host.publish(cameraId),
|
|
8683
|
+
cancel: (cameraId) => host.cancel(cameraId),
|
|
8684
|
+
cleanup: (cameraId) => host.cleanup(cameraId),
|
|
8685
|
+
importProfile: (cameraId, json) => host.importProfile(cameraId, json),
|
|
8686
|
+
validateProfile: (cameraId, json) => host.validateProfile(cameraId, json),
|
|
8687
|
+
state: (cameraId) => host.state(cameraId),
|
|
8688
|
+
ready: (cameraId) => host.ready(cameraId),
|
|
8689
|
+
backend: () => host.backend(),
|
|
8690
|
+
sampleCount: (cameraId) => host.sampleCount(cameraId),
|
|
8691
|
+
sampleQuality: (cameraId) => host.sampleQuality(cameraId),
|
|
8692
|
+
reprojectionErrorPx: (cameraId) => host.reprojectionErrorPx(cameraId),
|
|
8693
|
+
errorCode: (cameraId) => host.errorCode(cameraId),
|
|
8694
|
+
errorMessage: (cameraId) => host.errorMessage(cameraId),
|
|
8695
|
+
profileJson: (cameraId) => host.profileJson(cameraId)
|
|
8696
|
+
};
|
|
8697
|
+
return Object.freeze(capability);
|
|
8698
|
+
}
|
|
8699
|
+
//#endregion
|
|
8545
8700
|
//#region src/extension.ts
|
|
8546
8701
|
var blockDefinitions = block_definitions_default.blocks;
|
|
8547
8702
|
var defaultCameraId = "default";
|
|
@@ -8556,6 +8711,7 @@
|
|
|
8556
8711
|
this.controller.cancelAll();
|
|
8557
8712
|
};
|
|
8558
8713
|
this.handleDisposed = () => {
|
|
8714
|
+
if (this.runtime["kubohiroyaCameraCalibrationCapability"] === this.capability) delete this.runtime[runtimeCapabilityKey];
|
|
8559
8715
|
this.runtime.off?.("PROJECT_STOP_ALL", this.handleProjectBoundary);
|
|
8560
8716
|
this.runtime.off?.("PROJECT_LOADED", this.handleProjectBoundary);
|
|
8561
8717
|
this.runtime.off?.("RUNTIME_DISPOSED", this.handleDisposed);
|
|
@@ -8569,6 +8725,7 @@
|
|
|
8569
8725
|
backend: options.backend ?? openCvBackendFactory,
|
|
8570
8726
|
...nowMilliseconds ? { nowMilliseconds } : {}
|
|
8571
8727
|
});
|
|
8728
|
+
if (this.enabled) this.runtime[runtimeCapabilityKey] = this.createCapability();
|
|
8572
8729
|
this.runtime.on?.("PROJECT_STOP_ALL", this.handleProjectBoundary);
|
|
8573
8730
|
this.runtime.on?.("PROJECT_LOADED", this.handleProjectBoundary);
|
|
8574
8731
|
this.runtime.on?.("RUNTIME_DISPOSED", this.handleDisposed);
|
|
@@ -8649,6 +8806,38 @@
|
|
|
8649
8806
|
cameraCalibrationJson(args) {
|
|
8650
8807
|
return this.enabled ? this.controller.profileJson(normalizeId(args.CAMERA_ID)) : "";
|
|
8651
8808
|
}
|
|
8809
|
+
/**
|
|
8810
|
+
* The procedure, named rather than reached through opcodes.
|
|
8811
|
+
*
|
|
8812
|
+
* Every member routes to the same controller the blocks use, so a delegated
|
|
8813
|
+
* calibration and a calibration driven from the palette are one session and
|
|
8814
|
+
* not two views of one camera that disagree.
|
|
8815
|
+
*/
|
|
8816
|
+
createCapability() {
|
|
8817
|
+
this.capability = createRuntimeCapability({
|
|
8818
|
+
start: (options) => this.controller.start({
|
|
8819
|
+
...options,
|
|
8820
|
+
cameraId: normalizeId(options.cameraId)
|
|
8821
|
+
}),
|
|
8822
|
+
addSample: (cameraId) => this.controller.addSample(normalizeId(cameraId)),
|
|
8823
|
+
solve: (cameraId) => this.controller.solve(normalizeId(cameraId)),
|
|
8824
|
+
publish: (cameraId) => this.controller.publishProfile(normalizeId(cameraId)),
|
|
8825
|
+
cancel: (cameraId) => this.controller.cancel(normalizeId(cameraId)),
|
|
8826
|
+
cleanup: (cameraId) => this.controller.cleanup(normalizeId(cameraId)),
|
|
8827
|
+
importProfile: (cameraId, json) => this.controller.importProfile(normalizeId(cameraId), json),
|
|
8828
|
+
validateProfile: (cameraId, json) => this.controller.validateProfile(normalizeId(cameraId), json),
|
|
8829
|
+
state: (cameraId) => this.controller.state(normalizeId(cameraId)),
|
|
8830
|
+
ready: (cameraId) => this.controller.ready(normalizeId(cameraId)),
|
|
8831
|
+
backend: () => this.controller.backend(),
|
|
8832
|
+
sampleCount: (cameraId) => this.controller.sampleCount(normalizeId(cameraId)),
|
|
8833
|
+
sampleQuality: (cameraId) => this.controller.latestSampleQuality(normalizeId(cameraId)),
|
|
8834
|
+
reprojectionErrorPx: (cameraId) => this.controller.latestReprojectionError(normalizeId(cameraId)),
|
|
8835
|
+
errorCode: (cameraId) => this.controller.errorCode(normalizeId(cameraId)),
|
|
8836
|
+
errorMessage: (cameraId) => this.controller.errorMessage(normalizeId(cameraId)),
|
|
8837
|
+
profileJson: (cameraId) => this.controller.profileJson(normalizeId(cameraId))
|
|
8838
|
+
});
|
|
8839
|
+
return this.capability;
|
|
8840
|
+
}
|
|
8652
8841
|
blockEnabled(feature) {
|
|
8653
8842
|
return feature === "always" || this.enabled;
|
|
8654
8843
|
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The names a caller uses to talk about a calibration, without the machinery
|
|
3
|
+
* that runs one.
|
|
4
|
+
*
|
|
5
|
+
* Kept in a leaf of its own so the published `./runtime` sub-entry can describe
|
|
6
|
+
* the procedure without reaching the controller, which reaches Camera Source
|
|
7
|
+
* and, on its first sample, eleven megabytes of OpenCV. A consumer importing
|
|
8
|
+
* these declarations pays for none of that.
|
|
9
|
+
*/
|
|
10
|
+
import type { CalibrationBoard } from './types.js';
|
|
11
|
+
export type CalibrationState = 'idle' | 'acquiring-camera' | 'sampling' | 'ready' | 'solving' | 'solved' | 'cancelling' | 'error';
|
|
12
|
+
export type CalibrationErrorCode = '' | 'dependency-missing' | 'api-version-mismatch' | 'invalid-board' | 'camera-unavailable' | 'camera-ended' | 'resolution-mismatch' | 'capture-condition-mismatch' | 'board-not-found' | 'sample-low-quality' | 'sample-too-similar' | 'sample-limit' | 'sample-insufficient' | 'sample-failed' | 'solve-failed' | 'reprojection-too-high' | 'invalid-calibration' | 'credential-forbidden' | 'calibration-not-applicable' | 'not-calibrated' | 'publish-failed';
|
|
13
|
+
export interface CalibrationStartOptions {
|
|
14
|
+
cameraId: string;
|
|
15
|
+
calibrationId: string;
|
|
16
|
+
board: CalibrationBoard;
|
|
17
|
+
/** The session is refused rather than solved when the RMS exceeds this. */
|
|
18
|
+
maximumReprojectionErrorPx: number;
|
|
19
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/** A printed chessboard target, measured by its inner corner grid. */
|
|
2
|
+
export interface CalibrationBoard {
|
|
3
|
+
columns: number;
|
|
4
|
+
rows: number;
|
|
5
|
+
squareSizeMeters: number;
|
|
6
|
+
}
|
|
7
|
+
/** One still image taken from the leased camera. */
|
|
8
|
+
export interface CalibrationFrame {
|
|
9
|
+
element: HTMLVideoElement;
|
|
10
|
+
width: number;
|
|
11
|
+
height: number;
|
|
12
|
+
}
|
|
13
|
+
export interface CalibrationCorner {
|
|
14
|
+
x: number;
|
|
15
|
+
y: number;
|
|
16
|
+
}
|
|
17
|
+
/** One accepted board view, kept until the session solves or is cancelled. */
|
|
18
|
+
export interface CalibrationSample {
|
|
19
|
+
corners: CalibrationCorner[];
|
|
20
|
+
quality: number;
|
|
21
|
+
coverage: number;
|
|
22
|
+
sharpness: number;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Distortion models this extension can produce and validate, named after the
|
|
26
|
+
* OpenCV coefficient layouts. The coefficient count identifies the model.
|
|
27
|
+
*/
|
|
28
|
+
export type DistortionModel = 'none' | 'opencv-plumb-bob' | 'opencv-rational' | 'opencv-thin-prism' | 'opencv-tilted';
|
|
29
|
+
/** Coefficient counts OpenCV emits for each supported distortion model. */
|
|
30
|
+
export declare const DISTORTION_COEFFICIENT_COUNTS: Readonly<Record<DistortionModel, readonly number[]>>;
|
|
31
|
+
/**
|
|
32
|
+
* The intrinsic solution. External pose is deliberately absent: this extension
|
|
33
|
+
* calibrates one camera's optics and never reports where that camera stands.
|
|
34
|
+
*/
|
|
35
|
+
export interface CalibrationSolveResult {
|
|
36
|
+
intrinsicMatrix: number[];
|
|
37
|
+
distortionModel: DistortionModel;
|
|
38
|
+
distortionCoefficients: number[];
|
|
39
|
+
reprojectionErrorPx: number;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* The solver seam. Everything above this interface is arithmetic-free control
|
|
43
|
+
* flow, so tests drive the whole procedure without loading OpenCV.
|
|
44
|
+
*/
|
|
45
|
+
export interface CalibrationBackendPort {
|
|
46
|
+
readonly name: string;
|
|
47
|
+
captureSample(frame: CalibrationFrame, board: CalibrationBoard): Promise<CalibrationSample | undefined>;
|
|
48
|
+
solve(samples: readonly CalibrationSample[], board: CalibrationBoard, imageWidth: number, imageHeight: number): Promise<CalibrationSolveResult>;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Creates the solver on first use. The name is available without creating it so
|
|
52
|
+
* that the backend reporter never loads the OpenCV runtime.
|
|
53
|
+
*/
|
|
54
|
+
export interface CalibrationBackendFactory {
|
|
55
|
+
readonly name: string;
|
|
56
|
+
create(): Promise<CalibrationBackendPort>;
|
|
57
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** Coefficient counts OpenCV emits for each supported distortion model. */
|
|
2
|
+
export const DISTORTION_COEFFICIENT_COUNTS = Object.freeze({
|
|
3
|
+
'none': [0],
|
|
4
|
+
'opencv-plumb-bob': [4, 5],
|
|
5
|
+
'opencv-rational': [8],
|
|
6
|
+
'opencv-thin-prism': [12],
|
|
7
|
+
'opencv-tilted': [14]
|
|
8
|
+
});
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What other extensions may drive without going through blocks.
|
|
3
|
+
*
|
|
4
|
+
* Blocks are a palette for a project, not an API between extensions: an opcode
|
|
5
|
+
* is reached by name through the VM, takes Scratch-cast arguments, and cannot
|
|
6
|
+
* be type-checked by a caller. An extension that wants to run a calibration --
|
|
7
|
+
* because it used to own one and now delegates it -- needs a named surface
|
|
8
|
+
* instead, so this is published under a versioned key.
|
|
9
|
+
*
|
|
10
|
+
* Versioned rather than handed over as the extension object, so a consumer
|
|
11
|
+
* states the contract it was written against and is refused clearly instead of
|
|
12
|
+
* finding a method missing partway through a calibration the operator is
|
|
13
|
+
* already standing in front of.
|
|
14
|
+
*/
|
|
15
|
+
import type { CalibrationErrorCode, CalibrationStartOptions, CalibrationState } from './calibration/contract.js';
|
|
16
|
+
export declare const runtimeCapabilityKey = "kubohiroyaCameraCalibrationCapability";
|
|
17
|
+
export declare const runtimeCapabilityVersion: 1;
|
|
18
|
+
export interface CameraCalibrationCapabilityV1 {
|
|
19
|
+
readonly version: typeof runtimeCapabilityVersion;
|
|
20
|
+
requireVersion(version: number): CameraCalibrationCapabilityV1;
|
|
21
|
+
/**
|
|
22
|
+
* Begins a session, replacing whatever the camera was doing.
|
|
23
|
+
*
|
|
24
|
+
* Rejects with the refusal recorded in `errorCode`. A board that does not
|
|
25
|
+
* describe a usable target is refused before the running session is
|
|
26
|
+
* disturbed, so a mistyped square size costs nothing already collected.
|
|
27
|
+
*/
|
|
28
|
+
start(options: CalibrationStartOptions): Promise<void>;
|
|
29
|
+
/** Captures one board view. Rejects when the view is unusable, keeping the session. */
|
|
30
|
+
addSample(cameraId: string): Promise<void>;
|
|
31
|
+
/** Solves from the accepted samples and releases the camera. */
|
|
32
|
+
solve(cameraId: string): Promise<void>;
|
|
33
|
+
/** Hands the solved profile to Camera Source, which owns the profile contract. */
|
|
34
|
+
publish(cameraId: string): Promise<void>;
|
|
35
|
+
/** Ends the session and releases the camera lease. Keeps the solved profile. */
|
|
36
|
+
cancel(cameraId: string): Promise<void>;
|
|
37
|
+
/** Ends the session and forgets the solved profile as well. */
|
|
38
|
+
cleanup(cameraId: string): Promise<void>;
|
|
39
|
+
/** Adopts a profile from JSON, in either this contract or the legacy one. */
|
|
40
|
+
importProfile(cameraId: string, json: string): Promise<void>;
|
|
41
|
+
/** Answers whether the JSON would be adopted, without adopting it. */
|
|
42
|
+
validateProfile(cameraId: string, json: string): boolean;
|
|
43
|
+
state(cameraId: string): CalibrationState;
|
|
44
|
+
/** Whether a session is live and can take another sample or a solve. */
|
|
45
|
+
ready(cameraId: string): boolean;
|
|
46
|
+
/** Names the solver, without creating it. Reading this never loads OpenCV. */
|
|
47
|
+
backend(): string;
|
|
48
|
+
sampleCount(cameraId: string): number;
|
|
49
|
+
/** Quality of the most recent accepted sample. Zero when none was accepted. */
|
|
50
|
+
sampleQuality(cameraId: string): number;
|
|
51
|
+
/**
|
|
52
|
+
* Reprojection RMS of the most recent solve, in pixels.
|
|
53
|
+
*
|
|
54
|
+
* Zero means no solve has produced one -- not a perfect solve. `state` and
|
|
55
|
+
* `errorCode` are what tell the two apart, and a consumer that reads this
|
|
56
|
+
* alone will report an uncalibrated camera as a flawless one.
|
|
57
|
+
*/
|
|
58
|
+
reprojectionErrorPx(cameraId: string): number;
|
|
59
|
+
errorCode(cameraId: string): CalibrationErrorCode;
|
|
60
|
+
/** The refusal in full, including the detail behind the code. */
|
|
61
|
+
errorMessage(cameraId: string): string;
|
|
62
|
+
/** The solved or imported profile, or an empty string when there is none. */
|
|
63
|
+
profileJson(cameraId: string): string;
|
|
64
|
+
}
|
|
65
|
+
export declare function createRuntimeCapability(host: Omit<CameraCalibrationCapabilityV1, 'version' | 'requireVersion'>): CameraCalibrationCapabilityV1;
|