简体   繁体   English

为什么 python 抛出错误:AttributeError: 'numpy.ndarray' object has no attribute 'append'?

[英]Why is python throwing error : AttributeError: 'numpy.ndarray' object has no attribute 'append'?

I am trying to run the following code, but python is throwing the error that numpy has no argument 'append' for the line that says: "ids.append(id)"我正在尝试运行以下代码,但 python 抛出错误,即 numpy 没有参数 'append' 表示以下行:“ids.append(id)”

path = [os.path.join("data", f) for f in os.listdir("data")]
faces = []
ids = []

for image in path :
    img = Image.open(image).convert('L')
    nimg = np.array(img, 'uint8')
    id = int(os.path.split(image)[1].split(".")[1])
    faces.append(nimg)
    ids.append(id)
    ids = np.array(ids)
clf = cv2.face.EigenFaceRecognizer_create()
clf.train(faces, ids)
clf.write("classifier.yml")

You are converting ids into an array after the first iteration.您在第一次迭代后将 id 转换为数组。 When running it, you should have only one item in the array as is.运行它时,数组中应该只有一项。 I would move the line ids = np.array(ids) to outside the for loop.我会将ids = np.array(ids)移到 for 循环之外。 See below:见下文:

path = [os.path.join("data", f) for f in os.listdir("data")]
faces = []
ids = []

for image in path :
    img = Image.open(image).convert('L')
    nimg = np.array(img, 'uint8')
    id = int(os.path.split(image)[1].split(".")[1])
    faces.append(nimg)
    ids.append(id)

ids = np.array(ids)
clf = cv2.face.EigenFaceRecognizer_create()
clf.train(faces, ids)
clf.write("classifier.yml")

If you wanted to truly have it be an array, you would have to use the following styled code, but a list should also be fine until the end.如果你想真正让它成为一个数组,你必须使用下面的样式代码,但一个列表也应该没问题,直到最后。

ids = np.array(ids)
ids = np.append(ids, [id])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM