簡體   English   中英

ValueError:無法將字符串轉換為float'jpg'Python

[英]ValueError: could not convert string to float 'jpg' Python

我有以下代碼:

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

我收到此錯誤:

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'

如何解決這個問題?

Kera的img_to_array()方法將PIL.Image實例(由load_img()返回)作為其參數,而不是文件名。 因此,您需要將在第一行中加載的test_image傳遞給它:

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如上所述,粘貼整個錯誤消息(包括堆棧跟蹤)通常是一個好主意,因為-通過一些實踐-可以更輕松地了解正在發生的事情。 另外,顯示快速重現問題所需的導入內容將使調試方式更美觀👍

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM