0

I have this code:

test_image = image.load_img('dataset/kot/cat.1633.jpg', target_size = (64, 64))
test_image = image.img_to_array('cat.1633.jpg')

I get this error:

File "<string>", line 566, in run_nodebug
File "C:\Users\Nixid\Desktop\ta.py", line 37, in <module>
test_image = image.img_to_array('cat.1633.jpg')
File "C:\Users\Nixid\AppData\Local\Programs\Python\Python35\lib\site- 
packages\keras_preprocessing\image.py", line 423, in img_to_array
x = np.asarray(img, dtype=backend.floatx())
File "C:\Users\Nixid\AppData\Local\Programs\Python\Python35\lib\site- 
packages\numpy\core\numeric.py", line 492, in asarray
return array(a, dtype, copy=False, order=order)
ValueError: could not convert string to float: 'cat.1633.jpg'

How to fix this?

2
  • 2
    Please show the full traceback. Commented Sep 15, 2018 at 11:43
  • Try test_image = image.img_to_array(test_image) instead of test_image = image.img_to_array('cat.1633.jpg') Commented Sep 15, 2018 at 12:28

1 Answer 1

1

Kera's img_to_array() method takes a PIL.Image instance (as returned by load_img()) as its parameter, not the name of a file. So you will need to pass it the test_image you loaded in the first line:

from keras.preprocessing import image

test_image = image.load_img('dataset/kot/cat.1633.jpg', target_size=(64, 64))
test_image = image.img_to_array(test_image)

Hth :) dtk

PS As commented above, it's generally a good idea to paste the whole error message, including a stack trace, as - with some practice - that makes it much easier to understand what's going on. Also, showing the imports that are required to quickly reproduce the problem will make debugging way nicer 👍

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.