Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

[Samples] Report an unreadable input image in the Python samples (#39115)

Daksha43d4bce5da4f
diff --git a/samples/python/classification_sample_async/classification_sample_async.py b/samples/python/classification_sample_async/classification_sample_async.py
index 39842fe1e05..6e04f96004c 100755
--- a/samples/python/classification_sample_async/classification_sample_async.py
+++ b/samples/python/classification_sample_async/classification_sample_async.py
@@ -97,15 +97,17 @@ def main() -> int:
model = ppp.build()
# --------------------------- Step 4. Set up input --------------------------------------------------------------------
- # Read input images
- images = (cv2.imread(image_path) for image_path in args.input)
-
- # Resize images to model input dims
+ # Read input images and resize each one to the model input dims right away,
+ # so only the small resized copies stay in memory
_, h, w, _ = model.input().shape
- resized_images = (cv2.resize(image, (w, h)) for image in images)
-
- # Add N dimension
- input_tensors = (np.expand_dims(image, 0) for image in resized_images)
+ input_tensors = []
+ for image_path in args.input:
+ image = cv2.imread(image_path)
+ if image is None:
+ log.error(f'Failed to read the image: {image_path}')
+ return -1
+ # Add N dimension
+ input_tensors.append(np.expand_dims(cv2.resize(image, (w, h)), 0))
# --------------------------- Step 5. Loading model to the device -----------------------------------------------------
log.info('Loading the model to the plugin')
diff --git a/samples/python/hello_classification/hello_classification.py b/samples/python/hello_classification/hello_classification.py
index 4f1c6dff6f2..9b015a00eb9 100755
--- a/samples/python/hello_classification/hello_classification.py
+++ b/samples/python/hello_classification/hello_classification.py
@@ -43,6 +43,9 @@ def main():
# --------------------------- Step 3. Set up input --------------------------------------------------------------------
# Read input image
image = cv2.imread(image_path)
+ if image is None:
+ log.error(f'Failed to read the image: {image_path}')
+ return -1
# Add N dimension
input_tensor = np.expand_dims(image, 0)
diff --git a/samples/python/hello_reshape_ssd/hello_reshape_ssd.py b/samples/python/hello_reshape_ssd/hello_reshape_ssd.py
index 611540903dd..006c60eae31 100755
--- a/samples/python/hello_reshape_ssd/hello_reshape_ssd.py
+++ b/samples/python/hello_reshape_ssd/hello_reshape_ssd.py
@@ -44,6 +44,9 @@ def main():
# --------------------------- Step 3. Set up input --------------------------------------------------------------------
# Read input image
image = cv2.imread(image_path)
+ if image is None:
+ log.error(f'Failed to read the image: {image_path}')
+ return -1
# Add N dimension
input_tensor = np.expand_dims(image, 0)