ValueError: setting an array element with a sequence. while converting np array to float32
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 :) .
python numpy opencv
|
show 6 more comments
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 :) .
python numpy opencv
Instead of applying astype('float32') later on the combined list, try applying the same when adding the image to the list
– Ankur Goel
Jan 1 at 19:23
1
Tell us aboutimg_data
,shape
anddtype
.
– hpaulj
Jan 1 at 20:01
Probably, you have images with different sizes. If so, you cannot do it this way.
– Berriel
Jan 1 at 21:13
@AnkurGoel do you mean "img_data = np.array(img_data_list,dtype='float32')" i tried that it didn't work same error
– abhinay oja
Jan 2 at 13:51
@hpaulj img_data is initially where I stored my images which I later converted into a numpy array when i do img_data.shape i get (876,) I am converting dtype to float32 so i can divide by 255.0 the whole array in the later step
– abhinay oja
Jan 2 at 13:56
|
show 6 more comments
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 :) .
python numpy opencv
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 :) .
python numpy opencv
python numpy opencv
asked Jan 1 at 19:09


abhinay ojaabhinay oja
31
31
Instead of applying astype('float32') later on the combined list, try applying the same when adding the image to the list
– Ankur Goel
Jan 1 at 19:23
1
Tell us aboutimg_data
,shape
anddtype
.
– hpaulj
Jan 1 at 20:01
Probably, you have images with different sizes. If so, you cannot do it this way.
– Berriel
Jan 1 at 21:13
@AnkurGoel do you mean "img_data = np.array(img_data_list,dtype='float32')" i tried that it didn't work same error
– abhinay oja
Jan 2 at 13:51
@hpaulj img_data is initially where I stored my images which I later converted into a numpy array when i do img_data.shape i get (876,) I am converting dtype to float32 so i can divide by 255.0 the whole array in the later step
– abhinay oja
Jan 2 at 13:56
|
show 6 more comments
Instead of applying astype('float32') later on the combined list, try applying the same when adding the image to the list
– Ankur Goel
Jan 1 at 19:23
1
Tell us aboutimg_data
,shape
anddtype
.
– hpaulj
Jan 1 at 20:01
Probably, you have images with different sizes. If so, you cannot do it this way.
– Berriel
Jan 1 at 21:13
@AnkurGoel do you mean "img_data = np.array(img_data_list,dtype='float32')" i tried that it didn't work same error
– abhinay oja
Jan 2 at 13:51
@hpaulj img_data is initially where I stored my images which I later converted into a numpy array when i do img_data.shape i get (876,) I am converting dtype to float32 so i can divide by 255.0 the whole array in the later step
– abhinay oja
Jan 2 at 13:56
Instead of applying astype('float32') later on the combined list, try applying the same when adding the image to the list
– Ankur Goel
Jan 1 at 19:23
Instead of applying astype('float32') later on the combined list, try applying the same when adding the image to the list
– Ankur Goel
Jan 1 at 19:23
1
1
Tell us about
img_data
, shape
and dtype
.– hpaulj
Jan 1 at 20:01
Tell us about
img_data
, shape
and dtype
.– hpaulj
Jan 1 at 20:01
Probably, you have images with different sizes. If so, you cannot do it this way.
– Berriel
Jan 1 at 21:13
Probably, you have images with different sizes. If so, you cannot do it this way.
– Berriel
Jan 1 at 21:13
@AnkurGoel do you mean "img_data = np.array(img_data_list,dtype='float32')" i tried that it didn't work same error
– abhinay oja
Jan 2 at 13:51
@AnkurGoel do you mean "img_data = np.array(img_data_list,dtype='float32')" i tried that it didn't work same error
– abhinay oja
Jan 2 at 13:51
@hpaulj img_data is initially where I stored my images which I later converted into a numpy array when i do img_data.shape i get (876,) I am converting dtype to float32 so i can divide by 255.0 the whole array in the later step
– abhinay oja
Jan 2 at 13:56
@hpaulj img_data is initially where I stored my images which I later converted into a numpy array when i do img_data.shape i get (876,) I am converting dtype to float32 so i can divide by 255.0 the whole array in the later step
– abhinay oja
Jan 2 at 13:56
|
show 6 more comments
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53998195%2fvalueerror-setting-an-array-element-with-a-sequence-while-converting-np-array%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53998195%2fvalueerror-setting-an-array-element-with-a-sequence-while-converting-np-array%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Instead of applying astype('float32') later on the combined list, try applying the same when adding the image to the list
– Ankur Goel
Jan 1 at 19:23
1
Tell us about
img_data
,shape
anddtype
.– hpaulj
Jan 1 at 20:01
Probably, you have images with different sizes. If so, you cannot do it this way.
– Berriel
Jan 1 at 21:13
@AnkurGoel do you mean "img_data = np.array(img_data_list,dtype='float32')" i tried that it didn't work same error
– abhinay oja
Jan 2 at 13:51
@hpaulj img_data is initially where I stored my images which I later converted into a numpy array when i do img_data.shape i get (876,) I am converting dtype to float32 so i can divide by 255.0 the whole array in the later step
– abhinay oja
Jan 2 at 13:56