From d5f3bfa43e17af54e1f3a4f57640f6a2390dfb60 Mon Sep 17 00:00:00 2001 From: Vitaliy Urusovskij Date: Thu, 19 Jan 2023 10:51:32 +0400 Subject: [PATCH] RGB->Gray C and Py API2.0 (#15050) * Add RGB to GRAY to C API 2.0 * Add RGB to GRAY to Py API 2.0 * ClangFormat + PyFlake8 --- src/bindings/c/docs/api_overview.md | 2 ++ .../c/include/openvino/c/ov_prepostprocess.h | 1 + src/bindings/c/src/ov_prepostprocess.cpp | 1 + src/bindings/c/tests/ov_preprocess_test.cpp | 17 ++++++++++++++ .../graph/preprocess/pre_post_process.cpp | 1 + .../tests/test_graph/test_preprocess.py | 22 +++++++++++++++++++ 6 files changed, 44 insertions(+) diff --git a/src/bindings/c/docs/api_overview.md b/src/bindings/c/docs/api_overview.md index 2adc7cd7956..5d29a011fa2 100644 --- a/src/bindings/c/docs/api_overview.md +++ b/src/bindings/c/docs/api_overview.md @@ -207,6 +207,8 @@ enum colorformat_e { ​ BGR, ///< BGR color format, default in OpenVINO +​ GRAY, ///< GRAY color format + ​ RGBX, ///< RGBX color format with X ignored during inference ​ BGRX, ///< BGRX color format with X ignored during inference diff --git a/src/bindings/c/include/openvino/c/ov_prepostprocess.h b/src/bindings/c/include/openvino/c/ov_prepostprocess.h index 5ff71f52be4..d2df1d6efba 100644 --- a/src/bindings/c/include/openvino/c/ov_prepostprocess.h +++ b/src/bindings/c/include/openvino/c/ov_prepostprocess.h @@ -77,6 +77,7 @@ typedef enum { I420_THREE_PLANES, //!< Image in I420 format represented as separate tensors for Y, U and V planes. RGB, //!< Image in RGB interleaved format (3 channels) BGR, //!< Image in BGR interleaved format (3 channels) + GRAY, //!< Image in GRAY format (1 channel) RGBX, //!< Image in RGBX interleaved format (4 channels) BGRX //!< Image in BGRX interleaved format (4 channels) } ov_color_format_e; diff --git a/src/bindings/c/src/ov_prepostprocess.cpp b/src/bindings/c/src/ov_prepostprocess.cpp index 2c390416bfb..e4390b6027c 100644 --- a/src/bindings/c/src/ov_prepostprocess.cpp +++ b/src/bindings/c/src/ov_prepostprocess.cpp @@ -18,6 +18,7 @@ const std::map color_format_map {ov_color_format_e::I420_THREE_PLANES, ov::preprocess::ColorFormat::I420_THREE_PLANES}, {ov_color_format_e::RGB, ov::preprocess::ColorFormat::RGB}, {ov_color_format_e::BGR, ov::preprocess::ColorFormat::BGR}, + {ov_color_format_e::GRAY, ov::preprocess::ColorFormat::GRAY}, {ov_color_format_e::RGBX, ov::preprocess::ColorFormat::RGBX}, {ov_color_format_e::BGRX, ov::preprocess::ColorFormat::BGRX}}; diff --git a/src/bindings/c/tests/ov_preprocess_test.cpp b/src/bindings/c/tests/ov_preprocess_test.cpp index 0a755c3d004..e8e4746afe8 100644 --- a/src/bindings/c/tests/ov_preprocess_test.cpp +++ b/src/bindings/c/tests/ov_preprocess_test.cpp @@ -301,6 +301,23 @@ TEST_F(ov_preprocess, ov_preprocess_preprocess_steps_convert_color) { OV_EXPECT_OK(ov_preprocess_preprocess_steps_convert_color(input_process, ov_color_format_e::BGR)); } +TEST_F(ov_preprocess, ov_preprocess_preprocess_steps_convert_color_rgb_to_gray) { + OV_EXPECT_OK(ov_preprocess_prepostprocessor_create(model, &preprocess)); + EXPECT_NE(nullptr, preprocess); + + OV_EXPECT_OK(ov_preprocess_prepostprocessor_get_input_info_by_index(preprocess, 0, &input_info)); + EXPECT_NE(nullptr, input_info); + + OV_EXPECT_OK(ov_preprocess_input_info_get_preprocess_steps(input_info, &input_process)); + EXPECT_NE(nullptr, input_process); + + OV_EXPECT_OK(ov_preprocess_input_info_get_tensor_info(input_info, &input_tensor_info)); + EXPECT_NE(nullptr, input_tensor_info); + + OV_EXPECT_OK(ov_preprocess_input_tensor_info_set_color_format(input_tensor_info, ov_color_format_e::RGB)); + OV_EXPECT_OK(ov_preprocess_preprocess_steps_convert_color(input_process, ov_color_format_e::GRAY)); +} + TEST_F(ov_preprocess, ov_preprocess_prepostprocessor_get_output_info) { OV_EXPECT_OK(ov_preprocess_prepostprocessor_create(model, &preprocess)); EXPECT_NE(nullptr, preprocess); diff --git a/src/bindings/python/src/pyopenvino/graph/preprocess/pre_post_process.cpp b/src/bindings/python/src/pyopenvino/graph/preprocess/pre_post_process.cpp index 99210c9cc6d..335f8e8c530 100644 --- a/src/bindings/python/src/pyopenvino/graph/preprocess/pre_post_process.cpp +++ b/src/bindings/python/src/pyopenvino/graph/preprocess/pre_post_process.cpp @@ -453,6 +453,7 @@ static void regenum_graph_ColorFormat(py::module m) { .value("I420_THREE_PLANES", ov::preprocess::ColorFormat::I420_THREE_PLANES) .value("RGB", ov::preprocess::ColorFormat::RGB) .value("BGR", ov::preprocess::ColorFormat::BGR) + .value("GRAY", ov::preprocess::ColorFormat::GRAY) .value("RGBX", ov::preprocess::ColorFormat::RGBX) .value("BGRX", ov::preprocess::ColorFormat::BGRX) .export_values(); diff --git a/src/bindings/python/tests/test_graph/test_preprocess.py b/src/bindings/python/tests/test_graph/test_preprocess.py index d70e5662f34..2432a31f023 100644 --- a/src/bindings/python/tests/test_graph/test_preprocess.py +++ b/src/bindings/python/tests/test_graph/test_preprocess.py @@ -401,6 +401,28 @@ def test_graph_preprocess_steps(algorithm, color_format1, color_format2, is_fail assert op in model_operators +@pytest.mark.parametrize( + ("color_format1", "color_format2", "tensor_in_shape", "model_in_shape"), + [(ColorFormat.RGB, ColorFormat.GRAY, [1, 3, 3, 3], [1, 3, 3, 1]), + (ColorFormat.BGR, ColorFormat.GRAY, [1, 3, 3, 3], [1, 3, 3, 1]), + ]) +def test_graph_preprocess_convert_color(color_format1, color_format2, tensor_in_shape, model_in_shape): + parameter_a = ops.parameter(model_in_shape, dtype=np.float32, name="A") + model = parameter_a + function = Model(model, [parameter_a], "TestFunction") + + custom_processor = PrePostProcessor(function) + inp = custom_processor.input() + inp.tensor().set_color_format(color_format1) + inp.preprocess().convert_color(color_format2) + function = custom_processor.build() + + assert function.get_output_size() == 1 + assert list(function.inputs[0].shape) == tensor_in_shape + assert list(function.get_output_shape(0)) == model_in_shape + assert function.get_output_element_type(0) == Type.f32 + + def test_graph_preprocess_postprocess_layout(): shape = [1, 1, 3, 3] parameter_a = ops.parameter(shape, dtype=np.float32, name="A")