Compare commits

..

4 Commits

Author SHA1 Message Date
chenxingming 3e5023dcff modify bridge 2023-05-14 18:19:40 +08:00
chenxingming 8ffdf7fb50 包名修改 2023-04-20 14:11:55 +08:00
chenxingming cff79151d9 桥梁 docker 打包 2023-04-19 18:12:40 +08:00
chenxingming 1d5450054f x86 2023-04-19 17:41:59 +08:00
10 changed files with 116 additions and 27 deletions

View File

@ -1,10 +1,12 @@
FROM python:3.7.16
FROM tensorflow/tensorflow:2.5.1
RUN pip install gunicorn==20.1.0
RUN pip install setuptools==46.1.3
RUN apt-get install make g++ gcc
RUN pip3 install gunicorn
RUN pip3 install setuptools==46.1.3
RUN mkdir -p /app
WORKDIR /app
COPY requirements.txt /app
RUN pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/

View File

@ -1,16 +0,0 @@
FROM nvcr.io/nvidia/l4t-tensorflow:r32.6.1-tf2.5-py3
RUN apt-get install make g++ gcc
RUN pip3 install gunicorn
RUN pip3 install setuptools==46.1.3
RUN mkdir -p /app
WORKDIR /app
COPY requirements.txt /app
RUN pip3 install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/
COPY ./app /app
EXPOSE 5000
CMD ["gunicorn", "--bind", ":5000", "server:app"]

Binary file not shown.

BIN
app/crack_model.h5 Executable file

Binary file not shown.

View File

@ -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

View File

@ -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()

View File

@ -1,3 +1,3 @@
# /usr/bash
docker build --tag hpds-road-detection:1.0.0 .
docker build --tag hpds-bridge-detection:1.0.0 .

View File

@ -1,13 +1,13 @@
version: "3.6"
services:
hpds-python-model:
container_name: hpds-road-detection-model
image: hpds-road-detection:1.0.0
hpds-bridge-detection-model:
container_name: hpds-bridge-detection-model
image: hpds-bridge-detection:1.0.0
networks:
- hpds-network
restart: always
ports:
- "8000:5000"
- "8002:5000"
volumes:
- /usr/local/cuda/lib64:/usr/local/cuda/lib64

4
install.sh Executable file
View File

@ -0,0 +1,4 @@
#/bin/bash
pip3 install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple/

View File

@ -3,3 +3,5 @@ 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