这是我当前运行良好的Flask代码,它从客户端接收一个带有图像的POST请求,通过模型运行它(基于这个GH:https://github.com/matterport/Mask_RCNN),然后将一个被屏蔽的图像发送回客户端。
但是,它从Configuration文件加载模型并为每个请求加载权重,这需要很长时间。我想在服务器启动时加载模型和权重,并将其传递给index函数。我试过其他问题的答案,但没有成功。我想知道这是不是因为我加载了一个模型,然后加权,而不是仅仅加载一个h5模型文件?
How do I load a file on initialization in a flask application Run code after flask application has started
Flask应用:
from flask import Flask, jsonify, request
import base64
import cv2
import numpy as np
from Configuration import create_model
app = Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == "POST":
# Load the image sent from the client
imagefile = request.files['image'].read() # Type: bytes
jpg_as_np = np.frombuffer(imagefile, dtype=np.uint8) # Convert to numpy array
img = cv2.imdecode(jpg_as_np, flags=1) # Decode from numpy array to opencv object - This is an array
### Enter OpenCV/Tensorflow below ###
model = create_model()
image = img[..., ::-1]
# Detect objects
r = model.detect([image], verbose=0)[0]
REDACTED VISUALIATION CODE
### ###
string = base64.b64encode(cv2.imencode('.jpg', masked_image)[1]).decode() # Convert back to b64 string ready for json.
return jsonify({"count": str(r["masks"].shape[2]), 'image': string})
if __name__ == "__main__":
app.run()配置:
def create_model():
device = "/cpu:0"
weights_path = "weights.h5"
with tf.device(device):
model = modellib.MaskRCNN(mode="inference", model_dir=weights_path, config=InferenceConfig())
model.load_weights(weights_path, by_name=True)
print("Weights Loaded")
return model发布于 2021-11-05 14:26:59
我使用before_first_request装饰器解决了这个问题。下面是总体结构:
app = Flask(__name__)
@app.before_first_request
def before_first_request_func():
MOODEL WEIGHT LOADING CODE
return model
@app.route('/', methods=['POST'])
def index():
if request.method == "POST":
REDACTED LOADING CODE
# Detect objects
r = model.detect([image], verbose=0)[0]
REDACTED VISUALISATION CODE
string = base64.b64encode(cv2.imencode('.jpg', masked_image)[1]).decode() # Convert back to b64 string ready for json.
return jsonify({"count": str(r["masks"].shape[2]), 'image': string})
if __name__ == "__main__":
app.run()model存储在内存中,稍后可以在检测函数中引用。它可用于每个POST请求,不需要重新加载。
https://stackoverflow.com/questions/69807102
复制相似问题