ValueError: setting an array element with a sequence.

asked 2019-01-01 12:55:36 -0500

oja gravatar image

I took a bunch of images converted them into gray then stored them into img_data_list then i converted them to a numpy array everything works fine untill now here's part of code that works

import cv2
import os
import numpy as np

people = ['s1', 's2']
num_classes = 2
img_data_list = []
labels = []
valid_images = [".jpg",".gif",".png"]

for index, person in enumerate(people):
  print(index)
  dir_path = 'I:\\project_face_detection\\face_detetion\\training_data\\' + person
  for img_path in os.listdir(dir_path):
    name, ext = os.path.splitext(img_path)
    if ext.lower() not in valid_images:
        continue

    img_data = cv2.imread(dir_path + '\\' + img_path)
    # convert image to gray
    img_data=cv2.cvtColor(img_data, cv2.COLOR_BGR2GRAY)
    img_data_list.append(img_data)
    labels.append(index)


img_data = np.array(img_data_list)

but when I try to convert the array into float32 data type I get the ValueError

img_data = img_data.astype('float32')

here's the error

 ValueError: setting an array element with a sequence.

here's how my images are stored

- training_data
   - s1
       - 0.jpg
       - 1.jpg
   - s2
      - 0.jpg
      - ......

If you guys can help me out in any way I'd be grateful thanks for looking into it :) .

edit retag flag offensive close merge delete

Comments

Try this:

img_data = img_data.astype('float32')

to

img_data = img_data.astype(np.float32)
supra56 gravatar imagesupra56 ( 2019-01-02 09:35:54 -0500 )edit

@supra56 I am getting the same error

oja gravatar imageoja ( 2019-01-02 09:46:51 -0500 )edit

@oja. Sorry, I left out something.

img_data = img_data.astype(np.float32)
img = np.ones((25,25), np.uint8)
img_list = [img] * 4
img_list = [img.astype(np.float32) for i in img_list]
supra56 gravatar imagesupra56 ( 2019-01-02 11:45:30 -0500 )edit

@supra56 still getting the same error

oja gravatar imageoja ( 2019-01-04 01:46:16 -0500 )edit