@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,141 @@
1
+ import * as FakeShader from "./fakeShader.js";
2
+ const oneOver2PI = 0.159154943091895;
3
+ const ORIENTATION_NUM_BINS = 36;
4
+
5
+ const cache = {};
6
+ function GetPrograms(prunedExtremasT, radialPropertiesT, pyramidImagesLength) {
7
+ const key = `${pyramidImagesLength}|${prunedExtremasT.shape[0]}|${radialPropertiesT.shape[0]}`;
8
+ //if (!cache.hasOwnProperty(key)) {
9
+ const imageVariableNames = [];
10
+ for (let i = 1; i < pyramidImagesLength; i++) {
11
+ imageVariableNames.push("image" + i);
12
+ }
13
+
14
+ /* let kernel1SubCodes = `float getPixel(int octave, int y, int x) {`;
15
+ for (let i = 1; i < pyramidImagesLength; i++) {
16
+ kernel1SubCodes += `
17
+ if (octave == ${i}) {
18
+ return getImage${i}(y, x);
19
+ }
20
+ `;
21
+ }
22
+ kernel1SubCodes += `}`; */
23
+
24
+ const kernel1 = {
25
+ variableNames: [...imageVariableNames, "extrema", "radial"],
26
+ outputShape: [prunedExtremasT.shape[0], radialPropertiesT.shape[0], 2], // last dimension: [fbin, magnitude]
27
+ userCode: function () {
28
+ //${ kernel1SubCodes }
29
+ const getPixel = (octave, y, x) => {
30
+ const k = "getImage" + octave;
31
+ if (!this.hasOwnProperty(k)) {
32
+ //console.error(`ComputeOrientationHistograms:: ${k} does not exist! y:${y} x:${x}`);
33
+ return 0.0;
34
+ }
35
+ return this[k](y, x);
36
+ };
37
+ /** replicated undefined behavior like you have on OpenGL */
38
+ function atan(x, y) {
39
+ if (x == 0 && y == 0) return 1.57;
40
+ return Math.atan2(x, y);
41
+ }
42
+ //void main() {
43
+ const coords = this.getOutputCoords();
44
+ const featureIndex = coords[0];
45
+ const radialIndex = coords[1];
46
+ const propertyIndex = coords[2];
47
+
48
+ const radialY = this.int(this.getRadial(radialIndex, 0));
49
+ const radialX = this.int(this.getRadial(radialIndex, 1));
50
+ const radialW = this.getRadial(radialIndex, 2);
51
+
52
+ const octave = this.int(this.getExtrema(featureIndex, 1));
53
+ const y = this.int(this.getExtrema(featureIndex, 2));
54
+ const x = this.int(this.getExtrema(featureIndex, 3));
55
+
56
+ const xp = x + radialX;
57
+ const yp = y + radialY;
58
+
59
+ const dy = getPixel(octave, yp + 1, xp) - getPixel(octave, yp - 1, xp);
60
+ const dx = getPixel(octave, yp, xp + 1) - getPixel(octave, yp, xp - 1);
61
+
62
+ if (propertyIndex == 0) {
63
+ // be careful that atan(0, 0) gives 1.57 instead of 0 (different from js), but doesn't matter here, coz magnitude is 0
64
+
65
+ const angle = this.atan(dy, dx) + Math.PI;
66
+ const fbin = angle * ORIENTATION_NUM_BINS * oneOver2PI;
67
+ this.setOutput(fbin);
68
+ return;
69
+ }
70
+
71
+ if (propertyIndex == 1) {
72
+ const mag = Math.sqrt(dx * dx + dy * dy);
73
+ const magnitude = radialW * mag;
74
+ this.setOutput(magnitude);
75
+ return;
76
+ }
77
+ // }
78
+ },
79
+ };
80
+
81
+ const kernel2 = {
82
+ variableNames: ["fbinMag"],
83
+ outputShape: [prunedExtremasT.shape[0], ORIENTATION_NUM_BINS],
84
+ userCode: function () {
85
+ function imod(x, y) {
86
+ return Math.trunc(x - y * Math.floor(x / y));
87
+ }
88
+
89
+ const coords = this.getOutputCoords();
90
+ const featureIndex = coords[0];
91
+ const binIndex = coords[1];
92
+
93
+ let sum = 0;
94
+ for (let i = 0; i < radialPropertiesT.shape[0]; i++) {
95
+ const fbin = this.getFbinMag(featureIndex, i, 0);
96
+ const bin = Math.trunc(Math.floor(fbin - 0.5));
97
+ const b1 = imod(bin + ORIENTATION_NUM_BINS, ORIENTATION_NUM_BINS);
98
+ const b2 = imod(bin + 1 + ORIENTATION_NUM_BINS, ORIENTATION_NUM_BINS);
99
+
100
+ if (b1 == binIndex || b2 == binIndex) {
101
+ const magnitude = this.getFbinMag(featureIndex, i, 1);
102
+ const w2 = fbin - bin - 0.5;
103
+ const w1 = w2 * -1 + 1;
104
+
105
+ if (b1 == binIndex) {
106
+ sum += w1 * magnitude;
107
+ }
108
+ if (b2 == binIndex) {
109
+ sum += w2 * magnitude;
110
+ }
111
+ }
112
+ }
113
+ this.setOutput(sum);
114
+ },
115
+ };
116
+
117
+ return [kernel1, kernel2];
118
+ //}
119
+ //return cache[key];
120
+ }
121
+
122
+ export const computeOrientationHistograms = (args) => {
123
+ const { gaussianImagesT, prunedExtremasT, radialPropertiesT, pyramidImagesLength } = args.inputs;
124
+ /** @type {MathBackendCPU} */
125
+ const backend = args.backend;
126
+ const [program1, program2] = GetPrograms(prunedExtremasT, radialPropertiesT, pyramidImagesLength);
127
+
128
+ const result1 = FakeShader.runCode(
129
+ backend,
130
+ program1,
131
+ [...gaussianImagesT, prunedExtremasT, radialPropertiesT],
132
+ radialPropertiesT.dtype,
133
+ );
134
+ return FakeShader.runCode(backend, program2, [result1], radialPropertiesT.dtype);
135
+ };
136
+
137
+ export const computeOrientationHistogramsConfig = {
138
+ kernelName: "ComputeOrientationHistograms",
139
+ backendName: "cpu",
140
+ kernelFunc: computeOrientationHistograms, // as {} as KernelFunc,
141
+ };
@@ -0,0 +1,33 @@
1
+ import * as FakeShader from "./fakeShader.js";
2
+
3
+ export const downsampleBilinear = (args) => {
4
+ /** @type {import('@tensorflow/tfjs').TensorInfo} */
5
+ const x = args.inputs.image;
6
+ /** @type {MathBackendCPU} */
7
+ const backend = args.backend;
8
+
9
+ const kernel = {
10
+ variableNames: ["p"],
11
+ outputShape: [Math.floor(x.shape[0] / 2), Math.floor(x.shape[1] / 2)],
12
+ userCode: function () {
13
+ const coords = this.getOutputCoords();
14
+ const y = coords[0] * 2;
15
+ const x = coords[1] * 2;
16
+ let sum = new Float32Array(1);
17
+ sum[0] = Math.fround(this.getP(y, x) * 0.25);
18
+ sum[0] += Math.fround(this.getP(y + 1, x) * 0.25);
19
+ sum[0] += Math.fround(this.getP(y, x + 1) * 0.25);
20
+ sum[0] += Math.fround(this.getP(y + 1, x + 1) * 0.25);
21
+
22
+ this.setOutput(sum[0]);
23
+ },
24
+ };
25
+ return FakeShader.runCode(backend, kernel, [x], x.dtype);
26
+ };
27
+
28
+ export const downsampleBilinearConfig = {
29
+ //: KernelConfig
30
+ kernelName: "DownsampleBilinear",
31
+ backendName: "cpu",
32
+ kernelFunc: downsampleBilinear, // as {} as KernelFunc,
33
+ };
@@ -0,0 +1,53 @@
1
+ import * as FakeShader from "./fakeShader.js";
2
+ function GetProgram(outHeight, outWidth) {
3
+ const kernel = {
4
+ variableNames: ["extrema"],
5
+ outputShape: [outHeight, outWidth],
6
+ userCode: function () {
7
+ const coords = this.getOutputCoords();
8
+ const y = coords[0] * 2;
9
+ const x = coords[1] * 2;
10
+
11
+ let location = 0.0;
12
+ let values = this.getExtrema(y, x);
13
+
14
+ if (this.getExtrema(y + 1, x) != 0.0) {
15
+ location = 1.0;
16
+ values = this.getExtrema(y + 1, x);
17
+ } else if (this.getExtrema(y, x + 1) != 0.0) {
18
+ location = 2.0;
19
+ values = this.getExtrema(y, x + 1);
20
+ } else if (this.getExtrema(y + 1, x + 1) != 0.0) {
21
+ location = 3.0;
22
+ values = this.getExtrema(y + 1, x + 1);
23
+ }
24
+
25
+ if (values < 0.0) {
26
+ this.setOutput(location * -1000.0 + values);
27
+ } else {
28
+ this.setOutput(location * 1000.0 + values);
29
+ }
30
+ },
31
+ };
32
+ return kernel;
33
+ }
34
+
35
+ export const extremaReduction = (args) => {
36
+ const { extremasResultT } = args.inputs;
37
+ /** @type {MathBackendCPU} */
38
+ const backend = args.backend;
39
+ const extremaHeight = extremasResultT.shape[0];
40
+ const extremaWidth = extremasResultT.shape[1];
41
+ const outHeight = Math.floor(extremaHeight / 2.0);
42
+ const outWidth = Math.floor(extremaWidth / 2.0);
43
+ const program = GetProgram(outHeight, outWidth);
44
+
45
+ return FakeShader.runCode(backend, program, [extremasResultT], extremasResultT.dtype);
46
+ };
47
+
48
+ export const extremaReductionConfig = {
49
+ //: KernelConfig
50
+ kernelName: "ExtremaReduction",
51
+ backendName: "cpu",
52
+ kernelFunc: extremaReduction, // as {} as KernelFunc,
53
+ };
@@ -0,0 +1,88 @@
1
+ import { zeros, map, flatten as mathjsflatten } from "mathjs";
2
+ /**
3
+ * @typedef {Object} Kernel
4
+ * @property {string[]} variableNames
5
+ * @property {number[]} outputShape
6
+ * @property {Function} userCode
7
+ */
8
+
9
+ /**
10
+ *
11
+ * @param {MathBackendCPU} backend
12
+ * @param {Kernel} kernel
13
+ * @param {Array<.TensorInfo>} inputs
14
+ * @param {DataType} dtype
15
+ * @returns {Tensor}
16
+ */
17
+ function runCode(backend, kernel, inputs, dtype) {
18
+ const inputData = inputs.map((value) => {
19
+ return backend.data.get(value.dataId).values;
20
+ });
21
+
22
+ //create getter functions for every variable name, clamping the input.
23
+ const tempData = {};
24
+ kernel.variableNames.forEach((name, index) => {
25
+ const funName = `get${capFirstLetter(name)}`;
26
+ //console.log("Making function:",funName,inputs[index].shape);
27
+ tempData[funName] = function (...args) {
28
+ const inputIndex = index;
29
+ for (let i = 0; i < args.length; i++) {
30
+ args[i] = clampInt(args[i], 0, inputs[inputIndex].shape[i]);
31
+ }
32
+ return inputData[index][flatten(args, inputs[inputIndex].shape)];
33
+ };
34
+ });
35
+ tempData.int = Math.trunc;
36
+ tempData.atan = Math.atan2;
37
+ //create an empty matrix to map the output size, because i'm lazy and want to use Matrix.map(...)
38
+ //const temp = new Matrix();
39
+ //console.log("Creating output shape:",kernel.outputShape);
40
+ const temp = zeros(kernel.outputShape); //reshape([0,0,0],kernel.outputShape);
41
+ const output = map(temp, (value, index, matrix) => {
42
+ tempData.getOutputCoords = () => {
43
+ return index;
44
+ };
45
+ let out;
46
+
47
+ tempData.setOutput = (newValue) => {
48
+ out = Number.isNaN(newValue) ? 0 : Math.fround(newValue);
49
+ };
50
+ //bind the method calls and run the code
51
+ kernel.userCode.bind(tempData)();
52
+ return out;
53
+ });
54
+
55
+ //output.flat()
56
+ //convert the output from a matrix into a tensor
57
+
58
+ return backend.makeOutput(mathjsflatten(output), kernel.outputShape, dtype);
59
+ }
60
+
61
+ /**
62
+ *
63
+ * @param {string} word
64
+ * @returns
65
+ */
66
+ function capFirstLetter(word) {
67
+ return word[0].toUpperCase() + word.substring(1);
68
+ }
69
+
70
+ function clampInt(n, min, max) {
71
+ return Math.min(Math.max(n, min), max - 1);
72
+ }
73
+ /**
74
+ *
75
+ * @param {number[]} input
76
+ * @param {number[]} max
77
+ * @returns
78
+ */
79
+ function flatten(input, max) {
80
+ return input.reduce((prev, current, index) => {
81
+ for (let i = index + 1; i < max.length; i++) {
82
+ current *= max[i];
83
+ }
84
+ return prev + current;
85
+ }, 0);
86
+ }
87
+
88
+ export { runCode };
@@ -0,0 +1,26 @@
1
+ import { registerKernel } from "@tensorflow/tfjs";
2
+ import { binomialFilterConfig } from "./binomialFilter.js";
3
+ import { buildExtremasConfig } from "./buildExtremas.js";
4
+ import { computeExtremaAnglesConfig } from "./computeExtremaAngles.js";
5
+ import { computeExtremaFreakConfig } from "./computeExtremaFreak.js";
6
+ import { computeFreakDescriptorConfig } from "./computeFreakDescriptors.js";
7
+ import { computeLocalizationConfig } from "./computeLocalization.js";
8
+ import { computeOrientationHistogramsConfig } from "./computeOrientationHistograms.js";
9
+ import { downsampleBilinearConfig } from "./downsampleBilinear.js";
10
+ import { extremaReductionConfig } from "./extremaReduction.js";
11
+ import { smoothHistogramsConfig } from "./smoothHistograms.js";
12
+ import { upsampleBilinearConfig } from "./upsampleBilinear.js";
13
+
14
+ //export function Register(){
15
+ registerKernel(binomialFilterConfig);
16
+ registerKernel(buildExtremasConfig);
17
+ registerKernel(computeExtremaAnglesConfig);
18
+ registerKernel(computeExtremaFreakConfig);
19
+ registerKernel(computeFreakDescriptorConfig);
20
+ registerKernel(computeLocalizationConfig);
21
+ registerKernel(computeOrientationHistogramsConfig);
22
+ registerKernel(downsampleBilinearConfig);
23
+ registerKernel(extremaReductionConfig);
24
+ registerKernel(smoothHistogramsConfig);
25
+ registerKernel(upsampleBilinearConfig);
26
+ //}
@@ -0,0 +1,114 @@
1
+ import * as FakeShader from "./fakeShader.js";
2
+ /*
3
+ const kernel = {
4
+ variableNames: ['extrema'],
5
+ outputShape: [Math.floor(extremaHeight/2), Math.floor(extremaWidth/2)],
6
+ userCode: `
7
+ void main() {
8
+ ivec2 coords = getOutputCoords();
9
+ int y = coords[0] * 2;
10
+ int x = coords[1] * 2;
11
+
12
+ float location = 0.0;
13
+ float values = getExtrema(y, x);
14
+
15
+ if (getExtrema(y+1, x) != 0.0) {
16
+ location = 1.0;
17
+ values = getExtrema(y+1, x);
18
+ }
19
+ else if (getExtrema(y, x+1) != 0.0) {
20
+ location = 2.0;
21
+ values = getExtrema(y, x+1);
22
+ }
23
+ else if (getExtrema(y+1, x+1) != 0.0) {
24
+ location = 3.0;
25
+ values = getExtrema(y+1, x+1);
26
+ }
27
+
28
+ if (values < 0.0) {
29
+ setOutput(location * -1000.0 + values);
30
+ } else {
31
+ setOutput(location * 1000.0 + values);
32
+ }
33
+ }
34
+ `
35
+ }
36
+
37
+ */
38
+ function clamp(n, min, max) {
39
+ return Math.min(Math.max(min, n), max - 1);
40
+ }
41
+
42
+ const pruneImpl = (vals, width, height) => {
43
+ const w = Math.floor(width / 2);
44
+ const h = Math.floor(height / 2);
45
+ const resultValues = new Float32Array(w * h);
46
+ function getExtrema(x, y) {
47
+ x = clamp(x, 0, width);
48
+ y = clamp(y, 0, height);
49
+ return vals[y * width + x];
50
+ }
51
+ function setOutput(x, y, o) {
52
+ resultValues[y * width + x] = o;
53
+ }
54
+
55
+ for (let x = 0; x < w; x++) {
56
+ for (let y = 0; y < h; y++) {
57
+ const x2 = x * 2;
58
+ const y2 = y * 2;
59
+
60
+ let location = 0.0;
61
+ let values = getExtrema(x2, y2);
62
+
63
+ if (getExtrema(x2, y2 + 1) != 0.0) {
64
+ location = 1.0;
65
+ values = getExtrema(x2, y2 + 1);
66
+ } else if (getExtrema(x2 + 1, y2) != 0.0) {
67
+ location = 2.0;
68
+ values = getExtrema(x2 + 1, y2);
69
+ } else if (getExtrema(x2 + 1, y2 + 1) != 0.0) {
70
+ location = 3.0;
71
+ values = getExtrema(x2 + 1, y2 + 1);
72
+ }
73
+
74
+ if (values < 0.0) {
75
+ setOutput(x, y, location * -1000.0 + values);
76
+ } else {
77
+ setOutput(x, y, location * 1000.0 + values);
78
+ }
79
+ }
80
+ }
81
+ return resultValues;
82
+ };
83
+
84
+ const prune = (args) => {
85
+ /** @type {import('@tensorflow/tfjs').TensorInfo} */
86
+ const x = args.inputs.x;
87
+ /** @type {MathBackendCPU} */
88
+ const cpuBackend = args.backend;
89
+ const imageHeight = x.shape[0];
90
+ const imageWidth = x.shape[1];
91
+ /** @type {TypedArray} */
92
+ const values = cpuBackend.data.get(x.dataId).values;
93
+
94
+ const resultValues = pruneImpl(values, imageWidth, imageHeight);
95
+
96
+ return cpuBackend.makeOutput(
97
+ resultValues,
98
+ [Math.floor(imageHeight / 2), Math.floor(imageWidth / 2)],
99
+ "float32",
100
+ );
101
+ };
102
+
103
+ const pruneConfig = {
104
+ //: KernelConfig
105
+ kernelName: "Prune",
106
+ backendName: "cpu",
107
+ kernelFunc: prune, // as {} as KernelFunc,
108
+ };
109
+
110
+ module.exports = {
111
+ pruneConfig,
112
+ prune,
113
+ pruneImpl,
114
+ };
@@ -0,0 +1,57 @@
1
+ const ORIENTATION_NUM_BINS = 36;
2
+ const ORIENTATION_SMOOTHING_ITERATIONS = 5;
3
+
4
+ function smoothHistogramsImpl(histograms) {
5
+ const resultValues = new Float32Array(histograms.height * ORIENTATION_NUM_BINS);
6
+ function getHistogram(y, x) {
7
+ return histograms.values[y * histograms.width + x];
8
+ }
9
+ function setOutput(y, x, o) {
10
+ resultValues[y * ORIENTATION_NUM_BINS + x] = o;
11
+ }
12
+ function imod(x, y) {
13
+ return Math.trunc(x - y * Math.floor(x / y));
14
+ }
15
+ for (let featureIndex = 0; featureIndex < histograms.height; featureIndex++) {
16
+ for (let binIndex = 0; binIndex < ORIENTATION_NUM_BINS; binIndex++) {
17
+ const prevBin = imod(binIndex - 1 + ORIENTATION_NUM_BINS, ORIENTATION_NUM_BINS);
18
+ const nextBin = imod(binIndex + 1, ORIENTATION_NUM_BINS);
19
+ const result =
20
+ 0.274068619061197 * getHistogram(featureIndex, prevBin) +
21
+ 0.451862761877606 * getHistogram(featureIndex, binIndex) +
22
+ 0.274068619061197 * getHistogram(featureIndex, nextBin);
23
+
24
+ setOutput(featureIndex, binIndex, result);
25
+ }
26
+ }
27
+
28
+ return resultValues;
29
+ }
30
+
31
+ export const smoothHistograms = (args) => {
32
+ /** @type {import('@tensorflow/tfjs').TensorInfo} */
33
+ const { histograms } = args.inputs;
34
+ /** @type {MathBackendCPU} */
35
+ const backend = args.backend;
36
+ const histogramsData = {
37
+ values: backend.data.get(histograms.dataId).values,
38
+ height: histograms.shape[0],
39
+ width: histograms.shape[1],
40
+ };
41
+ //const program = GetProgram(histograms);
42
+ for (let i = 0; i < ORIENTATION_SMOOTHING_ITERATIONS; i++) {
43
+ histogramsData.values = smoothHistogramsImpl(histogramsData); //backend.runWebGLProgram(program, [histograms], histograms.dtype);//this._compileAndRun(program, [histograms]);
44
+ }
45
+ return backend.makeOutput(
46
+ histogramsData.values,
47
+ [histograms.shape[0], ORIENTATION_NUM_BINS],
48
+ histograms.dtype,
49
+ );
50
+ };
51
+
52
+ export const smoothHistogramsConfig = {
53
+ //: KernelConfig
54
+ kernelName: "SmoothHistograms",
55
+ backendName: "cpu",
56
+ kernelFunc: smoothHistograms, // as {} as KernelFunc,
57
+ };
@@ -0,0 +1,51 @@
1
+ import * as FakeShader from "./fakeShader.js";
2
+ function getProgram(targetImage) {
3
+ const kernel = {
4
+ variableNames: ["p"],
5
+ outputShape: [targetImage.shape[0], targetImage.shape[1]],
6
+ userCode: function () {
7
+ const coords = this.getOutputCoords();
8
+ const j = coords[0];
9
+ const i = coords[1];
10
+
11
+ const sj = Math.fround(0.5 * j) - 0.25;
12
+ const si = Math.fround(0.5 * i) - 0.25;
13
+
14
+ const sj0 = Math.floor(sj);
15
+ const sj1 = Math.ceil(sj);
16
+ const si0 = Math.floor(si);
17
+ const si1 = Math.ceil(si);
18
+
19
+ const sj0I = this.int(sj0);
20
+ const sj1I = this.int(sj1);
21
+ const si0I = this.int(si0);
22
+ const si1I = this.int(si1);
23
+
24
+ let sum = 0.0;
25
+ sum += this.getP(sj0I, si0I) * Math.fround((si1 - si) * (sj1 - sj));
26
+ sum += this.getP(sj1I, si0I) * Math.fround((si1 - si) * (sj - sj0));
27
+ sum += this.getP(sj0I, si1I) * Math.fround((si - si0) * (sj1 - sj));
28
+ sum += this.getP(sj1I, si1I) * Math.fround((si - si0) * (sj - sj0));
29
+ this.setOutput(sum);
30
+ },
31
+ };
32
+ return kernel;
33
+ }
34
+
35
+ export const upsampleBilinear = (args) => {
36
+ /** @type {import('@tensorflow/tfjs').TensorInfo} */
37
+ const { image, targetImage } = args.inputs;
38
+
39
+ /** @type {MathBackendCPU} */
40
+ const cpuBackend = args.backend;
41
+
42
+ const program = getProgram(targetImage);
43
+ return FakeShader.runCode(cpuBackend, program, [image], image.dtype);
44
+ };
45
+
46
+ export const upsampleBilinearConfig = {
47
+ //: KernelConfig
48
+ kernelName: "UpsampleBilinear",
49
+ backendName: "cpu",
50
+ kernelFunc: upsampleBilinear, // as {} as KernelFunc,
51
+ };
@@ -0,0 +1,2 @@
1
+ import "./webgl/index.js";
2
+ import "./cpu/index.js";
@@ -0,0 +1,72 @@
1
+ const cache = {};
2
+
3
+ /**
4
+ *
5
+ * @param {string} key
6
+ * @param {TensorInfo} image
7
+ * @returns {[GPGPUProgram,GPGPUProgram]}
8
+ */
9
+ function GetKernels(image) {
10
+ const imageWidth = image.shape[1];
11
+ const imageHeight = image.shape[0];
12
+ const key = "w" + imageWidth + "h" + imageHeight;
13
+ if (!cache.hasOwnProperty(key)) {
14
+ const kernel1 = {
15
+ variableNames: ["p"],
16
+ outputShape: [imageHeight, imageWidth],
17
+ userCode: `
18
+ void main() {
19
+ ivec2 coords = getOutputCoords();
20
+
21
+ float sum = getP(coords[0], coords[1]-2);
22
+ sum += getP(coords[0], coords[1]-1) * 4.;
23
+ sum += getP(coords[0], coords[1]) * 6.;
24
+ sum += getP(coords[0], coords[1]+1) * 4.;
25
+ sum += getP(coords[0], coords[1]+2);
26
+ setOutput(sum);
27
+ }
28
+ `,
29
+ };
30
+ const kernel2 = {
31
+ variableNames: ["p"],
32
+ outputShape: [imageHeight, imageWidth],
33
+ userCode: `
34
+ void main() {
35
+ ivec2 coords = getOutputCoords();
36
+
37
+ float sum = getP(coords[0]-2, coords[1]);
38
+ sum += getP(coords[0]-1, coords[1]) * 4.;
39
+ sum += getP(coords[0], coords[1]) * 6.;
40
+ sum += getP(coords[0]+1, coords[1]) * 4.;
41
+ sum += getP(coords[0]+2, coords[1]);
42
+ sum /= 256.;
43
+ setOutput(sum);
44
+ }
45
+ `,
46
+ };
47
+ cache[key] = [kernel1, kernel2];
48
+ }
49
+ return cache[key];
50
+ }
51
+
52
+ export const binomialFilter = (args) => {
53
+ //{inputs: UnaryInputs, backend: MathBackendCPU}
54
+ /** @type {import('@tensorflow/tfjs').TensorInfo} */
55
+ const image = args.inputs.image;
56
+ /** @type {MathBackendWebGL} */
57
+ const backend = args.backend;
58
+
59
+ const [kernel1, kernel2] = GetKernels(image);
60
+
61
+ const result1 = backend.runWebGLProgram(kernel1, [image], image.dtype);
62
+ const result2 = backend.runWebGLProgram(kernel2, [result1], image.dtype);
63
+ backend.disposeIntermediateTensorInfo(result1);
64
+ return result2;
65
+ };
66
+
67
+ export const binomialFilterConfig = {
68
+ //: KernelConfig
69
+ kernelName: "BinomialFilter",
70
+ backendName: "webgl",
71
+ kernelFunc: binomialFilter, // as {} as KernelFunc,
72
+ };