这是我已实现以测试POST API调用的测试功能。 我添加了一个带有图像链接的数组,不过我需要循环。
import requests
import json
from keras.preprocessing.image import img_to_array, array_to_img
from keras.preprocessing import image
flowers = ['c:/flower_photos/daisy/107592979_aaa9cdfe78_m.jpg', 'c:flower_photos/daisy/134409839_71069a95d1_m.jpg', 'c:/flower_photos/daisy/144099102_bf63a41e4f_n.jpg','c:/flower_photos/daisy/154332674_453cea64f4.jpg']
for x in flowers:
image = img_to_array(image.load_img(x, target_size=(128,128))) / 255
payload = {
"signature_name": "" ,
"instances": [{'image': image.tolist()}]
}
print("sending request...")
r = requests.post('http://localhost:8501/v1/models/flowers/versions/1:predict', json=payload)
print(r.content)
第一次迭代运行良好,但随后失败,并显示错误“ AttributeError:'numpy.ndarray'对象没有属性'load_img'”。什么是属性错误?
C:\flowers>client.py
Using TensorFlow backend.
sending request...
b'{\n "predictions": [[0.895928, 0.0106584, 0.000519791, 0.092504, 0.000389838]\n ]\n}'
Traceback (most recent call last):
File "C:\flowers\client.py", line 8, in <module>
image = img_to_array(image.load_img(x, target_size=(128,128))) / 255
AttributeError: 'numpy.ndarray' object has no attribute 'load_img'
C:\flowers>cmd \k
Microsoft Windows [Version 10.0.17763.253]
(c) 2018 Microsoft Corporation. All rights reserved.
如何用POST请求循环该数组?
答案 0 :(得分:1)
我相信这是因为第8行中的变量名称使您不知所措keras.preprocessing.image
。替换它应该可以解决您的问题。
答案 1 :(得分:0)
inp = load_img(file,target_size=(256,256))
inp = img_to_array(inp)
使用这样的功能。无需单独调用“ image.AttrName”。