1.在线web后台flask 服务
cnn_flask_pre.py
-- coding: utf-8 --
“”"
用训练好的CNN模型做预测
模型文件为 cnn-t10-fine-best.model
“”"
from cnn_model_t7 import CNN_MODEL_T7
import os,shutil
from database import update_logotype_30m
from flask import Flask, render_template, jsonify, request, make_response, send_from_directory, abort,Response
import random
import json,os,base64,time,sys
import datetime
#from PictureSearchFaissWeb.picture_classify.cnn_model_t7 import CNN_MODEL_T7
#from PictureSearchFaissWeb.picture_classify.database import update_logotype_30m
model = CNN_MODEL_T7(include_top=True)
app = Flask(__name__)
basedir = os.path.abspath(os.path.dirname(__file__))
print("basedir" ,basedir)
def create_uuid():
nowTime = datetime.datetime.now().strftime("%Y%m%d%H%M%S");
randomNum = random.randint(0, 100);
if randomNum <= 10:
randomNum = str(0) + str(randomNum);
uniqueNum = str(nowTime) + str(randomNum);
return uniqueNum;
@app.route("/health")
def health():
result = {'status': 'UP'}
return Response(json.dumps(result), mimetype='application/json')
@app.route("/getUser")
def getUser():
result = {'username': 'python', 'password': 'python'}
return Response(json.dumps(result), mimetype='application/json')
@app.route("/hello")
def hello():
result = {'username': 'admin', 'password': 'admin'}
return Response(json.dumps(result), mimetype='application/json')
@app.route('/')
@app.route('/upload')
def upload_test():
return render_template('image-classify.html')
@app.route('/classify_upload',methods=['POST'] )
def cnn_pred():
#path = "/opt/service/PictureSearchFaissWeb/src/PictureSearchFaissWeb/picture_classify/classify_upload/2019070907061019.jpg"
category = ['en', 'image', 'mixed_en', 'mixed_zh', 'mixed_zh_en', 'zh', 'zh_en']
image_file = request.files['image']
file_name = create_uuid() +'.' + image_file.name.split('.')[-1]
file_dir = os.path.join(basedir, "classify_upload")
if not os.path.exists(file_dir):
os.makedirs(file_dir)
print('filename' ,file_name)
# 将它用写入一个图片文件即可保存
image_file.save( file_dir+'/' +file_name )
file_path = file_dir +'/' + file_name
print('file_path' ,file_path)
logotype = model.predict_classes(file_path)
print('logotype' ,logotype)
body_data = {}
image_type = category[logotype]
body_data['type']=image_type;
body_json = json.dumps(body_data)
#return jsonify(body_json)
return image_type
if __name__ =='__main__':
app.run('172.16.1.141',8084,debug=True)