@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,29 @@
1
+ import * as FakeShader from "./fakeShader.js";
2
+ export const downsampleBilinear = (args) => {
3
+ /** @type {import('@tensorflow/tfjs').TensorInfo} */
4
+ const x = args.inputs.image;
5
+ /** @type {MathBackendCPU} */
6
+ const backend = args.backend;
7
+ const kernel = {
8
+ variableNames: ["p"],
9
+ outputShape: [Math.floor(x.shape[0] / 2), Math.floor(x.shape[1] / 2)],
10
+ userCode: function () {
11
+ const coords = this.getOutputCoords();
12
+ const y = coords[0] * 2;
13
+ const x = coords[1] * 2;
14
+ let sum = new Float32Array(1);
15
+ sum[0] = Math.fround(this.getP(y, x) * 0.25);
16
+ sum[0] += Math.fround(this.getP(y + 1, x) * 0.25);
17
+ sum[0] += Math.fround(this.getP(y, x + 1) * 0.25);
18
+ sum[0] += Math.fround(this.getP(y + 1, x + 1) * 0.25);
19
+ this.setOutput(sum[0]);
20
+ },
21
+ };
22
+ return FakeShader.runCode(backend, kernel, [x], x.dtype);
23
+ };
24
+ export const downsampleBilinearConfig = {
25
+ //: KernelConfig
26
+ kernelName: "DownsampleBilinear",
27
+ backendName: "cpu",
28
+ kernelFunc: downsampleBilinear, // as {} as KernelFunc,
29
+ };
@@ -0,0 +1,6 @@
1
+ export function extremaReduction(args: any): Tensor;
2
+ export namespace extremaReductionConfig {
3
+ export let kernelName: string;
4
+ export let backendName: string;
5
+ export { extremaReduction as kernelFunc };
6
+ }
@@ -0,0 +1,50 @@
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
+ let location = 0.0;
11
+ let values = this.getExtrema(y, x);
12
+ if (this.getExtrema(y + 1, x) != 0.0) {
13
+ location = 1.0;
14
+ values = this.getExtrema(y + 1, x);
15
+ }
16
+ else if (this.getExtrema(y, x + 1) != 0.0) {
17
+ location = 2.0;
18
+ values = this.getExtrema(y, x + 1);
19
+ }
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
+ if (values < 0.0) {
25
+ this.setOutput(location * -1000.0 + values);
26
+ }
27
+ else {
28
+ this.setOutput(location * 1000.0 + values);
29
+ }
30
+ },
31
+ };
32
+ return kernel;
33
+ }
34
+ export const extremaReduction = (args) => {
35
+ const { extremasResultT } = args.inputs;
36
+ /** @type {MathBackendCPU} */
37
+ const backend = args.backend;
38
+ const extremaHeight = extremasResultT.shape[0];
39
+ const extremaWidth = extremasResultT.shape[1];
40
+ const outHeight = Math.floor(extremaHeight / 2.0);
41
+ const outWidth = Math.floor(extremaWidth / 2.0);
42
+ const program = GetProgram(outHeight, outWidth);
43
+ return FakeShader.runCode(backend, program, [extremasResultT], extremasResultT.dtype);
44
+ };
45
+ export const extremaReductionConfig = {
46
+ //: KernelConfig
47
+ kernelName: "ExtremaReduction",
48
+ backendName: "cpu",
49
+ kernelFunc: extremaReduction, // as {} as KernelFunc,
50
+ };
@@ -0,0 +1,20 @@
1
+ export type Kernel = {
2
+ variableNames: string[];
3
+ outputShape: number[];
4
+ userCode: Function;
5
+ };
6
+ /**
7
+ * @typedef {Object} Kernel
8
+ * @property {string[]} variableNames
9
+ * @property {number[]} outputShape
10
+ * @property {Function} userCode
11
+ */
12
+ /**
13
+ *
14
+ * @param {MathBackendCPU} backend
15
+ * @param {Kernel} kernel
16
+ * @param {Array<.TensorInfo>} inputs
17
+ * @param {DataType} dtype
18
+ * @returns {Tensor}
19
+ */
20
+ export function runCode(backend: MathBackendCPU, kernel: Kernel, inputs: any, dtype: DataType): Tensor;
@@ -0,0 +1,80 @@
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
+ * @param {MathBackendCPU} backend
11
+ * @param {Kernel} kernel
12
+ * @param {Array<.TensorInfo>} inputs
13
+ * @param {DataType} dtype
14
+ * @returns {Tensor}
15
+ */
16
+ function runCode(backend, kernel, inputs, dtype) {
17
+ const inputData = inputs.map((value) => {
18
+ return backend.data.get(value.dataId).values;
19
+ });
20
+ //create getter functions for every variable name, clamping the input.
21
+ const tempData = {};
22
+ kernel.variableNames.forEach((name, index) => {
23
+ const funName = `get${capFirstLetter(name)}`;
24
+ //console.log("Making function:",funName,inputs[index].shape);
25
+ tempData[funName] = function (...args) {
26
+ const inputIndex = index;
27
+ for (let i = 0; i < args.length; i++) {
28
+ args[i] = clampInt(args[i], 0, inputs[inputIndex].shape[i]);
29
+ }
30
+ return inputData[index][flatten(args, inputs[inputIndex].shape)];
31
+ };
32
+ });
33
+ tempData.int = Math.trunc;
34
+ tempData.atan = Math.atan2;
35
+ //create an empty matrix to map the output size, because i'm lazy and want to use Matrix.map(...)
36
+ //const temp = new Matrix();
37
+ //console.log("Creating output shape:",kernel.outputShape);
38
+ const temp = zeros(kernel.outputShape); //reshape([0,0,0],kernel.outputShape);
39
+ const output = map(temp, (value, index, matrix) => {
40
+ tempData.getOutputCoords = () => {
41
+ return index;
42
+ };
43
+ let out;
44
+ tempData.setOutput = (newValue) => {
45
+ out = Number.isNaN(newValue) ? 0 : Math.fround(newValue);
46
+ };
47
+ //bind the method calls and run the code
48
+ kernel.userCode.bind(tempData)();
49
+ return out;
50
+ });
51
+ //output.flat()
52
+ //convert the output from a matrix into a tensor
53
+ return backend.makeOutput(mathjsflatten(output), kernel.outputShape, dtype);
54
+ }
55
+ /**
56
+ *
57
+ * @param {string} word
58
+ * @returns
59
+ */
60
+ function capFirstLetter(word) {
61
+ return word[0].toUpperCase() + word.substring(1);
62
+ }
63
+ function clampInt(n, min, max) {
64
+ return Math.min(Math.max(n, min), max - 1);
65
+ }
66
+ /**
67
+ *
68
+ * @param {number[]} input
69
+ * @param {number[]} max
70
+ * @returns
71
+ */
72
+ function flatten(input, max) {
73
+ return input.reduce((prev, current, index) => {
74
+ for (let i = index + 1; i < max.length; i++) {
75
+ current *= max[i];
76
+ }
77
+ return prev + current;
78
+ }, 0);
79
+ }
80
+ export { runCode };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,25 @@
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
+ //export function Register(){
14
+ registerKernel(binomialFilterConfig);
15
+ registerKernel(buildExtremasConfig);
16
+ registerKernel(computeExtremaAnglesConfig);
17
+ registerKernel(computeExtremaFreakConfig);
18
+ registerKernel(computeFreakDescriptorConfig);
19
+ registerKernel(computeLocalizationConfig);
20
+ registerKernel(computeOrientationHistogramsConfig);
21
+ registerKernel(downsampleBilinearConfig);
22
+ registerKernel(extremaReductionConfig);
23
+ registerKernel(smoothHistogramsConfig);
24
+ registerKernel(upsampleBilinearConfig);
25
+ //}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,103 @@
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
+ const pruneImpl = (vals, width, height) => {
42
+ const w = Math.floor(width / 2);
43
+ const h = Math.floor(height / 2);
44
+ const resultValues = new Float32Array(w * h);
45
+ function getExtrema(x, y) {
46
+ x = clamp(x, 0, width);
47
+ y = clamp(y, 0, height);
48
+ return vals[y * width + x];
49
+ }
50
+ function setOutput(x, y, o) {
51
+ resultValues[y * width + x] = o;
52
+ }
53
+ for (let x = 0; x < w; x++) {
54
+ for (let y = 0; y < h; y++) {
55
+ const x2 = x * 2;
56
+ const y2 = y * 2;
57
+ let location = 0.0;
58
+ let values = getExtrema(x2, y2);
59
+ if (getExtrema(x2, y2 + 1) != 0.0) {
60
+ location = 1.0;
61
+ values = getExtrema(x2, y2 + 1);
62
+ }
63
+ else if (getExtrema(x2 + 1, y2) != 0.0) {
64
+ location = 2.0;
65
+ values = getExtrema(x2 + 1, y2);
66
+ }
67
+ else if (getExtrema(x2 + 1, y2 + 1) != 0.0) {
68
+ location = 3.0;
69
+ values = getExtrema(x2 + 1, y2 + 1);
70
+ }
71
+ if (values < 0.0) {
72
+ setOutput(x, y, location * -1000.0 + values);
73
+ }
74
+ else {
75
+ setOutput(x, y, location * 1000.0 + values);
76
+ }
77
+ }
78
+ }
79
+ return resultValues;
80
+ };
81
+ const prune = (args) => {
82
+ /** @type {import('@tensorflow/tfjs').TensorInfo} */
83
+ const x = args.inputs.x;
84
+ /** @type {MathBackendCPU} */
85
+ const cpuBackend = args.backend;
86
+ const imageHeight = x.shape[0];
87
+ const imageWidth = x.shape[1];
88
+ /** @type {TypedArray} */
89
+ const values = cpuBackend.data.get(x.dataId).values;
90
+ const resultValues = pruneImpl(values, imageWidth, imageHeight);
91
+ return cpuBackend.makeOutput(resultValues, [Math.floor(imageHeight / 2), Math.floor(imageWidth / 2)], "float32");
92
+ };
93
+ const pruneConfig = {
94
+ //: KernelConfig
95
+ kernelName: "Prune",
96
+ backendName: "cpu",
97
+ kernelFunc: prune, // as {} as KernelFunc,
98
+ };
99
+ module.exports = {
100
+ pruneConfig,
101
+ prune,
102
+ pruneImpl,
103
+ };
@@ -0,0 +1,6 @@
1
+ export function smoothHistograms(args: any): any;
2
+ export namespace smoothHistogramsConfig {
3
+ export let kernelName: string;
4
+ export let backendName: string;
5
+ export { smoothHistograms as kernelFunc };
6
+ }
@@ -0,0 +1,47 @@
1
+ const ORIENTATION_NUM_BINS = 36;
2
+ const ORIENTATION_SMOOTHING_ITERATIONS = 5;
3
+ function smoothHistogramsImpl(histograms) {
4
+ const resultValues = new Float32Array(histograms.height * ORIENTATION_NUM_BINS);
5
+ function getHistogram(y, x) {
6
+ return histograms.values[y * histograms.width + x];
7
+ }
8
+ function setOutput(y, x, o) {
9
+ resultValues[y * ORIENTATION_NUM_BINS + x] = o;
10
+ }
11
+ function imod(x, y) {
12
+ return Math.trunc(x - y * Math.floor(x / y));
13
+ }
14
+ for (let featureIndex = 0; featureIndex < histograms.height; featureIndex++) {
15
+ for (let binIndex = 0; binIndex < ORIENTATION_NUM_BINS; binIndex++) {
16
+ const prevBin = imod(binIndex - 1 + ORIENTATION_NUM_BINS, ORIENTATION_NUM_BINS);
17
+ const nextBin = imod(binIndex + 1, ORIENTATION_NUM_BINS);
18
+ const result = 0.274068619061197 * getHistogram(featureIndex, prevBin) +
19
+ 0.451862761877606 * getHistogram(featureIndex, binIndex) +
20
+ 0.274068619061197 * getHistogram(featureIndex, nextBin);
21
+ setOutput(featureIndex, binIndex, result);
22
+ }
23
+ }
24
+ return resultValues;
25
+ }
26
+ export const smoothHistograms = (args) => {
27
+ /** @type {import('@tensorflow/tfjs').TensorInfo} */
28
+ const { histograms } = args.inputs;
29
+ /** @type {MathBackendCPU} */
30
+ const backend = args.backend;
31
+ const histogramsData = {
32
+ values: backend.data.get(histograms.dataId).values,
33
+ height: histograms.shape[0],
34
+ width: histograms.shape[1],
35
+ };
36
+ //const program = GetProgram(histograms);
37
+ for (let i = 0; i < ORIENTATION_SMOOTHING_ITERATIONS; i++) {
38
+ histogramsData.values = smoothHistogramsImpl(histogramsData); //backend.runWebGLProgram(program, [histograms], histograms.dtype);//this._compileAndRun(program, [histograms]);
39
+ }
40
+ return backend.makeOutput(histogramsData.values, [histograms.shape[0], ORIENTATION_NUM_BINS], histograms.dtype);
41
+ };
42
+ export const smoothHistogramsConfig = {
43
+ //: KernelConfig
44
+ kernelName: "SmoothHistograms",
45
+ backendName: "cpu",
46
+ kernelFunc: smoothHistograms, // as {} as KernelFunc,
47
+ };
@@ -0,0 +1,6 @@
1
+ export function upsampleBilinear(args: any): Tensor;
2
+ export namespace upsampleBilinearConfig {
3
+ export let kernelName: string;
4
+ export let backendName: string;
5
+ export { upsampleBilinear as kernelFunc };
6
+ }
@@ -0,0 +1,43 @@
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
+ const sj = Math.fround(0.5 * j) - 0.25;
11
+ const si = Math.fround(0.5 * i) - 0.25;
12
+ const sj0 = Math.floor(sj);
13
+ const sj1 = Math.ceil(sj);
14
+ const si0 = Math.floor(si);
15
+ const si1 = Math.ceil(si);
16
+ const sj0I = this.int(sj0);
17
+ const sj1I = this.int(sj1);
18
+ const si0I = this.int(si0);
19
+ const si1I = this.int(si1);
20
+ let sum = 0.0;
21
+ sum += this.getP(sj0I, si0I) * Math.fround((si1 - si) * (sj1 - sj));
22
+ sum += this.getP(sj1I, si0I) * Math.fround((si1 - si) * (sj - sj0));
23
+ sum += this.getP(sj0I, si1I) * Math.fround((si - si0) * (sj1 - sj));
24
+ sum += this.getP(sj1I, si1I) * Math.fround((si - si0) * (sj - sj0));
25
+ this.setOutput(sum);
26
+ },
27
+ };
28
+ return kernel;
29
+ }
30
+ export const upsampleBilinear = (args) => {
31
+ /** @type {import('@tensorflow/tfjs').TensorInfo} */
32
+ const { image, targetImage } = args.inputs;
33
+ /** @type {MathBackendCPU} */
34
+ const cpuBackend = args.backend;
35
+ const program = getProgram(targetImage);
36
+ return FakeShader.runCode(cpuBackend, program, [image], image.dtype);
37
+ };
38
+ export const upsampleBilinearConfig = {
39
+ //: KernelConfig
40
+ kernelName: "UpsampleBilinear",
41
+ backendName: "cpu",
42
+ kernelFunc: upsampleBilinear, // as {} as KernelFunc,
43
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,2 @@
1
+ import "./webgl/index.js";
2
+ import "./cpu/index.js";
@@ -0,0 +1,6 @@
1
+ export function binomialFilter(args: any): any;
2
+ export namespace binomialFilterConfig {
3
+ export let kernelName: string;
4
+ export let backendName: string;
5
+ export { binomialFilter as kernelFunc };
6
+ }
@@ -0,0 +1,67 @@
1
+ const cache = {};
2
+ /**
3
+ *
4
+ * @param {string} key
5
+ * @param {TensorInfo} image
6
+ * @returns {[GPGPUProgram,GPGPUProgram]}
7
+ */
8
+ function GetKernels(image) {
9
+ const imageWidth = image.shape[1];
10
+ const imageHeight = image.shape[0];
11
+ const key = "w" + imageWidth + "h" + imageHeight;
12
+ if (!cache.hasOwnProperty(key)) {
13
+ const kernel1 = {
14
+ variableNames: ["p"],
15
+ outputShape: [imageHeight, imageWidth],
16
+ userCode: `
17
+ void main() {
18
+ ivec2 coords = getOutputCoords();
19
+
20
+ float sum = getP(coords[0], coords[1]-2);
21
+ sum += getP(coords[0], coords[1]-1) * 4.;
22
+ sum += getP(coords[0], coords[1]) * 6.;
23
+ sum += getP(coords[0], coords[1]+1) * 4.;
24
+ sum += getP(coords[0], coords[1]+2);
25
+ setOutput(sum);
26
+ }
27
+ `,
28
+ };
29
+ const kernel2 = {
30
+ variableNames: ["p"],
31
+ outputShape: [imageHeight, imageWidth],
32
+ userCode: `
33
+ void main() {
34
+ ivec2 coords = getOutputCoords();
35
+
36
+ float sum = getP(coords[0]-2, coords[1]);
37
+ sum += getP(coords[0]-1, coords[1]) * 4.;
38
+ sum += getP(coords[0], coords[1]) * 6.;
39
+ sum += getP(coords[0]+1, coords[1]) * 4.;
40
+ sum += getP(coords[0]+2, coords[1]);
41
+ sum /= 256.;
42
+ setOutput(sum);
43
+ }
44
+ `,
45
+ };
46
+ cache[key] = [kernel1, kernel2];
47
+ }
48
+ return cache[key];
49
+ }
50
+ export const binomialFilter = (args) => {
51
+ //{inputs: UnaryInputs, backend: MathBackendCPU}
52
+ /** @type {import('@tensorflow/tfjs').TensorInfo} */
53
+ const image = args.inputs.image;
54
+ /** @type {MathBackendWebGL} */
55
+ const backend = args.backend;
56
+ const [kernel1, kernel2] = GetKernels(image);
57
+ const result1 = backend.runWebGLProgram(kernel1, [image], image.dtype);
58
+ const result2 = backend.runWebGLProgram(kernel2, [result1], image.dtype);
59
+ backend.disposeIntermediateTensorInfo(result1);
60
+ return result2;
61
+ };
62
+ export const binomialFilterConfig = {
63
+ //: KernelConfig
64
+ kernelName: "BinomialFilter",
65
+ backendName: "webgl",
66
+ kernelFunc: binomialFilter, // as {} as KernelFunc,
67
+ };
@@ -0,0 +1,6 @@
1
+ export function buildExtremas(args: any): any;
2
+ export namespace buildExtremasConfig {
3
+ export let kernelName: string;
4
+ export let backendName: string;
5
+ export { buildExtremas as kernelFunc };
6
+ }
@@ -0,0 +1,101 @@
1
+ import { engine } from "@tensorflow/tfjs";
2
+ const FREAK_EXPANSION_FACTOR = 7.0;
3
+ const LAPLACIAN_THRESHOLD = 3.0;
4
+ const LAPLACIAN_SQR_THRESHOLD = LAPLACIAN_THRESHOLD * LAPLACIAN_THRESHOLD;
5
+ const EDGE_THRESHOLD = 4.0;
6
+ const EDGE_HESSIAN_THRESHOLD = ((EDGE_THRESHOLD + 1) * (EDGE_THRESHOLD + 1)) / EDGE_THRESHOLD;
7
+ const cache = {};
8
+ function GetProgram(image) {
9
+ const imageWidth = image.shape[1];
10
+ const imageHeight = image.shape[0];
11
+ const kernelKey = "w" + imageWidth + "h" + imageHeight;
12
+ if (!cache.hasOwnProperty(kernelKey)) {
13
+ const kernel = {
14
+ variableNames: ["image0", "image1", "image2"],
15
+ outputShape: [imageHeight, imageWidth],
16
+ userCode: `
17
+ void main() {
18
+ ivec2 coords = getOutputCoords();
19
+
20
+ int y = coords[0];
21
+ int x = coords[1];
22
+
23
+ float value = getImage1(y, x);
24
+
25
+ // Step 1: find local maxima/minima
26
+ if (value * value < ${LAPLACIAN_SQR_THRESHOLD}.) {
27
+ setOutput(0.);
28
+ return;
29
+ }
30
+ if (y < ${FREAK_EXPANSION_FACTOR} || y > ${imageHeight - 1 - FREAK_EXPANSION_FACTOR}) {
31
+ setOutput(0.);
32
+ return;
33
+ }
34
+ if (x < ${FREAK_EXPANSION_FACTOR} || x > ${imageWidth - 1 - FREAK_EXPANSION_FACTOR}) {
35
+ setOutput(0.);
36
+ return;
37
+ }
38
+
39
+ bool isMax = true;
40
+ bool isMin = true;
41
+ for (int dy = -1; dy <= 1; dy++) {
42
+ for (int dx = -1; dx <= 1; dx++) {
43
+ float value0 = getImage0(y+dy, x+dx);
44
+ float value1 = getImage1(y+dy, x+dx);
45
+ float value2 = getImage2(y+dy, x+dx);
46
+
47
+ if (value < value0 || value < value1 || value < value2) {
48
+ isMax = false;
49
+ }
50
+ if (value > value0 || value > value1 || value > value2) {
51
+ isMin = false;
52
+ }
53
+ }
54
+ }
55
+
56
+ if (!isMax && !isMin) {
57
+ setOutput(0.);
58
+ return;
59
+ }
60
+
61
+ // compute edge score and reject based on threshold
62
+ float dxx = getImage1(y, x+1) + getImage1(y, x-1) - 2. * getImage1(y, x);
63
+ float dyy = getImage1(y+1, x) + getImage1(y-1, x) - 2. * getImage1(y, x);
64
+ float dxy = 0.25 * (getImage1(y-1,x-1) + getImage1(y+1,x+1) - getImage1(y-1,x+1) - getImage1(y+1,x-1));
65
+
66
+ float det = (dxx * dyy) - (dxy * dxy);
67
+
68
+ if (abs(det) < 0.0001) { // determinant undefined. no solution
69
+ setOutput(0.);
70
+ return;
71
+ }
72
+
73
+ float edgeScore = (dxx + dyy) * (dxx + dyy) / det;
74
+
75
+ if (abs(edgeScore) >= ${EDGE_HESSIAN_THRESHOLD} ) {
76
+ setOutput(0.);
77
+ return;
78
+ }
79
+ setOutput(getImage1(y,x));
80
+ }
81
+ `,
82
+ };
83
+ cache[kernelKey] = kernel;
84
+ }
85
+ return cache[kernelKey];
86
+ }
87
+ export const buildExtremas = (args) => {
88
+ let { image0, image1, image2 } = args.inputs;
89
+ /** @type {MathBackendWebGL} */
90
+ const backend = args.backend;
91
+ const program = GetProgram(image1);
92
+ image0 = engine().runKernel("DownsampleBilinear", { image: image0 });
93
+ image2 = engine().runKernel("UpsampleBilinear", { image: image2, targetImage: image1 });
94
+ return backend.runWebGLProgram(program, [image0, image1, image2], image1.dtype);
95
+ };
96
+ export const buildExtremasConfig = {
97
+ //: KernelConfig
98
+ kernelName: "BuildExtremas",
99
+ backendName: "webgl",
100
+ kernelFunc: buildExtremas, // as {} as KernelFunc,
101
+ };
@@ -0,0 +1,6 @@
1
+ export function computeExtremaAngles(args: any): any;
2
+ export namespace computeExtremaAnglesConfig {
3
+ export let kernelName: string;
4
+ export let backendName: string;
5
+ export { computeExtremaAngles as kernelFunc };
6
+ }