HTTP模型推理服务接口
更新时间:2024-05-16
说明
EdgeBoard开发管理平台V2.0提供HTTP模型推理服务功能,满足用户在不使用摄像头的情况下,通过在HTTP服务接口上传图片进行模型推理并获取结果。
HTTP服务接口
base64图片接口
POST /api/model/service/base64/mid=n
mid为模型ID
Request Header:Content-Type: application/json
Request:
{
"image": "xxxx",//图片base64字符串,不带data:image/jpg:base,头
"show":false //可选参数,是否返回结果图。选true时返回渲染后的图片(base64),其它无效值或为空则默认false。
}
Response:
{
"code": 0, //错误码
"data": {
"result": [ //结果数据
{
"extended": {//非必须字段,不同的模型可携带不同的信息
},
"index": 0, //分类信息
"label": "face_mask", //标签信息
"location": { //坐标信息,height和width为检测框的高和宽,left和top为左上角坐标值
"height": 361.66253662109375,
"left": 171.09437561035156,
"top": 113.68801879882812,
"width": 275.0394592285156
},
"score": 0.9814045429229736 //置信度
}
],
"image":"xxxx"//图片base64数据,非必须字段,可关闭
},
"msg": "success"
}
调用示例
import os
import requests
import base64
import json
def post_image_base64(url):
headers = {
'Content-Type':'application/json'
}
image_file = "image_file/face.jpeg"
with open(image_file, "rb") as f:
image_data = f.read()
image_base64 = str(base64.b64encode(image_data), encoding = 'utf-8')
#print(image_base64)
data = {
"image":image_base64,
"show":False
}
data_json = json.dumps(data)
response = requests.post(url, data = data_json, headers = headers)
result_json = response.json()
print(json.dumps(result_json, ensure_ascii=False, indent=4))
if __name__ == '__main__':
post_base64_url = 'http://192.168.1.116:9876/api/model/service/base64?mid=10'
post_image_base64(post_base64_url)
二进制图片接口
图片文件以form表单的形式提交请求。
POST /api/model/service/binary?mid=n?show=0
mid:模型ID
show:是否回传图片,1开启,0关闭
Request Headers: Content-Type: image/jpeg, Content-Length:1024(图片长度)
Request Body: 图片数据
Response
{
"code": 0, //错误码
"data": {
"result": [ //结果数据
{
"extended": {//非必须字段,不同的模型可携带不同的信息
},
"index": 0, //分类信息
"label": "face_mask", //标签信息
"location": { //坐标信息,height和width为检测框的高和宽,left和top为左上角坐标值
"height": 361.66253662109375,
"left": 171.09437561035156,
"top": 113.68801879882812,
"width": 275.0394592285156
},
"score": 0.9814045429229736 //置信度
}
],
"image":"xxxx"//图片base64数据,非必须字段,可关闭
},
"msg": "success"
}
接口调用示例
import os
import requests
import base64
import json
def post_image_binary(url):
image_file = "image_file/face.jpeg"
with open(image_file, "rb") as f:
image_data = f.read()
image_len = len(image_data)
#print(image_len)
headers = {
'Content-Type':'image/jpg',
'Content-Length':image_len
}
request_body = image_data
response = requests.post(url, request_body, headers)
result_json = response.json()
print(json.dumps(result_json, ensure_ascii=False, indent=4))
if __name__ == '__main__':
post_binary_url = 'http://192.168.1.116:9876/api/model/service/binary?mid=10&show=0'
post_image_binary(post_binary_url)
多模型推理
如果需要同时使用多个模型进行推理,支持上传一张图片,多个模型分别处理并返回结果。
POST /api/model/service/multipart?show=0
show:是否回传图片,1开启,0关闭
Request Headers: Content-Type: multipart/form-data
Request: 表单形式 file:图片二进制数据 mids:[0,1,...]
Response:
{
"code": 0,
"data": [//多模型推理结果的数组
{
"model_id": 7,//模型ID
"result": [
{
"extended": {
},
"index": 0,
"label": "face_mask",
"location": {
"height": 361.66253662109375,
"left": 171.09437561035156,
"top": 113.68801879882812,
"width": 275.0394592285156
},
"score": 0.9814045429229736
}
],
"image":"xxxx"//图片base64数据,非必须字段,可关闭
}
],
"msg": "success"
}
接口调用示例
import os
import requests
import base64
import json
def post_image_multipart(url):
image_file = "image_file/face.jpeg"
files = {'file':open(image_file, 'rb')}
data = {
"mids":"[10]"
}
response = requests.post(url, files = files, data = data)
result_json = response.json()
print(json.dumps(result_json, ensure_ascii=False, indent=4))
if __name__ == '__main__':
post_multipart_url = 'http://192.168.1.116:9876/api/model/service/multipart?show=0'
post_image_multipart(post_multipart_url)