forked from akshay772/Crack_detection_metal_part_python2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_predict.py
137 lines (113 loc) · 4.48 KB
/
main_predict.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
from flask import Flask, render_template, request
from werkzeug import secure_filename
from PIL import Image
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D, BatchNormalization
from keras.layers import Activation, Dropout, Flatten, Dense
from keras.preprocessing.image import ImageDataGenerator
from keras.models import load_model, model_from_json
import tensorflow as tf
import numpy as np
import cv2
import webcolors
import time, os
import sys, h5py
import shutil
import random
np.random.seed(0)
from image_preprocess import crack_detection_preprocess_image
from image_preprocess import move_files_test_folder
from image_preprocess import preprocess_img
from CNNclassifier import training
os.environ['KMP_DUPLICATE_LIB_OK']='True'
app = Flask(__name__)
# load json and create model
json_file = open('./models/model_92.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
# load model and weights
loaded_model = model_from_json(loaded_model_json)
# loaded_model = load_model("./models/first_try.h5")
loaded_model.load_weights("./models/first_try_92.h5")
# print(loaded_model.summary())
print "Loaded model from disk"
graph = tf.get_default_graph()
# root
@app.route("/")
def index():
"""
this is a root dir of my server
:return: str
"""
return '''This is testing API :
To start predicting, go to http://127.0.0.1:5000/crack_detection_test'''
@app.route('/crack_detection_test', methods = ['GET','POST'])
def crack_detection_test():
class_pred = ''
filenames = ''
filename = ''
# test for input file
if request.method =='POST':
file = request.files['file[]']
# print(file)
if file:
current_dir = os.path.abspath(os.path.dirname(__file__))
filename = secure_filename(file.filename)
if not os.path.exists(os.path.join(current_dir, 'uploads')):
os.makedirs(os.path.join(current_dir, 'uploads'))
filepath = os.path.join(current_dir, 'uploads', filename)
file.save(filepath)
# print(filepath)
img = cv2.imread(filepath)
gray = preprocess_img(filepath)
# new_im = Image.fromarray(gray)
# new_im.show()
# save the test image in test_dir
if not os.path.exists(os.path.join(current_dir, 'uploads', 'test', 'images')):
os.makedirs(os.path.join(current_dir, 'uploads', 'test', 'images'))
test_dir = os.path.join(current_dir, 'uploads', 'test')
# save the test file to test directory
savepath = os.path.join(test_dir, 'images', filename)
# print(filename)
# print(savepath)
cv2.imwrite(savepath, gray)
try :
batch_size = 16
test_datagen = ImageDataGenerator(rescale=1./255)
test_generator = test_datagen.flow_from_directory(
test_dir,
target_size=(512, 512),
batch_size=batch_size,
class_mode='binary',
shuffle=False)
#resetting generator
test_generator.reset()
with graph.as_default():
# make predictions
pred=loaded_model.predict_generator(test_generator, steps=len(test_generator), verbose=1)
# Get classes by np.round
cl = np.round(pred)
# Get filenames (set shuffle=false in generator is important)
filenames=test_generator.filenames
if cl[:,0][0] == np.float32(1.0):
class_pred = 'Healthy'
else :
class_pred = 'Defective'
# 0 is defective, 1 is healthy
print "\nfile : ", filenames ,"\nprediction : ", pred[:,0], "\nclass : ", cl[:,0][0], '\npredicted class : ', class_pred
except Exception as e:
print e
print "Please try again.... something has gone wrong"
finally :
# remove all uploaded files
os.remove(savepath)
os.remove(filepath)
# # evaluate loaded model on test data
# loaded_model.compile(loss='binary_crossentropy',
# optimizer='adam',
# metrics=['accuracy'])
# predict = loaded_model.predict(gray)
# print(predict)
return render_template('file_upload.html', string_variable= class_pred, image_name= filename)
if __name__ == '__main__':
app.run(debug=True)