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