Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for py bindings #264

Draft
wants to merge 13 commits into
base: master
Choose a base branch
from
4 changes: 4 additions & 0 deletions .github/workflows/test_accuracy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion examples/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion examples/cpp/asynchronous_api/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include <iomanip>
#include <iostream>
#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/imgcodecs.hpp>
#include <openvino/openvino.hpp>
#include <stdexcept>
#include <string>
Expand Down
2 changes: 1 addition & 1 deletion examples/cpp/synchronous_api/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include <iostream>
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/imgcodecs.hpp>
#include <openvino/openvino.hpp>
#include <stdexcept>
#include <string>
Expand Down
2 changes: 1 addition & 1 deletion tests/cpp/accuracy/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions tests/cpp/accuracy/conftest.py
Original file line number Diff line number Diff line change
@@ -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")
56 changes: 56 additions & 0 deletions tests/cpp/accuracy/test_bindings.py
Original file line number Diff line number Diff line change
@@ -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)
3 changes: 1 addition & 2 deletions tests/cpp/cmake/common.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Loading