创建一个目录以存储输出掩码,以进行图像分割任务
遵循教程 kaggle Notebook尝试创建一个可以将预测掩码存储在文件夹中的函数。尝试以下代码时,我会收到错误'内置函数imread返回null而不设置错误'
请建议对潜在解决方案进行修改或重定向。
import cv2
import os
image_dir = "/content/sample_data/Output"
def pred_images(sample_image, sample_mask, path):
pred_mask = model.predict(sample_image[tf.newaxis, ...])
print(pred_mask.shape)
pred_mask = pred_mask.reshape(img_size[0],img_size[1],1)
img= cv2.imread(pred_mask, 1)
cv2.imwrite(os.path.join(path, '*.png'), img)
for i in range(len(train_dataset)):
for image, mask in TRAIN.take(i):
sample_image, sample_mask= image, mask
pred_images(sample_image, sample_mask, {image_dir})
following the tutorial Kaggle Notebook for Unet, I am trying to create a function that could store the predicted mask in a folder. While trying below codes, I am getting error 'built-in function imread returned NULL without setting an error'
Please suggest a modification or redirect for potential solutions.
import cv2
import os
image_dir = "/content/sample_data/Output"
def pred_images(sample_image, sample_mask, path):
pred_mask = model.predict(sample_image[tf.newaxis, ...])
print(pred_mask.shape)
pred_mask = pred_mask.reshape(img_size[0],img_size[1],1)
img= cv2.imread(pred_mask, 1)
cv2.imwrite(os.path.join(path, '*.png'), img)
for i in range(len(train_dataset)):
for image, mask in TRAIN.take(i):
sample_image, sample_mask= image, mask
pred_images(sample_image, sample_mask, {image_dir})
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的代码中有几个错误:
您正在使用
imread
到底是什么?
cv2.imread
图像文件并将图像读取为变量。也就是说,用实际的像素值“转换” a string 将fileName带入矩阵。
但是,您将
imread
应用于矩阵mask_pred
- 您在那里做什么?这没有任何意义,因此您收到的错误消息。您正在尝试写您的图像,
img
到文件名'/contery/pontent/sample_data/output/*。png'
- - 这不是有效的文件名。您不允许您使用'*'
(还有一些其他特殊字符)名称。此外,您的
路径
参数设置为{image_dir}
- 也就是说,您正在制作 set 带有一个元素(image_dir
),然后尝试尝试使用此 set 作为<代码>路径文件。You have several errors in your code:
You are using
imread
for what exactly?cv2.imread
ismeant to open an image file and read the image into a variable. That is, to "convert" a string with filename into a matrix with the actual pixel values.However, you are applying
imread
to a matrixmask_pred
-- what are you doing there? This makes no sense, thus the error message you got.You are trying to write your image,
img
to a file name'/content/sample_data/Output/*.png'
-- this is NOT a valid file name. You are not allowed to use'*'
(and a few other special characters) in file names.Moreover, your
path
argument is set to{image_dir}
-- that is, you are making a set with one element (image_dir
) and then try and use this set as thepath
for the file.