python - 为什么我在 np.array 时出现重塑错误(图片)
问题描述
所以我正在做一个手写识别项目,它使用 IamDB 可以正常工作。但是,当我尝试自己拍摄手写照片时,出现此错误,知道如何解决吗?我尝试将图片更改为灰度,但它不起作用
images = []
PTH = Path_to_test
#filename = os.path.basename(PTH[0])
filename = PTH[0]
#******************************************************
print (filename)
im = Image.open(filename)
#print ("cu")
cur_width = im.size[0]
cur_height = im.size[1]
# print(cur_width, cur_height)
height_fac = 113 / cur_height
new_width = int(cur_width * height_fac)
size = new_width, 113
imresize = im.resize((size), Image.ANTIALIAS) # Resize so height = 113 while keeping aspect ratio
now_width = imresize.size[0]
now_height = imresize.size[1]
# Generate crops of size 113x113 from this resized image and keep random 10% of crops
avail_x_points = list(range(0, now_width - 113 ))# total x start points are from 0 to width -113
# Pick random x%
factor = 0.1
pick_num = int(len(avail_x_points)*factor)
random_startx = sample(avail_x_points, pick_num)
for start in random_startx:
imcrop = imresize.crop((start, 0, start+113, 113))
images.append(np.asarray(imcrop))
T_test = np.array(images)
print (T_test.shape)
T_test = T_test.reshape(T_test.shape[0], 113, 113, 1)
#convert to float and normalize
T_test = T_test.astype('float32')
T_test /= 255
shuffle(T_test)
print (T_test.shape)
T_test shape 为 (3, 113, 113, 3),是一张黑白的手写图片。这是错误:
34 T_test = np.array(images)
35 print (T_test.shape)
---> 36 T_test = T_test.reshape(T_test.shape[0], 113, 113, 1)
37 #convert to float and normalize
38 T_test = T_test.astype('float32')
ValueError:无法将大小为 114921 的数组重塑为形状 (3,113,113,1) 我也使用
predictions = model.predict(T_test, verbose =1)
所以我不能把它改成 (T_test.shape[0], 113, 113, 3)
解决方案
图像有 3 个通道,因此您需要使用 3 而不是 1:
T_test = T_test.reshape(T_test.shape[0], 113, 113, 3)
推荐阅读
- android - Google Play 计费库 3.0.3 导致内存泄漏
- python - 在更新嵌套文档时查找平均值 mongodb
- html - MaterializeCSS 单选按钮内嵌间隙
- java - 为什么在保存操作时删除了monogo集合文档?
- css - 在 url('data:image/svg+xml') 中使用 CSS 变量
- vue.js - 数据对象无法在 VUE js 3 中的计算属性内求和
- python - os.walk() 问题 - 如何包含 Windows 文件属性错误列为“0 字节”的子目录
- typescript - 如何修复 SyntaxError:无法在模块外使用 import 语句?
- sparql - 在流利过滤器中使用 STRSTARTS
- python - 关闭浏览器选项卡后如何删除烧瓶会话?