@weavelogic/knowledge-graph-agent 0.6.0 → 0.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +70 -3
- package/dist/_virtual/__vite-browser-external.js +2 -2
- package/dist/_virtual/__vite-browser-external.js.map +1 -1
- package/dist/_virtual/index12.js +7 -0
- package/dist/_virtual/index12.js.map +1 -0
- package/dist/_virtual/ort-web.min.js +8 -0
- package/dist/_virtual/ort-web.min.js.map +1 -0
- package/dist/_virtual/ort-web.min2.js +5 -0
- package/dist/_virtual/ort-web.min2.js.map +1 -0
- package/dist/agents/base-agent.d.ts +63 -0
- package/dist/agents/base-agent.d.ts.map +1 -1
- package/dist/agents/base-agent.js +139 -0
- package/dist/agents/base-agent.js.map +1 -1
- package/dist/agents/coordinator-agent.d.ts +422 -0
- package/dist/agents/coordinator-agent.d.ts.map +1 -0
- package/dist/agents/documenter-agent.d.ts +298 -0
- package/dist/agents/documenter-agent.d.ts.map +1 -0
- package/dist/agents/index.d.ts +11 -1
- package/dist/agents/index.d.ts.map +1 -1
- package/dist/agents/index.js +4 -0
- package/dist/agents/index.js.map +1 -1
- package/dist/agents/mixins/index.d.ts +9 -0
- package/dist/agents/mixins/index.d.ts.map +1 -0
- package/dist/agents/mixins/trajectory-mixin.d.ts +112 -0
- package/dist/agents/mixins/trajectory-mixin.d.ts.map +1 -0
- package/dist/agents/optimizer-agent.d.ts +388 -0
- package/dist/agents/optimizer-agent.d.ts.map +1 -0
- package/dist/agents/planner-agent.d.ts +395 -0
- package/dist/agents/planner-agent.d.ts.map +1 -0
- package/dist/agents/registry.d.ts.map +1 -1
- package/dist/agents/registry.js +5 -0
- package/dist/agents/registry.js.map +1 -1
- package/dist/agents/reviewer-agent.d.ts +330 -0
- package/dist/agents/reviewer-agent.d.ts.map +1 -0
- package/dist/agents/types.d.ts +12 -1
- package/dist/agents/types.d.ts.map +1 -1
- package/dist/agents/types.js +1 -0
- package/dist/agents/types.js.map +1 -1
- package/dist/cli/commands/hive-mind/add-frontmatter.d.ts +102 -0
- package/dist/cli/commands/hive-mind/add-frontmatter.d.ts.map +1 -0
- package/dist/cli/commands/hive-mind/add-frontmatter.js +439 -0
- package/dist/cli/commands/hive-mind/add-frontmatter.js.map +1 -0
- package/dist/cli/commands/hive-mind/analyze-links.d.ts +80 -0
- package/dist/cli/commands/hive-mind/analyze-links.d.ts.map +1 -0
- package/dist/cli/commands/hive-mind/analyze-links.js +367 -0
- package/dist/cli/commands/hive-mind/analyze-links.js.map +1 -0
- package/dist/cli/commands/hive-mind/find-connections.d.ts +75 -0
- package/dist/cli/commands/hive-mind/find-connections.d.ts.map +1 -0
- package/dist/cli/commands/hive-mind/find-connections.js +347 -0
- package/dist/cli/commands/hive-mind/find-connections.js.map +1 -0
- package/dist/cli/commands/hive-mind/index.d.ts +37 -0
- package/dist/cli/commands/hive-mind/index.d.ts.map +1 -0
- package/dist/cli/commands/hive-mind/index.js +33 -0
- package/dist/cli/commands/hive-mind/index.js.map +1 -0
- package/dist/cli/commands/hive-mind/validate-names.d.ts +79 -0
- package/dist/cli/commands/hive-mind/validate-names.d.ts.map +1 -0
- package/dist/cli/commands/hive-mind/validate-names.js +353 -0
- package/dist/cli/commands/hive-mind/validate-names.js.map +1 -0
- package/dist/cli/commands/vector.js +2 -0
- package/dist/cli/commands/vector.js.map +1 -1
- package/dist/cli/index.d.ts.map +1 -1
- package/dist/cli/index.js +7 -0
- package/dist/cli/index.js.map +1 -1
- package/dist/equilibrium/agent-equilibrium.d.ts +194 -0
- package/dist/equilibrium/agent-equilibrium.d.ts.map +1 -0
- package/dist/equilibrium/agent-equilibrium.js +304 -0
- package/dist/equilibrium/agent-equilibrium.js.map +1 -0
- package/dist/equilibrium/graph-equilibrium.d.ts +177 -0
- package/dist/equilibrium/graph-equilibrium.d.ts.map +1 -0
- package/dist/equilibrium/index.d.ts +11 -0
- package/dist/equilibrium/index.d.ts.map +1 -0
- package/dist/equilibrium/memory-equilibrium.d.ts +153 -0
- package/dist/equilibrium/memory-equilibrium.d.ts.map +1 -0
- package/dist/graphql/resolvers/index.d.ts.map +1 -1
- package/dist/graphql/resolvers/queries.d.ts +11 -0
- package/dist/graphql/resolvers/queries.d.ts.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +10 -4
- package/dist/index.js.map +1 -1
- package/dist/inference/index.d.ts +9 -0
- package/dist/inference/index.d.ts.map +1 -0
- package/dist/inference/model-selection.d.ts +131 -0
- package/dist/inference/model-selection.d.ts.map +1 -0
- package/dist/integrations/agentic-flow/adapters/agent-booster-adapter.d.ts +265 -0
- package/dist/integrations/agentic-flow/adapters/agent-booster-adapter.d.ts.map +1 -0
- package/dist/integrations/agentic-flow/adapters/agentdb-adapter.d.ts +197 -0
- package/dist/integrations/agentic-flow/adapters/agentdb-adapter.d.ts.map +1 -0
- package/dist/integrations/agentic-flow/adapters/agentdb-vector-store.d.ts +249 -0
- package/dist/integrations/agentic-flow/adapters/agentdb-vector-store.d.ts.map +1 -0
- package/dist/integrations/agentic-flow/adapters/base-adapter.d.ts +120 -0
- package/dist/integrations/agentic-flow/adapters/base-adapter.d.ts.map +1 -0
- package/dist/integrations/agentic-flow/adapters/federation-hub-adapter.d.ts +444 -0
- package/dist/integrations/agentic-flow/adapters/federation-hub-adapter.d.ts.map +1 -0
- package/dist/integrations/agentic-flow/adapters/index.d.ts +17 -0
- package/dist/integrations/agentic-flow/adapters/index.d.ts.map +1 -0
- package/dist/integrations/agentic-flow/adapters/model-router-adapter.d.ts +242 -0
- package/dist/integrations/agentic-flow/adapters/model-router-adapter.d.ts.map +1 -0
- package/dist/integrations/agentic-flow/adapters/quic-transport-adapter.d.ts +364 -0
- package/dist/integrations/agentic-flow/adapters/quic-transport-adapter.d.ts.map +1 -0
- package/dist/integrations/agentic-flow/adapters/reasoning-bank-adapter.d.ts +209 -0
- package/dist/integrations/agentic-flow/adapters/reasoning-bank-adapter.d.ts.map +1 -0
- package/dist/integrations/agentic-flow/benchmark/index.d.ts +9 -0
- package/dist/integrations/agentic-flow/benchmark/index.d.ts.map +1 -0
- package/dist/integrations/agentic-flow/benchmark/vector-benchmark.d.ts +253 -0
- package/dist/integrations/agentic-flow/benchmark/vector-benchmark.d.ts.map +1 -0
- package/dist/integrations/agentic-flow/config.d.ts +109 -0
- package/dist/integrations/agentic-flow/config.d.ts.map +1 -0
- package/dist/integrations/agentic-flow/feature-flags.d.ts +140 -0
- package/dist/integrations/agentic-flow/feature-flags.d.ts.map +1 -0
- package/dist/integrations/agentic-flow/index.d.ts +22 -0
- package/dist/integrations/agentic-flow/index.d.ts.map +1 -0
- package/dist/integrations/agentic-flow/migration/index.d.ts +9 -0
- package/dist/integrations/agentic-flow/migration/index.d.ts.map +1 -0
- package/dist/integrations/agentic-flow/migration/migrate-to-agentdb.d.ts +242 -0
- package/dist/integrations/agentic-flow/migration/migrate-to-agentdb.d.ts.map +1 -0
- package/dist/learning/index.d.ts +91 -0
- package/dist/learning/index.d.ts.map +1 -0
- package/dist/learning/learning-loop.d.ts +176 -0
- package/dist/learning/learning-loop.d.ts.map +1 -0
- package/dist/learning/services/ab-testing-framework.d.ts +135 -0
- package/dist/learning/services/ab-testing-framework.d.ts.map +1 -0
- package/dist/learning/services/agent-priming-service.d.ts +207 -0
- package/dist/learning/services/agent-priming-service.d.ts.map +1 -0
- package/dist/learning/services/daily-log-generator.d.ts +113 -0
- package/dist/learning/services/daily-log-generator.d.ts.map +1 -0
- package/dist/learning/services/index.d.ts +14 -0
- package/dist/learning/services/index.d.ts.map +1 -0
- package/dist/learning/services/memory-extraction-service.d.ts +87 -0
- package/dist/learning/services/memory-extraction-service.d.ts.map +1 -0
- package/dist/learning/services/task-completion-consumer.d.ts +162 -0
- package/dist/learning/services/task-completion-consumer.d.ts.map +1 -0
- package/dist/learning/services/trajectory-tracker.d.ts +174 -0
- package/dist/learning/services/trajectory-tracker.d.ts.map +1 -0
- package/dist/learning/types.d.ts +516 -0
- package/dist/learning/types.d.ts.map +1 -0
- package/dist/mcp/clients/claude-flow-memory-client.d.ts +259 -0
- package/dist/mcp/clients/claude-flow-memory-client.d.ts.map +1 -0
- package/dist/mcp/clients/claude-flow-memory-client.js +305 -0
- package/dist/mcp/clients/claude-flow-memory-client.js.map +1 -0
- package/dist/mcp/clients/index.d.ts +11 -0
- package/dist/mcp/clients/index.d.ts.map +1 -0
- package/dist/mcp/clients/mcp-client-adapter.d.ts +146 -0
- package/dist/mcp/clients/mcp-client-adapter.d.ts.map +1 -0
- package/dist/mcp/clients/mcp-client-adapter.js +372 -0
- package/dist/mcp/clients/mcp-client-adapter.js.map +1 -0
- package/dist/mcp/index.d.ts +10 -0
- package/dist/mcp/index.d.ts.map +1 -0
- package/dist/memory/vault-sync.d.ts +12 -0
- package/dist/memory/vault-sync.d.ts.map +1 -1
- package/dist/memory/vault-sync.js +94 -11
- package/dist/memory/vault-sync.js.map +1 -1
- package/dist/node_modules/@huggingface/jinja/dist/index.js +118 -0
- package/dist/node_modules/@huggingface/jinja/dist/index.js.map +1 -0
- package/dist/node_modules/@typescript-eslint/project-service/dist/index.js +1 -1
- package/dist/node_modules/@xenova/transformers/src/backends/onnx.js +24 -0
- package/dist/node_modules/@xenova/transformers/src/backends/onnx.js.map +1 -0
- package/dist/node_modules/@xenova/transformers/src/configs.js +52 -0
- package/dist/node_modules/@xenova/transformers/src/configs.js.map +1 -0
- package/dist/node_modules/@xenova/transformers/src/env.js +35 -0
- package/dist/node_modules/@xenova/transformers/src/env.js.map +1 -0
- package/dist/node_modules/@xenova/transformers/src/models.js +3852 -0
- package/dist/node_modules/@xenova/transformers/src/models.js.map +1 -0
- package/dist/node_modules/@xenova/transformers/src/tokenizers.js +144 -0
- package/dist/node_modules/@xenova/transformers/src/tokenizers.js.map +1 -0
- package/dist/node_modules/@xenova/transformers/src/utils/core.js +52 -0
- package/dist/node_modules/@xenova/transformers/src/utils/core.js.map +1 -0
- package/dist/node_modules/@xenova/transformers/src/utils/generation.js +623 -0
- package/dist/node_modules/@xenova/transformers/src/utils/generation.js.map +1 -0
- package/dist/node_modules/@xenova/transformers/src/utils/hub.js +395 -0
- package/dist/node_modules/@xenova/transformers/src/utils/hub.js.map +1 -0
- package/dist/node_modules/@xenova/transformers/src/utils/image.js +12 -0
- package/dist/node_modules/@xenova/transformers/src/utils/image.js.map +1 -0
- package/dist/node_modules/@xenova/transformers/src/utils/maths.js +89 -0
- package/dist/node_modules/@xenova/transformers/src/utils/maths.js.map +1 -0
- package/dist/node_modules/@xenova/transformers/src/utils/tensor.js +750 -0
- package/dist/node_modules/@xenova/transformers/src/utils/tensor.js.map +1 -0
- package/dist/node_modules/fdir/dist/index.js +13 -13
- package/dist/node_modules/fdir/dist/index.js.map +1 -1
- package/dist/node_modules/onnxruntime-common/dist/lib/backend-impl.js +67 -0
- package/dist/node_modules/onnxruntime-common/dist/lib/backend-impl.js.map +1 -0
- package/dist/node_modules/onnxruntime-common/dist/lib/env-impl.js +24 -0
- package/dist/node_modules/onnxruntime-common/dist/lib/env-impl.js.map +1 -0
- package/dist/node_modules/onnxruntime-common/dist/lib/env.js +6 -0
- package/dist/node_modules/onnxruntime-common/dist/lib/env.js.map +1 -0
- package/dist/node_modules/onnxruntime-common/dist/lib/index.js +11 -0
- package/dist/node_modules/onnxruntime-common/dist/lib/index.js.map +1 -0
- package/dist/node_modules/onnxruntime-common/dist/lib/inference-session-impl.js +162 -0
- package/dist/node_modules/onnxruntime-common/dist/lib/inference-session-impl.js.map +1 -0
- package/dist/node_modules/onnxruntime-common/dist/lib/inference-session.js +6 -0
- package/dist/node_modules/onnxruntime-common/dist/lib/inference-session.js.map +1 -0
- package/dist/node_modules/onnxruntime-common/dist/lib/tensor-impl.js +393 -0
- package/dist/node_modules/onnxruntime-common/dist/lib/tensor-impl.js.map +1 -0
- package/dist/node_modules/onnxruntime-common/dist/lib/tensor.js +6 -0
- package/dist/node_modules/onnxruntime-common/dist/lib/tensor.js.map +1 -0
- package/dist/node_modules/onnxruntime-web/dist/ort-web.min.js +12919 -0
- package/dist/node_modules/onnxruntime-web/dist/ort-web.min.js.map +1 -0
- package/dist/node_modules/tinyglobby/dist/index.js +14 -14
- package/dist/node_modules/tinyglobby/dist/index.js.map +1 -1
- package/dist/node_modules/typescript/lib/typescript.js +24 -24
- package/dist/node_modules/typescript/lib/typescript.js.map +1 -1
- package/dist/transport/agent-transport.d.ts +269 -0
- package/dist/transport/agent-transport.d.ts.map +1 -0
- package/dist/transport/index.d.ts +10 -0
- package/dist/transport/index.d.ts.map +1 -0
- package/dist/vector/index.d.ts +1 -1
- package/dist/vector/index.d.ts.map +1 -1
- package/dist/vector/services/embedding-service.d.ts +244 -0
- package/dist/vector/services/embedding-service.d.ts.map +1 -0
- package/dist/vector/services/embedding-service.js +10 -0
- package/dist/vector/services/embedding-service.js.map +1 -0
- package/dist/vector/services/hybrid-search.d.ts +320 -0
- package/dist/vector/services/hybrid-search.d.ts.map +1 -0
- package/dist/vector/services/hybrid-search.js +3 -0
- package/dist/vector/services/hybrid-search.js.map +1 -0
- package/dist/vector/services/index.d.ts +4 -0
- package/dist/vector/services/index.d.ts.map +1 -1
- package/package.json +10 -1
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import { resolveBackend } from "./backend-impl.js";
|
|
2
|
+
import { Tensor } from "./tensor.js";
|
|
3
|
+
class InferenceSession {
|
|
4
|
+
constructor(handler) {
|
|
5
|
+
this.handler = handler;
|
|
6
|
+
}
|
|
7
|
+
async run(feeds, arg1, arg2) {
|
|
8
|
+
const fetches = {};
|
|
9
|
+
let options = {};
|
|
10
|
+
if (typeof feeds !== "object" || feeds === null || feeds instanceof Tensor || Array.isArray(feeds)) {
|
|
11
|
+
throw new TypeError("'feeds' must be an object that use input names as keys and OnnxValue as corresponding values.");
|
|
12
|
+
}
|
|
13
|
+
let isFetchesEmpty = true;
|
|
14
|
+
if (typeof arg1 === "object") {
|
|
15
|
+
if (arg1 === null) {
|
|
16
|
+
throw new TypeError("Unexpected argument[1]: cannot be null.");
|
|
17
|
+
}
|
|
18
|
+
if (arg1 instanceof Tensor) {
|
|
19
|
+
throw new TypeError("'fetches' cannot be a Tensor");
|
|
20
|
+
}
|
|
21
|
+
if (Array.isArray(arg1)) {
|
|
22
|
+
if (arg1.length === 0) {
|
|
23
|
+
throw new TypeError("'fetches' cannot be an empty array.");
|
|
24
|
+
}
|
|
25
|
+
isFetchesEmpty = false;
|
|
26
|
+
for (const name of arg1) {
|
|
27
|
+
if (typeof name !== "string") {
|
|
28
|
+
throw new TypeError("'fetches' must be a string array or an object.");
|
|
29
|
+
}
|
|
30
|
+
if (this.outputNames.indexOf(name) === -1) {
|
|
31
|
+
throw new RangeError(`'fetches' contains invalid output name: ${name}.`);
|
|
32
|
+
}
|
|
33
|
+
fetches[name] = null;
|
|
34
|
+
}
|
|
35
|
+
if (typeof arg2 === "object" && arg2 !== null) {
|
|
36
|
+
options = arg2;
|
|
37
|
+
} else if (typeof arg2 !== "undefined") {
|
|
38
|
+
throw new TypeError("'options' must be an object.");
|
|
39
|
+
}
|
|
40
|
+
} else {
|
|
41
|
+
let isFetches = false;
|
|
42
|
+
const arg1Keys = Object.getOwnPropertyNames(arg1);
|
|
43
|
+
for (const name of this.outputNames) {
|
|
44
|
+
if (arg1Keys.indexOf(name) !== -1) {
|
|
45
|
+
const v = arg1[name];
|
|
46
|
+
if (v === null || v instanceof Tensor) {
|
|
47
|
+
isFetches = true;
|
|
48
|
+
isFetchesEmpty = false;
|
|
49
|
+
fetches[name] = v;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
if (isFetches) {
|
|
54
|
+
if (typeof arg2 === "object" && arg2 !== null) {
|
|
55
|
+
options = arg2;
|
|
56
|
+
} else if (typeof arg2 !== "undefined") {
|
|
57
|
+
throw new TypeError("'options' must be an object.");
|
|
58
|
+
}
|
|
59
|
+
} else {
|
|
60
|
+
options = arg1;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
} else if (typeof arg1 !== "undefined") {
|
|
64
|
+
throw new TypeError("Unexpected argument[1]: must be 'fetches' or 'options'.");
|
|
65
|
+
}
|
|
66
|
+
for (const name of this.inputNames) {
|
|
67
|
+
if (typeof feeds[name] === "undefined") {
|
|
68
|
+
throw new Error(`input '${name}' is missing in 'feeds'.`);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
if (isFetchesEmpty) {
|
|
72
|
+
for (const name of this.outputNames) {
|
|
73
|
+
fetches[name] = null;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
const results = await this.handler.run(feeds, fetches, options);
|
|
77
|
+
const returnValue = {};
|
|
78
|
+
for (const key in results) {
|
|
79
|
+
if (Object.hasOwnProperty.call(results, key)) {
|
|
80
|
+
returnValue[key] = new Tensor(results[key].type, results[key].data, results[key].dims);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return returnValue;
|
|
84
|
+
}
|
|
85
|
+
static async create(arg0, arg1, arg2, arg3) {
|
|
86
|
+
let filePathOrUint8Array;
|
|
87
|
+
let options = {};
|
|
88
|
+
if (typeof arg0 === "string") {
|
|
89
|
+
filePathOrUint8Array = arg0;
|
|
90
|
+
if (typeof arg1 === "object" && arg1 !== null) {
|
|
91
|
+
options = arg1;
|
|
92
|
+
} else if (typeof arg1 !== "undefined") {
|
|
93
|
+
throw new TypeError("'options' must be an object.");
|
|
94
|
+
}
|
|
95
|
+
} else if (arg0 instanceof Uint8Array) {
|
|
96
|
+
filePathOrUint8Array = arg0;
|
|
97
|
+
if (typeof arg1 === "object" && arg1 !== null) {
|
|
98
|
+
options = arg1;
|
|
99
|
+
} else if (typeof arg1 !== "undefined") {
|
|
100
|
+
throw new TypeError("'options' must be an object.");
|
|
101
|
+
}
|
|
102
|
+
} else if (arg0 instanceof ArrayBuffer || typeof SharedArrayBuffer !== "undefined" && arg0 instanceof SharedArrayBuffer) {
|
|
103
|
+
const buffer = arg0;
|
|
104
|
+
let byteOffset = 0;
|
|
105
|
+
let byteLength = arg0.byteLength;
|
|
106
|
+
if (typeof arg1 === "object" && arg1 !== null) {
|
|
107
|
+
options = arg1;
|
|
108
|
+
} else if (typeof arg1 === "number") {
|
|
109
|
+
byteOffset = arg1;
|
|
110
|
+
if (!Number.isSafeInteger(byteOffset)) {
|
|
111
|
+
throw new RangeError("'byteOffset' must be an integer.");
|
|
112
|
+
}
|
|
113
|
+
if (byteOffset < 0 || byteOffset >= buffer.byteLength) {
|
|
114
|
+
throw new RangeError(`'byteOffset' is out of range [0, ${buffer.byteLength}).`);
|
|
115
|
+
}
|
|
116
|
+
byteLength = arg0.byteLength - byteOffset;
|
|
117
|
+
if (typeof arg2 === "number") {
|
|
118
|
+
byteLength = arg2;
|
|
119
|
+
if (!Number.isSafeInteger(byteLength)) {
|
|
120
|
+
throw new RangeError("'byteLength' must be an integer.");
|
|
121
|
+
}
|
|
122
|
+
if (byteLength <= 0 || byteOffset + byteLength > buffer.byteLength) {
|
|
123
|
+
throw new RangeError(`'byteLength' is out of range (0, ${buffer.byteLength - byteOffset}].`);
|
|
124
|
+
}
|
|
125
|
+
if (typeof arg3 === "object" && arg3 !== null) {
|
|
126
|
+
options = arg3;
|
|
127
|
+
} else if (typeof arg3 !== "undefined") {
|
|
128
|
+
throw new TypeError("'options' must be an object.");
|
|
129
|
+
}
|
|
130
|
+
} else if (typeof arg2 !== "undefined") {
|
|
131
|
+
throw new TypeError("'byteLength' must be a number.");
|
|
132
|
+
}
|
|
133
|
+
} else if (typeof arg1 !== "undefined") {
|
|
134
|
+
throw new TypeError("'options' must be an object.");
|
|
135
|
+
}
|
|
136
|
+
filePathOrUint8Array = new Uint8Array(buffer, byteOffset, byteLength);
|
|
137
|
+
} else {
|
|
138
|
+
throw new TypeError("Unexpected argument[0]: must be 'path' or 'buffer'.");
|
|
139
|
+
}
|
|
140
|
+
const eps = options.executionProviders || [];
|
|
141
|
+
const backendHints = eps.map((i) => typeof i === "string" ? i : i.name);
|
|
142
|
+
const backend = await resolveBackend(backendHints);
|
|
143
|
+
const handler = await backend.createSessionHandler(filePathOrUint8Array, options);
|
|
144
|
+
return new InferenceSession(handler);
|
|
145
|
+
}
|
|
146
|
+
startProfiling() {
|
|
147
|
+
this.handler.startProfiling();
|
|
148
|
+
}
|
|
149
|
+
endProfiling() {
|
|
150
|
+
this.handler.endProfiling();
|
|
151
|
+
}
|
|
152
|
+
get inputNames() {
|
|
153
|
+
return this.handler.inputNames;
|
|
154
|
+
}
|
|
155
|
+
get outputNames() {
|
|
156
|
+
return this.handler.outputNames;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
export {
|
|
160
|
+
InferenceSession
|
|
161
|
+
};
|
|
162
|
+
//# sourceMappingURL=inference-session-impl.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"inference-session-impl.js","sources":["../../../../../node_modules/onnxruntime-common/dist/lib/inference-session-impl.js"],"sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved.\n// Licensed under the MIT License.\nimport { resolveBackend } from './backend-impl';\nimport { Tensor } from './tensor';\nexport class InferenceSession {\n constructor(handler) {\n this.handler = handler;\n }\n async run(feeds, arg1, arg2) {\n const fetches = {};\n let options = {};\n // check inputs\n if (typeof feeds !== 'object' || feeds === null || feeds instanceof Tensor || Array.isArray(feeds)) {\n throw new TypeError('\\'feeds\\' must be an object that use input names as keys and OnnxValue as corresponding values.');\n }\n let isFetchesEmpty = true;\n // determine which override is being used\n if (typeof arg1 === 'object') {\n if (arg1 === null) {\n throw new TypeError('Unexpected argument[1]: cannot be null.');\n }\n if (arg1 instanceof Tensor) {\n throw new TypeError('\\'fetches\\' cannot be a Tensor');\n }\n if (Array.isArray(arg1)) {\n if (arg1.length === 0) {\n throw new TypeError('\\'fetches\\' cannot be an empty array.');\n }\n isFetchesEmpty = false;\n // output names\n for (const name of arg1) {\n if (typeof name !== 'string') {\n throw new TypeError('\\'fetches\\' must be a string array or an object.');\n }\n if (this.outputNames.indexOf(name) === -1) {\n throw new RangeError(`'fetches' contains invalid output name: ${name}.`);\n }\n fetches[name] = null;\n }\n if (typeof arg2 === 'object' && arg2 !== null) {\n options = arg2;\n }\n else if (typeof arg2 !== 'undefined') {\n throw new TypeError('\\'options\\' must be an object.');\n }\n }\n else {\n // decide whether arg1 is fetches or options\n // if any output name is present and its value is valid OnnxValue, we consider it fetches\n let isFetches = false;\n const arg1Keys = Object.getOwnPropertyNames(arg1);\n for (const name of this.outputNames) {\n if (arg1Keys.indexOf(name) !== -1) {\n const v = arg1[name];\n if (v === null || v instanceof Tensor) {\n isFetches = true;\n isFetchesEmpty = false;\n fetches[name] = v;\n }\n }\n }\n if (isFetches) {\n if (typeof arg2 === 'object' && arg2 !== null) {\n options = arg2;\n }\n else if (typeof arg2 !== 'undefined') {\n throw new TypeError('\\'options\\' must be an object.');\n }\n }\n else {\n options = arg1;\n }\n }\n }\n else if (typeof arg1 !== 'undefined') {\n throw new TypeError('Unexpected argument[1]: must be \\'fetches\\' or \\'options\\'.');\n }\n // check if all inputs are in feed\n for (const name of this.inputNames) {\n if (typeof feeds[name] === 'undefined') {\n throw new Error(`input '${name}' is missing in 'feeds'.`);\n }\n }\n // if no fetches is specified, we use the full output names list\n if (isFetchesEmpty) {\n for (const name of this.outputNames) {\n fetches[name] = null;\n }\n }\n // feeds, fetches and options are prepared\n const results = await this.handler.run(feeds, fetches, options);\n const returnValue = {};\n for (const key in results) {\n if (Object.hasOwnProperty.call(results, key)) {\n returnValue[key] = new Tensor(results[key].type, results[key].data, results[key].dims);\n }\n }\n return returnValue;\n }\n static async create(arg0, arg1, arg2, arg3) {\n // either load from a file or buffer\n let filePathOrUint8Array;\n let options = {};\n if (typeof arg0 === 'string') {\n filePathOrUint8Array = arg0;\n if (typeof arg1 === 'object' && arg1 !== null) {\n options = arg1;\n }\n else if (typeof arg1 !== 'undefined') {\n throw new TypeError('\\'options\\' must be an object.');\n }\n }\n else if (arg0 instanceof Uint8Array) {\n filePathOrUint8Array = arg0;\n if (typeof arg1 === 'object' && arg1 !== null) {\n options = arg1;\n }\n else if (typeof arg1 !== 'undefined') {\n throw new TypeError('\\'options\\' must be an object.');\n }\n }\n else if (arg0 instanceof ArrayBuffer ||\n (typeof SharedArrayBuffer !== 'undefined' && arg0 instanceof SharedArrayBuffer)) {\n const buffer = arg0;\n let byteOffset = 0;\n let byteLength = arg0.byteLength;\n if (typeof arg1 === 'object' && arg1 !== null) {\n options = arg1;\n }\n else if (typeof arg1 === 'number') {\n byteOffset = arg1;\n if (!Number.isSafeInteger(byteOffset)) {\n throw new RangeError('\\'byteOffset\\' must be an integer.');\n }\n if (byteOffset < 0 || byteOffset >= buffer.byteLength) {\n throw new RangeError(`'byteOffset' is out of range [0, ${buffer.byteLength}).`);\n }\n byteLength = arg0.byteLength - byteOffset;\n if (typeof arg2 === 'number') {\n byteLength = arg2;\n if (!Number.isSafeInteger(byteLength)) {\n throw new RangeError('\\'byteLength\\' must be an integer.');\n }\n if (byteLength <= 0 || byteOffset + byteLength > buffer.byteLength) {\n throw new RangeError(`'byteLength' is out of range (0, ${buffer.byteLength - byteOffset}].`);\n }\n if (typeof arg3 === 'object' && arg3 !== null) {\n options = arg3;\n }\n else if (typeof arg3 !== 'undefined') {\n throw new TypeError('\\'options\\' must be an object.');\n }\n }\n else if (typeof arg2 !== 'undefined') {\n throw new TypeError('\\'byteLength\\' must be a number.');\n }\n }\n else if (typeof arg1 !== 'undefined') {\n throw new TypeError('\\'options\\' must be an object.');\n }\n filePathOrUint8Array = new Uint8Array(buffer, byteOffset, byteLength);\n }\n else {\n throw new TypeError('Unexpected argument[0]: must be \\'path\\' or \\'buffer\\'.');\n }\n // get backend hints\n const eps = options.executionProviders || [];\n const backendHints = eps.map(i => typeof i === 'string' ? i : i.name);\n const backend = await resolveBackend(backendHints);\n const handler = await backend.createSessionHandler(filePathOrUint8Array, options);\n return new InferenceSession(handler);\n }\n startProfiling() {\n this.handler.startProfiling();\n }\n endProfiling() {\n this.handler.endProfiling();\n }\n get inputNames() {\n return this.handler.inputNames;\n }\n get outputNames() {\n return this.handler.outputNames;\n }\n}\n//# sourceMappingURL=inference-session-impl.js.map"],"names":[],"mappings":"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","x_google_ignoreList":[0]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"inference-session.js","sources":["../../../../../node_modules/onnxruntime-common/dist/lib/inference-session.js"],"sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved.\n// Licensed under the MIT License.\nimport { InferenceSession as InferenceSessionImpl } from './inference-session-impl';\n// eslint-disable-next-line @typescript-eslint/naming-convention\nexport const InferenceSession = InferenceSessionImpl;\n//# sourceMappingURL=inference-session.js.map"],"names":["InferenceSessionImpl"],"mappings":";AAIY,MAAC,mBAAmBA;","x_google_ignoreList":[0]}
|
|
@@ -0,0 +1,393 @@
|
|
|
1
|
+
const isBigInt64ArrayAvailable = typeof BigInt64Array !== "undefined" && typeof BigInt64Array.from === "function";
|
|
2
|
+
const isBigUint64ArrayAvailable = typeof BigUint64Array !== "undefined" && typeof BigUint64Array.from === "function";
|
|
3
|
+
const NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP = /* @__PURE__ */ new Map([
|
|
4
|
+
["float32", Float32Array],
|
|
5
|
+
["uint8", Uint8Array],
|
|
6
|
+
["int8", Int8Array],
|
|
7
|
+
["uint16", Uint16Array],
|
|
8
|
+
["int16", Int16Array],
|
|
9
|
+
["int32", Int32Array],
|
|
10
|
+
["bool", Uint8Array],
|
|
11
|
+
["float64", Float64Array],
|
|
12
|
+
["uint32", Uint32Array]
|
|
13
|
+
]);
|
|
14
|
+
const NUMERIC_TENSOR_TYPEDARRAY_TO_TYPE_MAP = /* @__PURE__ */ new Map([
|
|
15
|
+
[Float32Array, "float32"],
|
|
16
|
+
[Uint8Array, "uint8"],
|
|
17
|
+
[Int8Array, "int8"],
|
|
18
|
+
[Uint16Array, "uint16"],
|
|
19
|
+
[Int16Array, "int16"],
|
|
20
|
+
[Int32Array, "int32"],
|
|
21
|
+
[Float64Array, "float64"],
|
|
22
|
+
[Uint32Array, "uint32"]
|
|
23
|
+
]);
|
|
24
|
+
if (isBigInt64ArrayAvailable) {
|
|
25
|
+
NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP.set("int64", BigInt64Array);
|
|
26
|
+
NUMERIC_TENSOR_TYPEDARRAY_TO_TYPE_MAP.set(BigInt64Array, "int64");
|
|
27
|
+
}
|
|
28
|
+
if (isBigUint64ArrayAvailable) {
|
|
29
|
+
NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP.set("uint64", BigUint64Array);
|
|
30
|
+
NUMERIC_TENSOR_TYPEDARRAY_TO_TYPE_MAP.set(BigUint64Array, "uint64");
|
|
31
|
+
}
|
|
32
|
+
const calculateSize = (dims) => {
|
|
33
|
+
let size = 1;
|
|
34
|
+
for (let i = 0; i < dims.length; i++) {
|
|
35
|
+
const dim = dims[i];
|
|
36
|
+
if (typeof dim !== "number" || !Number.isSafeInteger(dim)) {
|
|
37
|
+
throw new TypeError(`dims[${i}] must be an integer, got: ${dim}`);
|
|
38
|
+
}
|
|
39
|
+
if (dim < 0) {
|
|
40
|
+
throw new RangeError(`dims[${i}] must be a non-negative integer, got: ${dim}`);
|
|
41
|
+
}
|
|
42
|
+
size *= dim;
|
|
43
|
+
}
|
|
44
|
+
return size;
|
|
45
|
+
};
|
|
46
|
+
class Tensor {
|
|
47
|
+
constructor(arg0, arg1, arg2) {
|
|
48
|
+
let type;
|
|
49
|
+
let data;
|
|
50
|
+
let dims;
|
|
51
|
+
if (typeof arg0 === "string") {
|
|
52
|
+
type = arg0;
|
|
53
|
+
dims = arg2;
|
|
54
|
+
if (arg0 === "string") {
|
|
55
|
+
if (!Array.isArray(arg1)) {
|
|
56
|
+
throw new TypeError("A string tensor's data must be a string array.");
|
|
57
|
+
}
|
|
58
|
+
data = arg1;
|
|
59
|
+
} else {
|
|
60
|
+
const typedArrayConstructor = NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP.get(arg0);
|
|
61
|
+
if (typedArrayConstructor === void 0) {
|
|
62
|
+
throw new TypeError(`Unsupported tensor type: ${arg0}.`);
|
|
63
|
+
}
|
|
64
|
+
if (Array.isArray(arg1)) {
|
|
65
|
+
data = typedArrayConstructor.from(arg1);
|
|
66
|
+
} else if (arg1 instanceof typedArrayConstructor) {
|
|
67
|
+
data = arg1;
|
|
68
|
+
} else {
|
|
69
|
+
throw new TypeError(`A ${type} tensor's data must be type of ${typedArrayConstructor}`);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
} else {
|
|
73
|
+
dims = arg1;
|
|
74
|
+
if (Array.isArray(arg0)) {
|
|
75
|
+
if (arg0.length === 0) {
|
|
76
|
+
throw new TypeError("Tensor type cannot be inferred from an empty array.");
|
|
77
|
+
}
|
|
78
|
+
const firstElementType = typeof arg0[0];
|
|
79
|
+
if (firstElementType === "string") {
|
|
80
|
+
type = "string";
|
|
81
|
+
data = arg0;
|
|
82
|
+
} else if (firstElementType === "boolean") {
|
|
83
|
+
type = "bool";
|
|
84
|
+
data = Uint8Array.from(arg0);
|
|
85
|
+
} else {
|
|
86
|
+
throw new TypeError(`Invalid element type of data array: ${firstElementType}.`);
|
|
87
|
+
}
|
|
88
|
+
} else {
|
|
89
|
+
const mappedType = NUMERIC_TENSOR_TYPEDARRAY_TO_TYPE_MAP.get(arg0.constructor);
|
|
90
|
+
if (mappedType === void 0) {
|
|
91
|
+
throw new TypeError(`Unsupported type for tensor data: ${arg0.constructor}.`);
|
|
92
|
+
}
|
|
93
|
+
type = mappedType;
|
|
94
|
+
data = arg0;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
if (dims === void 0) {
|
|
98
|
+
dims = [data.length];
|
|
99
|
+
} else if (!Array.isArray(dims)) {
|
|
100
|
+
throw new TypeError("A tensor's dims must be a number array");
|
|
101
|
+
}
|
|
102
|
+
const size = calculateSize(dims);
|
|
103
|
+
if (size !== data.length) {
|
|
104
|
+
throw new Error(`Tensor's size(${size}) does not match data length(${data.length}).`);
|
|
105
|
+
}
|
|
106
|
+
this.dims = dims;
|
|
107
|
+
this.type = type;
|
|
108
|
+
this.data = data;
|
|
109
|
+
this.size = size;
|
|
110
|
+
}
|
|
111
|
+
// #endregion
|
|
112
|
+
/**
|
|
113
|
+
* Create a new tensor object from image object
|
|
114
|
+
*
|
|
115
|
+
* @param buffer - Extracted image buffer data - assuming RGBA format
|
|
116
|
+
* @param imageFormat - input image configuration - required configurations height, width, format
|
|
117
|
+
* @param tensorFormat - output tensor configuration - Default is RGB format
|
|
118
|
+
*/
|
|
119
|
+
static bufferToTensor(buffer, options) {
|
|
120
|
+
if (buffer === void 0) {
|
|
121
|
+
throw new Error("Image buffer must be defined");
|
|
122
|
+
}
|
|
123
|
+
if (options.height === void 0 || options.width === void 0) {
|
|
124
|
+
throw new Error("Image height and width must be defined");
|
|
125
|
+
}
|
|
126
|
+
const { height, width } = options;
|
|
127
|
+
const norm = options.norm;
|
|
128
|
+
let normMean;
|
|
129
|
+
let normBias;
|
|
130
|
+
if (norm === void 0 || norm.mean === void 0) {
|
|
131
|
+
normMean = 255;
|
|
132
|
+
} else {
|
|
133
|
+
normMean = norm.mean;
|
|
134
|
+
}
|
|
135
|
+
if (norm === void 0 || norm.bias === void 0) {
|
|
136
|
+
normBias = 0;
|
|
137
|
+
} else {
|
|
138
|
+
normBias = norm.bias;
|
|
139
|
+
}
|
|
140
|
+
const inputformat = options.bitmapFormat !== void 0 ? options.bitmapFormat : "RGBA";
|
|
141
|
+
const outputformat = options.tensorFormat !== void 0 ? options.tensorFormat !== void 0 ? options.tensorFormat : "RGB" : "RGB";
|
|
142
|
+
const offset = height * width;
|
|
143
|
+
const float32Data = outputformat === "RGBA" ? new Float32Array(offset * 4) : new Float32Array(offset * 3);
|
|
144
|
+
let step = 4, rImagePointer = 0, gImagePointer = 1, bImagePointer = 2, aImagePointer = 3;
|
|
145
|
+
let rTensorPointer = 0, gTensorPointer = offset, bTensorPointer = offset * 2, aTensorPointer = -1;
|
|
146
|
+
if (inputformat === "RGB") {
|
|
147
|
+
step = 3;
|
|
148
|
+
rImagePointer = 0;
|
|
149
|
+
gImagePointer = 1;
|
|
150
|
+
bImagePointer = 2;
|
|
151
|
+
aImagePointer = -1;
|
|
152
|
+
}
|
|
153
|
+
if (outputformat === "RGBA") {
|
|
154
|
+
aTensorPointer = offset * 3;
|
|
155
|
+
} else if (outputformat === "RBG") {
|
|
156
|
+
rTensorPointer = 0;
|
|
157
|
+
bTensorPointer = offset;
|
|
158
|
+
gTensorPointer = offset * 2;
|
|
159
|
+
} else if (outputformat === "BGR") {
|
|
160
|
+
bTensorPointer = 0;
|
|
161
|
+
gTensorPointer = offset;
|
|
162
|
+
rTensorPointer = offset * 2;
|
|
163
|
+
}
|
|
164
|
+
for (let i = 0; i < offset; i++, rImagePointer += step, bImagePointer += step, gImagePointer += step, aImagePointer += step) {
|
|
165
|
+
float32Data[rTensorPointer++] = (buffer[rImagePointer] + normBias) / normMean;
|
|
166
|
+
float32Data[gTensorPointer++] = (buffer[gImagePointer] + normBias) / normMean;
|
|
167
|
+
float32Data[bTensorPointer++] = (buffer[bImagePointer] + normBias) / normMean;
|
|
168
|
+
if (aTensorPointer !== -1 && aImagePointer !== -1) {
|
|
169
|
+
float32Data[aTensorPointer++] = (buffer[aImagePointer] + normBias) / normMean;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
const outputTensor = outputformat === "RGBA" ? new Tensor("float32", float32Data, [1, 4, height, width]) : new Tensor("float32", float32Data, [1, 3, height, width]);
|
|
173
|
+
return outputTensor;
|
|
174
|
+
}
|
|
175
|
+
static async fromImage(image, options) {
|
|
176
|
+
const isHTMLImageEle = typeof HTMLImageElement !== "undefined" && image instanceof HTMLImageElement;
|
|
177
|
+
const isImageDataEle = typeof ImageData !== "undefined" && image instanceof ImageData;
|
|
178
|
+
const isImageBitmap = typeof ImageBitmap !== "undefined" && image instanceof ImageBitmap;
|
|
179
|
+
const isURL = typeof String !== "undefined" && (image instanceof String || typeof image === "string");
|
|
180
|
+
let data;
|
|
181
|
+
let tensorConfig = {};
|
|
182
|
+
if (isHTMLImageEle) {
|
|
183
|
+
const canvas = document.createElement("canvas");
|
|
184
|
+
const pixels2DContext = canvas.getContext("2d");
|
|
185
|
+
if (pixels2DContext != null) {
|
|
186
|
+
let height = image.naturalHeight;
|
|
187
|
+
let width = image.naturalWidth;
|
|
188
|
+
if (options !== void 0 && options.resizedHeight !== void 0 && options.resizedWidth !== void 0) {
|
|
189
|
+
height = options.resizedHeight;
|
|
190
|
+
width = options.resizedWidth;
|
|
191
|
+
}
|
|
192
|
+
if (options !== void 0) {
|
|
193
|
+
tensorConfig = options;
|
|
194
|
+
if (options.tensorFormat !== void 0) {
|
|
195
|
+
throw new Error("Image input config format must be RGBA for HTMLImageElement");
|
|
196
|
+
} else {
|
|
197
|
+
tensorConfig.tensorFormat = "RGBA";
|
|
198
|
+
}
|
|
199
|
+
if (options.height !== void 0 && options.height !== height) {
|
|
200
|
+
throw new Error("Image input config height doesn't match HTMLImageElement height");
|
|
201
|
+
} else {
|
|
202
|
+
tensorConfig.height = height;
|
|
203
|
+
}
|
|
204
|
+
if (options.width !== void 0 && options.width !== width) {
|
|
205
|
+
throw new Error("Image input config width doesn't match HTMLImageElement width");
|
|
206
|
+
} else {
|
|
207
|
+
tensorConfig.width = width;
|
|
208
|
+
}
|
|
209
|
+
} else {
|
|
210
|
+
tensorConfig.tensorFormat = "RGBA";
|
|
211
|
+
tensorConfig.height = height;
|
|
212
|
+
tensorConfig.width = width;
|
|
213
|
+
}
|
|
214
|
+
canvas.width = width;
|
|
215
|
+
canvas.height = height;
|
|
216
|
+
pixels2DContext.drawImage(image, 0, 0, width, height);
|
|
217
|
+
data = pixels2DContext.getImageData(0, 0, width, height).data;
|
|
218
|
+
} else {
|
|
219
|
+
throw new Error("Can not access image data");
|
|
220
|
+
}
|
|
221
|
+
} else if (isImageDataEle) {
|
|
222
|
+
const format = "RGBA";
|
|
223
|
+
let height;
|
|
224
|
+
let width;
|
|
225
|
+
if (options !== void 0 && options.resizedWidth !== void 0 && options.resizedHeight !== void 0) {
|
|
226
|
+
height = options.resizedHeight;
|
|
227
|
+
width = options.resizedWidth;
|
|
228
|
+
} else {
|
|
229
|
+
height = image.height;
|
|
230
|
+
width = image.width;
|
|
231
|
+
}
|
|
232
|
+
if (options !== void 0) {
|
|
233
|
+
tensorConfig = options;
|
|
234
|
+
if (options.bitmapFormat !== void 0 && options.bitmapFormat !== format) {
|
|
235
|
+
throw new Error("Image input config format must be RGBA for ImageData");
|
|
236
|
+
} else {
|
|
237
|
+
tensorConfig.bitmapFormat = "RGBA";
|
|
238
|
+
}
|
|
239
|
+
} else {
|
|
240
|
+
tensorConfig.bitmapFormat = "RGBA";
|
|
241
|
+
}
|
|
242
|
+
tensorConfig.height = height;
|
|
243
|
+
tensorConfig.width = width;
|
|
244
|
+
if (options !== void 0) {
|
|
245
|
+
const tempCanvas = document.createElement("canvas");
|
|
246
|
+
tempCanvas.width = width;
|
|
247
|
+
tempCanvas.height = height;
|
|
248
|
+
const pixels2DContext = tempCanvas.getContext("2d");
|
|
249
|
+
if (pixels2DContext != null) {
|
|
250
|
+
pixels2DContext.putImageData(image, 0, 0);
|
|
251
|
+
data = pixels2DContext.getImageData(0, 0, width, height).data;
|
|
252
|
+
} else {
|
|
253
|
+
throw new Error("Can not access image data");
|
|
254
|
+
}
|
|
255
|
+
} else {
|
|
256
|
+
data = image.data;
|
|
257
|
+
}
|
|
258
|
+
} else if (isImageBitmap) {
|
|
259
|
+
if (options === void 0) {
|
|
260
|
+
throw new Error("Please provide image config with format for Imagebitmap");
|
|
261
|
+
}
|
|
262
|
+
if (options.bitmapFormat !== void 0) {
|
|
263
|
+
throw new Error("Image input config format must be defined for ImageBitmap");
|
|
264
|
+
}
|
|
265
|
+
const pixels2DContext = document.createElement("canvas").getContext("2d");
|
|
266
|
+
if (pixels2DContext != null) {
|
|
267
|
+
const height = image.height;
|
|
268
|
+
const width = image.width;
|
|
269
|
+
pixels2DContext.drawImage(image, 0, 0, width, height);
|
|
270
|
+
data = pixels2DContext.getImageData(0, 0, width, height).data;
|
|
271
|
+
if (options !== void 0) {
|
|
272
|
+
if (options.height !== void 0 && options.height !== height) {
|
|
273
|
+
throw new Error("Image input config height doesn't match ImageBitmap height");
|
|
274
|
+
} else {
|
|
275
|
+
tensorConfig.height = height;
|
|
276
|
+
}
|
|
277
|
+
if (options.width !== void 0 && options.width !== width) {
|
|
278
|
+
throw new Error("Image input config width doesn't match ImageBitmap width");
|
|
279
|
+
} else {
|
|
280
|
+
tensorConfig.width = width;
|
|
281
|
+
}
|
|
282
|
+
} else {
|
|
283
|
+
tensorConfig.height = height;
|
|
284
|
+
tensorConfig.width = width;
|
|
285
|
+
}
|
|
286
|
+
return Tensor.bufferToTensor(data, tensorConfig);
|
|
287
|
+
} else {
|
|
288
|
+
throw new Error("Can not access image data");
|
|
289
|
+
}
|
|
290
|
+
} else if (isURL) {
|
|
291
|
+
return new Promise((resolve, reject) => {
|
|
292
|
+
const canvas = document.createElement("canvas");
|
|
293
|
+
const context = canvas.getContext("2d");
|
|
294
|
+
if (!image || !context) {
|
|
295
|
+
return reject();
|
|
296
|
+
}
|
|
297
|
+
const newImage = new Image();
|
|
298
|
+
newImage.crossOrigin = "Anonymous";
|
|
299
|
+
newImage.src = image;
|
|
300
|
+
newImage.onload = () => {
|
|
301
|
+
canvas.width = newImage.width;
|
|
302
|
+
canvas.height = newImage.height;
|
|
303
|
+
context.drawImage(newImage, 0, 0, canvas.width, canvas.height);
|
|
304
|
+
const img = context.getImageData(0, 0, canvas.width, canvas.height);
|
|
305
|
+
if (options !== void 0) {
|
|
306
|
+
if (options.height !== void 0 && options.height !== canvas.height) {
|
|
307
|
+
throw new Error("Image input config height doesn't match ImageBitmap height");
|
|
308
|
+
} else {
|
|
309
|
+
tensorConfig.height = canvas.height;
|
|
310
|
+
}
|
|
311
|
+
if (options.width !== void 0 && options.width !== canvas.width) {
|
|
312
|
+
throw new Error("Image input config width doesn't match ImageBitmap width");
|
|
313
|
+
} else {
|
|
314
|
+
tensorConfig.width = canvas.width;
|
|
315
|
+
}
|
|
316
|
+
} else {
|
|
317
|
+
tensorConfig.height = canvas.height;
|
|
318
|
+
tensorConfig.width = canvas.width;
|
|
319
|
+
}
|
|
320
|
+
resolve(Tensor.bufferToTensor(img.data, tensorConfig));
|
|
321
|
+
};
|
|
322
|
+
});
|
|
323
|
+
} else {
|
|
324
|
+
throw new Error("Input data provided is not supported - aborted tensor creation");
|
|
325
|
+
}
|
|
326
|
+
if (data !== void 0) {
|
|
327
|
+
return Tensor.bufferToTensor(data, tensorConfig);
|
|
328
|
+
} else {
|
|
329
|
+
throw new Error("Input data provided is not supported - aborted tensor creation");
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
toImageData(options) {
|
|
333
|
+
var _a, _b;
|
|
334
|
+
const pixels2DContext = document.createElement("canvas").getContext("2d");
|
|
335
|
+
let image;
|
|
336
|
+
if (pixels2DContext != null) {
|
|
337
|
+
const width = this.dims[3];
|
|
338
|
+
const height = this.dims[2];
|
|
339
|
+
const channels = this.dims[1];
|
|
340
|
+
const inputformat = options !== void 0 ? options.format !== void 0 ? options.format : "RGB" : "RGB";
|
|
341
|
+
const normMean = options !== void 0 ? ((_a = options.norm) === null || _a === void 0 ? void 0 : _a.mean) !== void 0 ? options.norm.mean : 255 : 255;
|
|
342
|
+
const normBias = options !== void 0 ? ((_b = options.norm) === null || _b === void 0 ? void 0 : _b.bias) !== void 0 ? options.norm.bias : 0 : 0;
|
|
343
|
+
const offset = height * width;
|
|
344
|
+
if (options !== void 0) {
|
|
345
|
+
if (options.height !== void 0 && options.height !== height) {
|
|
346
|
+
throw new Error("Image output config height doesn't match tensor height");
|
|
347
|
+
}
|
|
348
|
+
if (options.width !== void 0 && options.width !== width) {
|
|
349
|
+
throw new Error("Image output config width doesn't match tensor width");
|
|
350
|
+
}
|
|
351
|
+
if (options.format !== void 0 && (channels === 4 && options.format !== "RGBA") || channels === 3 && (options.format !== "RGB" && options.format !== "BGR")) {
|
|
352
|
+
throw new Error("Tensor format doesn't match input tensor dims");
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
const step = 4;
|
|
356
|
+
let rImagePointer = 0, gImagePointer = 1, bImagePointer = 2, aImagePointer = 3;
|
|
357
|
+
let rTensorPointer = 0, gTensorPointer = offset, bTensorPointer = offset * 2, aTensorPointer = -1;
|
|
358
|
+
if (inputformat === "RGBA") {
|
|
359
|
+
rTensorPointer = 0;
|
|
360
|
+
gTensorPointer = offset;
|
|
361
|
+
bTensorPointer = offset * 2;
|
|
362
|
+
aTensorPointer = offset * 3;
|
|
363
|
+
} else if (inputformat === "RGB") {
|
|
364
|
+
rTensorPointer = 0;
|
|
365
|
+
gTensorPointer = offset;
|
|
366
|
+
bTensorPointer = offset * 2;
|
|
367
|
+
} else if (inputformat === "RBG") {
|
|
368
|
+
rTensorPointer = 0;
|
|
369
|
+
bTensorPointer = offset;
|
|
370
|
+
gTensorPointer = offset * 2;
|
|
371
|
+
}
|
|
372
|
+
image = pixels2DContext.createImageData(width, height);
|
|
373
|
+
for (let i = 0; i < height * width; rImagePointer += step, gImagePointer += step, bImagePointer += step, aImagePointer += step, i++) {
|
|
374
|
+
image.data[rImagePointer] = (this.data[rTensorPointer++] - normBias) * normMean;
|
|
375
|
+
image.data[gImagePointer] = (this.data[gTensorPointer++] - normBias) * normMean;
|
|
376
|
+
image.data[bImagePointer] = (this.data[bTensorPointer++] - normBias) * normMean;
|
|
377
|
+
image.data[aImagePointer] = aTensorPointer === -1 ? 255 : (this.data[aTensorPointer++] - normBias) * normMean;
|
|
378
|
+
}
|
|
379
|
+
} else {
|
|
380
|
+
throw new Error("Can not access image data");
|
|
381
|
+
}
|
|
382
|
+
return image;
|
|
383
|
+
}
|
|
384
|
+
// #endregion
|
|
385
|
+
// #region tensor utilities
|
|
386
|
+
reshape(dims) {
|
|
387
|
+
return new Tensor(this.type, this.data, dims);
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
export {
|
|
391
|
+
Tensor
|
|
392
|
+
};
|
|
393
|
+
//# sourceMappingURL=tensor-impl.js.map
|