Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

[PythonAPI] RGBtoNV12 and BGRtoNV12 Python API (#38674)

Mostafa8b228a5d0124
diff --git a/src/bindings/python/src/openvino/opset17/__init__.py b/src/bindings/python/src/openvino/opset17/__init__.py
index 04e8bff1cc8..367d747ceb4 100644
--- a/src/bindings/python/src/openvino/opset17/__init__.py
+++ b/src/bindings/python/src/openvino/opset17/__init__.py
@@ -3,8 +3,10 @@
# SPDX-License-Identifier: Apache-2.0
# New operations added in Opset17
+from openvino.opset17.ops import bgr_to_nv12
from openvino.opset17.ops import erfinv
from openvino.opset17.ops import grouped_matmul
+from openvino.opset17.ops import rgb_to_nv12
# Operators from previous opsets
# TODO (ticket: 179247): Add previous opset operators at the end of opset17 development
diff --git a/src/bindings/python/src/openvino/opset17/ops.py b/src/bindings/python/src/openvino/opset17/ops.py
index 9c466a39258..5de149061b9 100644
--- a/src/bindings/python/src/openvino/opset17/ops.py
+++ b/src/bindings/python/src/openvino/opset17/ops.py
@@ -63,3 +63,45 @@ def grouped_matmul(
"GroupedMatMul",
as_nodes(*inputs, name=name),
)
+
+
+@nameable_op
+def rgb_to_nv12(
+ arg: NodeInput,
+ single_plane: bool = True,
+ name: Optional[str] = None,
+) -> Node:
+ """Return a node which performs RGBtoNV12 operation.
+
+ :param arg: The node providing data for the operation in RGB format (NHWC layout).
+ :param single_plane: If True, output is single plane NV12 [N, H*3/2, W, 1].
+ If False, output has two planes: Y [N, H, W, 1] and UV [N, H/2, W/2, 2].
+ :param name: The optional name for the created output node.
+ :return: The new node performing RGBtoNV12 operation.
+ """
+ return _get_node_factory_opset17().create(
+ "RGBtoNV12",
+ as_nodes(arg, name=name),
+ {"single_plane": single_plane},
+ )
+
+
+@nameable_op
+def bgr_to_nv12(
+ arg: NodeInput,
+ single_plane: bool = True,
+ name: Optional[str] = None,
+) -> Node:
+ """Return a node which performs BGRtoNV12 operation.
+
+ :param arg: The node providing data for the operation in BGR format (NHWC layout).
+ :param single_plane: If True, output is single plane NV12 [N, H*3/2, W, 1].
+ If False, output has two planes: Y [N, H, W, 1] and UV [N, H/2, W/2, 2].
+ :param name: The optional name for the created output node.
+ :return: The new node performing BGRtoNV12 operation.
+ """
+ return _get_node_factory_opset17().create(
+ "BGRtoNV12",
+ as_nodes(arg, name=name),
+ {"single_plane": single_plane},
+ )
diff --git a/src/bindings/python/tests/test_graph/test_create_op.py b/src/bindings/python/tests/test_graph/test_create_op.py
index b812d15b9b8..0e5672a9c14 100644
--- a/src/bindings/python/tests/test_graph/test_create_op.py
+++ b/src/bindings/python/tests/test_graph/test_create_op.py
@@ -14,6 +14,7 @@ import openvino.opset5 as ov_opset5
import openvino.opset10 as ov_opset10
import openvino.opset15 as ov_opset15
import openvino.opset16 as ov_opset16
+import openvino.opset17 as ov_opset17
import openvino.opset11 as ov
from openvino.op.util import VariableInfo, Variable
@@ -1944,6 +1945,48 @@ def test_nv12_to_rgb():
assert list(node_separate_planes.get_output_shape(0)) == expected_output_shape
+def test_rgb_to_nv12():
+ # Single plane
+ arg = ov.parameter([1, 480, 640, 3], name="input", dtype=np.float32)
+ node_single_plane = ov_opset17.rgb_to_nv12(arg)
+
+ assert node_single_plane.get_type_name() == "RGBtoNV12"
+ assert node_single_plane.get_output_size() == 1
+ assert node_single_plane.get_output_element_type(0) == Type.f32
+ assert list(node_single_plane.get_output_shape(0)) == [1, 720, 640, 1]
+
+ # Two planes
+ node_two_planes = ov_opset17.rgb_to_nv12(arg, single_plane=False)
+
+ assert node_two_planes.get_type_name() == "RGBtoNV12"
+ assert node_two_planes.get_output_size() == 2
+ assert node_two_planes.get_output_element_type(0) == Type.f32
+ assert node_two_planes.get_output_element_type(1) == Type.f32
+ assert list(node_two_planes.get_output_shape(0)) == [1, 480, 640, 1]
+ assert list(node_two_planes.get_output_shape(1)) == [1, 240, 320, 2]
+
+
+def test_bgr_to_nv12():
+ # Single plane
+ arg = ov.parameter([1, 480, 640, 3], name="input", dtype=np.float32)
+ node_single_plane = ov_opset17.bgr_to_nv12(arg)
+
+ assert node_single_plane.get_type_name() == "BGRtoNV12"
+ assert node_single_plane.get_output_size() == 1
+ assert node_single_plane.get_output_element_type(0) == Type.f32
+ assert list(node_single_plane.get_output_shape(0)) == [1, 720, 640, 1]
+
+ # Two planes
+ node_two_planes = ov_opset17.bgr_to_nv12(arg, single_plane=False)
+
+ assert node_two_planes.get_type_name() == "BGRtoNV12"
+ assert node_two_planes.get_output_size() == 2
+ assert node_two_planes.get_output_element_type(0) == Type.f32
+ assert node_two_planes.get_output_element_type(1) == Type.f32
+ assert list(node_two_planes.get_output_shape(0)) == [1, 480, 640, 1]
+ assert list(node_two_planes.get_output_shape(1)) == [1, 240, 320, 2]
+
+
def test_softsign():
input_shape = [2, 4, 8, 16]