diff --git a/.github/workflows/test_accuracy.yml b/.github/workflows/test_accuracy.yml index 61c877ca..322a7b84 100644 --- a/.github/workflows/test_accuracy.yml +++ b/.github/workflows/test_accuracy.yml @@ -47,3 +47,7 @@ jobs: - name: Run CPP Test run: | build/test_accuracy -d data -p tests/python/accuracy/public_scope.json + - name: Run CPP-PY Bindings Test + run: | + source venv/bin/activate + PYTHONPATH="$PYTHONPATH:build/model_api/cpp/py_bindings/" pytest --data=./data --config=./tests/python/accuracy/public_scope.json tests/cpp/accuracy/test_bindings.py diff --git a/examples/cpp/CMakeLists.txt b/examples/cpp/CMakeLists.txt index 2fe5a7fd..b1ebab76 100644 --- a/examples/cpp/CMakeLists.txt +++ b/examples/cpp/CMakeLists.txt @@ -92,7 +92,7 @@ endmacro() find_package(OpenCV REQUIRED COMPONENTS imgcodecs) -set (ENABLE_PY_BINDINGS OFF) +set(ENABLE_PY_BINDINGS OFF) add_subdirectory(../../src/cpp ${Samples_BINARY_DIR}/src/cpp) add_example(NAME asynchronous_api SOURCES ./asynchronous_api/main.cpp DEPENDENCIES model_api) diff --git a/examples/cpp/asynchronous_api/main.cpp b/examples/cpp/asynchronous_api/main.cpp index acd361a2..105a5150 100644 --- a/examples/cpp/asynchronous_api/main.cpp +++ b/examples/cpp/asynchronous_api/main.cpp @@ -12,7 +12,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/examples/cpp/synchronous_api/main.cpp b/examples/cpp/synchronous_api/main.cpp index ec4ef869..fe3f4908 100644 --- a/examples/cpp/synchronous_api/main.cpp +++ b/examples/cpp/synchronous_api/main.cpp @@ -14,7 +14,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/tests/cpp/accuracy/CMakeLists.txt b/tests/cpp/accuracy/CMakeLists.txt index c06077ba..2ccbac9a 100644 --- a/tests/cpp/accuracy/CMakeLists.txt +++ b/tests/cpp/accuracy/CMakeLists.txt @@ -65,7 +65,7 @@ include(../cmake/common.cmake) find_package(OpenCV REQUIRED COMPONENTS core highgui videoio imgproc imgcodecs) -set(ENABLE_PY_BINDINGS OFF) +set(ENABLE_PY_BINDINGS ON) add_subdirectory(../../../src/cpp ${tests_BINARY_DIR}/model_api/cpp) add_test(NAME test_accuracy SOURCES test_accuracy.cpp DEPENDENCIES model_api) diff --git a/tests/cpp/accuracy/conftest.py b/tests/cpp/accuracy/conftest.py new file mode 100644 index 00000000..c05e5922 --- /dev/null +++ b/tests/cpp/accuracy/conftest.py @@ -0,0 +1,9 @@ +# +# Copyright (C) 2025 Intel Corporation +# SPDX-License-Identifier: Apache-2.0 +# + + +def pytest_addoption(parser): + parser.addoption("--data", action="store", help="data folder with dataset") + parser.addoption("--config", action="store", help="path to models config") diff --git a/tests/cpp/accuracy/test_bindings.py b/tests/cpp/accuracy/test_bindings.py new file mode 100644 index 00000000..314f2ac6 --- /dev/null +++ b/tests/cpp/accuracy/test_bindings.py @@ -0,0 +1,56 @@ +# +# Copyright (C) 2025 Intel Corporation +# SPDX-License-Identifier: Apache-2.0 +# + +import pytest +import json +from pathlib import Path + +import cv2 + +from model_api.models import Model +from py_model_api import ClassificationModel + + +def read_config(models_config: str, model_type: str): + with open(models_config, "r") as f: + data = json.load(f) + for item in data: + if item["type"] == model_type: + yield item + + +@pytest.fixture(scope="session") +def data(pytestconfig) -> str: + return pytestconfig.getoption("data") + + +@pytest.fixture(scope="session") +def models_config(pytestconfig) -> str: + return pytestconfig.getoption("config") + + +@pytest.fixture() +def classification_configs(models_config: str): + return read_config(models_config, "ClassificationModel") + + +def test_classification_models(data: str, classification_configs): + for model_data in classification_configs: + name = model_data["name"] + if ".xml" not in name: + continue + if name.endswith(".xml") or name.endswith(".onnx"): + name = f"{data}/{name}" + + model = Model.create_model(name, preload=True) + cpp_model = ClassificationModel.create_model(name, preload=True) + + image_path = Path(data) / next(iter(model_data["test_data"]))["image"] + image = cv2.imread(str(image_path)) + + py_result = model(image) + cpp_result = cpp_model(image) + + assert str(py_result) == str(cpp_result) diff --git a/tests/cpp/cmake/common.cmake b/tests/cpp/cmake/common.cmake index 9bb51b75..dff0a621 100644 --- a/tests/cpp/cmake/common.cmake +++ b/tests/cpp/cmake/common.cmake @@ -41,7 +41,6 @@ macro(add_test) target_link_libraries(${TEST_NAME} PRIVATE pthread) endif() - target_link_libraries(${TEST_NAME} PRIVATE gtest gtest_main) - target_link_libraries(${TEST_NAME} PRIVATE nlohmann_json::nlohmann_json) + target_link_libraries(${TEST_NAME} PRIVATE gtest_main gmock_main nlohmann_json::nlohmann_json) endmacro()