@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
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export const runtimeCapabilityKey = 'kubohiroyaCameraCalibrationCapability';
|
|
2
|
+
export const runtimeCapabilityVersion = 1;
|
|
3
|
+
export function createRuntimeCapability(host) {
|
|
4
|
+
const capability = {
|
|
5
|
+
version: runtimeCapabilityVersion,
|
|
6
|
+
requireVersion(version) {
|
|
7
|
+
if (version !== runtimeCapabilityVersion) {
|
|
8
|
+
throw new Error(`Unsupported Camera Calibration runtime capability version: ${version}; this build provides ${runtimeCapabilityVersion}.`);
|
|
9
|
+
}
|
|
10
|
+
return capability;
|
|
11
|
+
},
|
|
12
|
+
start: (options) => host.start(options),
|
|
13
|
+
addSample: (cameraId) => host.addSample(cameraId),
|
|
14
|
+
solve: (cameraId) => host.solve(cameraId),
|
|
15
|
+
publish: (cameraId) => host.publish(cameraId),
|
|
16
|
+
cancel: (cameraId) => host.cancel(cameraId),
|
|
17
|
+
cleanup: (cameraId) => host.cleanup(cameraId),
|
|
18
|
+
importProfile: (cameraId, json) => host.importProfile(cameraId, json),
|
|
19
|
+
validateProfile: (cameraId, json) => host.validateProfile(cameraId, json),
|
|
20
|
+
state: (cameraId) => host.state(cameraId),
|
|
21
|
+
ready: (cameraId) => host.ready(cameraId),
|
|
22
|
+
backend: () => host.backend(),
|
|
23
|
+
sampleCount: (cameraId) => host.sampleCount(cameraId),
|
|
24
|
+
sampleQuality: (cameraId) => host.sampleQuality(cameraId),
|
|
25
|
+
reprojectionErrorPx: (cameraId) => host.reprojectionErrorPx(cameraId),
|
|
26
|
+
errorCode: (cameraId) => host.errorCode(cameraId),
|
|
27
|
+
errorMessage: (cameraId) => host.errorMessage(cameraId),
|
|
28
|
+
profileJson: (cameraId) => host.profileJson(cameraId)
|
|
29
|
+
};
|
|
30
|
+
return Object.freeze(capability);
|
|
31
|
+
}
|
package/lib/runtime.d.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What another extension needs to run a calibration through this one, without
|
|
3
|
+
* importing it.
|
|
4
|
+
*
|
|
5
|
+
* The extension itself is loaded into TurboWarp from a URL and reaches OpenCV
|
|
6
|
+
* on its first sample; nobody wants that in their bundle to ask a camera
|
|
7
|
+
* whether it is calibrated. What a consumer needs is a key, a version, and the
|
|
8
|
+
* declarations describing the procedure -- which is all this entry holds.
|
|
9
|
+
*
|
|
10
|
+
* It is published as a sub-entry so a consumer imports these instead of writing
|
|
11
|
+
* its own copy. A copy is checked against nothing: this package could change
|
|
12
|
+
* the shape, the consumer would still typecheck, and the mismatch would surface
|
|
13
|
+
* in a browser. That is not hypothetical here -- this repository's own copy of
|
|
14
|
+
* Camera Source's registry named a method Camera Source does not have, and
|
|
15
|
+
* every attempt to publish a profile failed with a version mismatch that had
|
|
16
|
+
* nothing to do with versions.
|
|
17
|
+
*/
|
|
18
|
+
export type { CalibrationErrorCode, CalibrationStartOptions, CalibrationState } from './calibration/contract.js';
|
|
19
|
+
export type { CalibrationBoard } from './calibration/types.js';
|
|
20
|
+
export { createRuntimeCapability, runtimeCapabilityKey, runtimeCapabilityVersion, type CameraCalibrationCapabilityV1 } from './runtime-capability.js';
|
|
21
|
+
import { type CameraCalibrationCapabilityV1 } from './runtime-capability.js';
|
|
22
|
+
/** The extension ID, as it appears in opcodes and in a project's extension list. */
|
|
23
|
+
export declare const cameraCalibrationExtensionId = "kubohiroyacameracalibration";
|
|
24
|
+
/**
|
|
25
|
+
* Narrows a runtime value to this extension's capability.
|
|
26
|
+
*
|
|
27
|
+
* Returns undefined when the extension is absent or its feature is off. Those
|
|
28
|
+
* two are deliberately not distinguished here: the capability is withheld while
|
|
29
|
+
* the flag is off, because a calibration that cannot acquire a camera is not a
|
|
30
|
+
* calibration a caller should be able to start. A version this build does not
|
|
31
|
+
* implement is a different matter -- `requireVersion` refuses that out loud,
|
|
32
|
+
* because the extension is present and cannot do what was asked.
|
|
33
|
+
*/
|
|
34
|
+
export declare function readCameraCalibrationCapability(runtime: unknown): CameraCalibrationCapabilityV1 | undefined;
|
package/lib/runtime.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What another extension needs to run a calibration through this one, without
|
|
3
|
+
* importing it.
|
|
4
|
+
*
|
|
5
|
+
* The extension itself is loaded into TurboWarp from a URL and reaches OpenCV
|
|
6
|
+
* on its first sample; nobody wants that in their bundle to ask a camera
|
|
7
|
+
* whether it is calibrated. What a consumer needs is a key, a version, and the
|
|
8
|
+
* declarations describing the procedure -- which is all this entry holds.
|
|
9
|
+
*
|
|
10
|
+
* It is published as a sub-entry so a consumer imports these instead of writing
|
|
11
|
+
* its own copy. A copy is checked against nothing: this package could change
|
|
12
|
+
* the shape, the consumer would still typecheck, and the mismatch would surface
|
|
13
|
+
* in a browser. That is not hypothetical here -- this repository's own copy of
|
|
14
|
+
* Camera Source's registry named a method Camera Source does not have, and
|
|
15
|
+
* every attempt to publish a profile failed with a version mismatch that had
|
|
16
|
+
* nothing to do with versions.
|
|
17
|
+
*/
|
|
18
|
+
export { createRuntimeCapability, runtimeCapabilityKey, runtimeCapabilityVersion } from './runtime-capability.js';
|
|
19
|
+
import { runtimeCapabilityKey } from './runtime-capability.js';
|
|
20
|
+
/** The extension ID, as it appears in opcodes and in a project's extension list. */
|
|
21
|
+
export const cameraCalibrationExtensionId = 'kubohiroyacameracalibration';
|
|
22
|
+
/**
|
|
23
|
+
* Narrows a runtime value to this extension's capability.
|
|
24
|
+
*
|
|
25
|
+
* Returns undefined when the extension is absent or its feature is off. Those
|
|
26
|
+
* two are deliberately not distinguished here: the capability is withheld while
|
|
27
|
+
* the flag is off, because a calibration that cannot acquire a camera is not a
|
|
28
|
+
* calibration a caller should be able to start. A version this build does not
|
|
29
|
+
* implement is a different matter -- `requireVersion` refuses that out loud,
|
|
30
|
+
* because the extension is present and cannot do what was asked.
|
|
31
|
+
*/
|
|
32
|
+
export function readCameraCalibrationCapability(runtime) {
|
|
33
|
+
if (typeof runtime !== 'object' || runtime === null)
|
|
34
|
+
return undefined;
|
|
35
|
+
const candidate = runtime[runtimeCapabilityKey];
|
|
36
|
+
if (typeof candidate !== 'object' || candidate === null)
|
|
37
|
+
return undefined;
|
|
38
|
+
const { requireVersion } = candidate;
|
|
39
|
+
return typeof requireVersion === 'function'
|
|
40
|
+
? candidate
|
|
41
|
+
: undefined;
|
|
42
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kubohiroya/turbowarp-camera-calibration",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "A TurboWarp extension that calibrates a shared Camera Source camera and publishes its intrinsic calibration profile.",
|
|
5
5
|
"author": "Hiroya Kubo <hiroya@cuc.ac.jp>",
|
|
6
6
|
"license": "MPL-2.0",
|
|
@@ -26,12 +26,17 @@
|
|
|
26
26
|
"type": "module",
|
|
27
27
|
"exports": {
|
|
28
28
|
"./camera-calibration.js": "./dist/camera-calibration.js",
|
|
29
|
+
"./runtime": {
|
|
30
|
+
"types": "./lib/runtime.d.ts",
|
|
31
|
+
"default": "./lib/runtime.js"
|
|
32
|
+
},
|
|
29
33
|
"./package.json": "./package.json"
|
|
30
34
|
},
|
|
31
35
|
"unpkg": "./dist/camera-calibration.js",
|
|
32
36
|
"jsdelivr": "./dist/camera-calibration.js",
|
|
33
37
|
"files": [
|
|
34
38
|
"dist/",
|
|
39
|
+
"lib/",
|
|
35
40
|
"docs/",
|
|
36
41
|
"schemas/",
|
|
37
42
|
"src/",
|
|
@@ -52,7 +57,7 @@
|
|
|
52
57
|
"scripts": {
|
|
53
58
|
"dev": "vite build --watch",
|
|
54
59
|
"typecheck": "tsc --noEmit",
|
|
55
|
-
"build": "vite build",
|
|
60
|
+
"build": "vite build && tsc -p tsconfig.lib.json",
|
|
56
61
|
"lint": "eslint config src tests scripts vite.config.ts",
|
|
57
62
|
"test": "vitest run",
|
|
58
63
|
"docs": "node scripts/generate-readme.ts",
|
|
@@ -62,12 +67,14 @@
|
|
|
62
67
|
"pack:check": "npm pack --dry-run --ignore-scripts",
|
|
63
68
|
"release:check": "pnpm publish --dry-run --access public --no-git-checks",
|
|
64
69
|
"check": "pnpm run typecheck && pnpm run lint && pnpm run test && pnpm run docs:check && pnpm run check:dist && pnpm run repo:check && pnpm run pack:check",
|
|
65
|
-
"check:dist": "pnpm run build && node scripts/check-dist.ts"
|
|
70
|
+
"check:dist": "pnpm run build && node scripts/check-dist.ts",
|
|
71
|
+
"prepack": "pnpm run build"
|
|
66
72
|
},
|
|
67
73
|
"devDependencies": {
|
|
68
74
|
"@eslint/js": "^9.39.5",
|
|
69
75
|
"@kubohiroya/vite-plugin-turbowarp-extension": "0.4.0",
|
|
70
76
|
"@types/node": "^24.13.3",
|
|
77
|
+
"ajv": "^8.20.0",
|
|
71
78
|
"eslint": "^9.39.5",
|
|
72
79
|
"typescript": "^5.9.3",
|
|
73
80
|
"typescript-eslint": "^8.66.0",
|
|
@@ -75,6 +82,11 @@
|
|
|
75
82
|
"vitest": "^5.0.0"
|
|
76
83
|
},
|
|
77
84
|
"dependencies": {
|
|
85
|
+
"@kubohiroya/turbowarp-camera-source": "0.8.0",
|
|
78
86
|
"@techstark/opencv-js": "4.12.0-release.1"
|
|
79
|
-
}
|
|
87
|
+
},
|
|
88
|
+
"sideEffects": [
|
|
89
|
+
"src/index.ts",
|
|
90
|
+
"dist/*"
|
|
91
|
+
]
|
|
80
92
|
}
|
|
@@ -1,46 +1,28 @@
|
|
|
1
|
+
import {
|
|
2
|
+
cameraSourceCapabilityVersion,
|
|
3
|
+
readCameraSourceCapability,
|
|
4
|
+
readCameraSourceRuntime,
|
|
5
|
+
type CameraFrameSource,
|
|
6
|
+
type CameraIntrinsicProfileV1,
|
|
7
|
+
type CameraLease,
|
|
8
|
+
type CameraSourceCapabilityV1,
|
|
9
|
+
type DistortionModel as CameraSourceDistortionModel
|
|
10
|
+
} from '@kubohiroya/turbowarp-camera-source/runtime';
|
|
11
|
+
import {DISTORTION_COEFFICIENT_COUNTS, type DistortionModel} from './types.js';
|
|
1
12
|
import type {CameraIntrinsicsV1} from './profile.js';
|
|
2
13
|
|
|
3
|
-
|
|
4
|
-
export const CAMERA_SOURCE_RUNTIME_KEY = 'ext_kubohiroyacamerasource';
|
|
14
|
+
export type {CameraFrameSource, CameraLease};
|
|
5
15
|
|
|
6
16
|
/** The lease owner recorded with Camera Source, for diagnostics on its side. */
|
|
7
17
|
export const CALIBRATION_LEASE_OWNER = 'turbowarp-camera-calibration';
|
|
8
18
|
|
|
9
|
-
/**
|
|
10
|
-
|
|
11
|
-
* contract (kubohiroya/turbowarp-camera-source#14); until it ships a matching
|
|
12
|
-
* `calibrationApiVersion`, publishing reports `api-version-mismatch` rather
|
|
13
|
-
* than pretending a profile was registered.
|
|
14
|
-
*/
|
|
15
|
-
export const SUPPORTED_CALIBRATION_API_VERSION = 1;
|
|
19
|
+
/** Recorded on every published profile, so a reader knows what solved it. */
|
|
20
|
+
export const CALIBRATION_PRODUCER = 'turbowarp-camera-calibration';
|
|
16
21
|
|
|
17
|
-
export
|
|
18
|
-
readonly kind: 'video';
|
|
19
|
-
readonly element: HTMLVideoElement;
|
|
20
|
-
readonly width: number;
|
|
21
|
-
readonly height: number;
|
|
22
|
-
readonly mirrored: boolean;
|
|
23
|
-
readonly deviceId: string;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export interface CameraLeasePort {
|
|
27
|
-
getFrameSource(): CameraFrameSourcePort;
|
|
28
|
-
release(): Promise<void>;
|
|
29
|
-
}
|
|
22
|
+
export type {CameraIntrinsicProfileV1};
|
|
30
23
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* The producer side of the profile contract. Camera Source accepts profiles
|
|
37
|
-
* from any calibrator, so this is the only name this extension needs from it
|
|
38
|
-
* beyond the lease.
|
|
39
|
-
*/
|
|
40
|
-
export interface CalibrationProfileRegistryPort {
|
|
41
|
-
readonly calibrationApiVersion: number;
|
|
42
|
-
registerCalibrationProfile(profile: CameraIntrinsicsV1): Promise<void> | void;
|
|
43
|
-
}
|
|
24
|
+
/** The version of Camera Source's runtime capability this extension speaks. */
|
|
25
|
+
export const SUPPORTED_CAMERA_SOURCE_CAPABILITY_VERSION = cameraSourceCapabilityVersion;
|
|
44
26
|
|
|
45
27
|
export type CameraSourceErrorCode = 'dependency-missing' | 'api-version-mismatch';
|
|
46
28
|
|
|
@@ -54,54 +36,127 @@ export class CameraSourceError extends Error {
|
|
|
54
36
|
}
|
|
55
37
|
}
|
|
56
38
|
|
|
57
|
-
/** Returns the Camera Source
|
|
58
|
-
export function requireCameraSource(runtime: TurboWarpRuntime):
|
|
59
|
-
|
|
60
|
-
|
|
39
|
+
/** Returns the Camera Source camera surface, or reports that it is not loaded. */
|
|
40
|
+
export function requireCameraSource(runtime: TurboWarpRuntime): {
|
|
41
|
+
acquireCamera(options: {owner: string; cameraId: string}): Promise<CameraLease>;
|
|
42
|
+
} {
|
|
43
|
+
const candidate = readCameraSourceRuntime(runtime);
|
|
44
|
+
if (!candidate) {
|
|
61
45
|
throw new CameraSourceError(
|
|
62
46
|
'dependency-missing',
|
|
63
47
|
'TurboWarp Camera Source is not loaded. Load it before calibrating; this extension never opens its own camera.'
|
|
64
48
|
);
|
|
65
49
|
}
|
|
66
|
-
return candidate
|
|
50
|
+
return candidate;
|
|
67
51
|
}
|
|
68
52
|
|
|
69
53
|
/**
|
|
70
54
|
* Returns the profile registry when Camera Source publishes a version this
|
|
71
|
-
* extension speaks.
|
|
72
|
-
*
|
|
55
|
+
* extension speaks.
|
|
56
|
+
*
|
|
57
|
+
* The capability key is separate from the extension key on purpose: Camera
|
|
58
|
+
* Source withholds the capability when its own calibration feature is off, so
|
|
59
|
+
* an extension that is loaded but not offering profiles is distinguishable from
|
|
60
|
+
* one that is absent, and neither is reported as the other.
|
|
73
61
|
*/
|
|
74
62
|
export function requireProfileRegistry(
|
|
75
63
|
runtime: TurboWarpRuntime
|
|
76
|
-
):
|
|
77
|
-
|
|
78
|
-
if (!hasFunction(candidate, 'acquireCamera')) {
|
|
64
|
+
): CameraSourceCapabilityV1 {
|
|
65
|
+
if (!readCameraSourceRuntime(runtime)) {
|
|
79
66
|
throw new CameraSourceError(
|
|
80
67
|
'dependency-missing',
|
|
81
68
|
'TurboWarp Camera Source is not loaded, so no calibration profile registry exists.'
|
|
82
69
|
);
|
|
83
70
|
}
|
|
84
|
-
const
|
|
85
|
-
if (!
|
|
71
|
+
const capability = readCameraSourceCapability(runtime);
|
|
72
|
+
if (!capability) {
|
|
86
73
|
throw new CameraSourceError(
|
|
87
74
|
'api-version-mismatch',
|
|
88
|
-
`The loaded Camera Source
|
|
75
|
+
`The loaded Camera Source publishes no calibration profile registry. Version ${SUPPORTED_CAMERA_SOURCE_CAPABILITY_VERSION} of its runtime capability is required.`
|
|
89
76
|
);
|
|
90
77
|
}
|
|
91
|
-
|
|
92
|
-
|
|
78
|
+
try {
|
|
79
|
+
capability.requireVersion(SUPPORTED_CAMERA_SOURCE_CAPABILITY_VERSION);
|
|
80
|
+
} catch (error) {
|
|
93
81
|
throw new CameraSourceError(
|
|
94
82
|
'api-version-mismatch',
|
|
95
|
-
|
|
83
|
+
error instanceof Error ? error.message : String(error)
|
|
96
84
|
);
|
|
97
85
|
}
|
|
98
|
-
return
|
|
86
|
+
return capability;
|
|
99
87
|
}
|
|
100
88
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
89
|
+
/**
|
|
90
|
+
* Restates a solved profile as the document Camera Source validates.
|
|
91
|
+
*
|
|
92
|
+
* The two shapes are not the same and never were: this extension stores a flat
|
|
93
|
+
* profile with a packed nine-number matrix, and Camera Source names the pinhole
|
|
94
|
+
* parameters individually because an array cannot say whether it is row-major.
|
|
95
|
+
* The conversion happens here, at the boundary, so that what a project has
|
|
96
|
+
* saved and what `cameraCalibrationJson` reports keep their existing shape --
|
|
97
|
+
* an SB3 holding either format still imports.
|
|
98
|
+
*
|
|
99
|
+
* Nothing is invented on the way across. The image is stated as raw or already
|
|
100
|
+
* undistorted from `imageState` rather than assumed, and a distortion model
|
|
101
|
+
* with no counterpart is refused instead of being relabelled as the nearest
|
|
102
|
+
* one, which would leave a consumer applying the wrong coefficients to real
|
|
103
|
+
* pixels and getting a plausible wrong answer.
|
|
104
|
+
*/
|
|
105
|
+
export function toCameraSourceProfile(profile: CameraIntrinsicsV1): CameraIntrinsicProfileV1 {
|
|
106
|
+
const [fx, skew, cx, , fy, cy] = profile.intrinsicMatrix as [
|
|
107
|
+
number,
|
|
108
|
+
number,
|
|
109
|
+
number,
|
|
110
|
+
number,
|
|
111
|
+
number,
|
|
112
|
+
number
|
|
113
|
+
];
|
|
114
|
+
const document = {
|
|
115
|
+
schema: 'twcs/camera-intrinsics',
|
|
116
|
+
profileId: profile.calibrationId,
|
|
117
|
+
cameraId: profile.cameraId,
|
|
118
|
+
calibratedAt: profile.calibratedAt,
|
|
119
|
+
producer: CALIBRATION_PRODUCER,
|
|
120
|
+
cameraModel: profile.cameraModel,
|
|
121
|
+
image: {
|
|
122
|
+
width: profile.imageWidth,
|
|
123
|
+
height: profile.imageHeight,
|
|
124
|
+
undistorted: profile.imageState === 'undistorted'
|
|
125
|
+
},
|
|
126
|
+
intrinsics: {fx, fy, cx, cy, skew},
|
|
127
|
+
distortion: {
|
|
128
|
+
model: cameraSourceDistortionModel(profile.distortionModel),
|
|
129
|
+
coefficients: [...profile.distortionCoefficients]
|
|
130
|
+
}
|
|
131
|
+
} as const satisfies Omit<CameraIntrinsicProfileV1, 'version' | 'quality'>;
|
|
132
|
+
return profile.quality
|
|
133
|
+
? {...document, version: 1, quality: {...profile.quality}}
|
|
134
|
+
: {...document, version: 1};
|
|
107
135
|
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Names this extension's distortion model the way Camera Source names it.
|
|
139
|
+
*
|
|
140
|
+
* `opencv-plumb-bob` and `opencv-rational` are the radial-tangential family
|
|
141
|
+
* Camera Source calls `brown-conrady`, in the same coefficient order. The thin
|
|
142
|
+
* prism and tilted-sensor layouts have no counterpart in that contract, so a
|
|
143
|
+
* profile using one cannot be published rather than being sent under a name
|
|
144
|
+
* that would make a consumer read its coefficients as something else.
|
|
145
|
+
*/
|
|
146
|
+
export function cameraSourceDistortionModel(
|
|
147
|
+
model: DistortionModel
|
|
148
|
+
): CameraSourceDistortionModel {
|
|
149
|
+
switch (model) {
|
|
150
|
+
case 'none':
|
|
151
|
+
return 'none';
|
|
152
|
+
case 'opencv-plumb-bob':
|
|
153
|
+
case 'opencv-rational':
|
|
154
|
+
return 'brown-conrady';
|
|
155
|
+
default:
|
|
156
|
+
throw new CameraSourceError(
|
|
157
|
+
'api-version-mismatch',
|
|
158
|
+
`Camera Source has no distortion model matching ${model} (${String(DISTORTION_COEFFICIENT_COUNTS[model])} coefficients), so this profile cannot be published.`
|
|
159
|
+
);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
@@ -0,0 +1,51 @@
|
|
|
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
|
+
|
|
12
|
+
export type CalibrationState =
|
|
13
|
+
| 'idle'
|
|
14
|
+
| 'acquiring-camera'
|
|
15
|
+
| 'sampling'
|
|
16
|
+
| 'ready'
|
|
17
|
+
| 'solving'
|
|
18
|
+
| 'solved'
|
|
19
|
+
| 'cancelling'
|
|
20
|
+
| 'error';
|
|
21
|
+
|
|
22
|
+
export type CalibrationErrorCode =
|
|
23
|
+
| ''
|
|
24
|
+
| 'dependency-missing'
|
|
25
|
+
| 'api-version-mismatch'
|
|
26
|
+
| 'invalid-board'
|
|
27
|
+
| 'camera-unavailable'
|
|
28
|
+
| 'camera-ended'
|
|
29
|
+
| 'resolution-mismatch'
|
|
30
|
+
| 'capture-condition-mismatch'
|
|
31
|
+
| 'board-not-found'
|
|
32
|
+
| 'sample-low-quality'
|
|
33
|
+
| 'sample-too-similar'
|
|
34
|
+
| 'sample-limit'
|
|
35
|
+
| 'sample-insufficient'
|
|
36
|
+
| 'sample-failed'
|
|
37
|
+
| 'solve-failed'
|
|
38
|
+
| 'reprojection-too-high'
|
|
39
|
+
| 'invalid-calibration'
|
|
40
|
+
| 'credential-forbidden'
|
|
41
|
+
| 'calibration-not-applicable'
|
|
42
|
+
| 'not-calibrated'
|
|
43
|
+
| 'publish-failed';
|
|
44
|
+
|
|
45
|
+
export interface CalibrationStartOptions {
|
|
46
|
+
cameraId: string;
|
|
47
|
+
calibrationId: string;
|
|
48
|
+
board: CalibrationBoard;
|
|
49
|
+
/** The session is refused rather than solved when the RMS exceeds this. */
|
|
50
|
+
maximumReprojectionErrorPx: number;
|
|
51
|
+
}
|
|
@@ -3,8 +3,10 @@ import {
|
|
|
3
3
|
CameraSourceError,
|
|
4
4
|
requireCameraSource,
|
|
5
5
|
requireProfileRegistry,
|
|
6
|
-
|
|
7
|
-
type
|
|
6
|
+
toCameraSourceProfile,
|
|
7
|
+
type CameraFrameSource,
|
|
8
|
+
type CameraIntrinsicProfileV1,
|
|
9
|
+
type CameraLease
|
|
8
10
|
} from './camera-source.js';
|
|
9
11
|
import {
|
|
10
12
|
assertCameraIntrinsics,
|
|
@@ -12,6 +14,16 @@ import {
|
|
|
12
14
|
parseCalibrationProfile,
|
|
13
15
|
type CameraIntrinsicsV1
|
|
14
16
|
} from './profile.js';
|
|
17
|
+
export type {
|
|
18
|
+
CalibrationErrorCode,
|
|
19
|
+
CalibrationStartOptions,
|
|
20
|
+
CalibrationState
|
|
21
|
+
} from './contract.js';
|
|
22
|
+
import type {
|
|
23
|
+
CalibrationErrorCode,
|
|
24
|
+
CalibrationStartOptions,
|
|
25
|
+
CalibrationState
|
|
26
|
+
} from './contract.js';
|
|
15
27
|
import type {
|
|
16
28
|
CalibrationBackendFactory,
|
|
17
29
|
CalibrationBackendPort,
|
|
@@ -31,46 +43,6 @@ const PROFILE_ERROR_CODES: ReadonlySet<string> = new Set([
|
|
|
31
43
|
'calibration-not-applicable'
|
|
32
44
|
]);
|
|
33
45
|
|
|
34
|
-
export type CalibrationState =
|
|
35
|
-
| 'idle'
|
|
36
|
-
| 'acquiring-camera'
|
|
37
|
-
| 'sampling'
|
|
38
|
-
| 'ready'
|
|
39
|
-
| 'solving'
|
|
40
|
-
| 'solved'
|
|
41
|
-
| 'cancelling'
|
|
42
|
-
| 'error';
|
|
43
|
-
|
|
44
|
-
export type CalibrationErrorCode =
|
|
45
|
-
| ''
|
|
46
|
-
| 'dependency-missing'
|
|
47
|
-
| 'api-version-mismatch'
|
|
48
|
-
| 'invalid-board'
|
|
49
|
-
| 'camera-unavailable'
|
|
50
|
-
| 'camera-ended'
|
|
51
|
-
| 'resolution-mismatch'
|
|
52
|
-
| 'capture-condition-mismatch'
|
|
53
|
-
| 'board-not-found'
|
|
54
|
-
| 'sample-low-quality'
|
|
55
|
-
| 'sample-too-similar'
|
|
56
|
-
| 'sample-limit'
|
|
57
|
-
| 'sample-insufficient'
|
|
58
|
-
| 'sample-failed'
|
|
59
|
-
| 'solve-failed'
|
|
60
|
-
| 'reprojection-too-high'
|
|
61
|
-
| 'invalid-calibration'
|
|
62
|
-
| 'credential-forbidden'
|
|
63
|
-
| 'calibration-not-applicable'
|
|
64
|
-
| 'not-calibrated'
|
|
65
|
-
| 'publish-failed';
|
|
66
|
-
|
|
67
|
-
export interface CalibrationStartOptions {
|
|
68
|
-
cameraId: string;
|
|
69
|
-
calibrationId: string;
|
|
70
|
-
board: CalibrationBoard;
|
|
71
|
-
maximumReprojectionErrorPx: number;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
46
|
export interface CameraCalibrationControllerOptions {
|
|
75
47
|
runtime: TurboWarpRuntime;
|
|
76
48
|
backend: CalibrationBackendFactory;
|
|
@@ -84,7 +56,7 @@ interface CalibrationSession {
|
|
|
84
56
|
readonly imageWidth: number;
|
|
85
57
|
readonly imageHeight: number;
|
|
86
58
|
readonly deviceId: string;
|
|
87
|
-
readonly
|
|
59
|
+
readonly previewFlip: string;
|
|
88
60
|
}
|
|
89
61
|
|
|
90
62
|
/**
|
|
@@ -93,7 +65,7 @@ interface CalibrationSession {
|
|
|
93
65
|
*/
|
|
94
66
|
class CameraCalibration {
|
|
95
67
|
private session: CalibrationSession | undefined;
|
|
96
|
-
private lease:
|
|
68
|
+
private lease: CameraLease | undefined;
|
|
97
69
|
private samples: CalibrationSample[] = [];
|
|
98
70
|
private acquiring: Promise<void> | undefined;
|
|
99
71
|
private sampling: Promise<void> | undefined;
|
|
@@ -137,7 +109,7 @@ class CameraCalibration {
|
|
|
137
109
|
const operation = ++this.operation;
|
|
138
110
|
this.calibrationState = 'acquiring-camera';
|
|
139
111
|
this.clearError();
|
|
140
|
-
let lease:
|
|
112
|
+
let lease: CameraLease;
|
|
141
113
|
try {
|
|
142
114
|
lease = await requireCameraSource(this.runtime).acquireCamera({
|
|
143
115
|
owner: CALIBRATION_LEASE_OWNER,
|
|
@@ -155,7 +127,7 @@ class CameraCalibration {
|
|
|
155
127
|
await lease.release();
|
|
156
128
|
return;
|
|
157
129
|
}
|
|
158
|
-
let frame:
|
|
130
|
+
let frame: CameraFrameSource;
|
|
159
131
|
try {
|
|
160
132
|
frame = requireVideoFrame(lease);
|
|
161
133
|
} catch (error) {
|
|
@@ -169,7 +141,7 @@ class CameraCalibration {
|
|
|
169
141
|
imageWidth: frame.width,
|
|
170
142
|
imageHeight: frame.height,
|
|
171
143
|
deviceId: frame.deviceId,
|
|
172
|
-
|
|
144
|
+
previewFlip: frame.previewFlip
|
|
173
145
|
};
|
|
174
146
|
this.lease = lease;
|
|
175
147
|
this.samples = [];
|
|
@@ -298,11 +270,29 @@ class CameraCalibration {
|
|
|
298
270
|
} catch (error) {
|
|
299
271
|
this.refuseWith(errorCodeFor(error), error);
|
|
300
272
|
}
|
|
273
|
+
let document: CameraIntrinsicProfileV1;
|
|
274
|
+
try {
|
|
275
|
+
document = toCameraSourceProfile(profile);
|
|
276
|
+
} catch (error) {
|
|
277
|
+
this.refuseWith(errorCodeFor(error), error);
|
|
278
|
+
}
|
|
279
|
+
let result;
|
|
301
280
|
try {
|
|
302
|
-
|
|
281
|
+
result = registry.registerProfile(document);
|
|
303
282
|
} catch (error) {
|
|
304
283
|
this.refuseWith('publish-failed', error);
|
|
305
284
|
}
|
|
285
|
+
// A registry that answers rather than throws is the normal path: Camera
|
|
286
|
+
// Source validates the document and reports which member it objected to.
|
|
287
|
+
// Reading only the thrown case would record a refusal as a success.
|
|
288
|
+
if (!result.ok) {
|
|
289
|
+
this.refuseWith(
|
|
290
|
+
'publish-failed',
|
|
291
|
+
new Error(
|
|
292
|
+
`Camera Source refused the profile: ${result.error?.code ?? 'unknown'} at ${result.error?.path || '/'} -- ${result.error?.message ?? 'no detail reported'}`
|
|
293
|
+
)
|
|
294
|
+
);
|
|
295
|
+
}
|
|
306
296
|
this.clearError();
|
|
307
297
|
}
|
|
308
298
|
|
|
@@ -346,7 +336,7 @@ class CameraCalibration {
|
|
|
346
336
|
this.reject('sample-limit', `At most ${MAXIMUM_SAMPLES} samples may be retained.`);
|
|
347
337
|
}
|
|
348
338
|
this.calibrationState = 'sampling';
|
|
349
|
-
let frame:
|
|
339
|
+
let frame: CameraFrameSource;
|
|
350
340
|
try {
|
|
351
341
|
frame = requireVideoFrame(lease);
|
|
352
342
|
} catch (error) {
|
|
@@ -361,7 +351,7 @@ class CameraCalibration {
|
|
|
361
351
|
`Expected ${session.imageWidth}x${session.imageHeight}, received ${frame.width}x${frame.height}.`
|
|
362
352
|
);
|
|
363
353
|
}
|
|
364
|
-
if (frame.deviceId !== session.deviceId || frame.
|
|
354
|
+
if (frame.deviceId !== session.deviceId || frame.previewFlip !== session.previewFlip) {
|
|
365
355
|
this.reject(
|
|
366
356
|
'capture-condition-mismatch',
|
|
367
357
|
`The capture conditions changed after the session started. Restart the calibration for camera ${this.cameraId}.`
|
|
@@ -650,7 +640,7 @@ function errorCodeFor(error: unknown): CalibrationErrorCode {
|
|
|
650
640
|
return 'invalid-calibration';
|
|
651
641
|
}
|
|
652
642
|
|
|
653
|
-
function requireVideoFrame(lease:
|
|
643
|
+
function requireVideoFrame(lease: CameraLease): CameraFrameSource {
|
|
654
644
|
const frame = lease.getFrameSource();
|
|
655
645
|
if (frame.kind !== 'video' || frame.width < 1 || frame.height < 1) {
|
|
656
646
|
throw new Error('Camera Source has no current video frame.');
|