|
| 1 | +# |
| 2 | +# Copyright (C) 2025 Intel Corporation |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | +# |
| 5 | + |
| 6 | +import pytest |
| 7 | +import json |
| 8 | +from pathlib import Path |
| 9 | + |
| 10 | +import cv2 |
| 11 | + |
| 12 | +from model_api.models import Model |
| 13 | +from py_model_api import ClassificationModel |
| 14 | + |
| 15 | + |
| 16 | +def read_config(models_config: str, model_type: str): |
| 17 | + with open(models_config, "r") as f: |
| 18 | + data = json.load(f) |
| 19 | + for item in data: |
| 20 | + if item["type"] == model_type: |
| 21 | + yield item |
| 22 | + |
| 23 | + |
| 24 | +@pytest.fixture(scope="session") |
| 25 | +def data(pytestconfig) -> str: |
| 26 | + return pytestconfig.getoption("data") |
| 27 | + |
| 28 | + |
| 29 | +@pytest.fixture(scope="session") |
| 30 | +def models_config(pytestconfig) -> str: |
| 31 | + return pytestconfig.getoption("config") |
| 32 | + |
| 33 | + |
| 34 | +@pytest.fixture() |
| 35 | +def classification_configs(models_config: str): |
| 36 | + return read_config(models_config, "ClassificationModel") |
| 37 | + |
| 38 | + |
| 39 | +def test_classification_models(data: str, classification_configs): |
| 40 | + for model_data in classification_configs: |
| 41 | + name = model_data["name"] |
| 42 | + if ".xml" not in name: |
| 43 | + continue |
| 44 | + if name.endswith(".xml") or name.endswith(".onnx"): |
| 45 | + name = f"{data}/{name}" |
| 46 | + |
| 47 | + model = Model.create_model(name, preload=True) |
| 48 | + cpp_model = ClassificationModel.create_model(name, preload=True) |
| 49 | + |
| 50 | + image_path = Path(data) / next(iter(model_data["test_data"]))["image"] |
| 51 | + image = cv2.imread(str(image_path)) |
| 52 | + |
| 53 | + py_result = model(image) |
| 54 | + cpp_result = cpp_model(image) |
| 55 | + |
| 56 | + assert str(py_result) == str(cpp_result) |
0 commit comments