【发布时间】:2020-05-18 18:24:43
【问题描述】:
我想使用烧瓶创建我的 ML 模型的 API。 该模型只是简单地获取图像并对其进行分类。 我想上传我的模型的 pickle 文件,并希望用户在运行时动态上传图像文件作为我的功能运行的参数。
以下是我尝试过的代码 sn-p,但存在一些问题,因为我无法从硬盘中检索文件:
from flask import Flask, request
import requests
import os
from tensorflow import keras as tfk
import matplotlib.pyplot as plt
from PIL import Image
from numpy import asarray
import pickle
app = Flask(__name__)
@app.route("/")
def hello():
return "Image Classifier!"
@app.route('/', defaults={'path': ''})
@app.route('/image_reader/<path:image_path>/<pickle_file>', methods=['GET', 'POST'])
def image_classification(image_path):
if request.json:
data = request.get_json(image_path)
image_path = data.get("image_path")
print(image_path)
r = requests.get(image_path, timeout=60)
# save the image to disk
temp_file = 'tmp/temp.jpg'
f = open(temp_file, "wb")
f.write(r.content)
f.close()
model = pickle.load(pickle_file)
return image_path
else:
return "no json received"
if __name__ == '__main__':
app.run(debug=True)
最初,我只是尝试同时采用上述两个参数,即上传 pickle_file 并将用户的动态文件路径作为参数。
【问题讨论】: