[SAMPLES/PYTHON] Fix ACCESS_VIOLATION error when importing cv2 (#13443)

* [PYTHON] CV2 module required only when loading images

* [PYTHON] Update tools/benchmark_tool/openvino/tools/benchmark/utils/inputs_filling.py

Co-authored-by: Helena Kloosterman <helena.kloosterman@intel.com>

* [PYTHON] Make CV import not global

* [PYTHON] Update format of inputs_filling.py

* [PYTHON] Fix missing newline

* Update tools/benchmark_tool/openvino/tools/benchmark/utils/inputs_filling.py

Co-authored-by: Helena Kloosterman <helena.kloosterman@intel.com>

* [BENCHMARK_APP/PYTHON] Introduce exception when module is installed but fails to import

* [SAMPLES/PYTHON] Sporadic access violation fix through checking whether module was cached

Co-authored-by: Helena Kloosterman <helena.kloosterman@intel.com>
Co-authored-by: Michal Lukaszewski <michal.lukaszewski@intel.com>
This commit is contained in:
Piotr Krzemiński 2022-11-11 02:10:20 +01:00 committed by GitHub
parent 3443079a7b
commit 054eea3c5a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,10 +2,12 @@
# SPDX-License-Identifier: Apache-2.0
import os
import sys
import re
import numpy as np
from collections import defaultdict
from pathlib import Path
from importlib.util import find_spec
from openvino.runtime import Tensor, PartialShape
from openvino.runtime.utils.types import get_dtype
@ -13,6 +15,13 @@ from openvino.runtime.utils.types import get_dtype
from .constants import IMAGE_EXTENSIONS, BINARY_EXTENSIONS
from .logging import logger
if find_spec('cv2') is not None:
try:
import cv2
except ImportError as ex:
raise Exception("Failed to import opencv module. " \
"Please try to uninstall opencv-python " \
"and install opencv-python-headless instead.") from ex
class DataQueue:
def __init__(self, input_data: dict, batch_sizes: list):
@ -125,16 +134,11 @@ def get_input_data(paths_to_input, app_input_info):
return DataQueue(data, get_group_batch_sizes(app_input_info))
def get_image_tensors(image_paths, info, batch_sizes):
try:
import cv2
except ModuleNotFoundError as ex:
raise Exception("Loading images requires the opencv-python or opencv-python-headless package. " \
"Please install it before continuing or run benchmark without "\
"the -i flag to fill vectors with random data.") from ex
except ImportError as ex:
raise Exception("Failed to import opencv module. Please try to uninstall opencv-python " \
"and install opencv-python-headless instead.") from ex
def get_image_tensors(image_paths, info, batch_sizes):
if 'cv2' not in sys.modules:
logger.error("Loading images requires the opencv-python or opencv-python-headless package. " \
"Please install it before continuing or run benchmark without "\
"the -i flag to fill vectors with random data.")
processed_frames = 0
widthes = info.widthes if info.is_dynamic else [info.width]