Skip to content

Commit

Permalink
Update mediapipe.js (#1109)
Browse files Browse the repository at this point in the history
  • Loading branch information
lutzroeder committed Jul 16, 2023
1 parent cd7697a commit cff2e40
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 196 deletions.
288 changes: 93 additions & 195 deletions source/mediapipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,145 +31,107 @@ mediapipe.ModelFactory = class {

mediapipe.Model = class {

constructor(root) {
this._graphs = [ new mediapipe.Graph(root) ];
}

get format() {
return 'MediaPipe';
}

get graphs() {
return this._graphs;
constructor(config) {
this.format = 'MediaPipe';
this.graphs = [ new mediapipe.Graph(config) ];
}
};

mediapipe.Graph = class {

constructor(root) {
this._inputs = [];
this._outputs = [];
this._nodes = [];

if (root) {
if (root.input_stream) {
const inputs = Array.isArray(root.input_stream) ? root.input_stream : [ root.input_stream ];
for (const input of inputs) {
const parts = input.split(':');
const type = (parts.length > 1) ? parts.shift() : '';
const name = parts.shift();
this._inputs.push(new mediapipe.Argument(name, [
new mediapipe.Value(name, type, null)
]));
}
}
if (root.output_stream) {
const outputs = Array.isArray(root.output_stream) ? root.output_stream : [ root.output_stream ];
for (const output of outputs) {
const parts = output.split(':');
const type = (parts.length > 1) ? parts.shift() : '';
const name = parts.shift();
this._outputs.push(new mediapipe.Argument(name, [
new mediapipe.Value(name, type, null)
]));
}
}
if (root.input_side_packet) {
const inputs = Array.isArray(root.input_side_packet) ? root.input_side_packet : [ root.input_side_packet ];
for (const input of inputs) {
const parts = input.split(':');
const type = (parts.length > 1) ? parts.shift() : '';
const name = parts.shift();
this._inputs.push(new mediapipe.Argument(name, [
new mediapipe.Value(name, type, null)
]));
}
}
if (root.output_side_packet) {
const outputs = Array.isArray(root.output_side_packet) ? root.output_side_packet : [ root.output_side_packet ];
for (const output of outputs) {
const parts = output.split(':');
const type = (parts.length > 1) ? parts.shift() : '';
const name = parts.shift();
this._outputs.push(new mediapipe.Argument(output, [
new mediapipe.Value(name, type, null)
]));
}
}
if (root.node) {
const nodes = Array.isArray(root.node) ? root.node : [ root.node ];
for (const node of nodes) {
this._nodes.push(new mediapipe.Node(node));
constructor(config) {
config = config || {};
this.inputs = [];
this.outputs = [];
this.nodes = [];
const types = new Map();
const type = (list) => {
list = list ? Array.isArray(list) ? list : [ list ] : [];
return list.map((item) => {
const parts = item.split(':');
const name = parts.pop();
const type = parts.join(':');
if (!types.has(name)) {
const value = new Set();
if (type) {
value.add(type);
}
types.set(name, value);
} else if (type && !types.get(name).has(type)) {
types.get(name).add(type);
}
}
return name;
});
};
config.input_stream = type(config.input_stream);
config.output_stream = type(config.output_stream);
config.input_side_packet = type(config.input_side_packet);
config.output_side_packet = type(config.output_side_packet);
config.node = config.node ? Array.isArray(config.node) ? config.node : [ config.node ] : [];
for (const node of config.node) {
node.input_stream = type(node.input_stream);
node.output_stream = type(node.output_stream);
node.input_side_packet = type(node.input_side_packet);
node.output_side_packet = type(node.output_side_packet);
}
const values = new Map();
for (const entry of types) {
const type = Array.from(entry[1]).join(',');
values.set(entry[0], new mediapipe.Value(entry[0], type || null));
}
const value = (name) => {
return values.get(name);
};
for (const name of config.input_stream) {
const argument = new mediapipe.Argument(name, [ value(name) ]);
this.inputs.push(argument);
}
for (const name of config.output_stream) {
const argument = new mediapipe.Argument(name, [ value(name) ]);
this.outputs.push(argument);
}
for (const name of config.input_side_packet) {
const argument = new mediapipe.Argument(name, [ value(name, type) ]);
this.inputs.push(argument);
}
for (const output of config.output_side_packet) {
const parts = output.split(':');
const type = (parts.length > 1) ? parts.shift() : '';
const name = parts.shift();
const argument = new mediapipe.Argument(name, [ value(name, type) ]);
this.outputs.push(argument);
}
for (const node of config.node) {
this.nodes.push(new mediapipe.Node(node, value));
}
}

get inputs() {
return this._inputs;
}

get outputs() {
return this._outputs;
}

get nodes() {
return this._nodes;
}
};

mediapipe.Node = class {

constructor(node) {
constructor(node, value) {
const type = node.calculator || '?';
this._type = { name: type.replace(/Calculator$/, '') };
this._inputs = [];
this._outputs = [];
this._attributes = [];

this.name = '';
this.type = { name: type.replace(/Calculator$/, '') };
this.inputs = [];
this.outputs = [];
this.attributes = [];
if (node.input_stream) {
const args = [];
const inputs = Array.isArray(node.input_stream) ? node.input_stream : [ node.input_stream ];
for (const input of inputs) {
const parts = input.split(':');
const type = (parts.length > 1) ? parts.shift() : '';
const name = parts.shift();
args.push(new mediapipe.Value(name, type, null));
}
this._inputs.push(new mediapipe.Argument('input_stream', args));
const values = node.input_stream.map((name) => value(name));
const argument = new mediapipe.Argument('input_stream', values);
this.inputs.push(argument);
}
if (node.output_stream) {
const args = [];
const outputs = Array.isArray(node.output_stream) ? node.output_stream : [ node.output_stream ];
for (const output of outputs) {
const parts = output.split(':');
const type = (parts.length > 1) ? parts.shift() : '';
const name = parts.shift();
args.push(new mediapipe.Value(name, type, null));
}
this._outputs.push(new mediapipe.Argument('output_stream', args));
const values = node.output_stream.map((name) => value(name));
this.outputs.push(new mediapipe.Argument('output_stream', values));
}
if (node.input_side_packet) {
const args = [];
const inputs = Array.isArray(node.input_side_packet) ? node.input_side_packet : [ node.input_side_packet ];
for (const input of inputs) {
const parts = input.split(':');
const type = (parts.length > 1) ? parts.shift() : '';
const name = parts.shift();
args.push(new mediapipe.Value(name, type, null));
}
this._inputs.push(new mediapipe.Argument('input_side_packet', args));
const values = node.input_side_packet.map((name) => value(name));
this.inputs.push(new mediapipe.Argument('output_stream', values));
}
if (node.output_side_packet) {
const args = [];
const outputs = Array.isArray(node.output_side_packet) ? node.output_side_packet : [ node.output_side_packet ];
for (const output of outputs) {
const parts = output.split(':');
const type = (parts.length > 1) ? parts.shift() : '';
const name = parts.shift();
args.push(new mediapipe.Value(name, type, null));
}
this._outputs.push(new mediapipe.Argument('output_side_packet', args));
const values = node.output_side_packet.map((name) => value(name));
this.outputs.push(new mediapipe.Argument('output_side_packet', values));
}
const options = new Map();
if (node.options) {
Expand All @@ -194,108 +156,44 @@ mediapipe.Node = class {
}
}
const message = new mediapipe.Object(reader);
for (const key of Object.keys(message)) {
options.set(key, message[key]);
for (const entry of Object.entries(message)) {
options.set(entry[0], entry[1]);
}
}
} else {
for (const entry of node_options) {
for (const key of Object.keys(entry)) {
if (key !== '__type__') {
options.set(key, entry[key]);
for (const option of node_options) {
for (const entry of Object.entries(option)) {
if (entry[0] !== '__type__') {
options.set(entry[0], entry[1]);
}
}
}
}
for (const pair of options) {
this._attributes.push(new mediapipe.Attribute(pair[0], pair[1]));
this.attributes.push(new mediapipe.Argument(pair[0], pair[1]));
}
}

get name() {
return '';
}

get type() {
return this._type;
}

get inputs() {
return this._inputs;
}

get outputs() {
return this._outputs;
}

get attributes() {
return this._attributes;
}
};

mediapipe.Attribute = class {

constructor(name, value) {
this._name = name;
this._value = value;
}

get name() {
return this._name;
}

get value() {
return this._value;
}
};

mediapipe.Argument = class {

constructor(name, value) {
this._name = name;
this._value = value;
}

get name() {
return this._name;
}

get value() {
return this._value;
this.name = name;
this.value = value;
}
};

mediapipe.Value = class {

constructor(name, type, initializer) {
constructor(name, type) {
if (typeof name !== 'string') {
throw new mediapipe.Error("Invalid value identifier '" + JSON.stringify(name) + "'.");
}
this._name = name;
this._type = type || null;
this._initializer = initializer || null;
}

get name() {
return this._name;
}

get type() {
if (this._type) {
return this._type;
}
if (this._initializer) {
return this._initializer.type;
}
return null;
}

get initializer() {
return this._initializer;
this.name = name;
this.type = type || null;
}
};


mediapipe.Object = class {

constructor(reader, block) {
Expand Down
2 changes: 1 addition & 1 deletion test/models.js
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ class Target {
this.name = this.type ? this.type + '/' + this.target[0] : this.target[0];
this.measures = new Map([ [ 'name', this.name ] ]);
// TODO #1109 duplicate value name
this.skip1109 = [ 'coreml', 'kmodel', 'mediapipe', 'om', 'openvino' ].includes(this.type);
this.skip1109 = [ 'coreml', 'kmodel', 'om', 'openvino' ].includes(this.type);
}

match(patterns) {
Expand Down

0 comments on commit cff2e40

Please sign in to comment.