Skip to content

Commit 83e2c06

Browse files
CPP Python bindings accuracy tests (#287)
* Add tests for py bindings * Fix linter * Del outdated OMZ model * Disable yolo python tests * Disable cpp yolo tests as well * Minor fixes in cpp samples * Update cmake in tests * Revert py OV in tests cmake * Use python from venv only * Specify python dir manually * Enable bindings in cpp accuracy tests * Merge ModelBase into ImageModel Example works, but probably some missing parts. * Fix clang linter * Rename ImageModel to BaseModel as opposed to ModelBase as it was. So first it was ImageModel: ModelBase. Then ModelBase merged into ImageModel now rename ImageModel to BaseModel * Python binding compilation test area * Working with openvino from python package. OpenCV is using system opencv though... * Add Dockerfile for building the python bindings from c++ It's ugly, but its just an example to show it works * Working python wheel package with only opencv as dependency Currently the rpath is set to a relative path to the site-packages of python for openvino (../openvino/libs). This is maybe not great. OpenCV does not seem to be an issue * Clean up the Dockerfile.ubuntu for building the python package * Remove rpath setting * Extract opencv dependencies and setup windows part too * Clean up cmakelists, sadly its been broken since last couple commits * Revert "Clean up cmakelists, sadly its been broken since last couple commits" This reverts commit 3fb1326. * Revert "Extract opencv dependencies and setup windows part too" This reverts commit ffc65ea. * Fix cmake file properly now for ubuntu Windows will come * Fixing windows build again * Fix tests for cpp-py so that they run, not working yet Seems one model is causing an issue, either via segmentation fault if trying to catch error, or a nullptr tensor * Try to fix workflow * Remove cpp tests and use venv for building package * Remove the broken test and enable cpp tests again * Clean up a bit * make sure the .so's are included. Apparently scikit uses .gitignore to ignore what to add to the package * Apply feedback, remove docker files * Rename the py project for py bindings to vision api * Debug why model api is not found * Remove OpenCV and TBB from included dependencies for now * Install generated .so to cmake binary dir * Remove run.py --------- Co-authored-by: Vladisalv Sovrasov <[email protected]>
1 parent 3839c88 commit 83e2c06

File tree

18 files changed

+146
-34
lines changed

18 files changed

+146
-34
lines changed

.github/workflows/test_accuracy.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,15 @@ jobs:
4444
pip install typing_extensions==4.12.2
4545
cmake ../tests/cpp/accuracy/
4646
make -j
47+
- name: Build CPP-PY Bindings
48+
run: |
49+
source venv/bin/activate
50+
pip install src/cpp/py_bindings
4751
- name: Run CPP Test
4852
run: |
4953
build/test_accuracy -d data -p tests/python/accuracy/public_scope.json
54+
- name: Run CPP-PY Bindings Test
55+
run: |
56+
source venv/bin/activate
57+
pip list
58+
pytest --data=./data --config=./tests/python/accuracy/public_scope.json tests/cpp/accuracy/test_bindings.py

examples/cpp/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ endmacro()
9292

9393
find_package(OpenCV REQUIRED COMPONENTS imgcodecs)
9494

95-
set (ENABLE_PY_BINDINGS OFF)
9695
add_subdirectory(../../src/cpp ${Samples_BINARY_DIR}/src/cpp)
9796

9897
add_example(NAME asynchronous_api SOURCES ./asynchronous_api/main.cpp DEPENDENCIES model_api)

src/cpp/CMakeLists.txt

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
cmake_minimum_required(VERSION 3.26)
66

7-
option(ENABLE_PY_BINDINGS "Enables building python bindings package" ON)
8-
97
# Multi config generators such as Visual Studio ignore CMAKE_BUILD_TYPE. Multi config generators are configured with
108
# CMAKE_CONFIGURATION_TYPES, but limiting options in it completely removes such build options
119
get_property(GENERATOR_IS_MULTI_CONFIG_VAR GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
@@ -77,10 +75,6 @@ elseif(CMAKE_CXX_COMPILER_ID MATCHES "^GNU|(Apple)?Clang$")
7775
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC")
7876
endif()
7977

80-
if (ENABLE_PY_BINDINGS)
81-
add_subdirectory(py_bindings)
82-
endif()
83-
8478
include(GenerateExportHeader)
8579

8680
generate_export_header(model_api)

src/cpp/py_bindings/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
*.whl
2+
*.dll
3+
*.so*

src/cpp/py_bindings/CMakeLists.txt

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,17 @@
22
# SPDX-License-Identifier: Apache-2.0
33
#
44

5+
cmake_minimum_required(VERSION 3.26)
6+
7+
if(WIN32)
8+
set(CMAKE_GENERATOR_TOOLSET "v142")
9+
endif()
10+
11+
12+
add_subdirectory(../ model_api/cpp)
13+
514
set(Python_FIND_VIRTUALENV FIRST)
15+
project(_vision_api LANGUAGES CXX)
616
find_package(Python COMPONENTS Interpreter Development REQUIRED)
717

818
execute_process(
@@ -11,17 +21,19 @@ execute_process(
1121
find_package(nanobind CONFIG REQUIRED)
1222

1323

14-
file(GLOB BINDINGS_SOURCES ./*.cpp)
15-
file(GLOB BINDINGS_HEADERS ./*.hpp)
24+
file(GLOB BINDINGS_SOURCES src/vision_api/*.cpp)
25+
file(GLOB BINDINGS_HEADERS src/vision_api/*.hpp)
1626

17-
nanobind_add_module(py_model_api NB_STATIC STABLE_ABI LTO ${BINDINGS_SOURCES} ${BINDINGS_HEADERS})
27+
message(INFO ${BINDINGS_SOURCES})
28+
29+
nanobind_add_module(_vision_api NB_STATIC STABLE_ABI LTO ${BINDINGS_SOURCES} ${BINDINGS_HEADERS})
30+
31+
set_target_properties(_vision_api PROPERTIES
32+
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
33+
)
1834

19-
target_link_libraries(py_model_api PRIVATE model_api)
35+
target_link_libraries(_vision_api PRIVATE model_api)
2036

21-
nanobind_add_stub(
22-
py_model_api_stub
23-
MODULE py_model_api
24-
OUTPUT py_model_api.pyi
25-
PYTHON_PATH $<TARGET_FILE_DIR:py_model_api>
26-
DEPENDS py_model_api
37+
install(TARGETS _vision_api
38+
LIBRARY DESTINATION vision_api # Same place relative to package
2739
)

src/cpp/py_bindings/pyproject.toml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
[build-system]
2+
requires = ["scikit-build-core >=0.4.3", "nanobind >=1.3.2"]
3+
build-backend = "scikit_build_core.build"
4+
5+
[project]
6+
name = "vision_api"
7+
version = "0.3.0.2"
8+
requires-python = ">=3.9"
9+
authors = [
10+
{name = "Intel(R) Corporation"},
11+
]
12+
maintainers = [
13+
{name = "Intel(R) Corporation"},
14+
]
15+
description = "Model API: model wrappers and pipelines for inference with OpenVINO"
16+
readme = "../../python/README.md"
17+
classifiers = [
18+
"License :: OSI Approved :: Apache Software License",
19+
"Programming Language :: Python :: 3.9"
20+
]
21+
22+
[project.urls]
23+
Homepage = "https://github.com/open-edge-platform/model_api"
24+
25+
[tool.scikit-build]
26+
# Protect the configuration against future changes in scikit-build-core
27+
minimum-version = "0.4"
28+
# Setuptools-style build caching in a local directory
29+
build-dir = "build/{wheel_tag}"
30+
# Build stable ABI wheels for CPython 3.12+
31+
wheel.py-api = "cp312"
32+
sdist.include = ["*.so*"]
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Copyright (C) 2024 Intel Corporation
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
try:
5+
from openvino import Core
6+
7+
_ = Core() # Triggers loading of shared libs like libopenvino.so
8+
except Exception as e:
9+
raise ImportError(f"Failed to initialize OpenVINO runtime: {e}")
10+
11+
from ._vision_api import ClassificationModel
12+
13+
__all__ = [ClassificationModel]
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)