diff --git a/app/__pycache__/server.cpython-36.pyc b/app/__pycache__/server.cpython-36.pyc new file mode 100644 index 0000000..848540d Binary files /dev/null and b/app/__pycache__/server.cpython-36.pyc differ diff --git a/app/crack_model.h5 b/app/crack_model.h5 new file mode 100755 index 0000000..ecfd1a3 Binary files /dev/null and b/app/crack_model.h5 differ diff --git a/app/serve.py b/app/serve.py index 0a9c838..e74903e 100644 --- a/app/serve.py +++ b/app/serve.py @@ -1,6 +1,6 @@ import tensorflow as tf import numpy as np - +import keras.models def serve_unet_model(): TFLITE_MODEL = "../app/UNet_25_Crack.tflite" @@ -25,3 +25,9 @@ def serve_rcnn_model(): od_graph_def.ParseFromString(serialized_graph) tf.import_graph_def(od_graph_def, name='') return detection_graph + + +def serve_bridge_model(): + mp = "../app/crack_model.h5" + model = keras.models.load_model(mp) + return model diff --git a/app/server.py b/app/server.py index 062e2fc..88b9d2d 100644 --- a/app/server.py +++ b/app/server.py @@ -8,8 +8,9 @@ from PIL import Image, ImageDraw import tensorflow as tf import ops as utils_ops import visualization_utils as vis_util +import cv2 -from serve import serve_unet_model +from serve import serve_unet_model, serve_bridge_model from serve import serve_rcnn_model app = Flask(__name__) @@ -222,7 +223,7 @@ def index(): raw_bytes = io.BytesIO() raw_src = io.BytesIO() img.save(raw_bytes, "JPEG") - img_src.save(raw_src,"JPEG") + img_src.save(raw_src, "JPEG") raw_bytes.seek(0) raw_src.seek(0) img_byte = raw_bytes.getvalue() @@ -246,5 +247,95 @@ def index(): return jsonify(data) return "Road Damage Detection" + +@app.route("/predict/bridge", methods=["POST"]) +def bridge(): + if flask.request.method == "POST": + if flask.request.files.get("image"): + pred_data_colr = [] + pred_data_inv = [] + img_src = cv2.imread(flask.request.files["image"], 0) + image_dst = resize_keep_aspect_ratio(img_src, (227, 227)) + bi_inv, colored_img = process_image(image_dst) + pred_data_colr.append(colored_img) + pred_data_inv.append(bi_inv) + final_pred_colr = np.array(pred_data_colr).reshape((len(pred_data_colr), 227, 227, 1)) + final_pred_inv = np.array(pred_data_inv).reshape((len(pred_data_inv), 227, 227, 1)) + is_crack = predict_image_util(final_pred_inv) + image_np = load_image_into_numpy_array(img_src) + img = Image.fromarray(image_np.astype("uint8")) + img = img.resize((128, 128)) + raw_bytes = io.BytesIO() + img.save(raw_bytes, "JPEG") + raw_bytes.seek(0) + img_byte = raw_bytes.getvalue() + img_str = base64.b64encode(img_byte) + data = { + "result": is_crack, + "img": img_str.decode('utf-8') + } + return jsonify(data) + else: + data = { + "code": 10001, + "msg": "Could not find image" + } + return jsonify(data) + return "Bridge Detection" + + +def predict_image_util(final_pred_inv): + model = serve_bridge_model() + img_test = (final_pred_inv[0].reshape((1, 227, 227, 1))) + raw_predicted_label = model.predict(img_test, batch_size=None, verbose=0, steps=None)[0][0] + + predicted_label = 1 + if raw_predicted_label < 0.8: + predicted_label = 0 + + predicted_label_str = 'Crack' + if predicted_label == 0: + predicted_label_str = 'No Crack' + + print('Raw Predicted Label(Numeric): ' + str(raw_predicted_label)) + print('Predicted Label : ' + predicted_label_str) + return predicted_label + + +def process_image(img): + ret, bi_inv = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY_INV) + return bi_inv, img + + +def resize_keep_aspect_ratio(image_src, dst_size): + src_h, src_w = image_src.shape[:2] + dst_h, dst_w = dst_size + + # 判断应该按哪个边做等比缩放 + h = dst_w * (float(src_h) / src_w) # 按照w做等比缩放 + w = dst_h * (float(src_w) / src_h) # 按照h做等比缩放 + + h = int(h) + w = int(w) + + if h <= dst_h: + image_dst = cv2.resize(image_src, (dst_w, int(h))) + else: + image_dst = cv2.resize(image_src, (int(w), dst_h)) + + h_, w_ = image_dst.shape[:2] + + top = int((dst_h - h_) / 2) + down = int((dst_h - h_ + 1) / 2) + left = int((dst_w - w_) / 2) + right = int((dst_w - w_ + 1) / 2) + + value = [0, 0, 0] + border_type = cv2.BORDER_CONSTANT + image_dst = cv2.copyMakeBorder(image_dst, top, down, left, right, border_type, None, value) + + return image_dst + + if __name__ == "__main__": app.run() diff --git a/install.sh b/install.sh new file mode 100755 index 0000000..fc0d0e5 --- /dev/null +++ b/install.sh @@ -0,0 +1,4 @@ +#/bin/bash + +pip3 install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple/ + diff --git a/requirements.txt b/requirements.txt index 80d59c1..c1a0e59 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,7 @@ Flask==1.1.2 -numpy==1.18.4 +numpy==1.19.5 Pillow==7.1.2 six==1.15.0 +tensorflow==2.5.1 +opencv-contrib-python==4.5.3.56 +opencv-python==4.5.3.56