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

2024-04-19 02:24:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我得到错误,当我试图运行我的程序与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. 比格犬:0.9901
  2. 猎犬:0.0024
  3. 锅:0.0014
  4. 布列塔尼猎犬:0.0013
  5. 蓝勾:0.0011

Tags: theinpyimagerestapijsonurl