-2

I want to convert a raster image into NumPy array with (224, 224) dimensions before loading the VGG-16 neural network pre-trained on ImageNet. My code is raising error: OpenCV(4.5.1) ../modules/highgui/src/window.cpp:376: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'imshow' error even when the image could be successfully read from path.

from osgeo import gdal
import numpy as np
import sys
from keras.preprocessing import image as image_utils
from keras.applications.imagenet_utils import decode_predictions
from keras.applications.imagenet_utils import preprocess_input
from keras.applications.vgg16 import VGG16
from keras.applications.vgg19 import VGG19
from keras.applications.resnet import ResNet50
from keras.applications.inception_v3 import InceptionV3
from keras.applications.densenet import DenseNet121
import cv2

reg001_path = "../sample_data/mosaic/sample IDs A015-C-202 (Reg1) A001-C-002  (Reg2)_reg001.tif"
reg001 = gdal.Open(reg001_path)
channel_reg001 = np.array(reg001.GetRasterBand(1).ReadAsArray())

# Resize NumPy array to 224x224, the required input dimensions for the neural network
image = channel_reg001.resize((224, 224, 3))

# Expand the dimensions so we can pass it through the network
image = np.expand_dims(channel_reg001, axis=0)
# preprocess the image by subtracting the mean RGB pixel intensity from the ImageNet dataset
image = preprocess_input(image)

# load the VGG16 network pre-trained on the ImageNet dataset
print("[INFO] loading network...")
model = VGG16(weights="imagenet")
# classify the image
print("[INFO] classifying image...")
preds = model.predict(image)
P = decode_predictions(preds)
# loop over the predictions and display the rank-5 predictions +
# probabilities to our terminal
for (i, (imagenetID, label, prob)) in enumerate(P[0]):
    print("{}. {}: {:.2f}%".format(i + 1, label, prob * 100))
# load the image via OpenCV, draw the top prediction on the image,
# and display the image to our screen
orig = cv2.imread(r"reg001_path")
(imagenetID, label, prob) = P[0][0]
cv2.putText(orig, "Label: {}, {:.2f}%".format(label, prob * 100), (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 0), 2)
cv2.imshow("Classification", orig)
cv2.waitKey(0)
cv2.destroyAllWindows()

Traceback:

> --------------------------------------------------------------------------- error                                     Traceback (most recent call
> last) /tmp/ipykernel_7165/3707729371.py in <module>
>      15 (imagenetID, label, prob) = P[0][0]
>      16 cv2.putText(orig, "Label: {}, {:.2f}%".format(label, prob * 100), (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 0), 2)
> ---> 17 cv2.imshow("Classification", orig)
>      18 cv2.waitKey(0)
>      19 cv2.destroyAllWindows()
> 
> error: OpenCV(4.5.1) ../modules/highgui/src/window.cpp:376: error:
> (-215:Assertion failed) size.width>0 && size.height>0 in function
> 'imshow'

The image could be successfully read:

img = cv2.imread("../sample_data/mosaic/sample IDs A015-C-202 (Reg1) A001-C-002  (Reg2)_reg001.tif" ,0)
print(img)
[[  0   1  24 ...   2  40  25]
 [ 71   2  22 ...   3  23  58]
 [ 82   3  38 ... 147 116   3]
 ...
 [  0   0   0 ...  14   1   0]
 [183  31   0 ...  50  34   0]
 [119   3   8 ...   1  11   3]]
melololo
  • 35
  • 7
  • no, the image **couldn't** be read successfully. the code that throws the error is *different* from the code you claim proves otherwise. `orig` is None. look and you will see. `putText` *doesn't* throw an error with img being None. I just checked that. – Christoph Rackwitz Oct 17 '21 at 10:45
  • So what should I do? How can I edit the code? – melololo Oct 17 '21 at 10:51
  • I changed to `orig = cv2.imread(reg001_path)`, which is not empty. But it still throw the error. – melololo Oct 17 '21 at 10:54

1 Answers1

-1

The image could be successfully read:

img = cv2.imread("../sample_data/mosaic/sample IDs A015-C-202 (Reg1) A001-C-002 (Reg2)_reg001.tif" ,0)

reg001_path = "../sample_data/mosaic/sample IDs A015-C-202 (Reg1) A001-C-002 (Reg2)_reg001.tif"

if so, you probably wanted to load your orig image like:

orig = cv2.imread(reg001_path) # loading the *value* of 'reg001_path'

not like:

orig = cv2.imread(r"reg001_path") # loading the *name* of 'reg001_path'

however, please make it a habit to always check the result of imread() before continuing calculations, as it will return None silently on failure, not throw an exception

last, i'd consider putText() accepting invalid images silently an opencv bug !

berak
  • 1,226
  • 2
  • 5
  • 7