0

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)"

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")
2
  • 1
    ids = np.array(ids) should probably be outside the loop, if needed at all. Generally, re-assignment of a different type to the same variable should involve a double-take. Commented Sep 10, 2020 at 23:43
  • @user2864740. Thanks. it worked now Commented Sep 10, 2020 at 23:47

1 Answer 1

1

You are converting ids into an array after the first iteration. 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. 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])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.