-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
163 lines (104 loc) · 3.89 KB
/
main.py
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
from flask import Flask, render_template, request, flash, abort, send_file
from flask_cors import CORS
import base64
import random
import os
import glob
import cv2
import keras_segmentation as seg
from keras import backend as K
import time
import numpy as np
app = Flask(__name__)
app.secret_key = '208f2hdd029duq0isjc0jqwij0d2r0fj02'
CORS(app)
models_list = []
image_count = 0
t = 0
image1 = 0
image2 = 0
def getModels():
global models_list
m = os.listdir('model')
models_list+=m
def getcount():
return len(os.listdir("data/train/"))
@app.route("/")
def index():
return render_template("index.html")
@app.route("/getimages", methods=["GET", "POST"])
def getData():
getModels()
if request.method == "POST":
image1 = request.form.get("image1").split(",")[1]
image2 = request.form.get("image2").split(",")[1]
img1data = base64.b64decode(image1)
img2data = base64.b64decode(image2)
file_name = str(image_count)+".png"
with open("data/train/"+file_name, 'wb') as f:
f.write(img1data)
with open("data/ann/"+file_name, 'wb') as f:
f.write(img2data)
img = cv2.imread("data/ann/"+file_name, 0)
_,t_img = cv2.threshold(img, 100, 1, cv2.THRESH_BINARY)
t_img = cv2.resize(t_img, (224,224))
cv2.imwrite("data/ann/"+file_name, t_img)
img = cv2.imread("data/train/"+file_name, 0)
img = cv2.resize(img, (224,224))
cv2.imwrite("data/train/"+file_name, img)
flash(format("Got the image"),'success')
global image_count
image_count += 1
return render_template("index.html", models = models_list)
@app.route("/getcount")
def test():
real_count = len(os.listdir("data/train"))
return render_template("test.html", count = image_count, real = real_count)
def delt(path):
files = glob.glob(path)
for f in files:
os.remove(f)
@app.route("/delete")
def delete():
delt('data/train/*')
delt('data/ann/*')
return render_template("delete.html")
@app.route("/infer", methods=["GET", "POST"])
def infer():
global image1, image2
if request.method == "POST":
image = request.form.get("image").split(",")[1]
model_name = request.form['model']
imgdata = base64.b64decode(image)
with open("static/input.png", 'wb') as f:
f.write(imgdata)
if model_name in os.listdir('model'):
K.clear_session()
model = seg.models.segnet.mobilenet_segnet(n_classes=2, input_height=224, input_width=224)
model.load_weights("model/"+model_name)
out = model.predict_segmentation(inp = "static/input.png", out_fname= "static/out.png")
original = cv2.imread("static/input.png",0)
#original = cv2.resize(original, (224,224))
out = cv2.imread("static/out.png",0)
_,out2 = cv2.threshold(out, 155, 255, cv2.THRESH_BINARY)
edges = cv2.Canny(out2, 100, 200)
edges = cv2.bitwise_not(edges)
out3 = cv2.bitwise_and(original, original, mask=edges)
out3 = np.hstack((original, out, out3))
cv2.imwrite("static/out2.png", out3)
return render_template("infer.html", models = os.listdir('model'),im1= image1, im2 = image2)
@app.route("/result")
def result():
try:
return send_file("static/out2.png")
except:
abort(404)
@app.route("/train")
def training():
K.clear_session()
model = seg.models.segnet.mobilenet_segnet(n_classes=2, input_height=224, input_width=224)
model.train( train_images = "data/train", train_annotations = "data/ann", checkpoints_path = "model/" , epochs=1 )
model.save_weights('model/mobilent_segnet'+str(time.time()).split('.')[0]+'.h5')
return "training has finished, now you should be able to select new model"
if __name__ == '__main__':
app.run(debug = True, host = '0.0.0.0')