好家伙们从ML开始训练我的第一个分类器。使用带有狗/猫图像的Microsoft数据集。
我尝试的是将图像>数组>操作返回到Imagefile。我尝试了太多的事情,但都做不到。
我使用cv2.imread读取图像(500x500,jpg),并将值保存在列表中。IMG_SIZE = 100
img_array = cv2.imread(os.path.join(path, img), cv2.IMREAD_GRAYSCALE)
new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE))
test_data.append([new_array, class_num])
X_test = []
y_test = []
for features, label in test_data:
X_test.append(features)
y_test.append(label)
X_test = np.array(X_test).reshape(-1, IMG_SIZE, IMG_SIZE, 1)
X_test = X_test / 255
我尝试的就是撤销这些操作。
img_orig = X_test[:]
img_orig = img_orig * 255
img_orig.astype(int)
img_orig = PIL.Image.fromarray(np.uint8(img_orig))
获取以下错误
File "xxx\lib\site-packages\PIL\Image.py", line 2460, in fromarray
mode, rawmode = _fromarray_typemap[typekey]
KeyError: ((1, 1, 1), '|u1')
File "xxx\CatDog_predict.py", line 78, in <module>
img_orig = PIL.Image.fromarray(np.uint8(img_orig))
File "xxx\site-packages\PIL\Image.py", line 2463, in fromarray
raise TypeError("Cannot handle this data type")
TypeError: Cannot handle this data type
我遗漏了什么?
谢谢
发布于 2018-08-24 01:10:20
PIL.Image.fromarray可以容纳0-255的uint8镜像。要进行调试,首先要执行以下操作:
img_orig = X_test[:]
现在您需要验证img_orig.max()<=1.0
和img_orig.min()>=0.0
。您还可以使用img_orig.dtype
验证数据类型是否为float。如果这三个条件为真(您可以使它们为真),您可以这样做
img_orig = img_orig * 255
img_orig = img_orig.astype('uint8')
img_orig = PIL.Image.fromarray(img_orig)
你不需要额外的int转换。
https://stackoverflow.com/questions/51993553
复制