Skip to content

GComputation

gapiclassOpenCV 5.0.0
import { GComputation } from '@banou/opencv-wasm'

Use after await initOpenCV(). See the initialization and named imports guide.

ARGUMENTSConstructor or factory
CLASSGComputation
RETURN TYPEOwned native handle
Call structure. A void return can still write to destination arguments. The parameter descriptions define inputs, outputs and ownership.

Native object: release it with using or delete(). Factories can return null; check before calling methods.

namespace gapi \addtogroup gapi_main_classes

G-API classes for constructed and compiled graphs. GComputation class represents a captured computation graph. GComputation objects form boundaries for expression code user writes with G-API, allowing to compile and execute it.

G-API computations are defined with input/output data objects. G-API will track automatically which operations connect specified outputs to the inputs, forming up a call graph to be executed. The below example expresses calculation of Sobel operator for edge detection (G = \sqrt{G_x^2 + G_y^2}):

Full pipeline can be now captured with this object declaration:

Input/output data objects on which a call graph should be reconstructed are passed using special wrappers cv::GIn and cv::GOut. G-API will track automatically which operations form a path from inputs to outputs and build the execution graph appropriately.

Note that cv::GComputation doesn't take ownership on data objects it is defined. Moreover, multiple GComputation objects may be defined on the same expressions, e.g. a smaller pipeline which expects that image gradients are already pre-calculated may be defined like this:

The resulting graph would expect two inputs and produce one output. In this case, it doesn't matter if gx/gy data objects are results of cv::gapi::Sobel operators -- G-API will stop unrolling expressions and building the underlying graph one reaching this data objects.

The way how GComputation is defined is important as its definition specifies graph protocol -- the way how the graph should be used. Protocol is defined by number of inputs, number of outputs, and shapes of inputs and outputs.

In the above example, sobelEdge expects one Mat on input and produces one Mat; while sobelEdgeSub expects two Mats on input and produces one Mat. GComputation's protocol defines how other computation methods should be used -- cv::GComputation::compile() and cv::GComputation::apply(). For example, if a graph is defined on two GMat inputs, two cv::Mat objects have to be passed to apply() for execution. GComputation checks protocol correctness in runtime so passing a different number of objects in apply() or passing cv::Scalar instead of cv::Mat there would compile well as a C++ source but raise an exception in run-time. G-API also comes with a typed wrapper cv::GComputationT<> which introduces this type-checking in compile-time.

cv::GComputation itself is a thin object which just captures what the graph is. The compiled graph (which actually process data) is represented by class GCompiled. Use compile() method to generate a compiled graph with given compile options. cv::GComputation can also be used to process data with implicit graph compilation on-the-fly, see apply() for details.

GComputation is a reference-counted object -- once defined, all its copies will refer to the same instance.

See: GCompiled

Constructors and members

static new

Generator overload Generic GComputation constructor.

Constructs a new graph with a given protocol, specified as a flow of operations connecting input/output objects. Throws if the passed boundaries are invalid, e.g. if there's no functional dependency (path) between given outputs and inputs.

Note: Don't construct GProtoInputArgs/GProtoOutputArgs objects directly, use cv::GIn()/cv::GOut() wrapper functions instead.

See: gapi_data_objects

new(inputs: GProtoInputArgs, outputs: GProtoOutputArgs): GComputation;
inputs

Input data vector.

outputs

Output data vector.

Returns

The GComputation result. Release returned native handles with using or delete(), including handles nested in results.

static fromMatrices

Construct an owned native graph computation from matrix input and output node vectors.

fromMatrices(inputs: GMat, outputs: GMat): GComputation | null;
inputs

inputs argument (GMat).

outputs

outputs argument (GMat).

Returns

The GComputation | null result. Release returned native handles with using or delete(), including handles nested in results.

clone

Create another handle to the same native object. This retains the object without copying its pixels or algorithm state; dispose both handles separately.

clone(): this;
Returns

The this result.

compileStreaming

Serial asynchronous frame processing without native worker threads.

compileStreaming(options?: GCompileArgs): GraphStreamHandle;

apply

Execute this native graph using input values in GIn order and return owned outputs in GOut order. The first compilation retains its configuration; create a new computation to change kernels or models. Dispose returned native handles, including handles nested in arrays.

apply(inputs: readonly GraphValue[], args: GCompileArgs): GraphValue[];
2 available overloads
apply(inputs: readonly GraphValue[]): GraphValue[];
apply(inputs: readonly GraphValue[], args: GCompileArgs): GraphValue[];
inputs

inputs argument (readonly GraphValue[]).

args

args argument (GCompileArgs).

Returns

The GraphValue[] result. Release returned native handles with using or delete(), including handles nested in results.

applyWithMetadata

Execute the graph with signed 64-bit sequence and timestamp metadata attached to every input. Outputs are owned by the caller. Timestamps use microseconds; metadata graph operations should refer to input nodes.

applyWithMetadata(inputs: readonly GraphValue[], metadata: GraphFrameMetadata, _2: GCompileArgs): GraphValue[];
2 available overloads
applyWithMetadata(inputs: readonly GraphValue[], metadata: GraphFrameMetadata): GraphValue[];
applyWithMetadata(inputs: readonly GraphValue[], metadata: GraphFrameMetadata, _2: GCompileArgs): GraphValue[];
inputs

inputs argument (readonly GraphValue[]).

metadata

metadata argument (GraphFrameMetadata).

_2

2 argument (GCompileArgs).

Returns

The GraphValue[] result. Release returned native handles with using or delete(), including handles nested in results.

These signatures describe this package. Upstream documentation can mention optional backends that are absent from this build. Check runtime compatibility before choosing a backend or file format.