09API搭建与课程总结

This commit is contained in:
luzhisheng 2023-05-29 23:05:56 +08:00
parent 6b843d80a3
commit 5ca9ffa036
2 changed files with 52 additions and 0 deletions

View File

@ -0,0 +1,52 @@
# 知识点改写框架api调用
框架目录结构
![属性文本](./img/1.png)
main.py api启动文件
from flask import Flask, request, jsonify
from detect import YOLOv5
from utils.general import cv2
import numpy as np
import base64
app = Flask(__name__)
yolov_5 = YOLOv5()
@app.route("/", methods=["POST"])
def index():
images = request.values.get("images")
input_image = base64.b64decode(images)
imBytes = np.frombuffer(input_image, np.uint8)
iImage = cv2.imdecode(imBytes, cv2.IMREAD_COLOR)
result = yolov_5.infer(iImage)
result = result[0].view(-1).int()
res_dict = {'coordinate': result.tolist()}
print(res_dict)
return jsonify(res_dict)
if __name__ == '__main__':
app.run(
host='127.0.0.1',
port=8888,
debug=True
)
test.py 测试文件
import base64
import requests
with open('16329967796715117.jpg', 'rb') as f:
image = base64.b64encode(f.read())
data = {
'images': image
}
response = requests.post('http://127.0.0.1:8888/', data=data)
print(response.json())

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB