@srsergio/taptapp-ar 1.0.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.
Files changed (207) hide show
  1. package/README.md +62 -0
  2. package/dist/compiler/aframe.d.ts +1 -0
  3. package/dist/compiler/aframe.js +275 -0
  4. package/dist/compiler/compiler-base.d.ts +12 -0
  5. package/dist/compiler/compiler-base.js +165 -0
  6. package/dist/compiler/compiler.d.ts +9 -0
  7. package/dist/compiler/compiler.js +24 -0
  8. package/dist/compiler/compiler.worker.d.ts +1 -0
  9. package/dist/compiler/compiler.worker.js +28 -0
  10. package/dist/compiler/controller.d.ts +101 -0
  11. package/dist/compiler/controller.js +400 -0
  12. package/dist/compiler/controller.worker.d.ts +1 -0
  13. package/dist/compiler/controller.worker.js +61 -0
  14. package/dist/compiler/detector/crop-detector.d.ts +65 -0
  15. package/dist/compiler/detector/crop-detector.js +59 -0
  16. package/dist/compiler/detector/detector.d.ts +98 -0
  17. package/dist/compiler/detector/detector.js +1049 -0
  18. package/dist/compiler/detector/freak.d.ts +1 -0
  19. package/dist/compiler/detector/freak.js +89 -0
  20. package/dist/compiler/detector/kernels/cpu/binomialFilter.d.ts +6 -0
  21. package/dist/compiler/detector/kernels/cpu/binomialFilter.js +51 -0
  22. package/dist/compiler/detector/kernels/cpu/buildExtremas.d.ts +6 -0
  23. package/dist/compiler/detector/kernels/cpu/buildExtremas.js +89 -0
  24. package/dist/compiler/detector/kernels/cpu/computeExtremaAngles.d.ts +7 -0
  25. package/dist/compiler/detector/kernels/cpu/computeExtremaAngles.js +79 -0
  26. package/dist/compiler/detector/kernels/cpu/computeExtremaFreak.d.ts +6 -0
  27. package/dist/compiler/detector/kernels/cpu/computeExtremaFreak.js +68 -0
  28. package/dist/compiler/detector/kernels/cpu/computeFreakDescriptors.d.ts +6 -0
  29. package/dist/compiler/detector/kernels/cpu/computeFreakDescriptors.js +57 -0
  30. package/dist/compiler/detector/kernels/cpu/computeLocalization.d.ts +6 -0
  31. package/dist/compiler/detector/kernels/cpu/computeLocalization.js +54 -0
  32. package/dist/compiler/detector/kernels/cpu/computeOrientationHistograms.d.ts +6 -0
  33. package/dist/compiler/detector/kernels/cpu/computeOrientationHistograms.js +118 -0
  34. package/dist/compiler/detector/kernels/cpu/downsampleBilinear.d.ts +6 -0
  35. package/dist/compiler/detector/kernels/cpu/downsampleBilinear.js +29 -0
  36. package/dist/compiler/detector/kernels/cpu/extremaReduction.d.ts +6 -0
  37. package/dist/compiler/detector/kernels/cpu/extremaReduction.js +50 -0
  38. package/dist/compiler/detector/kernels/cpu/fakeShader.d.ts +20 -0
  39. package/dist/compiler/detector/kernels/cpu/fakeShader.js +80 -0
  40. package/dist/compiler/detector/kernels/cpu/index.d.ts +1 -0
  41. package/dist/compiler/detector/kernels/cpu/index.js +25 -0
  42. package/dist/compiler/detector/kernels/cpu/prune.d.ts +1 -0
  43. package/dist/compiler/detector/kernels/cpu/prune.js +103 -0
  44. package/dist/compiler/detector/kernels/cpu/smoothHistograms.d.ts +6 -0
  45. package/dist/compiler/detector/kernels/cpu/smoothHistograms.js +47 -0
  46. package/dist/compiler/detector/kernels/cpu/upsampleBilinear.d.ts +6 -0
  47. package/dist/compiler/detector/kernels/cpu/upsampleBilinear.js +43 -0
  48. package/dist/compiler/detector/kernels/index.d.ts +1 -0
  49. package/dist/compiler/detector/kernels/index.js +2 -0
  50. package/dist/compiler/detector/kernels/webgl/binomialFilter.d.ts +6 -0
  51. package/dist/compiler/detector/kernels/webgl/binomialFilter.js +67 -0
  52. package/dist/compiler/detector/kernels/webgl/buildExtremas.d.ts +6 -0
  53. package/dist/compiler/detector/kernels/webgl/buildExtremas.js +101 -0
  54. package/dist/compiler/detector/kernels/webgl/computeExtremaAngles.d.ts +6 -0
  55. package/dist/compiler/detector/kernels/webgl/computeExtremaAngles.js +78 -0
  56. package/dist/compiler/detector/kernels/webgl/computeExtremaFreak.d.ts +6 -0
  57. package/dist/compiler/detector/kernels/webgl/computeExtremaFreak.js +86 -0
  58. package/dist/compiler/detector/kernels/webgl/computeFreakDescriptors.d.ts +6 -0
  59. package/dist/compiler/detector/kernels/webgl/computeFreakDescriptors.js +52 -0
  60. package/dist/compiler/detector/kernels/webgl/computeLocalization.d.ts +6 -0
  61. package/dist/compiler/detector/kernels/webgl/computeLocalization.js +58 -0
  62. package/dist/compiler/detector/kernels/webgl/computeOrientationHistograms.d.ts +6 -0
  63. package/dist/compiler/detector/kernels/webgl/computeOrientationHistograms.js +116 -0
  64. package/dist/compiler/detector/kernels/webgl/downsampleBilinear.d.ts +6 -0
  65. package/dist/compiler/detector/kernels/webgl/downsampleBilinear.js +46 -0
  66. package/dist/compiler/detector/kernels/webgl/extremaReduction.d.ts +6 -0
  67. package/dist/compiler/detector/kernels/webgl/extremaReduction.js +48 -0
  68. package/dist/compiler/detector/kernels/webgl/index.d.ts +1 -0
  69. package/dist/compiler/detector/kernels/webgl/index.js +25 -0
  70. package/dist/compiler/detector/kernels/webgl/smoothHistograms.d.ts +6 -0
  71. package/dist/compiler/detector/kernels/webgl/smoothHistograms.js +49 -0
  72. package/dist/compiler/detector/kernels/webgl/upsampleBilinear.d.ts +6 -0
  73. package/dist/compiler/detector/kernels/webgl/upsampleBilinear.js +56 -0
  74. package/dist/compiler/estimation/esimate-experiment.d.ts +5 -0
  75. package/dist/compiler/estimation/esimate-experiment.js +267 -0
  76. package/dist/compiler/estimation/estimate.d.ts +5 -0
  77. package/dist/compiler/estimation/estimate.js +51 -0
  78. package/dist/compiler/estimation/estimator.d.ts +13 -0
  79. package/dist/compiler/estimation/estimator.js +30 -0
  80. package/dist/compiler/estimation/refine-estimate-experiment.d.ts +6 -0
  81. package/dist/compiler/estimation/refine-estimate-experiment.js +429 -0
  82. package/dist/compiler/estimation/refine-estimate.d.ts +6 -0
  83. package/dist/compiler/estimation/refine-estimate.js +299 -0
  84. package/dist/compiler/estimation/utils.d.ts +10 -0
  85. package/dist/compiler/estimation/utils.js +80 -0
  86. package/dist/compiler/image-list.d.ts +13 -0
  87. package/dist/compiler/image-list.js +52 -0
  88. package/dist/compiler/index.d.ts +3 -0
  89. package/dist/compiler/index.js +10 -0
  90. package/dist/compiler/input-loader.d.ts +23 -0
  91. package/dist/compiler/input-loader.js +88 -0
  92. package/dist/compiler/matching/hamming-distance.d.ts +1 -0
  93. package/dist/compiler/matching/hamming-distance.js +20 -0
  94. package/dist/compiler/matching/hierarchical-clustering.d.ts +7 -0
  95. package/dist/compiler/matching/hierarchical-clustering.js +109 -0
  96. package/dist/compiler/matching/hough.d.ts +1 -0
  97. package/dist/compiler/matching/hough.js +169 -0
  98. package/dist/compiler/matching/matcher.d.ts +28 -0
  99. package/dist/compiler/matching/matcher.js +48 -0
  100. package/dist/compiler/matching/matching.d.ts +41 -0
  101. package/dist/compiler/matching/matching.js +197 -0
  102. package/dist/compiler/matching/ransacHomography.d.ts +1 -0
  103. package/dist/compiler/matching/ransacHomography.js +136 -0
  104. package/dist/compiler/offline-compiler.d.ts +10 -0
  105. package/dist/compiler/offline-compiler.js +450 -0
  106. package/dist/compiler/tensorflow-setup.d.ts +7 -0
  107. package/dist/compiler/tensorflow-setup.js +73 -0
  108. package/dist/compiler/three.d.ts +66 -0
  109. package/dist/compiler/three.js +310 -0
  110. package/dist/compiler/tracker/extract-utils.d.ts +1 -0
  111. package/dist/compiler/tracker/extract-utils.js +29 -0
  112. package/dist/compiler/tracker/extract.d.ts +4 -0
  113. package/dist/compiler/tracker/extract.js +349 -0
  114. package/dist/compiler/tracker/tracker.d.ts +38 -0
  115. package/dist/compiler/tracker/tracker.js +327 -0
  116. package/dist/compiler/utils/cumsum.d.ts +5 -0
  117. package/dist/compiler/utils/cumsum.js +39 -0
  118. package/dist/compiler/utils/geometry.d.ts +8 -0
  119. package/dist/compiler/utils/geometry.js +101 -0
  120. package/dist/compiler/utils/homography.d.ts +1 -0
  121. package/dist/compiler/utils/homography.js +138 -0
  122. package/dist/compiler/utils/images.d.ts +24 -0
  123. package/dist/compiler/utils/images.js +99 -0
  124. package/dist/compiler/utils/randomizer.d.ts +5 -0
  125. package/dist/compiler/utils/randomizer.js +25 -0
  126. package/dist/index.d.ts +6 -0
  127. package/dist/index.js +7 -0
  128. package/dist/react/AREditor.d.ts +5 -0
  129. package/dist/react/AREditor.js +159 -0
  130. package/dist/react/ProgressDialog.d.ts +13 -0
  131. package/dist/react/ProgressDialog.js +57 -0
  132. package/dist/react/types.d.ts +22 -0
  133. package/dist/react/types.js +14 -0
  134. package/package.json +53 -0
  135. package/src/astro/ARScene.astro +59 -0
  136. package/src/astro/ARVideoTrigger.astro +73 -0
  137. package/src/astro/overlays/ErrorOverlay.astro +40 -0
  138. package/src/astro/overlays/LoadingOverlay.astro +28 -0
  139. package/src/astro/overlays/ScanningOverlay.astro +119 -0
  140. package/src/astro/scripts/ARScripts.astro +118 -0
  141. package/src/astro/styles/ARStyles.astro +147 -0
  142. package/src/compiler/aframe.js +343 -0
  143. package/src/compiler/compiler-base.js +195 -0
  144. package/src/compiler/compiler.js +25 -0
  145. package/src/compiler/compiler.worker.js +30 -0
  146. package/src/compiler/controller.js +473 -0
  147. package/src/compiler/controller.worker.js +77 -0
  148. package/src/compiler/detector/crop-detector.js +68 -0
  149. package/src/compiler/detector/detector.js +1130 -0
  150. package/src/compiler/detector/freak.js +91 -0
  151. package/src/compiler/detector/kernels/cpu/binomialFilter.js +59 -0
  152. package/src/compiler/detector/kernels/cpu/buildExtremas.js +108 -0
  153. package/src/compiler/detector/kernels/cpu/computeExtremaAngles.js +91 -0
  154. package/src/compiler/detector/kernels/cpu/computeExtremaFreak.js +92 -0
  155. package/src/compiler/detector/kernels/cpu/computeFreakDescriptors.js +68 -0
  156. package/src/compiler/detector/kernels/cpu/computeLocalization.js +71 -0
  157. package/src/compiler/detector/kernels/cpu/computeOrientationHistograms.js +141 -0
  158. package/src/compiler/detector/kernels/cpu/downsampleBilinear.js +33 -0
  159. package/src/compiler/detector/kernels/cpu/extremaReduction.js +53 -0
  160. package/src/compiler/detector/kernels/cpu/fakeShader.js +88 -0
  161. package/src/compiler/detector/kernels/cpu/index.js +26 -0
  162. package/src/compiler/detector/kernels/cpu/prune.js +114 -0
  163. package/src/compiler/detector/kernels/cpu/smoothHistograms.js +57 -0
  164. package/src/compiler/detector/kernels/cpu/upsampleBilinear.js +51 -0
  165. package/src/compiler/detector/kernels/index.js +2 -0
  166. package/src/compiler/detector/kernels/webgl/binomialFilter.js +72 -0
  167. package/src/compiler/detector/kernels/webgl/buildExtremas.js +109 -0
  168. package/src/compiler/detector/kernels/webgl/computeExtremaAngles.js +82 -0
  169. package/src/compiler/detector/kernels/webgl/computeExtremaFreak.js +105 -0
  170. package/src/compiler/detector/kernels/webgl/computeFreakDescriptors.js +56 -0
  171. package/src/compiler/detector/kernels/webgl/computeLocalization.js +70 -0
  172. package/src/compiler/detector/kernels/webgl/computeOrientationHistograms.js +129 -0
  173. package/src/compiler/detector/kernels/webgl/downsampleBilinear.js +50 -0
  174. package/src/compiler/detector/kernels/webgl/extremaReduction.js +50 -0
  175. package/src/compiler/detector/kernels/webgl/index.js +26 -0
  176. package/src/compiler/detector/kernels/webgl/smoothHistograms.js +53 -0
  177. package/src/compiler/detector/kernels/webgl/upsampleBilinear.js +62 -0
  178. package/src/compiler/estimation/esimate-experiment.js +316 -0
  179. package/src/compiler/estimation/estimate.js +67 -0
  180. package/src/compiler/estimation/estimator.js +34 -0
  181. package/src/compiler/estimation/refine-estimate-experiment.js +512 -0
  182. package/src/compiler/estimation/refine-estimate.js +365 -0
  183. package/src/compiler/estimation/utils.js +97 -0
  184. package/src/compiler/image-list.js +62 -0
  185. package/src/compiler/index.js +13 -0
  186. package/src/compiler/input-loader.js +107 -0
  187. package/src/compiler/matching/hamming-distance.js +23 -0
  188. package/src/compiler/matching/hierarchical-clustering.js +131 -0
  189. package/src/compiler/matching/hough.js +206 -0
  190. package/src/compiler/matching/matcher.js +59 -0
  191. package/src/compiler/matching/matching.js +237 -0
  192. package/src/compiler/matching/ransacHomography.js +192 -0
  193. package/src/compiler/offline-compiler.js +553 -0
  194. package/src/compiler/tensorflow-setup.js +88 -0
  195. package/src/compiler/three.js +368 -0
  196. package/src/compiler/tracker/extract-utils.js +34 -0
  197. package/src/compiler/tracker/extract.js +419 -0
  198. package/src/compiler/tracker/tracker.js +397 -0
  199. package/src/compiler/utils/cumsum.js +40 -0
  200. package/src/compiler/utils/geometry.js +114 -0
  201. package/src/compiler/utils/homography.js +150 -0
  202. package/src/compiler/utils/images.js +111 -0
  203. package/src/compiler/utils/randomizer.js +29 -0
  204. package/src/index.ts +8 -0
  205. package/src/react/AREditor.tsx +394 -0
  206. package/src/react/ProgressDialog.tsx +185 -0
  207. package/src/react/types.ts +35 -0
