简体   繁体   English

无法从文件夹中读取 bmp 图像,OpenCV (python)

[英]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.

相关问题 无法从 D 驱动器 [OpenCV] [Python] 读取或写入图像 - Can't read or write images from D Drive [OpenCV] [Python] 如何在 Python 中使用 opencv 从文件夹中读取图像序列(im1、im2、...)? - How can I read a sequence of images (im1, im2, ...) from a folder using opencv in Python? OpenCV 和 Python Flask 如何从文件夹和 ZF7B44CFAFD5C62223D7BZA2E 中读取图像到网站? - How can OpenCV with Python Flask read images from folder and stream them to website? 我尝试使用 OpenCv 为带有 python 的图像文件夹中的图像制作幻灯片,但它不适用于我 - I tried to make a slideshow for images from an images folder with python using OpenCv but it doesn't work with me 如何使用Python和OpenCV从目录中读取图像? - How to read images from a directory with Python and OpenCV? 如果 OpenCV 与 pip 一起安装,有没有办法从“samples”文件夹中读取图像? - Is there a way to read the images from the “samples” folder if OpenCV was installed with pip? Python/OpenCV - 如何按字母顺序从文件夹中加载所有图像 - Python/OpenCV - how to load all images from folder in alphabetical order Python - Opencv 读取 qr 图像 - Python - Opencv to read qr images OpenCV VideoCapture无法从流中读取 - OpenCV VideoCapture can't read from stream 如何在 python 中读取 bmp - How to read bmp in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM