[英]can't read bmp images from folder ,OpenCV (python)
I looked at so many sources but I didn't find how to load my.bmp data.我查看了很多来源,但没有找到如何加载 my.bmp 数据。
Here's my code for one image prediction.这是我用于一个图像预测的代码。 This is working.
这是有效的。
import cv2
import numpy as np
print('model{}'.format(r'C:\directory\model_best.pth'))
opencv_net = cv2.dnn.readNetFromONNX('resnet50.onnx')
input_img = cv2.imread('image.bmp')
gray = cv2.cvtColor(input_img, cv2.COLOR_BGR2GRAY)
img2 = np.zeros_like(input_img)
img2[:, :, 0] = gray
img2[:, :, 1] = gray
img2[:, :, 2] = gray
cv2.imwrite('image.bmp', img2)
scale = 1 / 255
input_blob = cv2.dnn.blobFromImage(
image=img2,
scalefactor=scale,
size=(224, 224),
)
preproc_img=input_blob
opencv_net.setInput(preproc_img)
out = opencv_net.forward()
print("OpenCV DNN prediction: \n")
print("* shape: ", out.shape)
test_class_id = np.argmax(out)
confidence = out[0][test_class_id]
print("* class ID: {}".format(test_class_id))
print("* confidence: {:.4f}".format(confidence))
but I have to use my all bmp images from my test images folder.但我必须使用我的测试图像文件夹中的所有 bmp 图像。 I used these codes to use my.bmp images folders:
我使用这些代码来使用 my.bmp 图像文件夹:
IMG_WIDTH=224
IMG_HEIGHT=224
img_folder=r'C:\directory\test'
def create_dataset(img_folder):
img_data_array = []
class_name = []
for dir1 in os.listdir(img_folder):
for file in os.listdir(os.path.join(img_folder, dir1)):
image_path = os.path.join(img_folder, dir1, file)
image = cv2.imread(image_path, cv2.COLOR_BGR2GRAY)
image = cv2.resize(image, (IMG_HEIGHT, IMG_WIDTH), interpolation=cv2.INTER_AREA)
image = np.array(image)
image = image.astype('float32')
image /= 255
img_data_array.append(image)
class_name.append(dir1)
return img_data_array, class_name
img_data, class_name =create_dataset(img_folder)
target_dict={k: v for v, k in enumerate(np.unique(class_name))}
print(target_dict)
target_val= [target_dict[class_name[i]] for i in range(len(class_name))]
print(len(target_val))
This gives:这给出了:
cv2.error: OpenCV(4.5.4) D:\a\opencv-python\opencv-python\opencv\modules\imgproc\src\resize.cpp:4051: error: (-215:Assertion failed) !ssize.empty() in function 'cv::resize
When I put the resize line in a comment, then I did this:当我将调整大小行放在评论中时,我这样做了:
print('model : {}'.format(r'C:\directory\model_best.pth'))
opencv_net = cv2.dnn.readNetFromONNX('resnet50.onnx')
img_dat_array=np.asarray(img_data)
img_dat_array=np.asarray(img_data)
print(img_dat_array)
input_blob = cv2.dnn.blobFromImage(
image=img_dat_array,
size=(224, 224),
)
which gives me img_data_array empty:[nan nan nan... nan nan nan]
这给了我
img_data_array empty:[nan nan nan... nan nan nan]
Simply replace the line:只需替换该行:
image = cv2.imread(image_path, cv2.COLOR_BGR2GRAY)
with:和:
image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.