-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclassifier.py
More file actions
79 lines (62 loc) · 2.87 KB
/
classifier.py
File metadata and controls
79 lines (62 loc) · 2.87 KB
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
import os, shutil
from flask import Flask, request, redirect, url_for, render_template, Markup
from werkzeug.utils import secure_filename
from tensorflow.keras.models import Sequential, load_model
from PIL import Image
import numpy as np
UPLOAD_FOLDER = "./static/images/"
labels = ["ア","イ", "空欄", "ウ"]
n_class = len(labels)
img_size = 64
app = Flask(__name__)
app.config["UPLOAD_FOLDER"] = UPLOAD_FOLDER
@app.route("/", methods=["GET", "POST"])
def index():
return render_template("index.html")
@app.route("/result", methods=["GET","POST"])
def result():
if request.method == "POST":
files = request.files.getlist('file')
ans = request.form.get('sel')
# ファイルの保存
if os.path.isdir(UPLOAD_FOLDER):
shutil.rmtree(UPLOAD_FOLDER)
os.mkdir(UPLOAD_FOLDER)
table_result = ""
count = 0
for file in files:
count += 1
filename = secure_filename(file.filename) # ファイル名を安全なものに
filepath = os.path.join(UPLOAD_FOLDER, filename)
file.save(filepath)
# 画像の読み込み
image = Image.open(filepath)
image = image.convert("RGB")
image = image.resize((img_size, img_size))
x = np.array(image, dtype=float)
x = x.reshape(1, img_size, img_size, 3) / 255
# 予測:小さい文字にも対応
model = load_model("./image_classifier2.h5")
y = model.predict(x)[0]
sorted_idx = np.argsort(y)[::-1] # 降順でソート
id = "<td>" + str(count) + "</td>"
img = "<td><img src=" + filepath + "></td>"
first_index = sorted_idx[0]
first_ratio = y[first_index]
first_label = labels[first_index]
first_result = '<td><p style="font-size: 30px; font-weight: bold;">' + first_label + "</p><small>(" + str(round(first_ratio*100, 1)) + "%)</small></td>"
second_index = sorted_idx[1]
second_ratio = y[second_index]
second_label = labels[second_index]
second_result = "<td><p>" + second_label + "</p><small>(" + str(round(second_ratio*100, 1)) + "%)</small></td>"
if first_label == ans:
result = '<td style="font-size: 30px; font-weight: bold;">〇</td>'
table_result += ("<tr>" + id + img + first_result + second_result + result + "</tr>")
else:
result = '<td style="font-size: 30px;">X</td>'
table_result += ('<tr style="background-color: #EEEEEE;">' + id + img + first_result + second_result + result + "</tr>")
return render_template("result.html", result=Markup(table_result), ans=Markup(ans))
else:
return redirect(url_for("index"))
if __name__ == "__main__":
app.run(debug=True)