@@ -0,0 +1 @@
1
+ export const FREAKPOINTS: any[];
@@ -0,0 +1,89 @@
1
+ // 37 points = 6 rings x 6 points per ring + 1 center
2
+ const FREAK_RINGS = [
3
+ // ring 5
4
+ {
5
+ sigma: 0.55,
6
+ points: [
7
+ [-1.0, 0.0],
8
+ [-0.5, -0.866025],
9
+ [0.5, -0.866025],
10
+ [1.0, -0.0],
11
+ [0.5, 0.866025],
12
+ [-0.5, 0.866025],
13
+ ],
14
+ },
15
+ // ring 4
16
+ {
17
+ sigma: 0.475,
18
+ points: [
19
+ [0.0, 0.930969],
20
+ [-0.806243, 0.465485],
21
+ [-0.806243, -0.465485],
22
+ [-0.0, -0.930969],
23
+ [0.806243, -0.465485],
24
+ [0.806243, 0.465485],
25
+ ],
26
+ },
27
+ // ring 3
28
+ {
29
+ sigma: 0.4,
30
+ points: [
31
+ [0.847306, -0.0],
32
+ [0.423653, 0.733789],
33
+ [-0.423653, 0.733789],
34
+ [-0.847306, 0.0],
35
+ [-0.423653, -0.733789],
36
+ [0.423653, -0.733789],
37
+ ],
38
+ },
39
+ // ring 2
40
+ {
41
+ sigma: 0.325,
42
+ points: [
43
+ [-0.0, -0.741094],
44
+ [0.641806, -0.370547],
45
+ [0.641806, 0.370547],
46
+ [0.0, 0.741094],
47
+ [-0.641806, 0.370547],
48
+ [-0.641806, -0.370547],
49
+ ],
50
+ },
51
+ // ring 1
52
+ {
53
+ sigma: 0.25,
54
+ points: [
55
+ [-0.595502, 0.0],
56
+ [-0.297751, -0.51572],
57
+ [0.297751, -0.51572],
58
+ [0.595502, -0.0],
59
+ [0.297751, 0.51572],
60
+ [-0.297751, 0.51572],
61
+ ],
62
+ },
63
+ // ring 0
64
+ {
65
+ sigma: 0.175,
66
+ points: [
67
+ [0.0, 0.362783],
68
+ [-0.314179, 0.181391],
69
+ [-0.314179, -0.181391],
70
+ [-0.0, -0.362783],
71
+ [0.314179, -0.181391],
72
+ [0.314179, 0.181391],
73
+ ],
74
+ },
75
+ // center
76
+ {
77
+ sigma: 0.1,
78
+ points: [[0, 0]],
79
+ },
80
+ ];
81
+ const FREAKPOINTS = [];
82
+ for (let r = 0; r < FREAK_RINGS.length; r++) {
83
+ const sigma = FREAK_RINGS[r].sigma;
84
+ for (let i = 0; i < FREAK_RINGS[r].points.length; i++) {
85
+ const point = FREAK_RINGS[r].points[i];
86
+ FREAKPOINTS.push([sigma, point[0], point[1]]);
87
+ }
88
+ }
89
+ export { FREAKPOINTS };
@@ -0,0 +1,6 @@
1
+ export function binomialFilter(args: any): Tensor;
2
+ export namespace binomialFilterConfig {
3
+ export let kernelName: string;
4
+ export let backendName: string;
5
+ export { binomialFilter as kernelFunc };
6
+ }
@@ -0,0 +1,51 @@
1
+ import * as FakeShader from "./fakeShader.js";
2
+ function GetKernels(image) {
3
+ const imageWidth = image.shape[1];
4
+ const key = "w" + imageWidth;
5
+ const imageHeight = image.shape[0];
6
+ const kernel1 = {
7
+ variableNames: ["p"],
8
+ outputShape: [imageHeight, imageWidth],
9
+ userCode: function () {
10
+ const coords = this.getOutputCoords();
11
+ let sum = this.getP(coords[0], coords[1] - 2);
12
+ sum += this.getP(coords[0], coords[1] - 1) * 4;
13
+ sum += this.getP(coords[0], coords[1]) * 6;
14
+ sum += this.getP(coords[0], coords[1] + 1) * 4;
15
+ sum += this.getP(coords[0], coords[1] + 2);
16
+ this.setOutput(sum);
17
+ },
18
+ };
19
+ const kernel2 = {
20
+ variableNames: ["p"],
21
+ outputShape: [imageHeight, imageWidth],
22
+ userCode: function () {
23
+ const coords = this.getOutputCoords();
24
+ let sum = this.getP(coords[0] - 2, coords[1]);
25
+ sum += this.getP(coords[0] - 1, coords[1]) * 4;
26
+ sum += this.getP(coords[0], coords[1]) * 6;
27
+ sum += this.getP(coords[0] + 1, coords[1]) * 4;
28
+ sum += this.getP(coords[0] + 2, coords[1]);
29
+ sum /= 256;
30
+ this.setOutput(sum);
31
+ },
32
+ };
33
+ return [kernel1, kernel2];
34
+ //}
35
+ }
36
+ export const binomialFilter = (args) => {
37
+ //{inputs: UnaryInputs, backend: MathBackendCPU}
38
+ /** @type {import('@tensorflow/tfjs').TensorInfo} */
39
+ const image = args.inputs.image;
40
+ /** @type {MathBackendCPU} */
41
+ const backend = args.backend;
42
+ const [kernel1, kernel2] = GetKernels(image);
43
+ const result1 = FakeShader.runCode(backend, kernel1, [image], image.dtype);
44
+ return FakeShader.runCode(backend, kernel2, [result1], image.dtype);
45
+ };
46
+ export const binomialFilterConfig = {
47
+ //: KernelConfig
48
+ kernelName: "BinomialFilter",
49
+ backendName: "cpu",
50
+ kernelFunc: binomialFilter, // as {} as KernelFunc,
51
+ };
@@ -0,0 +1,6 @@
1
+ export function buildExtremas(args: any): Tensor;
2
+ export namespace buildExtremasConfig {
3
+ export let kernelName: string;
4
+ export let backendName: string;
5
+ export { buildExtremas as kernelFunc };
6
+ }
@@ -0,0 +1,89 @@
1
+ import * as FakeShader from "./fakeShader.js";
2
+ import { engine } from "@tensorflow/tfjs";
3
+ const FREAK_EXPANSION_FACTOR = 7.0;
4
+ const LAPLACIAN_THRESHOLD = 3.0;
5
+ const LAPLACIAN_SQR_THRESHOLD = LAPLACIAN_THRESHOLD * LAPLACIAN_THRESHOLD;
6
+ const EDGE_THRESHOLD = 4.0;
7
+ const EDGE_HESSIAN_THRESHOLD = ((EDGE_THRESHOLD + 1) * (EDGE_THRESHOLD + 1)) / EDGE_THRESHOLD;
8
+ function GetProgram(image) {
9
+ const imageWidth = image.shape[1];
10
+ const imageHeight = image.shape[0];
11
+ const kernel = {
12
+ variableNames: ["image0", "image1", "image2"],
13
+ outputShape: [imageHeight, imageWidth],
14
+ userCode: function () {
15
+ const coords = this.getOutputCoords();
16
+ const y = coords[0];
17
+ const x = coords[1];
18
+ const value = this.getImage1(y, x);
19
+ // Step 1: find local maxima/minima
20
+ if (value * value < LAPLACIAN_SQR_THRESHOLD) {
21
+ this.setOutput(0.0);
22
+ return;
23
+ }
24
+ if (y < FREAK_EXPANSION_FACTOR || y > imageHeight - 1 - FREAK_EXPANSION_FACTOR) {
25
+ this.setOutput(0.0);
26
+ return;
27
+ }
28
+ if (x < FREAK_EXPANSION_FACTOR || x > imageWidth - 1 - FREAK_EXPANSION_FACTOR) {
29
+ this.setOutput(0.0);
30
+ return;
31
+ }
32
+ let isMax = true;
33
+ let isMin = true;
34
+ for (let dy = -1; dy <= 1; dy++) {
35
+ for (let dx = -1; dx <= 1; dx++) {
36
+ const value0 = this.getImage0(y + dy, x + dx);
37
+ const value1 = this.getImage1(y + dy, x + dx);
38
+ const value2 = this.getImage2(y + dy, x + dx);
39
+ if (value < value0 || value < value1 || value < value2) {
40
+ isMax = false;
41
+ }
42
+ if (value > value0 || value > value1 || value > value2) {
43
+ isMin = false;
44
+ }
45
+ }
46
+ }
47
+ if (!isMax && !isMin) {
48
+ this.setOutput(0.0);
49
+ return;
50
+ }
51
+ // compute edge score and reject based on threshold
52
+ const dxx = this.getImage1(y, x + 1) + this.getImage1(y, x - 1) - 2 * this.getImage1(y, x);
53
+ const dyy = this.getImage1(y + 1, x) + this.getImage1(y - 1, x) - 2 * this.getImage1(y, x);
54
+ const dxy = 0.25 *
55
+ (this.getImage1(y - 1, x - 1) +
56
+ this.getImage1(y + 1, x + 1) -
57
+ this.getImage1(y - 1, x + 1) -
58
+ this.getImage1(y + 1, x - 1));
59
+ const det = dxx * dyy - dxy * dxy;
60
+ if (Math.abs(det) < 0.0001) {
61
+ // determinant undefined. no solution
62
+ this.setOutput(0.0);
63
+ return;
64
+ }
65
+ const edgeScore = ((dxx + dyy) * (dxx + dyy)) / det;
66
+ if (Math.abs(edgeScore) >= EDGE_HESSIAN_THRESHOLD) {
67
+ this.setOutput(0.0);
68
+ return;
69
+ }
70
+ this.setOutput(this.getImage1(y, x));
71
+ },
72
+ };
73
+ return kernel;
74
+ }
75
+ export const buildExtremas = (args) => {
76
+ let { image0, image1, image2 } = args.inputs;
77
+ /** @type {MathBackendCPU} */
78
+ const backend = args.backend;
79
+ image0 = engine().runKernel("DownsampleBilinear", { image: image0 });
80
+ image2 = engine().runKernel("UpsampleBilinear", { image: image2, targetImage: image1 });
81
+ const program = GetProgram(image1);
82
+ return FakeShader.runCode(backend, program, [image0, image1, image2], image1.dtype);
83
+ };
84
+ export const buildExtremasConfig = {
85
+ //: KernelConfig
86
+ kernelName: "BuildExtremas",
87
+ backendName: "cpu",
88
+ kernelFunc: buildExtremas, // as {} as KernelFunc,
89
+ };
@@ -0,0 +1,7 @@
1
+ export function computeExtremaAnglesImpl(histogram: any): Float32Array<any>;
2
+ export function computeExtremaAngles(args: any): import("@tensorflow/tfjs-core").Tensor<import("@tensorflow/tfjs-core").Rank>;
3
+ export namespace computeExtremaAnglesConfig {
4
+ export let kernelName: string;
5
+ export let backendName: string;
6
+ export { computeExtremaAngles as kernelFunc };
7
+ }
@@ -0,0 +1,79 @@
1
+ const ORIENTATION_NUM_BINS = 36;
2
+ export function computeExtremaAnglesImpl(histogram) {
3
+ const resultValues = new Float32Array(histogram.height);
4
+ function getHistogram(featureIndex, prev) {
5
+ return histogram.values[featureIndex * histogram.width + prev];
6
+ }
7
+ function setOutput(featureIndex, an) {
8
+ resultValues[featureIndex] = an;
9
+ }
10
+ function imod(x, y) {
11
+ return Math.trunc(x - y * Math.floor(x / y));
12
+ }
13
+ for (let featureIndex = 0; featureIndex < histogram.height; featureIndex++) {
14
+ let maxIndex = 0;
15
+ for (let i = 1; i < ORIENTATION_NUM_BINS; i++) {
16
+ if (getHistogram(featureIndex, i) > getHistogram(featureIndex, maxIndex)) {
17
+ maxIndex = i;
18
+ }
19
+ }
20
+ let prev = imod(maxIndex - 1 + ORIENTATION_NUM_BINS, ORIENTATION_NUM_BINS);
21
+ let next = imod(maxIndex + 1, ORIENTATION_NUM_BINS);
22
+ /**
23
+ * Fit a quatratic to 3 points. The system of equations is:
24
+ *
25
+ * y0 = A*x0^2 + B*x0 + C
26
+ * y1 = A*x1^2 + B*x1 + C
27
+ * y2 = A*x2^2 + B*x2 + C
28
+ *
29
+ * This system of equations is solved for A,B,C.
30
+ */
31
+ const p10 = maxIndex - 1;
32
+ const p11 = getHistogram(featureIndex, prev);
33
+ const p20 = maxIndex;
34
+ const p21 = getHistogram(featureIndex, maxIndex);
35
+ const p30 = maxIndex + 1;
36
+ const p31 = getHistogram(featureIndex, next);
37
+ const d1 = (p30 - p20) * (p30 - p10);
38
+ const d2 = (p10 - p20) * (p30 - p10);
39
+ const d3 = p10 - p20;
40
+ // If any of the denominators are zero then, just use maxIndex.
41
+ let fbin = maxIndex;
42
+ if (Math.abs(d1) > 0.00001 && Math.abs(d2) > 0.00001 && Math.abs(d3) > 0.00001) {
43
+ const a = p10 * p10;
44
+ const b = p20 * p20;
45
+ // Solve for the coefficients A,B,C
46
+ let A = (p31 - p21) / d1 - (p11 - p21) / d2;
47
+ if (Number.isNaN(A))
48
+ A = 0;
49
+ const B = (p11 - p21 + A * (b - a)) / d3;
50
+ //const C = p11 - (A * a) - (B * p10);
51
+ fbin = -B / (2.0 * A);
52
+ if (Number.isNaN(fbin))
53
+ fbin = 0; //console.warn(`computeExtremaAngles::NaN! fbin=${fbin} maxIndex=${maxIndex} A=${A} B=${B} p31=${p31} p21=${p21} p11=${p11}`);
54
+ }
55
+ const an = (2.0 * Math.PI * (fbin + 0.5)) / ORIENTATION_NUM_BINS - Math.PI;
56
+ setOutput(featureIndex, an);
57
+ }
58
+ return resultValues;
59
+ }
60
+ export const computeExtremaAngles = (args) => {
61
+ /** @type {import('@tensorflow/tfjs').TensorInfo} */
62
+ const { histograms } = args.inputs;
63
+ /** @type {import('@tensorflow/tfjs-backend-cpu').MathBackendCPU} */
64
+ const backend = args.backend;
65
+ /** @type {TypedArray} */
66
+ const histogramValues = {
67
+ values: backend.data.get(histograms.dataId).values,
68
+ width: histograms.shape[1],
69
+ height: histograms.shape[0],
70
+ };
71
+ const resultValues = computeExtremaAnglesImpl(histogramValues);
72
+ return backend.makeOutput(resultValues, [histograms.shape[0]], histograms.dtype);
73
+ };
74
+ export const computeExtremaAnglesConfig = {
75
+ //: KernelConfig
76
+ kernelName: "ComputeExtremaAngles",
77
+ backendName: "cpu",
78
+ kernelFunc: computeExtremaAngles, // as {} as KernelFunc,
79
+ };
@@ -0,0 +1,6 @@
1
+ export function computeExtremaFreak(args: any): Tensor;
2
+ export namespace computeExtremaFreakConfig {
3
+ export let kernelName: string;
4
+ export let backendName: string;
5
+ export { computeExtremaFreak as kernelFunc };
6
+ }
@@ -0,0 +1,68 @@
1
+ import { FREAKPOINTS } from "../../freak.js";
2
+ import * as FakeShader from "./fakeShader.js";
3
+ const FREAK_EXPANSION_FACTOR = 7.0;
4
+ function GetProgram(prunedExtremasHeight, pyramidImagesLength) {
5
+ const imageVariableNames = [];
6
+ for (let i = 1; i < pyramidImagesLength; i++) {
7
+ imageVariableNames.push("image" + i);
8
+ }
9
+ const kernel = {
10
+ variableNames: [...imageVariableNames, "extrema", "angles", "freakPoints"],
11
+ outputShape: [prunedExtremasHeight, FREAKPOINTS.length],
12
+ userCode: function () {
13
+ const getPixel = (octave, y, x) => {
14
+ const key = "getImage" + octave;
15
+ if (octave < 1 || octave >= pyramidImagesLength)
16
+ return 0.0;
17
+ return this[key](y, x);
18
+ };
19
+ const coords = this.getOutputCoords();
20
+ const featureIndex = coords[0];
21
+ const freakIndex = coords[1];
22
+ //const freakSigma = this.getFreakPoints(freakIndex, 0);
23
+ const freakX = this.getFreakPoints(freakIndex, 1);
24
+ const freakY = this.getFreakPoints(freakIndex, 2);
25
+ const octave = this.int(this.getExtrema(featureIndex, 1));
26
+ const inputY = this.getExtrema(featureIndex, 2);
27
+ const inputX = this.getExtrema(featureIndex, 3);
28
+ const inputAngle = this.getAngles(featureIndex);
29
+ const cos = FREAK_EXPANSION_FACTOR * Math.cos(inputAngle);
30
+ const sin = FREAK_EXPANSION_FACTOR * Math.sin(inputAngle);
31
+ const yp = inputY + freakX * sin + freakY * cos;
32
+ const xp = inputX + freakX * cos + freakY * -sin;
33
+ const x0 = this.int(Math.floor(xp));
34
+ const x1 = x0 + 1;
35
+ const y0 = this.int(Math.floor(yp));
36
+ const y1 = y0 + 1;
37
+ const f1 = getPixel(octave, y0, x0);
38
+ const f2 = getPixel(octave, y0, x1);
39
+ const f3 = getPixel(octave, y1, x0);
40
+ const f4 = getPixel(octave, y1, x1);
41
+ /* const x1f = float(x1);
42
+ const y1f = float(y1);
43
+ const x0f = float(x0);
44
+ const y0f = float(y0); */
45
+ // ratio for interpolation between four neighbouring points
46
+ const value = (x1 - xp) * (y1 - yp) * f1 +
47
+ (xp - x0) * (y1 - yp) * f2 +
48
+ (x1 - xp) * (yp - y0) * f3 +
49
+ (xp - x0) * (yp - y0) * f4;
50
+ this.setOutput(value);
51
+ },
52
+ };
53
+ return kernel;
54
+ }
55
+ export const computeExtremaFreak = (args) => {
56
+ /** @type {import('@tensorflow/tfjs').TensorInfo} */
57
+ const { gaussianImagesT, prunedExtremas, prunedExtremasAngles, freakPointsT, pyramidImagesLength, } = args.inputs;
58
+ /** @type {MathBackendCPU} */
59
+ const backend = args.backend;
60
+ const prog = GetProgram(prunedExtremas.shape[0], pyramidImagesLength);
61
+ return FakeShader.runCode(backend, prog, [...gaussianImagesT, prunedExtremas, prunedExtremasAngles, freakPointsT], "float32");
62
+ };
63
+ export const computeExtremaFreakConfig = {
64
+ //: KernelConfig
65
+ kernelName: "ComputeExtremaFreak",
66
+ backendName: "cpu",
67
+ kernelFunc: computeExtremaFreak, // as {} as KernelFunc,
68
+ };
@@ -0,0 +1,6 @@
1
+ export function computeFreakDescriptor(args: any): any;
2
+ export namespace computeFreakDescriptorConfig {
3
+ export let kernelName: string;
4
+ export let backendName: string;
5
+ export { computeFreakDescriptor as kernelFunc };
6
+ }
@@ -0,0 +1,57 @@
1
+ import { FREAKPOINTS } from "../../freak.js";
2
+ const FREAK_CONPARISON_COUNT = ((FREAKPOINTS.length - 1) * FREAKPOINTS.length) / 2;
3
+ const descriptorCount = Math.ceil(FREAK_CONPARISON_COUNT / 8);
4
+ function computeFreakDescriptorImpl(extremaFreaks, positionT) {
5
+ const resultValues = new Float32Array(extremaFreaks.height * descriptorCount);
6
+ function getP(y, x) {
7
+ return positionT.values[y * positionT.width + x];
8
+ }
9
+ function getFreak(y, x) {
10
+ return extremaFreaks.values[y * extremaFreaks.width + x];
11
+ }
12
+ function setOutput(y, x, o) {
13
+ resultValues[y * descriptorCount + x] = o;
14
+ }
15
+ for (let featureIndex = 0; featureIndex < extremaFreaks.height; featureIndex++) {
16
+ for (let _descIndex = 0; _descIndex < descriptorCount; _descIndex++) {
17
+ const descIndex = _descIndex * 8;
18
+ let sum = 0;
19
+ for (let i = 0; i < 8; i++) {
20
+ if (descIndex + i >= FREAK_CONPARISON_COUNT) {
21
+ continue;
22
+ }
23
+ const p1 = Math.trunc(getP(descIndex + i, 0));
24
+ const p2 = Math.trunc(getP(descIndex + i, 1));
25
+ const v1 = getFreak(featureIndex, p1);
26
+ const v2 = getFreak(featureIndex, p2);
27
+ if (v1 < v2 + 0.01) {
28
+ sum += Math.trunc(Math.pow(2.0, 7.0 - i));
29
+ }
30
+ }
31
+ setOutput(featureIndex, _descIndex, sum);
32
+ }
33
+ }
34
+ return resultValues;
35
+ }
36
+ export const computeFreakDescriptor = (args) => {
37
+ const { extremaFreaks, positionT } = args.inputs;
38
+ const { backend } = args;
39
+ const freaksData = {
40
+ values: backend.data.get(extremaFreaks.dataId).values,
41
+ height: extremaFreaks.shape[0],
42
+ width: extremaFreaks.shape[1],
43
+ };
44
+ const positionData = {
45
+ values: backend.data.get(positionT.dataId).values,
46
+ width: positionT.shape[1],
47
+ };
48
+ //backend.runWebGLProgram(program,[extremaFreaks, positionT],'int32');
49
+ const resultValues = computeFreakDescriptorImpl(freaksData, positionData);
50
+ return backend.makeOutput(resultValues, [extremaFreaks.shape[0], descriptorCount], "int32");
51
+ };
52
+ export const computeFreakDescriptorConfig = {
53
+ //: KernelConfig
54
+ kernelName: "ComputeFreakDescriptors",
55
+ backendName: "cpu",
56
+ kernelFunc: computeFreakDescriptor, // as {} as KernelFunc,
57
+ };
@@ -0,0 +1,6 @@
1
+ export function computeLocalization(args: any): Tensor;
2
+ export namespace computeLocalizationConfig {
3
+ export let kernelName: string;
4
+ export let backendName: string;
5
+ export { computeLocalization as kernelFunc };
6
+ }
@@ -0,0 +1,54 @@
1
+ import { tensor } from "@tensorflow/tfjs";
2
+ import * as FakeShader from "./fakeShader.js";
3
+ function GetProgram(numDogPyramidImages, extremasListLength) {
4
+ const dogVariableNames = [];
5
+ for (let i = 1; i < numDogPyramidImages; i++) {
6
+ dogVariableNames.push("image" + i);
7
+ }
8
+ const program = {
9
+ variableNames: [...dogVariableNames, "extrema"],
10
+ outputShape: [extremasListLength, 3, 3], // 3x3 pixels around the extrema
11
+ userCode: function () {
12
+ const getPixel = (octave, y, x) => {
13
+ const k = "getImage" + octave;
14
+ if (!this.hasOwnProperty(k)) {
15
+ throw new Error(`ComputeLocalization:: ${k} does not exist`);
16
+ }
17
+ return this[k](y, x);
18
+ };
19
+ const coords = this.getOutputCoords();
20
+ const featureIndex = coords[0];
21
+ const score = this.getExtrema(featureIndex, 0);
22
+ if (score == 0.0) {
23
+ return;
24
+ }
25
+ const dy = coords[1] - 1;
26
+ const dx = coords[2] - 1;
27
+ const octave = this.int(this.getExtrema(featureIndex, 1));
28
+ const y = this.int(this.getExtrema(featureIndex, 2));
29
+ const x = this.int(this.getExtrema(featureIndex, 3));
30
+ this.setOutput(getPixel(octave, y + dy, x + dx));
31
+ },
32
+ };
33
+ //}
34
+ return program;
35
+ }
36
+ const int = Math.trunc;
37
+ function clamp(n, min, max) {
38
+ return Math.min(Math.max(min, n), max - 1);
39
+ }
40
+ export const computeLocalization = (args) => {
41
+ /** @type {import('@tensorflow/tfjs').TensorInfo} */
42
+ const { prunedExtremasList, dogPyramidImagesT } = args.inputs;
43
+ /** @type {MathBackendCPU} */
44
+ const backend = args.backend;
45
+ const program = GetProgram(dogPyramidImagesT.length, prunedExtremasList.length);
46
+ const prunedExtremasT = tensor(prunedExtremasList, [prunedExtremasList.length, prunedExtremasList[0].length], "int32");
47
+ return FakeShader.runCode(backend, program, [...dogPyramidImagesT.slice(1), prunedExtremasT], dogPyramidImagesT[0].dtype);
48
+ };
49
+ export const computeLocalizationConfig = {
50
+ //: KernelConfig
51
+ kernelName: "ComputeLocalization",
52
+ backendName: "cpu",
53
+ kernelFunc: computeLocalization, // as {} as KernelFunc,
54
+ };
@@ -0,0 +1,6 @@
1
+ export function computeOrientationHistograms(args: any): Tensor;
2
+ export namespace computeOrientationHistogramsConfig {
3
+ export let kernelName: string;
4
+ export let backendName: string;
5
+ export { computeOrientationHistograms as kernelFunc };
6
+ }
@@ -0,0 +1,118 @@
1
+ import * as FakeShader from "./fakeShader.js";
2
+ const oneOver2PI = 0.159154943091895;
3
+ const ORIENTATION_NUM_BINS = 36;
4
+ const cache = {};
5
+ function GetPrograms(prunedExtremasT, radialPropertiesT, pyramidImagesLength) {
6
+ const key = `${pyramidImagesLength}|${prunedExtremasT.shape[0]}|${radialPropertiesT.shape[0]}`;
7
+ //if (!cache.hasOwnProperty(key)) {
8
+ const imageVariableNames = [];
9
+ for (let i = 1; i < pyramidImagesLength; i++) {
10
+ imageVariableNames.push("image" + i);
11
+ }
12
+ /* let kernel1SubCodes = `float getPixel(int octave, int y, int x) {`;
13
+ for (let i = 1; i < pyramidImagesLength; i++) {
14
+ kernel1SubCodes += `
15
+ if (octave == ${i}) {
16
+ return getImage${i}(y, x);
17
+ }
18
+ `;
19
+ }
20
+ kernel1SubCodes += `}`; */
21
+ const kernel1 = {
22
+ variableNames: [...imageVariableNames, "extrema", "radial"],
23
+ outputShape: [prunedExtremasT.shape[0], radialPropertiesT.shape[0], 2], // last dimension: [fbin, magnitude]
24
+ userCode: function () {
25
+ //${ kernel1SubCodes }
26
+ const getPixel = (octave, y, x) => {
27
+ const k = "getImage" + octave;
28
+ if (!this.hasOwnProperty(k)) {
29
+ //console.error(`ComputeOrientationHistograms:: ${k} does not exist! y:${y} x:${x}`);
30
+ return 0.0;
31
+ }
32
+ return this[k](y, x);
33
+ };
34
+ /** replicated undefined behavior like you have on OpenGL */
35
+ function atan(x, y) {
36
+ if (x == 0 && y == 0)
37
+ return 1.57;
38
+ return Math.atan2(x, y);
39
+ }
40
+ //void main() {
41
+ const coords = this.getOutputCoords();
42
+ const featureIndex = coords[0];
43
+ const radialIndex = coords[1];
44
+ const propertyIndex = coords[2];
45
+ const radialY = this.int(this.getRadial(radialIndex, 0));
46
+ const radialX = this.int(this.getRadial(radialIndex, 1));
47
+ const radialW = this.getRadial(radialIndex, 2);
48
+ const octave = this.int(this.getExtrema(featureIndex, 1));
49
+ const y = this.int(this.getExtrema(featureIndex, 2));
50
+ const x = this.int(this.getExtrema(featureIndex, 3));
51
+ const xp = x + radialX;
52
+ const yp = y + radialY;
53
+ const dy = getPixel(octave, yp + 1, xp) - getPixel(octave, yp - 1, xp);
54
+ const dx = getPixel(octave, yp, xp + 1) - getPixel(octave, yp, xp - 1);
55
+ if (propertyIndex == 0) {
56
+ // be careful that atan(0, 0) gives 1.57 instead of 0 (different from js), but doesn't matter here, coz magnitude is 0
57
+ const angle = this.atan(dy, dx) + Math.PI;
58
+ const fbin = angle * ORIENTATION_NUM_BINS * oneOver2PI;
59
+ this.setOutput(fbin);
60
+ return;
61
+ }
62
+ if (propertyIndex == 1) {
63
+ const mag = Math.sqrt(dx * dx + dy * dy);
64
+ const magnitude = radialW * mag;
65
+ this.setOutput(magnitude);
66
+ return;
67
+ }
68
+ // }
69
+ },
70
+ };
71
+ const kernel2 = {
72
+ variableNames: ["fbinMag"],
73
+ outputShape: [prunedExtremasT.shape[0], ORIENTATION_NUM_BINS],
74
+ userCode: function () {
75
+ function imod(x, y) {
76
+ return Math.trunc(x - y * Math.floor(x / y));
77
+ }
78
+ const coords = this.getOutputCoords();
79
+ const featureIndex = coords[0];
80
+ const binIndex = coords[1];
81
+ let sum = 0;
82
+ for (let i = 0; i < radialPropertiesT.shape[0]; i++) {
83
+ const fbin = this.getFbinMag(featureIndex, i, 0);
84
+ const bin = Math.trunc(Math.floor(fbin - 0.5));
85
+ const b1 = imod(bin + ORIENTATION_NUM_BINS, ORIENTATION_NUM_BINS);
86
+ const b2 = imod(bin + 1 + ORIENTATION_NUM_BINS, ORIENTATION_NUM_BINS);
87
+ if (b1 == binIndex || b2 == binIndex) {
88
+ const magnitude = this.getFbinMag(featureIndex, i, 1);
89
+ const w2 = fbin - bin - 0.5;
90
+ const w1 = w2 * -1 + 1;
91
+ if (b1 == binIndex) {
92
+ sum += w1 * magnitude;
93
+ }
94
+ if (b2 == binIndex) {
95
+ sum += w2 * magnitude;
96
+ }
97
+ }
98
+ }
99
+ this.setOutput(sum);
100
+ },
101
+ };
102
+ return [kernel1, kernel2];
103
+ //}
104
+ //return cache[key];
105
+ }
106
+ export const computeOrientationHistograms = (args) => {
107
+ const { gaussianImagesT, prunedExtremasT, radialPropertiesT, pyramidImagesLength } = args.inputs;
108
+ /** @type {MathBackendCPU} */
109
+ const backend = args.backend;
110
+ const [program1, program2] = GetPrograms(prunedExtremasT, radialPropertiesT, pyramidImagesLength);
111
+ const result1 = FakeShader.runCode(backend, program1, [...gaussianImagesT, prunedExtremasT, radialPropertiesT], radialPropertiesT.dtype);
112
+ return FakeShader.runCode(backend, program2, [result1], radialPropertiesT.dtype);
113
+ };
114
+ export const computeOrientationHistogramsConfig = {
115
+ kernelName: "ComputeOrientationHistograms",
116
+ backendName: "cpu",
117
+ kernelFunc: computeOrientationHistograms, // as {} as KernelFunc,
118
+ };
@@ -0,0 +1,6 @@
1
+ export function downsampleBilinear(args: any): Tensor;
2
+ export namespace downsampleBilinearConfig {
3
+ export let kernelName: string;
4
+ export let backendName: string;
5
+ export { downsampleBilinear as kernelFunc };
6
+ }