【python】手写数字识别模型api接口调用

写在前面

我用pytorch搭建了一个cnn模型,训练mnist数据集,然后将训练后的模型部署到了线上,并提供一个可供调用的api接口。

成果演示

http://pytorch-cnn-mnist.herokuapp.com/

在这里插入图片描述

接口文档

请求地址

http://pytorch-cnn-mnist.herokuapp.com/predict/

请求方法

post

必要参数

字段类型描述
data字符串图片的base64编码(包含头部信息)

成功响应

字段类型描述
prediction整型手写数字的预测结果
confidence字符串预测结果正确的概率

请求示例

测试图片

在这里插入图片描述

请求代码

import requests, base64, json

with open('test.jpg', 'rb') as f:
    data = base64.b64encode(f.read()).decode()
data = 'data:image/jpeg;base64,'+data
url = 'http://pytorch-cnn-mnist.herokuapp.com/predict/'
res = requests.post(url, data=data).json()

print(json.dumps(res, indent=4))

响应结果

{
	"prediction": 2,
    "confidence": "98.30%"
}

温馨提示

接口仅供调用测试,有次数限制,非法滥用会封IP,请勿用于实际生产环境。

原文地址:https://www.cnblogs.com/ghgxj/p/14219100.html