@srsergio/taptapp-ar 1.0.2 → 1.0.3

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.
Files changed (83) hide show
  1. package/README.md +47 -45
  2. package/dist/compiler/aframe.js +0 -3
  3. package/dist/compiler/compiler-base.d.ts +3 -7
  4. package/dist/compiler/compiler-base.js +28 -14
  5. package/dist/compiler/compiler.js +1 -1
  6. package/dist/compiler/compiler.worker.js +1 -1
  7. package/dist/compiler/controller.js +4 -5
  8. package/dist/compiler/controller.worker.js +0 -2
  9. package/dist/compiler/detector/crop-detector.js +0 -2
  10. package/dist/compiler/detector/detector-lite.d.ts +73 -0
  11. package/dist/compiler/detector/detector-lite.js +430 -0
  12. package/dist/compiler/detector/detector.js +236 -243
  13. package/dist/compiler/detector/kernels/cpu/binomialFilter.js +0 -1
  14. package/dist/compiler/detector/kernels/cpu/computeLocalization.js +0 -4
  15. package/dist/compiler/detector/kernels/cpu/computeOrientationHistograms.js +0 -18
  16. package/dist/compiler/detector/kernels/cpu/fakeShader.js +1 -1
  17. package/dist/compiler/detector/kernels/cpu/prune.d.ts +7 -1
  18. package/dist/compiler/detector/kernels/cpu/prune.js +1 -42
  19. package/dist/compiler/detector/kernels/webgl/upsampleBilinear.js +2 -2
  20. package/dist/compiler/estimation/refine-estimate.js +0 -1
  21. package/dist/compiler/estimation/utils.d.ts +1 -1
  22. package/dist/compiler/estimation/utils.js +1 -14
  23. package/dist/compiler/image-list.js +4 -4
  24. package/dist/compiler/input-loader.js +2 -2
  25. package/dist/compiler/matching/hamming-distance.js +13 -13
  26. package/dist/compiler/matching/hierarchical-clustering.js +1 -1
  27. package/dist/compiler/matching/matching.d.ts +20 -4
  28. package/dist/compiler/matching/matching.js +67 -41
  29. package/dist/compiler/matching/ransacHomography.js +1 -2
  30. package/dist/compiler/node-worker.d.ts +1 -0
  31. package/dist/compiler/node-worker.js +84 -0
  32. package/dist/compiler/offline-compiler.d.ts +171 -6
  33. package/dist/compiler/offline-compiler.js +303 -421
  34. package/dist/compiler/tensorflow-setup.js +27 -1
  35. package/dist/compiler/three.js +3 -5
  36. package/dist/compiler/tracker/extract.d.ts +1 -0
  37. package/dist/compiler/tracker/extract.js +200 -244
  38. package/dist/compiler/tracker/tracker.d.ts +1 -1
  39. package/dist/compiler/tracker/tracker.js +13 -18
  40. package/dist/compiler/utils/cumsum.d.ts +4 -2
  41. package/dist/compiler/utils/cumsum.js +17 -19
  42. package/dist/compiler/utils/gpu-compute.d.ts +57 -0
  43. package/dist/compiler/utils/gpu-compute.js +262 -0
  44. package/dist/compiler/utils/images.d.ts +4 -4
  45. package/dist/compiler/utils/images.js +67 -53
  46. package/dist/compiler/utils/worker-pool.d.ts +14 -0
  47. package/dist/compiler/utils/worker-pool.js +84 -0
  48. package/package.json +11 -13
  49. package/src/compiler/aframe.js +2 -4
  50. package/src/compiler/compiler-base.js +29 -14
  51. package/src/compiler/compiler.js +1 -1
  52. package/src/compiler/compiler.worker.js +1 -1
  53. package/src/compiler/controller.js +4 -5
  54. package/src/compiler/controller.worker.js +0 -2
  55. package/src/compiler/detector/crop-detector.js +0 -2
  56. package/src/compiler/detector/detector-lite.js +494 -0
  57. package/src/compiler/detector/detector.js +1052 -1063
  58. package/src/compiler/detector/kernels/cpu/binomialFilter.js +0 -1
  59. package/src/compiler/detector/kernels/cpu/computeLocalization.js +0 -4
  60. package/src/compiler/detector/kernels/cpu/computeOrientationHistograms.js +0 -17
  61. package/src/compiler/detector/kernels/cpu/fakeShader.js +1 -1
  62. package/src/compiler/detector/kernels/cpu/prune.js +1 -37
  63. package/src/compiler/detector/kernels/webgl/upsampleBilinear.js +2 -2
  64. package/src/compiler/estimation/refine-estimate.js +0 -1
  65. package/src/compiler/estimation/utils.js +9 -24
  66. package/src/compiler/image-list.js +4 -4
  67. package/src/compiler/input-loader.js +2 -2
  68. package/src/compiler/matching/hamming-distance.js +11 -15
  69. package/src/compiler/matching/hierarchical-clustering.js +1 -1
  70. package/src/compiler/matching/matching.js +72 -42
  71. package/src/compiler/matching/ransacHomography.js +0 -2
  72. package/src/compiler/node-worker.js +93 -0
  73. package/src/compiler/offline-compiler.js +339 -504
  74. package/src/compiler/tensorflow-setup.js +29 -1
  75. package/src/compiler/three.js +3 -5
  76. package/src/compiler/tracker/extract.js +211 -267
  77. package/src/compiler/tracker/tracker.js +13 -22
  78. package/src/compiler/utils/cumsum.js +17 -19
  79. package/src/compiler/utils/gpu-compute.js +303 -0
  80. package/src/compiler/utils/images.js +84 -53
  81. package/src/compiler/utils/worker-pool.js +89 -0
  82. package/src/compiler/estimation/esimate-experiment.js +0 -316
  83. package/src/compiler/estimation/refine-estimate-experiment.js +0 -512
