您好,我正在尝试构建一个简单的 API 来接收图像并返回一些推理值。我的文件系统如下:
Mask_RCNN
|-MaskDetector.py
|-FireDetector(class)
fire_app
|-server.py
|-saved_img
如果我打开 python 控制台并运行这个:
import sys
sys.path.insert(0, '../Mask_RCNN')
new FireDetector.FireDetector('../trained_weights')
fire_detector.detect('/home/fctmasterthesis/fire_app/saved_img/image7.jpg')
它返回推理的预期结果:
({'rois': array([[ 238, 170, 988, 1359], (...)
但是如果我在 Flask server.py 中像这样运行它:
from flask import Flask
from flask import render_template
from flask import request, flash, redirect
from werkzeug.utils import secure_filename
import os
import sys
sys.path.insert(0, '../Mask_RCNN')
from MaskDetector import FireDetector
fire_detector = FireDetector('../trained_weights')
UPLOAD_FOLDER = 'saved_img'
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg'])
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/',methods=['GET', 'POST'])
def hello():
if request.method == 'POST':
if 'fire_image' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['fire_image']
if file.filename == '':
flash('No selected file')
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
return fire_detector.detect('/home/fctmasterthesis/fire_app/saved_img/'+'/'+filename)
return render_template('upload.html')
app.run(host='0.0.0.0',port='80')
运行时出现代码 500,错误如下:
ValueError: Tensor Tensor("mrcnn_detection/Reshape_1:0", shape=(1, 100, 6), dtype=float32) is not an element of this graph.
答案1
赶紧跑
model.keras_model._make_predict_function()
紧接着
model.loadweights(...)