JSONDecodeError:期望值:Keras+Rest API 应用程序的第 1 行第 1 列(字符 0)
Posted
技术标签:
【中文标题】JSONDecodeError:期望值:Keras+Rest API 应用程序的第 1 行第 1 列(字符 0)【英文标题】:JSONDecodeError: Expecting value: line 1 column 1 (char 0) for Keras+Rest API Application 【发布时间】:2020-02-19 05:50:29 【问题描述】:我正在尝试从我的 keras 模型返回 HTTP 响应。
@app.route("/predict", methods=["POST"])
def predict():
# initialize the data dictionary that will be returned from the
# view
data = "success": False
# ensure an image was properly uploaded to our endpoint
if flask.request.method == "POST":
if flask.request.files.get("image"):
# read the image in PIL format
image = flask.request.files["image"].read()
image = Image.open(io.BytesIO(image))
# preprocess the image and prepare it for classification
image = prepare_image(image, target=(224, 224))
proba = model.predict(image)[0]
idx = np.argmax(proba)
label = lb.classes_[idx]
r = "label": label, "probability": float(proba[idx] * 100)
y = json.dumps(r)
# return the data dictionary as a JSON response
return y
if __name__ == "__main__":
print(("* Loading Keras model and Flask starting server..."
"please wait until server has fully started"))
load_my_model()
app.run()
import requests
# initialize the Keras REST API endpoint URL along with the input
# image path
KERAS_REST_API_URL = "http://localhost:5000/my_predict"
IMAGE_PATH = "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 successful
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”)
第一部分运行:
正在加载 Keras 模型和 Flask 启动服务器...请等待服务器完全启动 服务 Flask 应用“main”(延迟加载) 环境:生产 警告:这是一个开发服务器。不要在生产部署中使用它。 请改用生产 WSGI 服务器。 调试模式:关闭 在http://127.0.0.1:5000/ 上运行但下一部分给了我:JSONDecodeError: Expecting value: line 1 column 1 (char 0)
【问题讨论】:
你试过我的答案了吗?需要更多解释吗? 【参考方案1】:您可以查看this,这是一个关于如何通过发布请求发送图像的示例,您不需要发送 json,只需发送图像: 导入cv2
# prepare headers for http request
content_type = 'image/jpeg'
headers = 'content-type': content_type
img = cv2.imread('lena.jpg')
# encode image as jpeg
_, img_encoded = cv2.imencode('.jpg', img)
# send http request with image and receive response
response = requests.post(url, data=img_encoded.tostring(), headers=headers)
# decode response
print(json.loads(response.text))
仅在烧瓶服务器上解码图像:
import cv2
# convert string of image data to uint8
nparr = np.fromstring(flask.request.data, np.uint8)
# decode image
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
# do some fancy processing here....
【讨论】:
以上是关于JSONDecodeError:期望值:Keras+Rest API 应用程序的第 1 行第 1 列(字符 0)的主要内容,如果未能解决你的问题,请参考以下文章
json.decoder.JSONDecodeError:期望值:第 1 行第 1 列(字符 0)python
ubuntu linux上的python:json.decoder.JSONDecodeError:期望值:第2行第6列
Python/Django 请求 JSONDecodeError:期望值:第 1 行第 1 列(字符 0)
json.decoder.JSONDecodeError:期望值:尝试写入json文件时,第1行第1列(字符0)
在反序列化数据的时候报错raise JSONDecodeError("Expecting value", s, err.value) from None json.decode