#-*-coding:utf-8-*-
from flask import Flask,render_template,request,jsonify
from flask_uploads import configure_uploads,UploadSet
import os,base64
import time
import random
import tensorflow as tf
from cnnmodel import cnnModel
import pickle
import numpy as np
from PIL import Image
app = Flask(__name__)
base=os.path.dirname(os.path.abspath(__file__))
base = os.path.join(base,'static')
app.config['UPLOADS_DEFAULT_DEST']=base
photo=UploadSet()
configure_uploads(app,photo)
feed = '1234567890abcdefghijklmnopqrstuvwxyz'
def predict(data):
model = tf.keras.models.load_model('static/cnn_model.h5')
predicton = model.predict(data)
index = tf.math.argmax(predicton[0].numpy)
return label_names_dict[index]
@
app.route('/')
def index():
return render_template('index.html')
@
app.route('/api', methods=['POST'])
def modeltest():
if request.method == "POST":
id = ''.join(random.sample(feed,16))
photo.save(request.files['file'],time.strftime("%Y-%m-%d",time.localtime()),id+'.png')
file ='static/batches.meta'
patch_bin_file = open(file,'rb')
label_names_dict = pickle.load(patch_bin_file)['label_names']
img = Image.open('static/file'+time.strftime("%Y-%m-%d",time.localtime()+'/'+id+'.png')
r,g,b = img.split()
r_arr = np.arry(r)
g_arr = np.arry(g)
b_arr = np.arry(b)
img = np.concatenate((r_arr,_arr,b_arr))
image = img.reshape([1,32,32,3])/255
return predict(image)
if __name__ == "__main__":
app.run()
求大佬指点
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
https://www.v2ex.com/t/632385
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.