Python:如何使用FlaskAPI部署对象检测模型
我在笔记本电脑上使用Tensorflow GPU进行目标检测。 现在,管理团队希望在自己的位置使用URL检查它。 我从未在web上发布/部署过该模型,因为我不是python开发人员,但现在我必须这样做。为此,我尝试阅读了一些Flask的在线教程,但它们没有多大帮助 如何使用Flask API发布模型? 是否有使用Flask在URL上部署对象检测模型的指南/博客/视频 我的项目结构是这样的Python:如何使用FlaskAPI部署对象检测模型,python,flask,data-science,object-detection,Python,Flask,Data Science,Object Detection,我在笔记本电脑上使用Tensorflow GPU进行目标检测。 现在,管理团队希望在自己的位置使用URL检查它。 我从未在web上发布/部署过该模型,因为我不是python开发人员,但现在我必须这样做。为此,我尝试阅读了一些Flask的在线教程,但它们没有多大帮助 如何使用Flask API发布模型? 是否有使用Flask在URL上部署对象检测模型的指南/博客/视频 我的项目结构是这样的 您可以编写一个restful api,它可以与任何其他服务一起使用 对于基于图像的任务,在发出请求时使用ba
您可以编写一个restful api,它可以与任何其他服务一起使用
base64
编码图像总是明智的。这减少了大量的带宽开销test
路由,用于测试服务器是否处于活动状态。最后,另一个路由用于处理带有base64映像的post请求,它将base64映像转换为numpy
数组(便于传递到ML模型)
您可以更改中间部件以使其适合您
ml_app.py
要运行,只需执行以下操作:
python ml_app.py
更多示例:
darkent/yolo:当我开始使用flask进行模型部署时,我参考了DeepLizard的教程:如果这似乎是相关的,我已经编写了一个代码,通过REST api公开模型,--想法是将模型/pickle加载到内存中,每当您得到api调用时,您只需调用模型的序列化版本。-文件-酒店推荐和方法-post行返回预测-预测水晶球()。水晶球预测(输入查询)谢谢,@Yatin请检查我项目结构的附加屏幕截图。我的项目结构基本相同,但不知道如何使用js/HTML/python flask API来处理发布的Hanks@Saurabh请查看我上面的评论请查看我项目结构的附加屏幕截图。我的项目结构基本相同,但不知道如何使用js/HTML/PythonFlaskAPI进行发布
from flask import Flask
from flask_restful import Resource, Api, reqparse
import werkzeug, os
import json
import numpy as np
import base64
class NumpyEncoder(json.JSONEncoder): # useful for sending numpy arrays
def default(self, obj):
if isinstance(obj, np.ndarray):
return obj.tolist()
return json.JSONEncoder.default(self, obj)
app = Flask(__name__)
api = Api(app)
parser = reqparse.RequestParser()
parser.add_argument('file', type=werkzeug.datastructures.FileStorage, location='files')
parser.add_argument('imgb64')
# add other arguments if needed
# test response, check if live
class Test(Resource):
def get(self):
return {'status': 'ok'}
class PredictB64(Resource): # for detecting from base64 images
def post(self):
data = parser.parse_args()
if data['imgb64'] == "":
return {
'data':'',
'message':'No file found',
'status':'error'
}
img = data['imgb64']
#print(img)
br = base64.b64decode(img)
im = np.frombuffer(br, dtype=np.uint8). reshape(-1, 416, 3) # width will be always 416, which is generally the bigger dimension
# reshape with the actual dimension of your image
#print(im.shape)
#print(type(im))
#print(im.shape)
if img:
r = # call your model here
#print(r)
return json.dumps({
'data': json.dumps(list(r)), #(images), # may change based on your output, could be a string too
'message':'darknet processed',
'status':'success'
}, cls=NumpyEncoder)
return {
'data':'',
'message':'Something when wrong',
'status':'error'
}
api.add_resource(Test, '/test')
api.add_resource(PredictB64,'/predict_b64')
if __name__ == '__main__':
app.run(debug=True, host = '0.0.0.0', port = 5000, threaded=True)