gpt4 book ai didi

python - 为什么当我 np.array (图像)时出现 reshape 错误

转载 作者:行者123 更新时间:2023-11-30 09:42:28 25 4
gpt4 key购买 nike

所以我正在开发一个手写识别项目,它使用 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 形状为(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: cannot reshape array of size 114921 into shape (3,113,113,1) I also use

predictions = model.predict(T_test, verbose =1)

所以我无法将其更改为 (T_test.shape[0], 113, 113, 3)

最佳答案

图像有 3 个 channel ,因此您需要使用 3 个而不是 1 个:

T_test = T_test.reshape(T_test.shape[0], 113, 113, 3)

关于python - 为什么当我 np.array (图像)时出现 reshape 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56963268/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com