-
Notifications
You must be signed in to change notification settings - Fork 1
/
web.py.bak
executable file
·115 lines (96 loc) · 3.88 KB
/
web.py.bak
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# -*- coding: utf-8 -*-
import os
from werkzeug.utils import secure_filename
from flask import Flask, redirect, url_for, request, render_template, flash
import time
import json
from .src import cnnModel
from .src import util
from .src import dataPreprocess
def st_diagnose(imgPath):
print("predicting: " + imgPath)
x = util.getImageMatrix(imgPath)
x = dataPreprocess.preprocessImgMatrix(x)
x = x.reshape([1] + list(x.shape))
res = []
for i in range(3):
res.append(models[i].predict_on_batch(x).tolist()[0])
# res.append(models[i].predict_on_batch(x))
print(res)
# res = [[0.0, 1.0, 0.0], [0, 0.1, 0.9999, 0.0], [0.0, 1.0]]
return res
# Predictive model path
modelPath = '../models/'
models = []
for i in range(3):
print('loading model '+str(i))
models.append(cnnModel.loadModelFromFile(modelPath + 'dataType' + str(i) + '-epoch45-size256.h5'))
# Uploaded image folder
UPLOAD_FOLDER = 'images/'
ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])
# Flask
app = Flask(__name__, static_folder='', static_url_path='')
# Set up upload folder
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
# Define a function to check if an extension is valid
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/', methods=['GET', 'POST'])
def home():
res = [[0.0, 1.0, 0.0], [0, 0.1, 0.9999, 0.0], [0.0, 1.0]]
labelDict = {0: {0: '舌色偏红', 1: '舌色正常', 2: '舌色偏紫'},
1: {0: '无舌苔', 1: '舌苔正常', 2: '舌苔偏白', 3: '舌苔偏黄'},
2: {0: '无齿痕', 1: '有齿痕'}}
if request.method == 'GET':
return render_template('base.html', results=res, labelDict=labelDict)
elif request.method == 'POST':
# check if the post request has the file part
if 'file' not in request.files:
flash('No file part')
return render_template('base.html', results=res, labelDict=labelDict)
file = request.files['file']
# if user does not select file, browser also
# submit a empty part without filename
if file.filename == '':
flash('No selected file')
return render_template('base.html', results=res, labelDict=labelDict)
if file and allowed_file(file.filename):
# filename = secure_filename(file.filename)
filename = str(time.time()) + '.jpg'
# file_path = 'images/' + filename
# image_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
image_path = '/home/bach/tongue-diag/web/images/' + filename
file.save(image_path)
res = st_diagnose(image_path)
# return redirect(url_for('predict'))
return render_template('base.html', results=res, labelDict=labelDict)
# @app.route('/upload', methods=['GET', 'POST'])
# def upload():
# if request.method == 'GET':
# return render_template('upload.html')
# else:
# file = request.files['file']
# if file:
# name = str(time.time()) + '.jpg'
# srcFile = 'images/' + name
# print(srcFile)
# file.save(srcFile)
# # tgtFile = 'static/shetou1.jpg'
# # util.removeFile(tgtFile)
# # shutil.move(srcFile, tgtFile)
# return '上传成功<script>window.location.href ="index.html?img=' + name + '"</script>'
# # return redirect(url_for('predict'))
@app.route('/predict', methods=['GET'])
def predict():
path = 'images/' + request.args.get('img')
result = st_diagnose(path)
result = json.dumps(result)
print(result)
return result
# imgPath = '/home/bach/tongue-diag/web/images/st.jpg'
# res = st_diagnose(imgPath)
# print(st_diagnose('./images/st.jpg'))
if __name__ == '__main__':
# app.debug = True
app.run(host='0.0.0.0')