json.decoder.JSONDecodeError:期望值:第1行第1列(字符0)python请求

时间:2019-03-27 19:03:40

标签: python python-requests

当我尝试使用API​​运行程序时出现错误。像这样

Traceback (most recent call last):
  File "/Users/hanson/Desktop/LEARNING/Python/model-test/simple-keras-rest-api-master/simple_request.py", line 17, in <module>
    r = requests.post(KERAS_REST_API_URL, files=payload).json()
  File "/Users/hanson/ICP6/lib/python3.6/site-packages/requests/models.py", line 897, in json
    return complexjson.loads(self.text, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/__init__.py", line 354, in loads
    return _default_decoder.decode(s)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/decoder.py", line 339, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/decoder.py", line 357, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

这是我的pyfile的源代码

import requests

# initialize the Keras REST API endpoint URL along with the input
# image path
KERAS_REST_API_URL = "http://localhost:5000/predict"
IMAGE_PATH = "/Users/hanson/Desktop/LEARNING/Python/model-test/simple-keras-rest-api-master/dog.jpg"

# load the input image and construct the payload for the request
image = open(IMAGE_PATH, "rb").read()
payload = {"image": image}

# submit the request
r = requests.post(KERAS_REST_API_URL, files=payload).json()

# ensure the request was sucessful
if r["success"]:
    # loop over the predictions and display them
    for (i, result) in enumerate(r["predictions"]):
        print("{}. {}: {:.4f}".format(i + 1, result["label"],
            result["probability"]))

# otherwise, the request failed
else:
    print("Request failed")

请求的API应该是这样的

  1. beagle:0.9901
  2. 步行猎犬:0.0024
  3. 锅:0.0014
  4. Brittany_spaniel:0.0013
  5. bluetick:0.0011

0 个答案:

没有答案