path是图片的路径 w,h是图片的设定长宽
```def read_img(path):
cate=[path+x for x in os.listdir(path) if os.path.isdir(path+x)]
imgs=[]
labels=[]
for idx,folder in enumerate(cate):
for im in glob.glob(folder+'/*.jpg'):
print('reading the images:%s'%(im))
img=io.imread(im)
img=transform.resize(img,(w,h))
imgs.append(img)
labels.append(idx)
return np.asarray(imgs,np.float32),np.asarray(labels,np.int32)
data,label=read_img(path)
我运行花卉图片加载的时候无错误,但换个路径运行猫狗识别的时候就报错
File "C:/Users/spirit/Desktop/实验练习/tensorflow/猫狗识别/训练模型/猫狗识别.py", line 34, in <module>data,label=read_img(path)
File "C:/Users/spirit/Desktop/实验练习/tensorflow/猫狗识别/训练模型/猫狗识别.py", line 31, in read_img
return np.asarray(imgs,np.float32),np.asarray(labels,np.int32)
File "D:\Anaconda\envs\tensorflow\lib\site-packages\numpy\core\numeric.py", line 501, in asarray
return array(a, dtype, copy=False, order=order)
ValueError: could not broadcast input array from shape (100,100,3) into shape (100,100)
我真心不懂,只是换了其他图片加载,为什么就报错,真心求教!
我在想是不是我的猫狗图片出了问题,但看了也感觉没什么问题啊,头痛
图片问题, 有的图片颜色空间是 Grey 的,imread 读出的数据是(100,100),和正常RGB 图片读出的 (100,100,3)无法合并成同类型数据
可以尝试 一下 io.imread(im, as _grey=true),我是直接把 grey 的图片删除了
可以尝试一下cv2.imread。之前为了防止不能读取中文路径自定义了一个imread函数,结果报了这种错误。改回cv2.imread就可以了,可能是自己写的还不完善吧。