modify bridge
This commit is contained in:
		
							parent
							
								
									8ffdf7fb50
								
							
						
					
					
						commit
						3e5023dcff
					
				
										
											Binary file not shown.
										
									
								
							
										
											Binary file not shown.
										
									
								
							|  | @ -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 | ||||
|  |  | |||
|  | @ -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() | ||||
|  |  | |||
|  | @ -0,0 +1,4 @@ | |||
| #/bin/bash | ||||
| 
 | ||||
| pip3 install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple/ | ||||
| 
 | ||||
|  | @ -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 | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue