@kubohiroya/turbowarp-camera-source 0.10.0 → 0.12.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 +74 -4
- package/README.md +148 -4
- package/dist/calibration/camera-info.d.ts +32 -0
- package/dist/calibration/camera-info.d.ts.map +1 -0
- package/dist/calibration/camera-info.js +336 -0
- package/dist/calibration/camera-info.js.map +1 -0
- package/dist/calibration/profile-text.d.ts +10 -0
- package/dist/calibration/profile-text.d.ts.map +1 -0
- package/dist/calibration/profile-text.js +25 -0
- package/dist/calibration/profile-text.js.map +1 -0
- package/dist/calibration/profile.d.ts +47 -0
- package/dist/calibration/profile.d.ts.map +1 -0
- package/dist/calibration/profile.js +479 -0
- package/dist/calibration/profile.js.map +1 -0
- package/dist/calibration/yaml.d.ts +21 -0
- package/dist/calibration/yaml.d.ts.map +1 -0
- package/dist/calibration/yaml.js +441 -0
- package/dist/calibration/yaml.js.map +1 -0
- package/dist/camera-source.js +967 -10
- package/dist/extension-manifest.json +50 -0
- package/dist/profile.d.ts +21 -0
- package/dist/profile.d.ts.map +1 -0
- package/dist/profile.js +18 -0
- package/dist/profile.js.map +1 -0
- package/package.json +5 -1
package/dist/camera-source.js
CHANGED
|
@@ -35,6 +35,15 @@
|
|
|
35
35
|
}
|
|
36
36
|
}
|
|
37
37
|
},
|
|
38
|
+
{
|
|
39
|
+
"opcode": "startRequestedSharedCamera",
|
|
40
|
+
"blockType": "COMMAND",
|
|
41
|
+
"text": "start shared camera [CAMERA_ID] requested by this page",
|
|
42
|
+
"arguments": { "CAMERA_ID": {
|
|
43
|
+
"type": "STRING",
|
|
44
|
+
"defaultValue": "default"
|
|
45
|
+
} }
|
|
46
|
+
},
|
|
38
47
|
{
|
|
39
48
|
"opcode": "stopSharedCamera",
|
|
40
49
|
"blockType": "COMMAND",
|
|
@@ -213,6 +222,15 @@
|
|
|
213
222
|
"defaultValue": "default"
|
|
214
223
|
} }
|
|
215
224
|
},
|
|
225
|
+
{
|
|
226
|
+
"opcode": "cameraProfileYaml",
|
|
227
|
+
"blockType": "REPORTER",
|
|
228
|
+
"text": "camera profile YAML for [CAMERA_ID]",
|
|
229
|
+
"arguments": { "CAMERA_ID": {
|
|
230
|
+
"type": "STRING",
|
|
231
|
+
"defaultValue": "default"
|
|
232
|
+
} }
|
|
233
|
+
},
|
|
216
234
|
{
|
|
217
235
|
"opcode": "cameraProfileError",
|
|
218
236
|
"blockType": "REPORTER",
|
|
@@ -297,6 +315,33 @@
|
|
|
297
315
|
"defaultValue": "default"
|
|
298
316
|
} }
|
|
299
317
|
},
|
|
318
|
+
{
|
|
319
|
+
"opcode": "restoreStoredCameraProfileForDevice",
|
|
320
|
+
"blockType": "COMMAND",
|
|
321
|
+
"text": "restore stored camera profile calibrated on the device of [CAMERA_ID]",
|
|
322
|
+
"arguments": { "CAMERA_ID": {
|
|
323
|
+
"type": "STRING",
|
|
324
|
+
"defaultValue": "default"
|
|
325
|
+
} }
|
|
326
|
+
},
|
|
327
|
+
{
|
|
328
|
+
"opcode": "bindCameraProfileToDevice",
|
|
329
|
+
"blockType": "COMMAND",
|
|
330
|
+
"text": "bind camera profile for [CAMERA_ID] to its current device",
|
|
331
|
+
"arguments": { "CAMERA_ID": {
|
|
332
|
+
"type": "STRING",
|
|
333
|
+
"defaultValue": "default"
|
|
334
|
+
} }
|
|
335
|
+
},
|
|
336
|
+
{
|
|
337
|
+
"opcode": "cameraProfileOnDevice",
|
|
338
|
+
"blockType": "BOOLEAN",
|
|
339
|
+
"text": "camera profile for [CAMERA_ID] belongs to its current device?",
|
|
340
|
+
"arguments": { "CAMERA_ID": {
|
|
341
|
+
"type": "STRING",
|
|
342
|
+
"defaultValue": "default"
|
|
343
|
+
} }
|
|
344
|
+
},
|
|
300
345
|
{
|
|
301
346
|
"opcode": "storedCameraProfileResult",
|
|
302
347
|
"blockType": "REPORTER",
|
|
@@ -742,7 +787,7 @@
|
|
|
742
787
|
skew: requireBounded(record["skew"], member(path, "skew"), -1e7, MAXIMUM_PIXEL_MAGNITUDE)
|
|
743
788
|
};
|
|
744
789
|
}
|
|
745
|
-
function readDistortion(value, path) {
|
|
790
|
+
function readDistortion$1(value, path) {
|
|
746
791
|
const record = requireRecord(value, path);
|
|
747
792
|
requireExactKeys(record, path, ["model", "coefficients"]);
|
|
748
793
|
const modelPath = member(path, "model");
|
|
@@ -830,7 +875,7 @@
|
|
|
830
875
|
"device"
|
|
831
876
|
]);
|
|
832
877
|
const image = readImage(record["image"], "image");
|
|
833
|
-
const distortion = readDistortion(record["distortion"], "distortion");
|
|
878
|
+
const distortion = readDistortion$1(record["distortion"], "distortion");
|
|
834
879
|
if (image.undistorted && distortion.model !== "none") reject("inconsistent-profile", "distortion.model", "A profile for an already undistorted image cannot also carry distortion coefficients.");
|
|
835
880
|
const capture = "capture" in record ? readCapture(record["capture"], "capture") : void 0;
|
|
836
881
|
const quality = "quality" in record ? readQuality(record["quality"], "quality") : void 0;
|
|
@@ -1079,6 +1124,789 @@
|
|
|
1079
1124
|
}
|
|
1080
1125
|
};
|
|
1081
1126
|
//#endregion
|
|
1127
|
+
//#region src/calibration/yaml.ts
|
|
1128
|
+
var YamlError = class extends Error {
|
|
1129
|
+
constructor(message, line) {
|
|
1130
|
+
super(line > 0 ? `Line ${line}: ${message}` : message);
|
|
1131
|
+
this.name = "YamlError";
|
|
1132
|
+
this.line = line;
|
|
1133
|
+
}
|
|
1134
|
+
};
|
|
1135
|
+
var MAXIMUM_DEPTH = 16;
|
|
1136
|
+
var MAXIMUM_LENGTH = 65536;
|
|
1137
|
+
function parseYaml(source) {
|
|
1138
|
+
if (source.length > MAXIMUM_LENGTH) throw new YamlError("The document is longer than a calibration file ever is.", 0);
|
|
1139
|
+
const lines = splitLines(source);
|
|
1140
|
+
if (lines.length === 0) throw new YamlError("The document is empty.", 0);
|
|
1141
|
+
const reader = new BlockReader(lines);
|
|
1142
|
+
const value = reader.readBlock(lines[0].indent, 0);
|
|
1143
|
+
if (!reader.done()) throw new YamlError("Unexpected indentation.", reader.peek().number);
|
|
1144
|
+
return value;
|
|
1145
|
+
}
|
|
1146
|
+
function splitLines(source) {
|
|
1147
|
+
const lines = [];
|
|
1148
|
+
let started = false;
|
|
1149
|
+
source.replace(/^\uFEFF/, "").split(/\r\n|\r|\n/).forEach((raw, index) => {
|
|
1150
|
+
const number = index + 1;
|
|
1151
|
+
if (/\t/.test(raw.match(/^\s*/)[0])) throw new YamlError("Tabs cannot indent YAML.", number);
|
|
1152
|
+
const text = stripComment(raw).trimEnd();
|
|
1153
|
+
const content = text.trimStart();
|
|
1154
|
+
if (content.length === 0) return;
|
|
1155
|
+
if (!started && content.startsWith("%")) return;
|
|
1156
|
+
if (content === "---" || content.startsWith("--- ")) {
|
|
1157
|
+
if (started) throw new YamlError("Only one document is read from a calibration file.", number);
|
|
1158
|
+
started = true;
|
|
1159
|
+
const rest = content.slice(3).trim();
|
|
1160
|
+
if (rest.length > 0) lines.push({
|
|
1161
|
+
number,
|
|
1162
|
+
indent: 0,
|
|
1163
|
+
text: rest
|
|
1164
|
+
});
|
|
1165
|
+
return;
|
|
1166
|
+
}
|
|
1167
|
+
if (content === "...") {
|
|
1168
|
+
started = true;
|
|
1169
|
+
return;
|
|
1170
|
+
}
|
|
1171
|
+
started = true;
|
|
1172
|
+
lines.push({
|
|
1173
|
+
number,
|
|
1174
|
+
indent: text.length - content.length,
|
|
1175
|
+
text: content
|
|
1176
|
+
});
|
|
1177
|
+
});
|
|
1178
|
+
return lines;
|
|
1179
|
+
}
|
|
1180
|
+
/** Removes a `#` comment that is not inside quotes. A `#` only starts one after whitespace. */
|
|
1181
|
+
function stripComment(raw) {
|
|
1182
|
+
let quote;
|
|
1183
|
+
for (let index = 0; index < raw.length; index += 1) {
|
|
1184
|
+
const character = raw[index];
|
|
1185
|
+
if (quote) {
|
|
1186
|
+
if (character === "\\" && quote === "\"") index += 1;
|
|
1187
|
+
else if (character === quote) quote = void 0;
|
|
1188
|
+
continue;
|
|
1189
|
+
}
|
|
1190
|
+
if (character === "\"" || character === "'") quote = character;
|
|
1191
|
+
else if (character === "#" && (index === 0 || /\s/.test(raw[index - 1]))) return raw.slice(0, index);
|
|
1192
|
+
}
|
|
1193
|
+
return raw;
|
|
1194
|
+
}
|
|
1195
|
+
var BlockReader = class {
|
|
1196
|
+
constructor(lines) {
|
|
1197
|
+
this.lines = lines;
|
|
1198
|
+
this.index = 0;
|
|
1199
|
+
}
|
|
1200
|
+
done() {
|
|
1201
|
+
return this.index >= this.lines.length;
|
|
1202
|
+
}
|
|
1203
|
+
peek() {
|
|
1204
|
+
return this.lines[this.index];
|
|
1205
|
+
}
|
|
1206
|
+
readBlock(indent, depth) {
|
|
1207
|
+
if (depth > MAXIMUM_DEPTH) throw new YamlError("The document is nested too deeply.", this.peek()?.number ?? 0);
|
|
1208
|
+
const first = this.peek();
|
|
1209
|
+
if (!first) throw new YamlError("Expected a value.", 0);
|
|
1210
|
+
if (first.text === "-" || first.text.startsWith("- ")) return this.readSequence(indent, depth);
|
|
1211
|
+
if (findMappingColon(first.text) >= 0) return this.readMapping(indent, depth);
|
|
1212
|
+
this.index += 1;
|
|
1213
|
+
return this.readInline(first.text, first.number, first.indent);
|
|
1214
|
+
}
|
|
1215
|
+
readMapping(indent, depth) {
|
|
1216
|
+
const mapping = {};
|
|
1217
|
+
while (!this.done()) {
|
|
1218
|
+
const line = this.peek();
|
|
1219
|
+
if (line.indent < indent) break;
|
|
1220
|
+
if (line.indent > indent) throw new YamlError("Unexpected indentation.", line.number);
|
|
1221
|
+
const colon = findMappingColon(line.text);
|
|
1222
|
+
if (colon < 0) throw new YamlError("Expected a \"key: value\" pair.", line.number);
|
|
1223
|
+
const key = readKey(line.text.slice(0, colon).trim(), line.number);
|
|
1224
|
+
if (Object.prototype.hasOwnProperty.call(mapping, key)) throw new YamlError(`The key "${key}" appears twice.`, line.number);
|
|
1225
|
+
const rest = line.text.slice(colon + 1).trim();
|
|
1226
|
+
this.index += 1;
|
|
1227
|
+
if (rest.length > 0) {
|
|
1228
|
+
mapping[key] = this.readInline(rest, line.number, line.indent);
|
|
1229
|
+
continue;
|
|
1230
|
+
}
|
|
1231
|
+
const next = this.peek();
|
|
1232
|
+
const nestedSequence = next !== void 0 && next.indent === indent && (next.text === "-" || next.text.startsWith("- "));
|
|
1233
|
+
if (next === void 0 || next.indent <= indent && !nestedSequence) mapping[key] = null;
|
|
1234
|
+
else mapping[key] = this.readBlock(next.indent, depth + 1);
|
|
1235
|
+
}
|
|
1236
|
+
return mapping;
|
|
1237
|
+
}
|
|
1238
|
+
readSequence(indent, depth) {
|
|
1239
|
+
const sequence = [];
|
|
1240
|
+
while (!this.done()) {
|
|
1241
|
+
const line = this.peek();
|
|
1242
|
+
if (line.indent < indent || !(line.text === "-" || line.text.startsWith("- "))) break;
|
|
1243
|
+
if (line.indent > indent) throw new YamlError("Unexpected indentation.", line.number);
|
|
1244
|
+
const rest = line.text.slice(1).trim();
|
|
1245
|
+
this.index += 1;
|
|
1246
|
+
if (rest.length === 0) {
|
|
1247
|
+
const next = this.peek();
|
|
1248
|
+
sequence.push(next !== void 0 && next.indent > indent ? this.readBlock(next.indent, depth + 1) : null);
|
|
1249
|
+
} else if (findMappingColon(rest) >= 0 && !/^[[{"']/.test(rest)) throw new YamlError("A mapping inside a block sequence is not part of a calibration file.", line.number);
|
|
1250
|
+
else sequence.push(this.readInline(rest, line.number, line.indent));
|
|
1251
|
+
}
|
|
1252
|
+
return sequence;
|
|
1253
|
+
}
|
|
1254
|
+
/**
|
|
1255
|
+
* Reads a value that starts on one line. A flow collection may continue on the lines after it, as
|
|
1256
|
+
* PyYAML writes long matrices, so those lines are drawn in until the brackets balance.
|
|
1257
|
+
*/
|
|
1258
|
+
readInline(text, number, indent) {
|
|
1259
|
+
if (text.startsWith("[") || text.startsWith("{")) {
|
|
1260
|
+
let joined = text;
|
|
1261
|
+
while (!flowClosed(joined)) {
|
|
1262
|
+
const next = this.peek();
|
|
1263
|
+
if (!next || next.indent <= indent) throw new YamlError("A bracket is never closed.", number);
|
|
1264
|
+
joined += ` ${next.text}`;
|
|
1265
|
+
this.index += 1;
|
|
1266
|
+
}
|
|
1267
|
+
const flow = new FlowReader(joined, number);
|
|
1268
|
+
const value = flow.readValue(0);
|
|
1269
|
+
flow.expectEnd();
|
|
1270
|
+
return value;
|
|
1271
|
+
}
|
|
1272
|
+
return readScalar(text, number);
|
|
1273
|
+
}
|
|
1274
|
+
};
|
|
1275
|
+
/** Where the `: ` separating a key from its value is, or -1. Colons inside quotes do not count. */
|
|
1276
|
+
function findMappingColon(text) {
|
|
1277
|
+
let quote;
|
|
1278
|
+
for (let index = 0; index < text.length; index += 1) {
|
|
1279
|
+
const character = text[index];
|
|
1280
|
+
if (quote) {
|
|
1281
|
+
if (character === "\\" && quote === "\"") index += 1;
|
|
1282
|
+
else if (character === quote) quote = void 0;
|
|
1283
|
+
continue;
|
|
1284
|
+
}
|
|
1285
|
+
if (index === 0 && (character === "[" || character === "{")) return -1;
|
|
1286
|
+
if (character === "\"" || character === "'") quote = character;
|
|
1287
|
+
else if (character === ":" && (index === text.length - 1 || text[index + 1] === " ")) return index;
|
|
1288
|
+
}
|
|
1289
|
+
return -1;
|
|
1290
|
+
}
|
|
1291
|
+
function flowClosed(text) {
|
|
1292
|
+
let depth = 0;
|
|
1293
|
+
let quote;
|
|
1294
|
+
for (let index = 0; index < text.length; index += 1) {
|
|
1295
|
+
const character = text[index];
|
|
1296
|
+
if (quote) {
|
|
1297
|
+
if (character === "\\" && quote === "\"") index += 1;
|
|
1298
|
+
else if (character === quote) quote = void 0;
|
|
1299
|
+
continue;
|
|
1300
|
+
}
|
|
1301
|
+
if (character === "\"" || character === "'") quote = character;
|
|
1302
|
+
else if (character === "[" || character === "{") depth += 1;
|
|
1303
|
+
else if (character === "]" || character === "}") depth -= 1;
|
|
1304
|
+
}
|
|
1305
|
+
return depth <= 0;
|
|
1306
|
+
}
|
|
1307
|
+
function readKey(text, number) {
|
|
1308
|
+
const value = readScalar(text, number);
|
|
1309
|
+
if (typeof value !== "string") return String(value);
|
|
1310
|
+
return value;
|
|
1311
|
+
}
|
|
1312
|
+
var FlowReader = class {
|
|
1313
|
+
constructor(text, number) {
|
|
1314
|
+
this.text = text;
|
|
1315
|
+
this.number = number;
|
|
1316
|
+
this.index = 0;
|
|
1317
|
+
}
|
|
1318
|
+
readValue(depth) {
|
|
1319
|
+
if (depth > MAXIMUM_DEPTH) throw new YamlError("The document is nested too deeply.", this.number);
|
|
1320
|
+
this.skipSpace();
|
|
1321
|
+
const character = this.text[this.index];
|
|
1322
|
+
if (character === "[") return this.readSequence(depth);
|
|
1323
|
+
if (character === "{") return this.readMapping(depth);
|
|
1324
|
+
return readScalar(this.readToken(), this.number);
|
|
1325
|
+
}
|
|
1326
|
+
expectEnd() {
|
|
1327
|
+
this.skipSpace();
|
|
1328
|
+
if (this.index < this.text.length) throw new YamlError("Unexpected text after a closing bracket.", this.number);
|
|
1329
|
+
}
|
|
1330
|
+
readSequence(depth) {
|
|
1331
|
+
this.index += 1;
|
|
1332
|
+
const sequence = [];
|
|
1333
|
+
this.skipSpace();
|
|
1334
|
+
if (this.text[this.index] === "]") {
|
|
1335
|
+
this.index += 1;
|
|
1336
|
+
return sequence;
|
|
1337
|
+
}
|
|
1338
|
+
for (;;) {
|
|
1339
|
+
sequence.push(this.readValue(depth + 1));
|
|
1340
|
+
this.skipSpace();
|
|
1341
|
+
const separator = this.text[this.index];
|
|
1342
|
+
this.index += 1;
|
|
1343
|
+
if (separator === "]") return sequence;
|
|
1344
|
+
if (separator !== ",") throw new YamlError("Expected \",\" or \"]\" in a sequence.", this.number);
|
|
1345
|
+
this.skipSpace();
|
|
1346
|
+
if (this.text[this.index] === "]") {
|
|
1347
|
+
this.index += 1;
|
|
1348
|
+
return sequence;
|
|
1349
|
+
}
|
|
1350
|
+
}
|
|
1351
|
+
}
|
|
1352
|
+
readMapping(depth) {
|
|
1353
|
+
this.index += 1;
|
|
1354
|
+
const mapping = {};
|
|
1355
|
+
this.skipSpace();
|
|
1356
|
+
if (this.text[this.index] === "}") {
|
|
1357
|
+
this.index += 1;
|
|
1358
|
+
return mapping;
|
|
1359
|
+
}
|
|
1360
|
+
for (;;) {
|
|
1361
|
+
this.skipSpace();
|
|
1362
|
+
const key = readKey(this.readToken(":"), this.number);
|
|
1363
|
+
this.skipSpace();
|
|
1364
|
+
if (this.text[this.index] !== ":") throw new YamlError("Expected \":\" after a key.", this.number);
|
|
1365
|
+
this.index += 1;
|
|
1366
|
+
if (Object.prototype.hasOwnProperty.call(mapping, key)) throw new YamlError(`The key "${key}" appears twice.`, this.number);
|
|
1367
|
+
mapping[key] = this.readValue(depth + 1);
|
|
1368
|
+
this.skipSpace();
|
|
1369
|
+
const separator = this.text[this.index];
|
|
1370
|
+
this.index += 1;
|
|
1371
|
+
if (separator === "}") return mapping;
|
|
1372
|
+
if (separator !== ",") throw new YamlError("Expected \",\" or \"}\" in a mapping.", this.number);
|
|
1373
|
+
this.skipSpace();
|
|
1374
|
+
if (this.text[this.index] === "}") {
|
|
1375
|
+
this.index += 1;
|
|
1376
|
+
return mapping;
|
|
1377
|
+
}
|
|
1378
|
+
}
|
|
1379
|
+
}
|
|
1380
|
+
/** A quoted string, or plain text up to the next flow delimiter. */
|
|
1381
|
+
readToken(stopAlso = "") {
|
|
1382
|
+
this.skipSpace();
|
|
1383
|
+
const start = this.index;
|
|
1384
|
+
const character = this.text[this.index];
|
|
1385
|
+
if (character === "\"" || character === "'") {
|
|
1386
|
+
this.index += 1;
|
|
1387
|
+
while (this.index < this.text.length) {
|
|
1388
|
+
const current = this.text[this.index];
|
|
1389
|
+
if (current === "\\" && character === "\"") {
|
|
1390
|
+
this.index += 2;
|
|
1391
|
+
continue;
|
|
1392
|
+
}
|
|
1393
|
+
if (current === character) {
|
|
1394
|
+
if (character === "'" && this.text[this.index + 1] === "'") {
|
|
1395
|
+
this.index += 2;
|
|
1396
|
+
continue;
|
|
1397
|
+
}
|
|
1398
|
+
this.index += 1;
|
|
1399
|
+
return this.text.slice(start, this.index);
|
|
1400
|
+
}
|
|
1401
|
+
this.index += 1;
|
|
1402
|
+
}
|
|
1403
|
+
throw new YamlError("A quoted string is never closed.", this.number);
|
|
1404
|
+
}
|
|
1405
|
+
while (this.index < this.text.length && !`,]}${stopAlso}`.includes(this.text[this.index])) this.index += 1;
|
|
1406
|
+
return this.text.slice(start, this.index).trim();
|
|
1407
|
+
}
|
|
1408
|
+
skipSpace() {
|
|
1409
|
+
while (this.index < this.text.length && /\s/.test(this.text[this.index])) this.index += 1;
|
|
1410
|
+
}
|
|
1411
|
+
};
|
|
1412
|
+
var INTEGER = /^[-+]?(?:0|[1-9][0-9_]*)$/;
|
|
1413
|
+
var FLOAT = /^[-+]?(?:[0-9][0-9_]*)?\.?[0-9]*(?:[eE][-+]?[0-9]+)?$/;
|
|
1414
|
+
function readScalar(text, number) {
|
|
1415
|
+
const value = text.trim();
|
|
1416
|
+
if (value.length === 0) return null;
|
|
1417
|
+
const first = value[0];
|
|
1418
|
+
if (first === "\"") return readDoubleQuoted(value, number);
|
|
1419
|
+
if (first === "'") {
|
|
1420
|
+
if (value.length < 2 || !value.endsWith("'")) throw new YamlError("A quoted string is never closed.", number);
|
|
1421
|
+
return value.slice(1, -1).replace(/''/g, "'");
|
|
1422
|
+
}
|
|
1423
|
+
if (first === "&" || first === "*") throw new YamlError("Anchors and aliases are not read.", number);
|
|
1424
|
+
if (first === "!") throw new YamlError("Tags are not read.", number);
|
|
1425
|
+
if (first === "|" || first === ">") throw new YamlError("Block scalars are not read.", number);
|
|
1426
|
+
if (first === "@" || first === "`") throw new YamlError(`A plain value cannot start with "${first}".`, number);
|
|
1427
|
+
if (value === "~" || value === "null" || value === "Null" || value === "NULL") return null;
|
|
1428
|
+
if (value === "true" || value === "True" || value === "TRUE") return true;
|
|
1429
|
+
if (value === "false" || value === "False" || value === "FALSE") return false;
|
|
1430
|
+
if (/^[-+]?\.(?:inf|Inf|INF|nan|NaN|NAN)$/.test(value)) throw new YamlError("Infinity and NaN are not calibration values.", number);
|
|
1431
|
+
if (INTEGER.test(value)) return Number(value.replace(/_/g, ""));
|
|
1432
|
+
if (/[0-9]/.test(value) && FLOAT.test(value)) {
|
|
1433
|
+
const parsed = Number(value.replace(/_/g, ""));
|
|
1434
|
+
if (Number.isFinite(parsed)) return parsed;
|
|
1435
|
+
}
|
|
1436
|
+
return value;
|
|
1437
|
+
}
|
|
1438
|
+
function readDoubleQuoted(value, number) {
|
|
1439
|
+
if (value.length < 2 || !value.endsWith("\"")) throw new YamlError("A quoted string is never closed.", number);
|
|
1440
|
+
const body = value.slice(1, -1);
|
|
1441
|
+
let result = "";
|
|
1442
|
+
for (let index = 0; index < body.length; index += 1) {
|
|
1443
|
+
const character = body[index];
|
|
1444
|
+
if (character !== "\\") {
|
|
1445
|
+
result += character;
|
|
1446
|
+
continue;
|
|
1447
|
+
}
|
|
1448
|
+
const escape = body[index + 1];
|
|
1449
|
+
index += 1;
|
|
1450
|
+
switch (escape) {
|
|
1451
|
+
case "\"":
|
|
1452
|
+
case "\\":
|
|
1453
|
+
case "/":
|
|
1454
|
+
result += escape;
|
|
1455
|
+
break;
|
|
1456
|
+
case "n":
|
|
1457
|
+
result += "\n";
|
|
1458
|
+
break;
|
|
1459
|
+
case "t":
|
|
1460
|
+
result += " ";
|
|
1461
|
+
break;
|
|
1462
|
+
case "r":
|
|
1463
|
+
result += "\r";
|
|
1464
|
+
break;
|
|
1465
|
+
case "0":
|
|
1466
|
+
result += "\0";
|
|
1467
|
+
break;
|
|
1468
|
+
case "u": {
|
|
1469
|
+
const hex = body.slice(index + 1, index + 5);
|
|
1470
|
+
if (!/^[0-9a-fA-F]{4}$/.test(hex)) throw new YamlError("Invalid \\u escape.", number);
|
|
1471
|
+
result += String.fromCharCode(parseInt(hex, 16));
|
|
1472
|
+
index += 4;
|
|
1473
|
+
break;
|
|
1474
|
+
}
|
|
1475
|
+
default: throw new YamlError(`Unsupported escape "\\${escape ?? ""}".`, number);
|
|
1476
|
+
}
|
|
1477
|
+
}
|
|
1478
|
+
return result;
|
|
1479
|
+
}
|
|
1480
|
+
//#endregion
|
|
1481
|
+
//#region src/calibration/camera-info.ts
|
|
1482
|
+
/**
|
|
1483
|
+
* The file a calibration is exchanged in: a ROS `camera_info` YAML document.
|
|
1484
|
+
*
|
|
1485
|
+
* The profile contract is how this extension holds a calibration; it is not a format anyone outside
|
|
1486
|
+
* this family reads. A file an operator carries between machines is better written in the format
|
|
1487
|
+
* the tools they will meet already read, and for one camera's intrinsics that is the YAML that
|
|
1488
|
+
* ROS's `camera_calibration_parsers` reads and writes -- OpenCV-based pipelines, ROS and ROS 2
|
|
1489
|
+
* drivers, and SLAM tools load it as it is.
|
|
1490
|
+
*
|
|
1491
|
+
* ROS carries what projection needs and nothing about when or how the camera was configured, which
|
|
1492
|
+
* is exactly what deciding whether a profile still fits requires. Those members travel in one extra
|
|
1493
|
+
* top-level mapping, `turbowarp_camera_source`, named after their owner. ROS's reader looks keys up
|
|
1494
|
+
* by name and never enumerates the document, so a file with the extra mapping still loads there,
|
|
1495
|
+
* and a file without it is still ROS.
|
|
1496
|
+
*
|
|
1497
|
+
* A file that lacks the mapping altogether cannot become a profile: when it was calibrated and which
|
|
1498
|
+
* run it was are required members, and inventing either would turn "unknown" into a record.
|
|
1499
|
+
*/
|
|
1500
|
+
/** The top-level key holding the members ROS has no place for. */
|
|
1501
|
+
var CAMERA_INFO_EXTENSION_KEY = "turbowarp_camera_source";
|
|
1502
|
+
var MATRIX_TOLERANCE = 1e-6;
|
|
1503
|
+
/**
|
|
1504
|
+
* Every top-level key a ROS file may have, and the extra mapping.
|
|
1505
|
+
*
|
|
1506
|
+
* Anything else is refused rather than dropped. A calibration file that carries a key nobody reads
|
|
1507
|
+
* is either from a tool that means something by it or has had something pasted into it, and a
|
|
1508
|
+
* profile validator that fails closed does not start ignoring members because they arrived in YAML.
|
|
1509
|
+
*/
|
|
1510
|
+
var ROOT_KEYS = /* @__PURE__ */ new Set([
|
|
1511
|
+
"image_width",
|
|
1512
|
+
"image_height",
|
|
1513
|
+
"camera_name",
|
|
1514
|
+
"camera_matrix",
|
|
1515
|
+
"distortion_model",
|
|
1516
|
+
"distortion_coefficients",
|
|
1517
|
+
"rectification_matrix",
|
|
1518
|
+
"projection_matrix",
|
|
1519
|
+
"binning_x",
|
|
1520
|
+
"binning_y",
|
|
1521
|
+
"roi",
|
|
1522
|
+
CAMERA_INFO_EXTENSION_KEY
|
|
1523
|
+
]);
|
|
1524
|
+
/** ROS distortion model names, as `sensor_msgs/distortion_models.hpp` spells them. */
|
|
1525
|
+
var PLUMB_BOB = "plumb_bob";
|
|
1526
|
+
var RATIONAL_POLYNOMIAL = "rational_polynomial";
|
|
1527
|
+
var EQUIDISTANT = "equidistant";
|
|
1528
|
+
var CameraInfoRejection = class extends Error {
|
|
1529
|
+
constructor(detail) {
|
|
1530
|
+
super(detail.message);
|
|
1531
|
+
this.detail = detail;
|
|
1532
|
+
}
|
|
1533
|
+
};
|
|
1534
|
+
function refuse(code, path, message) {
|
|
1535
|
+
throw new CameraInfoRejection({
|
|
1536
|
+
code,
|
|
1537
|
+
path,
|
|
1538
|
+
message
|
|
1539
|
+
});
|
|
1540
|
+
}
|
|
1541
|
+
/**
|
|
1542
|
+
* Renders a profile as a ROS `camera_info` YAML document.
|
|
1543
|
+
*
|
|
1544
|
+
* The layout follows what `camera_calibration_parsers` writes, key for key and in the same order, so
|
|
1545
|
+
* the file reads like one ROS produced. The projection matrix is the calibration matrix with a zero
|
|
1546
|
+
* fourth column, which is what a monocular camera with no rectification has; binning and region of
|
|
1547
|
+
* interest are left out, as ROS treats them as optional.
|
|
1548
|
+
*
|
|
1549
|
+
* Strings in the extra mapping are always double-quoted. A timestamp left plain is read as a date
|
|
1550
|
+
* by YAML 1.1 readers such as PyYAML, and an id made only of digits as a number.
|
|
1551
|
+
*/
|
|
1552
|
+
function serializeCameraInfoYaml(profile) {
|
|
1553
|
+
const { fx, fy, cx, cy, skew } = profile.intrinsics;
|
|
1554
|
+
const { model, coefficients } = rosDistortion(profile);
|
|
1555
|
+
const lines = [
|
|
1556
|
+
`image_width: ${profile.image.width}`,
|
|
1557
|
+
`image_height: ${profile.image.height}`,
|
|
1558
|
+
`camera_name: ${plainOrQuoted(profile.cameraId)}`,
|
|
1559
|
+
...matrix("camera_matrix", 3, 3, [
|
|
1560
|
+
fx,
|
|
1561
|
+
skew,
|
|
1562
|
+
cx,
|
|
1563
|
+
0,
|
|
1564
|
+
fy,
|
|
1565
|
+
cy,
|
|
1566
|
+
0,
|
|
1567
|
+
0,
|
|
1568
|
+
1
|
|
1569
|
+
]),
|
|
1570
|
+
`distortion_model: ${model}`,
|
|
1571
|
+
...matrix("distortion_coefficients", 1, coefficients.length, coefficients),
|
|
1572
|
+
...matrix("rectification_matrix", 3, 3, [
|
|
1573
|
+
1,
|
|
1574
|
+
0,
|
|
1575
|
+
0,
|
|
1576
|
+
0,
|
|
1577
|
+
1,
|
|
1578
|
+
0,
|
|
1579
|
+
0,
|
|
1580
|
+
0,
|
|
1581
|
+
1
|
|
1582
|
+
]),
|
|
1583
|
+
...matrix("projection_matrix", 3, 4, [
|
|
1584
|
+
fx,
|
|
1585
|
+
skew,
|
|
1586
|
+
cx,
|
|
1587
|
+
0,
|
|
1588
|
+
0,
|
|
1589
|
+
fy,
|
|
1590
|
+
cy,
|
|
1591
|
+
0,
|
|
1592
|
+
0,
|
|
1593
|
+
0,
|
|
1594
|
+
1,
|
|
1595
|
+
0
|
|
1596
|
+
]),
|
|
1597
|
+
`${CAMERA_INFO_EXTENSION_KEY}:`,
|
|
1598
|
+
` schema: ${quoted(profile.schema)}`,
|
|
1599
|
+
` version: ${profile.version}`,
|
|
1600
|
+
` profileId: ${quoted(profile.profileId)}`,
|
|
1601
|
+
` calibratedAt: ${quoted(profile.calibratedAt)}`,
|
|
1602
|
+
` producer: ${quoted(profile.producer)}`,
|
|
1603
|
+
` undistorted: ${profile.image.undistorted}`
|
|
1604
|
+
];
|
|
1605
|
+
const capture = profile.capture;
|
|
1606
|
+
if (capture !== void 0) {
|
|
1607
|
+
lines.push(" capture:");
|
|
1608
|
+
if (Object.keys(capture).length === 0) lines[lines.length - 1] += " {}";
|
|
1609
|
+
appendMember(lines, "frameRate", capture.frameRate);
|
|
1610
|
+
appendMember(lines, "facingMode", capture.facingMode);
|
|
1611
|
+
appendMember(lines, "resizeMode", capture.resizeMode);
|
|
1612
|
+
appendMember(lines, "zoom", capture.zoom);
|
|
1613
|
+
appendMember(lines, "focusMode", capture.focusMode);
|
|
1614
|
+
appendMember(lines, "focusDistance", capture.focusDistance);
|
|
1615
|
+
}
|
|
1616
|
+
if (profile.quality !== void 0) {
|
|
1617
|
+
lines.push(" quality:");
|
|
1618
|
+
appendMember(lines, "sampleCount", profile.quality.sampleCount);
|
|
1619
|
+
appendMember(lines, "reprojectionErrorPx", profile.quality.reprojectionErrorPx);
|
|
1620
|
+
}
|
|
1621
|
+
if (profile.device !== void 0) {
|
|
1622
|
+
lines.push(" device:");
|
|
1623
|
+
if (Object.keys(profile.device).length === 0) lines[lines.length - 1] += " {}";
|
|
1624
|
+
appendMember(lines, "label", profile.device.label);
|
|
1625
|
+
appendMember(lines, "deviceId", profile.device.deviceId);
|
|
1626
|
+
}
|
|
1627
|
+
return `${lines.join("\n")}\n`;
|
|
1628
|
+
}
|
|
1629
|
+
/**
|
|
1630
|
+
* Turns a ROS `camera_info` YAML document into a profile document, ready to be validated.
|
|
1631
|
+
*
|
|
1632
|
+
* Only the shape ROS defines is checked here -- matrix sizes, the fixed entries of the calibration
|
|
1633
|
+
* matrix, a rectification that does nothing, a distortion model ROS names. Everything the profile
|
|
1634
|
+
* contract says about the values themselves is left to the profile validator, so there is one
|
|
1635
|
+
* place that decides whether a calibration is acceptable.
|
|
1636
|
+
*/
|
|
1637
|
+
function readCameraInfoYaml(text) {
|
|
1638
|
+
let parsed;
|
|
1639
|
+
try {
|
|
1640
|
+
parsed = parseYaml(text);
|
|
1641
|
+
} catch (error) {
|
|
1642
|
+
if (error instanceof YamlError) return {
|
|
1643
|
+
ok: false,
|
|
1644
|
+
error: {
|
|
1645
|
+
code: "not-an-object",
|
|
1646
|
+
path: "",
|
|
1647
|
+
message: `The profile is not valid YAML. ${error.message}`
|
|
1648
|
+
}
|
|
1649
|
+
};
|
|
1650
|
+
throw error;
|
|
1651
|
+
}
|
|
1652
|
+
try {
|
|
1653
|
+
return {
|
|
1654
|
+
ok: true,
|
|
1655
|
+
document: toProfileDocument(parsed)
|
|
1656
|
+
};
|
|
1657
|
+
} catch (error) {
|
|
1658
|
+
if (error instanceof CameraInfoRejection) return {
|
|
1659
|
+
ok: false,
|
|
1660
|
+
error: error.detail
|
|
1661
|
+
};
|
|
1662
|
+
throw error;
|
|
1663
|
+
}
|
|
1664
|
+
}
|
|
1665
|
+
function toProfileDocument(parsed) {
|
|
1666
|
+
const root = record(parsed, "");
|
|
1667
|
+
for (const key of Object.keys(root)) if (!ROOT_KEYS.has(key)) refuse("unexpected-field", key, "Unknown member.");
|
|
1668
|
+
readBinningAndRoi(root);
|
|
1669
|
+
const width = root["image_width"];
|
|
1670
|
+
const height = root["image_height"];
|
|
1671
|
+
if (width === void 0) refuse("missing-field", "image_width", "Required member is missing.");
|
|
1672
|
+
if (height === void 0) refuse("missing-field", "image_height", "Required member is missing.");
|
|
1673
|
+
const k = readMatrix(root, "camera_matrix", 3, 3);
|
|
1674
|
+
expectEntries(k, "camera_matrix", [
|
|
1675
|
+
[3, 0],
|
|
1676
|
+
[6, 0],
|
|
1677
|
+
[7, 0],
|
|
1678
|
+
[8, 1]
|
|
1679
|
+
]);
|
|
1680
|
+
if ("rectification_matrix" in root) expectEntries(readMatrix(root, "rectification_matrix", 3, 3), "rectification_matrix", [
|
|
1681
|
+
1,
|
|
1682
|
+
0,
|
|
1683
|
+
0,
|
|
1684
|
+
0,
|
|
1685
|
+
1,
|
|
1686
|
+
0,
|
|
1687
|
+
0,
|
|
1688
|
+
0,
|
|
1689
|
+
1
|
|
1690
|
+
].map((value, index) => [index, value]), "A rectification other than identity belongs to a stereo pair, not to one camera's intrinsics.");
|
|
1691
|
+
if ("projection_matrix" in root) readMatrix(root, "projection_matrix", 3, 4);
|
|
1692
|
+
const distortion = readDistortion(root);
|
|
1693
|
+
const extension = root[CAMERA_INFO_EXTENSION_KEY];
|
|
1694
|
+
if (extension === void 0) refuse("missing-field", CAMERA_INFO_EXTENSION_KEY, "The file is a ROS camera_info document without the calibration record: when it was calibrated and under which camera settings are not known.");
|
|
1695
|
+
const extra = record(extension, CAMERA_INFO_EXTENSION_KEY);
|
|
1696
|
+
const known = /* @__PURE__ */ new Set([
|
|
1697
|
+
"schema",
|
|
1698
|
+
"version",
|
|
1699
|
+
"profileId",
|
|
1700
|
+
"calibratedAt",
|
|
1701
|
+
"producer",
|
|
1702
|
+
"undistorted",
|
|
1703
|
+
"capture",
|
|
1704
|
+
"quality",
|
|
1705
|
+
"device"
|
|
1706
|
+
]);
|
|
1707
|
+
for (const key of Object.keys(extra)) if (!known.has(key)) refuse("unexpected-field", `${CAMERA_INFO_EXTENSION_KEY}.${key}`, "Unknown member.");
|
|
1708
|
+
if (extra["schema"] !== "twcs/camera-intrinsics") refuse("unsupported-schema", `${CAMERA_INFO_EXTENSION_KEY}.schema`, `Expected ${JSON.stringify(CAMERA_INTRINSIC_PROFILE_SCHEMA)}.`);
|
|
1709
|
+
if (extra["version"] !== 1) refuse("unsupported-version", `${CAMERA_INFO_EXTENSION_KEY}.version`, `Expected 1.`);
|
|
1710
|
+
const undistorted = extra["undistorted"] ?? false;
|
|
1711
|
+
return {
|
|
1712
|
+
schema: CAMERA_INTRINSIC_PROFILE_SCHEMA,
|
|
1713
|
+
version: 1,
|
|
1714
|
+
profileId: extra["profileId"],
|
|
1715
|
+
cameraId: root["camera_name"],
|
|
1716
|
+
calibratedAt: extra["calibratedAt"],
|
|
1717
|
+
producer: extra["producer"],
|
|
1718
|
+
cameraModel: "pinhole",
|
|
1719
|
+
image: {
|
|
1720
|
+
width,
|
|
1721
|
+
height,
|
|
1722
|
+
undistorted
|
|
1723
|
+
},
|
|
1724
|
+
intrinsics: {
|
|
1725
|
+
fx: k[0],
|
|
1726
|
+
fy: k[4],
|
|
1727
|
+
cx: k[2],
|
|
1728
|
+
cy: k[5],
|
|
1729
|
+
skew: k[1]
|
|
1730
|
+
},
|
|
1731
|
+
distortion,
|
|
1732
|
+
..."capture" in extra ? { capture: extra["capture"] } : {},
|
|
1733
|
+
..."quality" in extra ? { quality: extra["quality"] } : {},
|
|
1734
|
+
..."device" in extra ? { device: extra["device"] } : {}
|
|
1735
|
+
};
|
|
1736
|
+
}
|
|
1737
|
+
/**
|
|
1738
|
+
* Maps a ROS distortion model onto the profile's.
|
|
1739
|
+
*
|
|
1740
|
+
* `plumb_bob` coefficients that are all zero read as no distortion. ROS has no model for "none"; a
|
|
1741
|
+
* lens-free image is written as plumb_bob with five zeros, and reading it back must not invent a
|
|
1742
|
+
* Brown-Conrady lens that happens to do nothing.
|
|
1743
|
+
*/
|
|
1744
|
+
function readDistortion(root) {
|
|
1745
|
+
const rawModel = root["distortion_model"] ?? PLUMB_BOB;
|
|
1746
|
+
const coefficients = readMatrix(root, "distortion_coefficients", 1, void 0);
|
|
1747
|
+
switch (rawModel) {
|
|
1748
|
+
case PLUMB_BOB:
|
|
1749
|
+
if (coefficients.length !== 4 && coefficients.length !== 5) refuse("invalid-distortion", "distortion_coefficients", `plumb_bob takes 5 coefficients, not ${coefficients.length}.`);
|
|
1750
|
+
return isAllZero(coefficients) ? {
|
|
1751
|
+
model: "none",
|
|
1752
|
+
coefficients: []
|
|
1753
|
+
} : {
|
|
1754
|
+
model: "brown-conrady",
|
|
1755
|
+
coefficients
|
|
1756
|
+
};
|
|
1757
|
+
case RATIONAL_POLYNOMIAL:
|
|
1758
|
+
if (coefficients.length !== 8) refuse("invalid-distortion", "distortion_coefficients", `rational_polynomial takes 8 coefficients, not ${coefficients.length}.`);
|
|
1759
|
+
return {
|
|
1760
|
+
model: "brown-conrady",
|
|
1761
|
+
coefficients
|
|
1762
|
+
};
|
|
1763
|
+
case EQUIDISTANT:
|
|
1764
|
+
if (coefficients.length !== 4) refuse("invalid-distortion", "distortion_coefficients", `equidistant takes 4 coefficients, not ${coefficients.length}.`);
|
|
1765
|
+
return {
|
|
1766
|
+
model: "kannala-brandt",
|
|
1767
|
+
coefficients
|
|
1768
|
+
};
|
|
1769
|
+
default: refuse("invalid-distortion", "distortion_model", `Expected ${PLUMB_BOB}, ${RATIONAL_POLYNOMIAL} or ${EQUIDISTANT}.`);
|
|
1770
|
+
}
|
|
1771
|
+
}
|
|
1772
|
+
/**
|
|
1773
|
+
* Binning and a region of interest change which pixels the calibration describes. ROS writes both
|
|
1774
|
+
* with their do-nothing values, and only those are accepted: a calibration of a binned or cropped
|
|
1775
|
+
* image is a different calibration, and reading it as the full frame's would move the principal
|
|
1776
|
+
* point without anyone noticing.
|
|
1777
|
+
*/
|
|
1778
|
+
function readBinningAndRoi(root) {
|
|
1779
|
+
for (const name of ["binning_x", "binning_y"]) {
|
|
1780
|
+
const value = root[name];
|
|
1781
|
+
if (value !== void 0 && value !== 0 && value !== 1) refuse("inconsistent-profile", name, "A binned image is not the image the calibration describes.");
|
|
1782
|
+
}
|
|
1783
|
+
const roi = root["roi"];
|
|
1784
|
+
if (roi === void 0) return;
|
|
1785
|
+
const region = record(roi, "roi");
|
|
1786
|
+
const width = region["width"] ?? 0;
|
|
1787
|
+
const height = region["height"] ?? 0;
|
|
1788
|
+
const offsetX = region["x_offset"] ?? 0;
|
|
1789
|
+
const offsetY = region["y_offset"] ?? 0;
|
|
1790
|
+
const whole = width === 0 && height === 0;
|
|
1791
|
+
const full = width === root["image_width"] && height === root["image_height"];
|
|
1792
|
+
if (offsetX !== 0 || offsetY !== 0 || !(whole || full)) refuse("inconsistent-profile", "roi", "A region of interest is not the image the calibration describes.");
|
|
1793
|
+
}
|
|
1794
|
+
function rosDistortion(profile) {
|
|
1795
|
+
const coefficients = [...profile.distortion.coefficients];
|
|
1796
|
+
switch (profile.distortion.model) {
|
|
1797
|
+
case "none": return {
|
|
1798
|
+
model: PLUMB_BOB,
|
|
1799
|
+
coefficients: [
|
|
1800
|
+
0,
|
|
1801
|
+
0,
|
|
1802
|
+
0,
|
|
1803
|
+
0,
|
|
1804
|
+
0
|
|
1805
|
+
]
|
|
1806
|
+
};
|
|
1807
|
+
case "kannala-brandt": return {
|
|
1808
|
+
model: EQUIDISTANT,
|
|
1809
|
+
coefficients
|
|
1810
|
+
};
|
|
1811
|
+
case "brown-conrady":
|
|
1812
|
+
if (coefficients.length === 8) return {
|
|
1813
|
+
model: RATIONAL_POLYNOMIAL,
|
|
1814
|
+
coefficients
|
|
1815
|
+
};
|
|
1816
|
+
return {
|
|
1817
|
+
model: PLUMB_BOB,
|
|
1818
|
+
coefficients: coefficients.length === 4 ? [...coefficients, 0] : coefficients
|
|
1819
|
+
};
|
|
1820
|
+
}
|
|
1821
|
+
}
|
|
1822
|
+
function readMatrix(root, name, rows, cols) {
|
|
1823
|
+
const value = root[name];
|
|
1824
|
+
if (value === void 0) refuse("missing-field", name, "Required member is missing.");
|
|
1825
|
+
const matrix = record(value, name);
|
|
1826
|
+
const declaredRows = matrix["rows"];
|
|
1827
|
+
const declaredCols = matrix["cols"];
|
|
1828
|
+
const data = matrix["data"];
|
|
1829
|
+
if (declaredRows !== rows) refuse("invalid-value", `${name}.rows`, `Expected ${rows}.`);
|
|
1830
|
+
if (cols !== void 0 && declaredCols !== cols) refuse("invalid-value", `${name}.cols`, `Expected ${cols}.`);
|
|
1831
|
+
if (typeof declaredCols !== "number" || !Number.isInteger(declaredCols) || declaredCols < 0) refuse("invalid-value", `${name}.cols`, "Expected a column count.");
|
|
1832
|
+
if (!Array.isArray(data)) refuse("invalid-type", `${name}.data`, "Expected a sequence of numbers.");
|
|
1833
|
+
if (data.length !== rows * declaredCols) refuse("invalid-value", `${name}.data`, `Expected ${rows * declaredCols} numbers for a ${rows}x${declaredCols} matrix, not ${data.length}.`);
|
|
1834
|
+
return data.map((entry, index) => {
|
|
1835
|
+
if (typeof entry !== "number" || !Number.isFinite(entry)) refuse("invalid-type", `${name}.data[${index}]`, "Expected a number.");
|
|
1836
|
+
return entry;
|
|
1837
|
+
});
|
|
1838
|
+
}
|
|
1839
|
+
function expectEntries(data, name, entries, message) {
|
|
1840
|
+
for (const [index, expected] of entries) if (Math.abs(data[index] - expected) > MATRIX_TOLERANCE) refuse("inconsistent-profile", `${name}.data[${index}]`, message ?? `Expected ${expected}; the value read as ${data[index]}.`);
|
|
1841
|
+
}
|
|
1842
|
+
function record(value, path) {
|
|
1843
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) refuse("not-an-object", path, "Expected a mapping.");
|
|
1844
|
+
return value;
|
|
1845
|
+
}
|
|
1846
|
+
function isAllZero(values) {
|
|
1847
|
+
return values.every((value) => value === 0);
|
|
1848
|
+
}
|
|
1849
|
+
function matrix(name, rows, cols, data) {
|
|
1850
|
+
return [
|
|
1851
|
+
`${name}:`,
|
|
1852
|
+
` rows: ${rows}`,
|
|
1853
|
+
` cols: ${cols}`,
|
|
1854
|
+
` data: [${data.map(number).join(", ")}]`
|
|
1855
|
+
];
|
|
1856
|
+
}
|
|
1857
|
+
/** Numbers as JavaScript writes them, which is the shortest text that reads back to the same value. */
|
|
1858
|
+
function number(value) {
|
|
1859
|
+
return Object.is(value, -0) ? "0" : String(value);
|
|
1860
|
+
}
|
|
1861
|
+
function quoted(text) {
|
|
1862
|
+
return JSON.stringify(text);
|
|
1863
|
+
}
|
|
1864
|
+
/** Camera names are identifiers and read the same plain, which is how ROS writes them. */
|
|
1865
|
+
function plainOrQuoted(text) {
|
|
1866
|
+
return /^[A-Za-z][A-Za-z0-9._-]*$/.test(text) && !/^(?:true|false|null|yes|no|on|off|y|n)$/i.test(text) ? text : quoted(text);
|
|
1867
|
+
}
|
|
1868
|
+
function appendMember(lines, name, value) {
|
|
1869
|
+
if (value === void 0) return;
|
|
1870
|
+
lines.push(` ${name}: ${typeof value === "string" ? quoted(value) : number(value)}`);
|
|
1871
|
+
}
|
|
1872
|
+
//#endregion
|
|
1873
|
+
//#region src/calibration/profile-text.ts
|
|
1874
|
+
/**
|
|
1875
|
+
* Profile text as an operator hands it over: a ROS `camera_info` YAML file, or profile JSON.
|
|
1876
|
+
*
|
|
1877
|
+
* The operator does not know which one a file is and should not have to. JSON always starts with a
|
|
1878
|
+
* brace; a calibration YAML file never does, because its top level is a block mapping. That one
|
|
1879
|
+
* character decides, and each branch names what it expected when the text is not what it looked
|
|
1880
|
+
* like.
|
|
1881
|
+
*/
|
|
1882
|
+
function readProfileText(text) {
|
|
1883
|
+
const trimmed = text.trim();
|
|
1884
|
+
if (trimmed.length === 0) return {
|
|
1885
|
+
ok: false,
|
|
1886
|
+
error: {
|
|
1887
|
+
code: "not-an-object",
|
|
1888
|
+
path: "",
|
|
1889
|
+
message: "The profile is empty."
|
|
1890
|
+
}
|
|
1891
|
+
};
|
|
1892
|
+
if (trimmed.startsWith("{")) try {
|
|
1893
|
+
return {
|
|
1894
|
+
ok: true,
|
|
1895
|
+
document: JSON.parse(trimmed)
|
|
1896
|
+
};
|
|
1897
|
+
} catch {
|
|
1898
|
+
return {
|
|
1899
|
+
ok: false,
|
|
1900
|
+
error: {
|
|
1901
|
+
code: "not-an-object",
|
|
1902
|
+
path: "",
|
|
1903
|
+
message: "The profile is not valid JSON."
|
|
1904
|
+
}
|
|
1905
|
+
};
|
|
1906
|
+
}
|
|
1907
|
+
return readCameraInfoYaml(trimmed);
|
|
1908
|
+
}
|
|
1909
|
+
//#endregion
|
|
1082
1910
|
//#region src/calibration/store.ts
|
|
1083
1911
|
/**
|
|
1084
1912
|
* Profiles kept in the browser between sessions.
|
|
@@ -1420,6 +2248,38 @@
|
|
|
1420
2248
|
function normalizeId(value, fallback = defaultCameraId) {
|
|
1421
2249
|
return String(value ?? "").trim() || fallback;
|
|
1422
2250
|
}
|
|
2251
|
+
/**
|
|
2252
|
+
* The camera a page was opened for, from its query parameters.
|
|
2253
|
+
*
|
|
2254
|
+
* Returns only what was named, so a page opened without parameters starts a camera exactly as the
|
|
2255
|
+
* plain block does. A size or rate that is not a positive number is ignored rather than guessed.
|
|
2256
|
+
*/
|
|
2257
|
+
function requestedCamera(search) {
|
|
2258
|
+
let parameters;
|
|
2259
|
+
try {
|
|
2260
|
+
parameters = new URLSearchParams(search);
|
|
2261
|
+
} catch {
|
|
2262
|
+
return {};
|
|
2263
|
+
}
|
|
2264
|
+
const deviceId = optionalText(parameters.get("cameraDeviceId"));
|
|
2265
|
+
const video = {};
|
|
2266
|
+
if (deviceId) video.deviceId = { exact: deviceId };
|
|
2267
|
+
const ideal = (name) => {
|
|
2268
|
+
const value = Number(parameters.get(name) ?? "");
|
|
2269
|
+
return Number.isFinite(value) && value > 0 ? value : void 0;
|
|
2270
|
+
};
|
|
2271
|
+
const width = ideal("cameraWidth");
|
|
2272
|
+
const height = ideal("cameraHeight");
|
|
2273
|
+
const frameRate = ideal("cameraFrameRate");
|
|
2274
|
+
if (width !== void 0) video.width = { ideal: width };
|
|
2275
|
+
if (height !== void 0) video.height = { ideal: height };
|
|
2276
|
+
if (frameRate !== void 0) video.frameRate = { ideal: frameRate };
|
|
2277
|
+
if (Object.keys(video).length === 0) return {};
|
|
2278
|
+
return {
|
|
2279
|
+
...deviceId ? { deviceId } : {},
|
|
2280
|
+
video
|
|
2281
|
+
};
|
|
2282
|
+
}
|
|
1423
2283
|
function optionalText(value) {
|
|
1424
2284
|
return String(value ?? "").trim();
|
|
1425
2285
|
}
|
|
@@ -1764,7 +2624,77 @@
|
|
|
1764
2624
|
* so trying a candidate that loses never displaces the registered one even for a moment.
|
|
1765
2625
|
*/
|
|
1766
2626
|
async restoreStoredCameraProfile(args = {}) {
|
|
2627
|
+
await this.restoreStoredProfile(normalizeId(args.CAMERA_ID), false);
|
|
2628
|
+
}
|
|
2629
|
+
/**
|
|
2630
|
+
* Like `restore stored camera profile`, but only from profiles calibrated on the device the camera
|
|
2631
|
+
* is running on now.
|
|
2632
|
+
*
|
|
2633
|
+
* Compatibility alone cannot tell two cameras of the same model apart: they report the same label
|
|
2634
|
+
* and the same capture conditions, so either one's calibration reads as `compatible` for the other.
|
|
2635
|
+
* What differs is the device id, which is scoped to this origin and browser profile. That scope is
|
|
2636
|
+
* exactly where browser storage lives, so a profile saved here and a camera opened here agree on it
|
|
2637
|
+
* for as long as the browser keeps both.
|
|
2638
|
+
*
|
|
2639
|
+
* A camera that is not running has no device id, and the answer is then `undetermined`. A profile
|
|
2640
|
+
* brought in from another browser carries another id and is never a candidate; register it with
|
|
2641
|
+
* the file and `bind camera profile ... to its current device` first.
|
|
2642
|
+
*/
|
|
2643
|
+
async restoreStoredCameraProfileForDevice(args = {}) {
|
|
2644
|
+
await this.restoreStoredProfile(normalizeId(args.CAMERA_ID), true);
|
|
2645
|
+
}
|
|
2646
|
+
/**
|
|
2647
|
+
* Records the device the camera is running on in its registered profile.
|
|
2648
|
+
*
|
|
2649
|
+
* For the case where the operator has said which camera a profile belongs to, typically by loading
|
|
2650
|
+
* a file for it. Only a profile `compatible` with the camera as it is now is bound: a profile that
|
|
2651
|
+
* does not fit is not made more trustworthy by naming a device in it. A label the camera reports is
|
|
2652
|
+
* written too when the profile has none; one it already has has been compared and matched.
|
|
2653
|
+
*/
|
|
2654
|
+
bindCameraProfileToDevice(args = {}) {
|
|
2655
|
+
const cameraId = normalizeId(args.CAMERA_ID);
|
|
2656
|
+
const profile = this.profiles.get(cameraId);
|
|
2657
|
+
if (!profile) return;
|
|
2658
|
+
const conditions = this.conditionsOf(cameraId);
|
|
2659
|
+
if (conditions.deviceId.length === 0) return;
|
|
2660
|
+
if (evaluateProfileCompatibility(profile, conditions).state !== "compatible") return;
|
|
2661
|
+
const document = JSON.parse(serializeCameraIntrinsicProfile(profile));
|
|
2662
|
+
const label = profile.device?.label ?? conditions.label;
|
|
2663
|
+
document["device"] = {
|
|
2664
|
+
...label === void 0 ? {} : { label },
|
|
2665
|
+
deviceId: conditions.deviceId
|
|
2666
|
+
};
|
|
2667
|
+
this.profiles.register(document);
|
|
2668
|
+
}
|
|
2669
|
+
/** Whether the registered profile was calibrated on, or bound to, the device the camera runs on. */
|
|
2670
|
+
cameraProfileOnDevice(args = {}) {
|
|
1767
2671
|
const cameraId = normalizeId(args.CAMERA_ID);
|
|
2672
|
+
const deviceId = this.profiles.get(cameraId)?.device?.deviceId;
|
|
2673
|
+
return deviceId !== void 0 && deviceId.length > 0 && deviceId === this.conditionsOf(cameraId).deviceId;
|
|
2674
|
+
}
|
|
2675
|
+
/**
|
|
2676
|
+
* Starts a camera the way the page's query parameters ask, or as `start shared camera` does when
|
|
2677
|
+
* they ask nothing.
|
|
2678
|
+
*
|
|
2679
|
+
* How one app hands a camera to another it opens on the same origin. An app running several USB
|
|
2680
|
+
* cameras opens the lens calibration app for one of them, and the calibration has to be solved on
|
|
2681
|
+
* that device at the size the app will use: a camera the browser picks may be the other camera of
|
|
2682
|
+
* the same model, and a profile solved at another size is `incompatible`.
|
|
2683
|
+
*
|
|
2684
|
+
* `cameraDeviceId` is required exactly. `cameraWidth`, `cameraHeight` and `cameraFrameRate` are
|
|
2685
|
+
* asked for as `ideal`, the way the opening app asked, so the camera settles on the same mode.
|
|
2686
|
+
*/
|
|
2687
|
+
async startRequestedSharedCamera(args = {}) {
|
|
2688
|
+
const cameraId = normalizeId(args.CAMERA_ID);
|
|
2689
|
+
if (this.blockLeases.has(cameraId)) return;
|
|
2690
|
+
const lease = await this.acquireCamera({
|
|
2691
|
+
owner: "camera-source-block",
|
|
2692
|
+
cameraId,
|
|
2693
|
+
...requestedCamera(globalThis.location?.search ?? "")
|
|
2694
|
+
});
|
|
2695
|
+
this.blockLeases.set(cameraId, lease);
|
|
2696
|
+
}
|
|
2697
|
+
async restoreStoredProfile(cameraId, sameDevice) {
|
|
1768
2698
|
let records;
|
|
1769
2699
|
try {
|
|
1770
2700
|
records = await this.profileStore.list();
|
|
@@ -1776,14 +2706,26 @@
|
|
|
1776
2706
|
return;
|
|
1777
2707
|
}
|
|
1778
2708
|
const conditions = this.conditionsOf(cameraId);
|
|
2709
|
+
if (sameDevice && conditions.deviceId.length === 0) {
|
|
2710
|
+
this.storedProfileOutcomes.set(cameraId, {
|
|
2711
|
+
state: "undetermined",
|
|
2712
|
+
detail: "The camera is not running, so the device a saved profile was calibrated on cannot be compared."
|
|
2713
|
+
});
|
|
2714
|
+
return;
|
|
2715
|
+
}
|
|
1779
2716
|
let incompatible;
|
|
1780
2717
|
let undetermined;
|
|
2718
|
+
let otherDevices = 0;
|
|
1781
2719
|
for (const record of [...records].sort(newestFirst)) {
|
|
1782
2720
|
const parsed = parseJson(record.document);
|
|
1783
2721
|
if (!parsed.ok) continue;
|
|
1784
2722
|
const document = rebindCameraId(parsed.value, cameraId);
|
|
1785
2723
|
const result = readCameraProfileDocument(document);
|
|
1786
2724
|
if (!result.ok) continue;
|
|
2725
|
+
if (sameDevice && result.profile.device?.deviceId !== conditions.deviceId) {
|
|
2726
|
+
otherDevices += 1;
|
|
2727
|
+
continue;
|
|
2728
|
+
}
|
|
1787
2729
|
const report = evaluateProfileCompatibility(result.profile, conditions);
|
|
1788
2730
|
if (report.state === "compatible") {
|
|
1789
2731
|
this.profiles.register(document);
|
|
@@ -1799,7 +2741,7 @@
|
|
|
1799
2741
|
const decisive = incompatible ?? undetermined;
|
|
1800
2742
|
this.storedProfileOutcomes.set(cameraId, decisive === void 0 ? {
|
|
1801
2743
|
state: "none",
|
|
1802
|
-
detail: ""
|
|
2744
|
+
detail: otherDevices === 0 ? "" : `${otherDevices} saved profile(s) were calibrated on other devices and were not considered.`
|
|
1803
2745
|
} : {
|
|
1804
2746
|
state: decisive.state,
|
|
1805
2747
|
detail: compatibilityDetail(decisive)
|
|
@@ -1830,6 +2772,18 @@
|
|
|
1830
2772
|
const profile = this.profiles.get(normalizeId(args.CAMERA_ID));
|
|
1831
2773
|
return profile ? serializeCameraIntrinsicProfile(profile) : "";
|
|
1832
2774
|
}
|
|
2775
|
+
/**
|
|
2776
|
+
* The registered profile as a ROS `camera_info` YAML document, which is how a calibration is
|
|
2777
|
+
* written to a file or handed to another machine.
|
|
2778
|
+
*
|
|
2779
|
+
* The standard part loads in ROS and OpenCV-based tools as it is. What deciding compatibility needs
|
|
2780
|
+
* and ROS has no place for travels in the `turbowarp_camera_source` mapping, which ROS's reader
|
|
2781
|
+
* does not look at.
|
|
2782
|
+
*/
|
|
2783
|
+
cameraProfileYaml(args = {}) {
|
|
2784
|
+
const profile = this.profiles.get(normalizeId(args.CAMERA_ID));
|
|
2785
|
+
return profile ? serializeCameraInfoYaml(profile) : "";
|
|
2786
|
+
}
|
|
1833
2787
|
cameraProfileError() {
|
|
1834
2788
|
return this.profileError?.code ?? "";
|
|
1835
2789
|
}
|
|
@@ -1862,17 +2816,20 @@
|
|
|
1862
2816
|
cameraConditionsGeneration(args = {}) {
|
|
1863
2817
|
return this.generationOf(normalizeId(args.CAMERA_ID));
|
|
1864
2818
|
}
|
|
2819
|
+
/**
|
|
2820
|
+
* Registers profile text, whichever of the two forms an operator was handed.
|
|
2821
|
+
*
|
|
2822
|
+
* A ROS `camera_info` YAML file is what the calibration app writes and what leaves the PC; profile
|
|
2823
|
+
* JSON is what this extension renders and stores. Both reach the same validator, so a file is
|
|
2824
|
+
* accepted or refused for the same reasons whichever form it came in.
|
|
2825
|
+
*/
|
|
1865
2826
|
registerProfileText(value, prepare) {
|
|
1866
|
-
const parsed =
|
|
2827
|
+
const parsed = readProfileText(Scratch.Cast.toString(value ?? ""));
|
|
1867
2828
|
if (!parsed.ok) {
|
|
1868
|
-
this.profileError =
|
|
1869
|
-
code: "not-an-object",
|
|
1870
|
-
path: "",
|
|
1871
|
-
message: "The profile is not valid JSON."
|
|
1872
|
-
};
|
|
2829
|
+
this.profileError = parsed.error;
|
|
1873
2830
|
return;
|
|
1874
2831
|
}
|
|
1875
|
-
const result = this.profiles.register(prepare(parsed.
|
|
2832
|
+
const result = this.profiles.register(prepare(parsed.document));
|
|
1876
2833
|
this.profileError = result.ok ? void 0 : result.error;
|
|
1877
2834
|
}
|
|
1878
2835
|
/**
|