Skip to content

Commit 7061e92

Browse files
Expose Model.get_output_shape() method to Node.js API (openvinotoolkit#23843)
### Details: A method `get_output_shape()` to the Model class in the Node.js API of OpenVINO framework.This method would allow users to retrieve the output shape of a model, which is useful for understanding the model's output data. ### Implemented - [x] add get_output_shape() method on C++ side (src/bindings/js/node/src/model.cpp) - [x] update TypeScript definition (src/bindings/js/node/lib/addon.ts) - [x] create unit test for added functionality using Node.js Test Runner Fixes openvinotoolkit#23568 --------- Co-authored-by: Vishniakov Nikolai <nikolai.vishniakov@intel.com>
1 parent 7e64e41 commit 7061e92

File tree

4 files changed

+27
-1
lines changed

4 files changed

+27
-1
lines changed

src/bindings/js/node/include/model_wrap.hpp

+7
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,13 @@ class ModelWrap : public Napi::ObjectWrap<ModelWrap> {
109109
*/
110110
Napi::Value get_friendly_name(const Napi::CallbackInfo& info);
111111

112+
/**
113+
* @brief Helper function to access model outputs shape.
114+
* @param info Contains information about the environment and passed arguments
115+
* @return Napi::Array containing a shape of requested output.
116+
*/
117+
Napi::Value get_output_shape(const Napi::CallbackInfo& info);
118+
112119
private:
113120
std::shared_ptr<ov::Model> _model;
114121
ov::Core _core;

src/bindings/js/node/lib/addon.ts

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ interface Model {
7676
getOutputSize(): number;
7777
setFriendlyName(name: string): void;
7878
getFriendlyName(): string;
79+
getOutputShape(): number[];
7980
}
8081

8182
interface CompiledModel {

src/bindings/js/node/src/model_wrap.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include "node/include/addon.hpp"
77
#include "node/include/errors.hpp"
8+
#include "node/include/helper.hpp"
89
#include "node/include/node_output.hpp"
910

1011
ModelWrap::ModelWrap(const Napi::CallbackInfo& info)
@@ -23,6 +24,7 @@ Napi::Function ModelWrap::get_class(Napi::Env env) {
2324
InstanceMethod("getOutputSize", &ModelWrap::get_output_size),
2425
InstanceMethod("setFriendlyName", &ModelWrap::set_friendly_name),
2526
InstanceMethod("getFriendlyName", &ModelWrap::get_friendly_name),
27+
InstanceMethod("getOutputShape", &ModelWrap::get_output_shape),
2628
InstanceAccessor<&ModelWrap::get_inputs>("inputs"),
2729
InstanceAccessor<&ModelWrap::get_outputs>("outputs")});
2830
}
@@ -165,3 +167,19 @@ Napi::Value ModelWrap::get_friendly_name(const Napi::CallbackInfo& info) {
165167
const auto friendly_name = _model->get_friendly_name();
166168
return Napi::String::New(env, friendly_name);
167169
}
170+
171+
Napi::Value ModelWrap::get_output_shape(const Napi::CallbackInfo& info) {
172+
if (info.Length() != 1 || !info[0].IsNumber()) {
173+
reportError(info.Env(), "Invalid argument. Expected a single number.");
174+
return info.Env().Undefined();
175+
}
176+
177+
try {
178+
auto idx = info[0].As<Napi::Number>().Int32Value();
179+
auto output = _model->output(idx);
180+
return cpp_to_js<ov::Shape, Napi::Array>(info, output.get_shape());
181+
} catch (const std::exception& e) {
182+
reportError(info.Env(), e.what());
183+
return info.Env().Undefined();
184+
}
185+
}

src/bindings/js/node/tests/model.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,4 @@ describe('Model.getOutputSize()', () => {
111111
it('should return 1 for the default model', () => {
112112
assert.strictEqual(model.getOutputSize(), 1, 'Expected getOutputSize to return 1 for the default model');
113113
});
114-
});
114+
});

0 commit comments

Comments
 (0)