@@ -0,0 +1,89 @@
1
+ import { Worker } from 'node:worker_threads';
2
+ import os from 'node:os';
3
+
4
+ export class WorkerPool {
5
+ constructor(workerPath, poolSize = os.cpus().length) {
6
+ this.workerPath = workerPath;
7
+ this.poolSize = poolSize;
8
+ this.workers = [];
9
+ this.queue = [];
10
+ this.activeWorkers = 0;
11
+ }
12
+
13
+ runTask(taskData) {
14
+ return new Promise((resolve, reject) => {
15
+ const task = { taskData, resolve, reject };
16
+ if (this.workers.length > 0) {
17
+ this._executeTask(this.workers.pop(), task);
18
+ } else if (this.activeWorkers < this.poolSize) {
19
+ this._executeTask(this._createWorker(), task);
20
+ } else {
21
+ this.queue.push(task);
22
+ }
23
+ });
24
+ }
25
+
26
+ _createWorker() {
27
+ this.activeWorkers++;
28
+ const worker = new Worker(this.workerPath);
29
+ return worker;
30
+ }
31
+
32
+ _executeTask(worker, task) {
33
+ const onMessage = (msg) => {
34
+ if (msg.type === 'progress' && task.taskData.onProgress) {
35
+ task.taskData.onProgress(msg.percent);
36
+ } else if (msg.type === 'compileDone') {
37
+ cleanup();
38
+ this._finishTask(worker, task.resolve, msg.trackingData);
39
+ } else if (msg.type === 'matchDone') {
40
+ cleanup();
41
+ this._finishTask(worker, task.resolve, msg.matchingData);
42
+ } else if (msg.type === 'error') {
43
+ cleanup();
44
+ this._finishTask(worker, task.reject, new Error(msg.error));
45
+ }
46
+ };
47
+
48
+ const onError = (err) => {
49
+ cleanup();
50
+ this._finishTask(worker, task.reject, err);
51
+ };
52
+
53
+ const cleanup = () => {
54
+ worker.removeListener('message', onMessage);
55
+ worker.removeListener('error', onError);
56
+ };
57
+
58
+ worker.on('message', onMessage);
59
+ worker.on('error', onError);
60
+
61
+ // Create a copy of taskData without functions for the worker
62
+ const serializableData = {};
63
+ for (const [key, value] of Object.entries(task.taskData)) {
64
+ if (typeof value !== 'function') {
65
+ serializableData[key] = value;
66
+ }
67
+ }
68
+
69
+ worker.postMessage({
70
+ type: 'compile',
71
+ ...serializableData
72
+ });
73
+ }
74
+
75
+ _finishTask(worker, callback, result) {
76
+ if (this.queue.length > 0) {
77
+ this._executeTask(worker, this.queue.shift());
78
+ } else {
79
+ this.workers.push(worker);
80
+ }
81
+ callback(result);
82
+ }
83
+
84
+ async destroy() {
85
+ await Promise.all(this.workers.map(w => w.terminate()));
86
+ this.workers = [];
87
+ this.activeWorkers = 0;
88
+ }
89
+ }
@@ -1,316 +0,0 @@
1
- // try to implement https://hal.inria.fr/inria-00174036/PDF/RR-6303.pdf
2
- import { Matrix, inverse } from "ml-matrix";
3
- import { SVD } from "svd-js";
4
- import { solveHomography } from "../utils/homography";
5
- import { computeScreenCoordiate } from "./utils";
6
-
7
- const opposites_of_minors = (M, row, col) => {
8
- let x1 = col === 0 ? 1 : 0;
9
- let x2 = col === 2 ? 1 : 2;
10
- let y1 = row === 0 ? 1 : 0;
11
- let y2 = row === 2 ? 1 : 2;
12
- return M[y1][x2] * M[y2][x1] - M[y1][x1] * M[y2][x2];
13
- };
14
-
15
- const findRmatFrom_tstar_n = (H, tstar, n, v) => {
16
- // computes R = H( I - (2/v)*te_star*ne_t )
17
- const twoDivV = 2 / v;
18
- const tmp = [
19
- [1 - twoDivV * tstar[0] * n[0], 0 - twoDivV * tstar[0] * n[1], 0 - twoDivV * tstar[0] * n[2]],
20
- [0 - twoDivV * tstar[1] * n[0], 1 - twoDivV * tstar[1] * n[1], 0 - twoDivV * tstar[1] * n[2]],
21
- [0 - twoDivV * tstar[2] * n[0], 0 - twoDivV * tstar[2] * n[1], 1 - twoDivV * tstar[2] * n[2]],
22
- ];
23
-
24
- const R = [
25
- [0, 0, 0],
26
- [0, 0, 0],
27
- [0, 0, 0],
28
- ];
29
- for (let i = 0; i < 3; i++) {
30
- for (let j = 0; j < 3; j++) {
31
- for (let k = 0; k < 3; k++) {
32
- R[i][j] += H[i][k] * tmp[k][j];
33
- }
34
- }
35
- }
36
- //const R = H.mmul( new Matrix(tmp));
37
- return R;
38
- };
39
-
40
- const estimate = ({ screenCoords, worldCoords, projectionTransform }) => {
41
- const Harray = solveHomography(
42
- worldCoords.map((p) => [p.x, p.y]),
43
- screenCoords.map((p) => [p.x, p.y]),
44
- );
45
- const G = new Matrix([
46
- [Harray[0], Harray[1], Harray[2]],
47
- [Harray[3], Harray[4], Harray[5]],
48
- [Harray[6], Harray[7], Harray[8]],
49
- ]);
50
- const K = new Matrix(projectionTransform);
51
- const KInv = inverse(K);
52
- const KInvArr = KInv.to2DArray();
53
- const KArr = K.to2DArray();
54
-
55
- const Hhat = KInv.mmul(G).mmul(K);
56
- const { q } = SVD(Hhat.to2DArray());
57
-
58
- const H = Hhat.div(q[1]);
59
-
60
- const HTH = H.transpose().mmul(H);
61
- const S = Matrix.sub(HTH, Matrix.eye(3, 3)).to2DArray();
62
-
63
- console.log("G", G);
64
- console.log("svd q", q);
65
- console.log("Hhat", Hhat);
66
- console.log("H", H);
67
- console.log("HTH", HTH);
68
- console.log("S", S);
69
-
70
- // M00, M11, M22
71
- const M00 = opposites_of_minors(S, 0, 0);
72
- const M11 = opposites_of_minors(S, 1, 1);
73
- const M22 = opposites_of_minors(S, 2, 2);
74
- const rtM00 = Math.sqrt(M00);
75
- const rtM11 = Math.sqrt(M11);
76
- const rtM22 = Math.sqrt(M22);
77
-
78
- // M01, M12, M02
79
- const M01 = opposites_of_minors(S, 0, 1);
80
- const e01 = M01 >= 0 ? 1 : -1;
81
- const M12 = opposites_of_minors(S, 1, 2);
82
- const e12 = M12 >= 0 ? 1 : -1;
83
- const M02 = opposites_of_minors(S, 0, 2);
84
- const e02 = M02 >= 0 ? 1 : -1;
85
-
86
- let maxIndex = 0;
87
- if (Math.abs(S[1][1]) > Math.abs(S[maxIndex][maxIndex])) maxIndex = 1;
88
- if (Math.abs(S[2][2]) > Math.abs(S[maxIndex][maxIndex])) maxIndex = 2;
89
-
90
- console.log("rtM00", rtM00, rtM11, rtM22);
91
- console.log("M01", M01, M12, M02, e01, e12, e02);
92
-
93
- let npa = [0, 0, 0];
94
- let npb = [0, 0, 0];
95
-
96
- console.log("max index", maxIndex);
97
- if (maxIndex === 0) {
98
- npa[0] = npb[0] = S[0][0];
99
- npa[1] = S[0][1] + rtM22;
100
- npb[1] = S[0][1] - rtM22;
101
- npa[2] = S[0][2] + e12 * rtM11;
102
- npb[2] = S[0][2] - e12 * rtM11;
103
- } else if (maxIndex === 1) {
104
- npa[0] = S[0][1] + rtM22;
105
- npb[0] = S[0][1] - rtM22;
106
- npa[1] = npb[1] = S[1][1];
107
- npa[2] = S[1][2] - e02 * rtM00;
108
- npb[2] = S[1][2] + e02 * rtM00;
109
- } else if (maxIndex === 2) {
110
- npa[0] = S[0][2] + e01 * rtM11;
111
- npb[0] = S[0][2] - e01 * rtM11;
112
- npa[1] = S[1][2] + rtM00;
113
- npb[1] = S[1][2] - rtM00;
114
- npa[2] = npb[2] = S[2][2];
115
- }
116
-
117
- console.log("npa", npa);
118
- console.log("npb", npb);
119
-
120
- const traceS = S[0][0] + S[1][1] + S[2][2];
121
- const v = 2.0 * Math.sqrt(1 + traceS - M00 - M11 - M22);
122
-
123
- const ESii = S[maxIndex][maxIndex] >= 0 ? 1 : -1;
124
-
125
- const r_2 = 2 + traceS + v;
126
- const nt_2 = 2 + traceS - v;
127
-
128
- const r = Math.sqrt(r_2);
129
- const n_t = Math.sqrt(nt_2);
130
-
131
- console.log("r n_t", r, n_t);
132
-
133
- const npaNorm = Math.sqrt(npa[0] * npa[0] + npa[1] * npa[1] + npa[2] * npa[2]);
134
- const npbNorm = Math.sqrt(npb[0] * npb[0] + npb[1] * npb[1] + npb[2] * npb[2]);
135
-
136
- const na = [npa[0] / npaNorm, npa[1] / npaNorm, npa[2] / npaNorm];
137
- const nb = [npb[0] / npbNorm, npb[1] / npbNorm, npb[2] / npbNorm];
138
-
139
- console.log("na nb", na, nb);
140
-
141
- const half_nt = 0.5 * n_t;
142
- const esii_t_r = ESii * r;
143
-
144
- const ta_star = [];
145
- for (let i = 0; i < 3; i++) {
146
- ta_star[i] = half_nt * (esii_t_r * nb[i] - n_t * na[i]);
147
- }
148
- const tb_star = [];
149
- for (let i = 0; i < 3; i++) {
150
- tb_star[i] = half_nt * (esii_t_r * na[i] - n_t * nb[i]);
151
- }
152
-
153
- const HArr = H.to2DArray();
154
- console.log("ta_star", ta_star, tb_star);
155
-
156
- /*
157
- """solutions = []
158
- # Ra, ta
159
- R = findRmatFrom_tstar_n(H, ta_star, na, v)
160
- t = R.dot(ta_star)
161
- solutions.append((R, t, na))
162
- # Ra, -ta
163
- solutions.append((R, -t, -na))
164
- # Rb, tb
165
- R = findRmatFrom_tstar_n(H, tb_star, nb, v)
166
- t = R.dot(tb_star)
167
- solutions.append((R, t, nb))
168
- # Rb, -tb
169
- solutions.append((R, -t, -nb))
170
- */
171
-
172
- const findT = (R1, ta_star) => {
173
- const t = [
174
- R1[0][0] * ta_star[0] + R1[0][1] * ta_star[1] + R1[0][2] * ta_star[2],
175
- R1[1][0] * ta_star[0] + R1[1][1] * ta_star[1] + R1[1][2] * ta_star[2],
176
- R1[2][0] * ta_star[0] + R1[2][1] * ta_star[1] + R1[2][2] * ta_star[2],
177
- ];
178
- return t;
179
- };
180
-
181
- const Ra = findRmatFrom_tstar_n(HArr, ta_star, na, v);
182
- const ta = findT(Ra, ta_star);
183
- const nta = [-ta[0], -ta[1], -ta[2]];
184
-
185
- console.log("RaTRa", new Matrix(Ra).transpose().mmul(new Matrix(Ra)));
186
-
187
- const Rb = findRmatFrom_tstar_n(HArr, tb_star, nb, v);
188
- const tb = findT(Rb, tb_star);
189
- const ntb = [-tb[0], -tb[1], -tb[2]];
190
-
191
- const findModelViewProjectionTransform = (R, t) => {
192
- const transform = [
193
- [R[0][0], R[0][1], R[0][2], t[0]],
194
- [R[1][0], R[1][1], R[1][2], t[1]],
195
- [R[2][0], R[2][1], R[2][2], t[2]],
196
- ];
197
- return transform;
198
-
199
- const modelViewProjectionTransform = [[], [], []];
200
- for (let j = 0; j < 3; j++) {
201
- for (let i = 0; i < 4; i++) {
202
- modelViewProjectionTransform[j][i] =
203
- KArr[j][0] * transform[0][i] +
204
- KArr[j][1] * transform[1][i] +
205
- KArr[j][2] * transform[2][i];
206
- }
207
- }
208
- return modelViewProjectionTransform;
209
- };
210
- console.log("Ra ta", Ra, ta);
211
- console.log("Rb tb", Rb, tb);
212
-
213
- const tnT = new Matrix([
214
- [ta[0] * na[0], ta[0] * na[1], ta[0] * na[2]],
215
- [ta[1] * na[0], ta[1] * na[1], ta[1] * na[2]],
216
- [ta[2] * na[0], ta[2] * na[1], ta[2] * na[2]],
217
- ]);
218
- const RtnT = new Matrix(Ra).add(tnT);
219
- console.log("tnT", tnT);
220
- console.log("RtnT", RtnT);
221
-
222
- const modelViewProjectionTransforms = [];
223
- modelViewProjectionTransforms.push(findModelViewProjectionTransform(Ra, ta));
224
- modelViewProjectionTransforms.push(findModelViewProjectionTransform(Ra, nta));
225
- modelViewProjectionTransforms.push(findModelViewProjectionTransform(Rb, tb));
226
- modelViewProjectionTransforms.push(findModelViewProjectionTransform(Rb, ntb));
227
-
228
- const applyMatrix = (K, pt) => {
229
- let kx = K[0][0] * pt[0] + K[0][1] * pt[1] + K[0][2];
230
- let ky = K[1][0] * pt[0] + K[1][1] * pt[1] + K[1][2];
231
- let kz = K[2][0] * pt[0] + K[2][1] * pt[1] + K[2][2];
232
- kx /= kz;
233
- ky /= kz;
234
- return [kx, ky];
235
- };
236
-
237
- for (let s = 0; s < modelViewProjectionTransforms.length; s++) {
238
- console.log("solution", s);
239
- const modelViewProjectionTransform = modelViewProjectionTransforms[s];
240
- for (let i = 0; i < worldCoords.length; i++) {
241
- let world = applyMatrix(KInvArr, [worldCoords[i].x, worldCoords[i].y]);
242
- let world2 = applyMatrix(RtnT.to2DArray(), world);
243
- let screen = applyMatrix(KInvArr, [screenCoords[i].x, screenCoords[i].y]);
244
-
245
- console.log("map", worldCoords[i], screenCoords[i]);
246
- console.log("mapped", world, world2, screen);
247
-
248
- //const mapped = computeScreenCoordiate(modelViewProjectionTransform, worldCoords[i].x, worldCoords[i].y, 0);
249
- //console.log("mapped", worldCoords[i], screenCoords[i], mapped);
250
- //console.log("mapped", worldCoords[i], screenCoords[i], kx2, ky2, mapped);
251
- }
252
- }
253
-
254
- for (let s = 0; s < modelViewProjectionTransforms.length; s++) {
255
- console.log("mvp solution", s);
256
- const modelViewProjectionTransform = modelViewProjectionTransforms[s];
257
- for (let i = 0; i < worldCoords.length; i++) {
258
- let world = applyMatrix(KInvArr, [worldCoords[i].x, worldCoords[i].y]);
259
- let screen = applyMatrix(KInvArr, [screenCoords[i].x, screenCoords[i].y]);
260
- //const mapped = computeScreenCoordiate(modelViewProjectionTransform, worldCoords[i].x, worldCoords[i].y, 0);
261
- const mapped = computeScreenCoordiate(modelViewProjectionTransform, world[0], world[1], 0);
262
- console.log("mapped", worldCoords[i], screenCoords[i], world, screen, mapped);
263
- }
264
- }
265
-
266
- return null;
267
-
268
- /*
269
-
270
- const R1 = findRmatFrom_tstar_n(HArr, ta_star, na, v);
271
- const R2 = findRmatFrom_tstar_n(HArr, tb_star, nb, v);
272
- console.log("R1", R1);
273
- console.log("R2", R2);
274
-
275
- const t = [
276
- R1[0][0] * ta_star[0] + R1[0][1] * ta_star[1] + R1[0][2] * ta_star[2],
277
- R1[1][0] * ta_star[0] + R1[1][1] * ta_star[1] + R1[1][2] * ta_star[2],
278
- R1[2][0] * ta_star[0] + R1[2][1] * ta_star[1] + R1[2][2] * ta_star[2]
279
- ]
280
-
281
- const R = R2;
282
-
283
- const modelViewProjectionTransform = [
284
- [R[0][0], R[0][1], R[0][2], t[0]],
285
- [R[1][0], R[1][1], R[1][2], t[0]],
286
- [R[2][0], R[2][1], R[2][2], t[0]],
287
- ];
288
- */
289
-
290
- for (let i = 0; i < worldCoords.length; i++) {
291
- const mapped = computeScreenCoordiate(
292
- modelViewProjectionTransform,
293
- worldCoords[i].x,
294
- worldCoords[i].y,
295
- 0,
296
- );
297
- console.log("mapped", worldCoords[i], screenCoords[i], mapped);
298
- }
299
-
300
- // this is the full computation if the projectTransform does not look like the expected format, but more computations
301
- const modelViewTransform = [[], [], []];
302
- for (let j = 0; j < 3; j++) {
303
- for (let i = 0; i < 4; i++) {
304
- modelViewTransform[j][i] =
305
- KInvArr[j][0] * modelViewProjectionTransform[0][i] +
306
- KInvArr[j][1] * modelViewProjectionTransform[1][i] +
307
- KInvArr[j][2] * modelViewProjectionTransform[2][i];
308
- }
309
- }
310
- console.log("KInvArr", KInvArr);
311
- console.log("modelViewProjectionTransform", modelViewProjectionTransform);
312
- console.log("modelViewTransform", modelViewTransform);
313
- return modelViewTransform;
314
- };
315
-
316
- export { estimate };