@kubohiroya/turbowarp-camera-calibration 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json ADDED
@@ -0,0 +1,80 @@
1
+ {
2
+ "name": "@kubohiroya/turbowarp-camera-calibration",
3
+ "version": "0.1.0",
4
+ "description": "A TurboWarp extension that calibrates a shared Camera Source camera and publishes its intrinsic calibration profile.",
5
+ "author": "Hiroya Kubo <hiroya@cuc.ac.jp>",
6
+ "license": "MPL-2.0",
7
+ "homepage": "https://kubohiroya.github.io/turbowarp-camera-calibration/",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git+https://github.com/kubohiroya/turbowarp-camera-calibration.git"
11
+ },
12
+ "bugs": {
13
+ "url": "https://github.com/kubohiroya/turbowarp-camera-calibration/issues"
14
+ },
15
+ "keywords": [
16
+ "turbowarp",
17
+ "scratch",
18
+ "extension",
19
+ "camera",
20
+ "calibration",
21
+ "intrinsics",
22
+ "camera-source",
23
+ "vite",
24
+ "typescript"
25
+ ],
26
+ "type": "module",
27
+ "exports": {
28
+ "./camera-calibration.js": "./dist/camera-calibration.js",
29
+ "./package.json": "./package.json"
30
+ },
31
+ "unpkg": "./dist/camera-calibration.js",
32
+ "jsdelivr": "./dist/camera-calibration.js",
33
+ "files": [
34
+ "dist/",
35
+ "docs/",
36
+ "schemas/",
37
+ "src/",
38
+ "templates/",
39
+ "README.md",
40
+ "README.ja.md",
41
+ "LICENSE",
42
+ "THIRD_PARTY_NOTICES.md"
43
+ ],
44
+ "publishConfig": {
45
+ "access": "public",
46
+ "registry": "https://registry.npmjs.org/"
47
+ },
48
+ "packageManager": "pnpm@11.11.0",
49
+ "engines": {
50
+ "node": ">=22.18.0"
51
+ },
52
+ "scripts": {
53
+ "dev": "vite build --watch",
54
+ "typecheck": "tsc --noEmit",
55
+ "build": "vite build",
56
+ "lint": "eslint config src tests scripts vite.config.ts",
57
+ "test": "vitest run",
58
+ "docs": "node scripts/generate-readme.ts",
59
+ "docs:check": "pnpm run docs && git diff --exit-code -- README.md",
60
+ "template:readme": "node scripts/apply-readme-template.ts",
61
+ "repo:check": "node scripts/check-repo.ts",
62
+ "pack:check": "npm pack --dry-run --ignore-scripts",
63
+ "release:check": "pnpm publish --dry-run --access public --no-git-checks",
64
+ "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"
66
+ },
67
+ "devDependencies": {
68
+ "@eslint/js": "^9.39.5",
69
+ "@kubohiroya/vite-plugin-turbowarp-extension": "0.4.0",
70
+ "@types/node": "^24.13.3",
71
+ "eslint": "^9.39.5",
72
+ "typescript": "^5.9.3",
73
+ "typescript-eslint": "^8.66.0",
74
+ "vite": "^8.2.2",
75
+ "vitest": "^5.0.0"
76
+ },
77
+ "dependencies": {
78
+ "@techstark/opencv-js": "4.12.0-release.1"
79
+ }
80
+ }
@@ -0,0 +1,72 @@
1
+ {
2
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
3
+ "$id": "https://kubohiroya.github.io/turbowarp-camera-calibration/schema/camera-intrinsics-v1.json",
4
+ "title": "Camera intrinsic calibration v1",
5
+ "description": "One camera's optics at one image size. Intrinsics only: this profile never carries an external world pose.",
6
+ "type": "object",
7
+ "additionalProperties": false,
8
+ "required": [
9
+ "schema",
10
+ "version",
11
+ "calibrationId",
12
+ "cameraId",
13
+ "cameraModel",
14
+ "imageWidth",
15
+ "imageHeight",
16
+ "imageState",
17
+ "intrinsicMatrix",
18
+ "distortionModel",
19
+ "distortionCoefficients",
20
+ "calibratedAt"
21
+ ],
22
+ "properties": {
23
+ "schema": {"const": "camerasource/camera-intrinsics"},
24
+ "version": {"const": 1},
25
+ "calibrationId": {"type": "string", "pattern": "^[A-Za-z0-9._-]{1,64}$"},
26
+ "cameraId": {"type": "string", "pattern": "^[A-Za-z0-9._-]{1,64}$"},
27
+ "cameraModel": {"enum": ["pinhole"]},
28
+ "imageWidth": {"type": "integer", "minimum": 1, "maximum": 16384},
29
+ "imageHeight": {"type": "integer", "minimum": 1, "maximum": 16384},
30
+ "imageState": {
31
+ "enum": ["raw", "undistorted"],
32
+ "description": "raw means the coefficients still have to be applied; undistorted means the image is already corrected and they must not be applied again."
33
+ },
34
+ "intrinsicMatrix": {
35
+ "type": "array",
36
+ "minItems": 9,
37
+ "maxItems": 9,
38
+ "items": {"type": "number", "minimum": -1000000, "maximum": 1000000},
39
+ "description": "Row-major zero-skew pinhole matrix [fx, 0, cx, 0, fy, cy, 0, 0, 1] in pixels of the calibrated image size."
40
+ },
41
+ "distortionModel": {
42
+ "enum": [
43
+ "none",
44
+ "opencv-plumb-bob",
45
+ "opencv-rational",
46
+ "opencv-thin-prism",
47
+ "opencv-tilted"
48
+ ]
49
+ },
50
+ "distortionCoefficients": {
51
+ "type": "array",
52
+ "minItems": 0,
53
+ "maxItems": 14,
54
+ "items": {"type": "number", "minimum": -1000000, "maximum": 1000000},
55
+ "description": "Coefficients in OpenCV order. The count must match distortionModel: 0, 4 or 5, 8, 12, or 14."
56
+ },
57
+ "quality": {
58
+ "type": "object",
59
+ "additionalProperties": false,
60
+ "required": ["sampleCount", "reprojectionErrorPx"],
61
+ "properties": {
62
+ "sampleCount": {"type": "integer", "minimum": 0},
63
+ "reprojectionErrorPx": {"type": "number", "minimum": 0, "maximum": 1000000}
64
+ },
65
+ "description": "Absent when the producer of the profile did not measure it."
66
+ },
67
+ "calibratedAt": {
68
+ "type": "string",
69
+ "pattern": "^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(?:\\.\\d+)?Z$"
70
+ }
71
+ }
72
+ }
@@ -0,0 +1,49 @@
1
+ {
2
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
3
+ "$id": "https://raw.githubusercontent.com/kubohiroya/turbowarp-extension-template/main/schemas/extension-manifest.schema.json",
4
+ "title": "TurboWarp Extension API Manifest",
5
+ "type": "object",
6
+ "additionalProperties": false,
7
+ "required": ["formatVersion", "id", "blocks", "menus"],
8
+ "properties": {
9
+ "formatVersion": {"const": 1},
10
+ "id": {"type": "string", "pattern": "^[a-z0-9]+$"},
11
+ "blocks": {
12
+ "type": "array",
13
+ "items": {
14
+ "type": "object",
15
+ "additionalProperties": false,
16
+ "required": ["opcode", "blockType", "arguments"],
17
+ "properties": {
18
+ "opcode": {"type": "string", "minLength": 1},
19
+ "blockType": {"type": "string", "minLength": 1},
20
+ "arguments": {
21
+ "type": "array",
22
+ "items": {
23
+ "type": "object",
24
+ "additionalProperties": false,
25
+ "required": ["id", "type"],
26
+ "properties": {
27
+ "id": {"type": "string", "minLength": 1},
28
+ "type": {"type": "string", "minLength": 1},
29
+ "menu": {"type": "string", "minLength": 1}
30
+ }
31
+ }
32
+ }
33
+ }
34
+ }
35
+ },
36
+ "menus": {
37
+ "type": "array",
38
+ "items": {
39
+ "type": "object",
40
+ "additionalProperties": false,
41
+ "required": ["id", "acceptReporters"],
42
+ "properties": {
43
+ "id": {"type": "string", "minLength": 1},
44
+ "acceptReporters": {"type": "boolean"}
45
+ }
46
+ }
47
+ }
48
+ }
49
+ }
@@ -0,0 +1,180 @@
1
+ {
2
+ "extensionName": "TurboWarp-Camera-Calibration",
3
+ "blocks": [
4
+ {
5
+ "opcode": "startCameraCalibration",
6
+ "feature": "cameraCalibrationV1",
7
+ "blockType": "COMMAND",
8
+ "text": "start camera [CAMERA_ID] calibration [CALIBRATION_ID] board [COLUMNS] by [ROWS] square [SQUARE_METERS] m max error [MAX_ERROR_PX] px",
9
+ "description": "Leases one shared Camera Source camera and fixes its real capture resolution for a chessboard calibration session. The board is measured in inner corners, not printed squares.",
10
+ "arguments": {
11
+ "CAMERA_ID": {"type": "STRING", "defaultValue": "default"},
12
+ "CALIBRATION_ID": {"type": "STRING", "defaultValue": "calibration-1"},
13
+ "COLUMNS": {"type": "NUMBER", "defaultValue": 9},
14
+ "ROWS": {"type": "NUMBER", "defaultValue": 6},
15
+ "SQUARE_METERS": {"type": "NUMBER", "defaultValue": 0.025},
16
+ "MAX_ERROR_PX": {"type": "NUMBER", "defaultValue": 1.5}
17
+ }
18
+ },
19
+ {
20
+ "opcode": "addCameraCalibrationSample",
21
+ "feature": "cameraCalibrationV1",
22
+ "blockType": "COMMAND",
23
+ "text": "add calibration sample for camera [CAMERA_ID]",
24
+ "description": "Detects the complete board in the current shared frame and retains it when its quality and its novelty against the retained samples both pass.",
25
+ "arguments": {
26
+ "CAMERA_ID": {"type": "STRING", "defaultValue": "default"}
27
+ }
28
+ },
29
+ {
30
+ "opcode": "solveCameraCalibration",
31
+ "feature": "cameraCalibrationV1",
32
+ "blockType": "COMMAND",
33
+ "text": "solve calibration for camera [CAMERA_ID]",
34
+ "description": "Solves the intrinsic matrix and distortion coefficients from at least eight accepted samples, then releases the camera lease. No external pose is produced.",
35
+ "arguments": {
36
+ "CAMERA_ID": {"type": "STRING", "defaultValue": "default"}
37
+ }
38
+ },
39
+ {
40
+ "opcode": "cancelCameraCalibration",
41
+ "feature": "cameraCalibrationV1",
42
+ "blockType": "COMMAND",
43
+ "text": "cancel calibration for camera [CAMERA_ID]",
44
+ "description": "Releases the camera lease and the retained samples while preserving the last validated profile.",
45
+ "arguments": {
46
+ "CAMERA_ID": {"type": "STRING", "defaultValue": "default"}
47
+ }
48
+ },
49
+ {
50
+ "opcode": "cleanupCameraCalibration",
51
+ "feature": "cameraCalibrationV1",
52
+ "blockType": "COMMAND",
53
+ "text": "cleanup calibration for camera [CAMERA_ID]",
54
+ "description": "Releases the session and also clears the in-memory calibration profile for that camera.",
55
+ "arguments": {
56
+ "CAMERA_ID": {"type": "STRING", "defaultValue": "default"}
57
+ }
58
+ },
59
+ {
60
+ "opcode": "publishCameraCalibration",
61
+ "feature": "cameraCalibrationV1",
62
+ "blockType": "COMMAND",
63
+ "text": "publish calibration profile for camera [CAMERA_ID]",
64
+ "description": "Registers the solved profile with Camera Source, which owns the profile contract. Reports an explicit error when Camera Source is absent, speaks another contract version, or has no profile to publish.",
65
+ "arguments": {
66
+ "CAMERA_ID": {"type": "STRING", "defaultValue": "default"}
67
+ }
68
+ },
69
+ {
70
+ "opcode": "importCameraCalibration",
71
+ "feature": "cameraCalibrationV1",
72
+ "blockType": "COMMAND",
73
+ "text": "import calibration profile [JSON] for camera [CAMERA_ID]",
74
+ "description": "Imports a calibration profile after schema, credential, and applicability checks. Pre-migration profiles are accepted and their world pose is dropped rather than reinterpreted.",
75
+ "arguments": {
76
+ "JSON": {"type": "STRING", "defaultValue": "{}"},
77
+ "CAMERA_ID": {"type": "STRING", "defaultValue": "default"}
78
+ }
79
+ },
80
+ {
81
+ "opcode": "cameraCalibrationJsonValid",
82
+ "feature": "cameraCalibrationV1",
83
+ "blockType": "BOOLEAN",
84
+ "text": "calibration profile [JSON] valid for camera [CAMERA_ID]?",
85
+ "description": "Validates a profile and records why it failed, without replacing the stored profile.",
86
+ "arguments": {
87
+ "JSON": {"type": "STRING", "defaultValue": "{}"},
88
+ "CAMERA_ID": {"type": "STRING", "defaultValue": "default"}
89
+ }
90
+ },
91
+ {
92
+ "opcode": "cameraCalibrationReady",
93
+ "feature": "cameraCalibrationV1",
94
+ "blockType": "BOOLEAN",
95
+ "text": "camera [CAMERA_ID] calibration ready?",
96
+ "description": "Reports whether a fixed-resolution session for that camera can accept a sample or solve.",
97
+ "arguments": {
98
+ "CAMERA_ID": {"type": "STRING", "defaultValue": "default"}
99
+ }
100
+ },
101
+ {
102
+ "opcode": "cameraCalibrationState",
103
+ "feature": "always",
104
+ "blockType": "REPORTER",
105
+ "text": "camera calibration state [CAMERA_ID]",
106
+ "description": "Returns the calibration session state for one shared Camera Source camera: idle, acquiring-camera, sampling, ready, solving, solved, cancelling, or error. A camera that has never been calibrated reports idle.",
107
+ "arguments": {
108
+ "CAMERA_ID": {"type": "STRING", "defaultValue": "default"}
109
+ }
110
+ },
111
+ {
112
+ "opcode": "cameraCalibrationBackend",
113
+ "feature": "cameraCalibrationV1",
114
+ "blockType": "REPORTER",
115
+ "text": "camera calibration backend",
116
+ "description": "Returns the pinned solver identifier. Reading it does not load the solver.",
117
+ "arguments": {}
118
+ },
119
+ {
120
+ "opcode": "cameraCalibrationSampleCount",
121
+ "feature": "cameraCalibrationV1",
122
+ "blockType": "REPORTER",
123
+ "text": "camera [CAMERA_ID] calibration sample count",
124
+ "description": "Returns the accepted sample count for the current or last solved session.",
125
+ "arguments": {
126
+ "CAMERA_ID": {"type": "STRING", "defaultValue": "default"}
127
+ }
128
+ },
129
+ {
130
+ "opcode": "cameraCalibrationSampleQuality",
131
+ "feature": "cameraCalibrationV1",
132
+ "blockType": "REPORTER",
133
+ "text": "camera [CAMERA_ID] calibration sample quality",
134
+ "description": "Returns the latest accepted sample's combined board coverage and sharpness score, from zero to one.",
135
+ "arguments": {
136
+ "CAMERA_ID": {"type": "STRING", "defaultValue": "default"}
137
+ }
138
+ },
139
+ {
140
+ "opcode": "cameraCalibrationReprojectionError",
141
+ "feature": "cameraCalibrationV1",
142
+ "blockType": "REPORTER",
143
+ "text": "camera [CAMERA_ID] calibration reprojection error px",
144
+ "description": "Returns the RMS reprojection error in pixels for the latest solve or imported profile.",
145
+ "arguments": {
146
+ "CAMERA_ID": {"type": "STRING", "defaultValue": "default"}
147
+ }
148
+ },
149
+ {
150
+ "opcode": "cameraCalibrationErrorCode",
151
+ "feature": "cameraCalibrationV1",
152
+ "blockType": "REPORTER",
153
+ "text": "camera [CAMERA_ID] calibration error code",
154
+ "description": "Returns a stable code for dependency, board, camera, sample, solve, reprojection, profile, or publication errors, or an empty string.",
155
+ "arguments": {
156
+ "CAMERA_ID": {"type": "STRING", "defaultValue": "default"}
157
+ }
158
+ },
159
+ {
160
+ "opcode": "cameraCalibrationError",
161
+ "feature": "cameraCalibrationV1",
162
+ "blockType": "REPORTER",
163
+ "text": "camera [CAMERA_ID] calibration error",
164
+ "description": "Returns the detailed calibration diagnostic for that camera.",
165
+ "arguments": {
166
+ "CAMERA_ID": {"type": "STRING", "defaultValue": "default"}
167
+ }
168
+ },
169
+ {
170
+ "opcode": "cameraCalibrationJson",
171
+ "feature": "cameraCalibrationV1",
172
+ "blockType": "REPORTER",
173
+ "text": "camera [CAMERA_ID] calibration profile JSON",
174
+ "description": "Exports the stored intrinsic profile for that camera, or an empty string when none exists.",
175
+ "arguments": {
176
+ "CAMERA_ID": {"type": "STRING", "defaultValue": "default"}
177
+ }
178
+ }
179
+ ]
180
+ }
@@ -0,0 +1,107 @@
1
+ import type {CameraIntrinsicsV1} from './profile.js';
2
+
3
+ /** The runtime key Camera Source publishes itself under. */
4
+ export const CAMERA_SOURCE_RUNTIME_KEY = 'ext_kubohiroyacamerasource';
5
+
6
+ /** The lease owner recorded with Camera Source, for diagnostics on its side. */
7
+ export const CALIBRATION_LEASE_OWNER = 'turbowarp-camera-calibration';
8
+
9
+ /**
10
+ * The profile contract version this extension speaks. Camera Source owns the
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;
16
+
17
+ export interface CameraFrameSourcePort {
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
+ }
30
+
31
+ export interface CameraSourcePort {
32
+ acquireCamera(options: {owner: string; cameraId: string}): Promise<CameraLeasePort>;
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
+ }
44
+
45
+ export type CameraSourceErrorCode = 'dependency-missing' | 'api-version-mismatch';
46
+
47
+ export class CameraSourceError extends Error {
48
+ public constructor(
49
+ public readonly code: CameraSourceErrorCode,
50
+ message: string
51
+ ) {
52
+ super(message);
53
+ this.name = 'CameraSourceError';
54
+ }
55
+ }
56
+
57
+ /** Returns the Camera Source capability, or reports that it is not loaded. */
58
+ export function requireCameraSource(runtime: TurboWarpRuntime): CameraSourcePort {
59
+ const candidate = runtime[CAMERA_SOURCE_RUNTIME_KEY];
60
+ if (!hasFunction(candidate, 'acquireCamera')) {
61
+ throw new CameraSourceError(
62
+ 'dependency-missing',
63
+ 'TurboWarp Camera Source is not loaded. Load it before calibrating; this extension never opens its own camera.'
64
+ );
65
+ }
66
+ return candidate as unknown as CameraSourcePort;
67
+ }
68
+
69
+ /**
70
+ * Returns the profile registry when Camera Source publishes a version this
71
+ * extension speaks. A missing registry is an explicit mismatch, never a
72
+ * silently skipped publication.
73
+ */
74
+ export function requireProfileRegistry(
75
+ runtime: TurboWarpRuntime
76
+ ): CalibrationProfileRegistryPort {
77
+ const candidate = runtime[CAMERA_SOURCE_RUNTIME_KEY];
78
+ if (!hasFunction(candidate, 'acquireCamera')) {
79
+ throw new CameraSourceError(
80
+ 'dependency-missing',
81
+ 'TurboWarp Camera Source is not loaded, so no calibration profile registry exists.'
82
+ );
83
+ }
84
+ const registry = candidate as Record<string, unknown>;
85
+ if (!hasFunction(registry, 'registerCalibrationProfile')) {
86
+ throw new CameraSourceError(
87
+ 'api-version-mismatch',
88
+ `The loaded Camera Source has no calibration profile registry. Version ${SUPPORTED_CALIBRATION_API_VERSION} of the profile contract is required.`
89
+ );
90
+ }
91
+ const version = registry.calibrationApiVersion;
92
+ if (version !== SUPPORTED_CALIBRATION_API_VERSION) {
93
+ throw new CameraSourceError(
94
+ 'api-version-mismatch',
95
+ `Camera Source publishes calibration profile contract ${String(version)}; this extension speaks ${SUPPORTED_CALIBRATION_API_VERSION}.`
96
+ );
97
+ }
98
+ return registry as unknown as CalibrationProfileRegistryPort;
99
+ }
100
+
101
+ function hasFunction(value: unknown, name: string): boolean {
102
+ return (
103
+ typeof value === 'object' &&
104
+ value !== null &&
105
+ typeof (value as Record<string, unknown>)[name] === 'function'
106
+ );
107
+ }