代码拉取完成,页面将自动刷新
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2019/9/15 14:27
# @Author : Paulson
# @File : app.py
# @Software: PyCharm
# @define : function
import re
import json
import base64
import numpy as np
import tensorflow.keras as keras
from flask import Flask, render_template, request, jsonify
from tensorflow.keras.preprocessing.image import img_to_array, load_img
# 使用 redis 统计总访问次数,今日访问次数
from redis_util import get_today, get_visit_num_all, get_visit_num_today, inc_visit_num
app = Flask(__name__)
model_file = './model/model.h5'
global model
model = keras.models.load_model(model_file)
@app.route('/')
def index():
inc_visit_num()
response = get_visit_info()
return render_template("index2.html", **response) # 如果没有使用 redis 统计访问次数功能,请使用index.html
@app.route('/predict/', methods=['Get', 'POST'])
def preditc():
inc_visit_num() # 每访问一次,增加访问次数
parseImage(request.get_data())
img = img_to_array(load_img('output.png', target_size=(28, 28), color_mode="grayscale")) / 255.
img = np.expand_dims(img, axis=0)
code = model.predict_classes(img)[0]
response = get_visit_info(int(code))
print(response)
return jsonify(response)
def get_visit_info(code=0):
response = {}
response['code'] = code
response['visits_all'] = get_visit_num_all()
response['visits_today'] = get_visit_num_today()
response['today'] = get_today()
return response
def parseImage(imgData):
imgStr = re.search(b'base64,(.*)', imgData).group(1)
with open('./output.png', 'wb') as output:
output.write(base64.decodebytes(imgStr))
if __name__ == '__main__':
app.run(host="0.0.0.0", port=3335)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。