append() 问题 - AttributeError: 'numpy.ndarray' 对象没有属性 'append'

问题描述 投票:0回答:1

我有错误 AttributeError: 'numpy.ndarray' 对象没有属性 'append' 而我没有定义任何数组:

X = list()
Y = list()

for i, cat in enumerate(classes):

    for image in images:
        img = cv2.imread(image)
        #img1 = cv2.resize(img, (IMG_SIZE, IMG_SIZE))
 
        #X = append(X,cv2.resize(img, (IMG_SIZE, IMG_SIZE)))
        #X = np.append(X,cv2.resize(cv2.imread(image), (IMG_SIZE, IMG_SIZE)))
        #X = np.append(X, img1)
        X.append(cv2.resize(img, (IMG_SIZE, IMG_SIZE)))
        Y.append(i)

正如您在这里所看到的,我将 X 和 Y 都定义为列表。没有任何数组的定义。

我尝试纠正它(您可以看到注释的代码行),但循环永远不会结束(图像大约为 5000)。 IMG_SIZE = 256

请帮忙!

python append attributeerror
1个回答
0
投票

问题可能与线路有关,

X.append(cv2.resize(img, (IMG_SIZE, IMG_SIZE)))

选项_1:通过调试(例如使用打印语句)检查“img”的数据类型

for image in images:
    img = cv2.imread(image)
    print(type(img))  # Option-1
    X.append(cv2.resize(img, (IMG_SIZE, IMG_SIZE)))
    Y.append(i)

Option_2:确保其np.ndarray或list

img = cv2.imread(image)
img = img.tolist()  # Convert numpy ndarray to a list
X.append(cv2.resize(img, (IMG_SIZE, IMG_SIZE)))
Y.append(i)

我希望这些选项会有所帮助!

© www.soinside.com 2019 - 2024. All rights reserved.