From 793b0c603035808cfd58e19c428c01617211ae5d Mon Sep 17 00:00:00 2001 From: chenxingming <13019562@qq.com> Date: Thu, 20 Apr 2023 15:34:59 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile | 2 -- README.md | 46 +++--------------------- app/server.py | 110 +--------------------------------------------------------- 3 files changed, 5 insertions(+), 153 deletions(-) diff --git a/Dockerfile b/Dockerfile index fa11c4f..dd0ac8f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -11,5 +11,3 @@ RUN pip3 install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/ COPY ./app /app EXPOSE 5000 CMD ["gunicorn", "--bind", ":5000", "server:app"] - - diff --git a/README.md b/README.md index 7f9a518..5b7c73d 100644 --- a/README.md +++ b/README.md @@ -1,14 +1,14 @@ -# 道路病害检测 +# 病害检测 -## 利用了cnn网络和unet网络进行道路裂缝和坑洼图片的检测. +## 利用unet网络进行图片的裂缝检测. ## API 接口 -### 道路裂缝检测接口(U-Net CNN) +### 裂缝检测接口(U-Net CNN) - 请求 -```curl -k -X POST -F 'image=@image_path/ -v http://0.0.0.0:5000/segment ``` +```curl -k -X POST -F 'image=@image_path/ -v http://0.0.0.0:5000/bridge/crack ``` - 返回接口 @@ -17,41 +17,3 @@ | 返回结果 | result | bool | 是否有裂缝 | | 返回图片 | img | string | 图像的base64编码字符串 | - -### 道路坑洼检测接口(R-CNN) - -```curl -k -X POST -F 'image=@image_path/ -v http://0.0.0.0:5000/detect/rcnn ``` - - -- 返回接口 - -| 名称 | 参数 | 类型 | 说明 | -|------|------|-------|-------| -| 返回结果 | result | bool | 是否有坑洼 | -| 返回图片 | img | string | 图像的base64编码字符串 | - - -### 裂缝和坑洼检测接口 - -```curl -k -X POST -F 'image=@image_path/ -v http://0.0.0.0:5000/ ``` - - -- 返回接口 - -| 名称 | 参数 | 类型 | 说明 | -|------|------|--------|------------------| -| 接口编码 | code | int | 0:正常 ; 10001: 异常 | -| 原始图片 | img_src | string | 图像的base64编码字符串 | -| 是否有裂缝 | crack | bool | 是否有裂缝 | -| 是否有坑洼 | pothole | bool | 是否有坑洼 | - - -## 编译说明 - -### x86编译docker - -```docker build -t hpds-road-detection:v1.0 .``` - -### arm64编译docker - -```docker buildx build -t hpds-road-detection-edge:v1.0 . --platform=linux/arm64``` diff --git a/app/server.py b/app/server.py index 062e2fc..04980f6 100644 --- a/app/server.py +++ b/app/server.py @@ -42,45 +42,7 @@ def load_image_into_numpy_array(image): (im_height, im_width, 3)).astype(np.uint8) -@app.route("/detect/rcnn", methods=["POST"]) -def detect_rcnn(): - if flask.request.method == "POST": - if flask.request.files.get("image"): - image = Image.open(flask.request.files["image"]) - image_np = load_image_into_numpy_array(image) - # image_np_expanded = np.expand_dims(image_np, axis=0) - output_dict = run_inference_for_single_image(image_np, detection_graph) - category_index = {0: {"name": "pothole"}, 1: {"name": "pothole"}} - print(output_dict.get('detection_masks')) - i, is_crack = vis_util.visualize_boxes_and_labels_on_image_array( - image_np, - output_dict['detection_boxes'], - output_dict['detection_classes'], - output_dict['detection_scores'], - category_index, - instance_masks=output_dict.get('detection_masks'), - use_normalized_coordinates=True, - line_thickness=8, - skip_scores=True, - skip_labels=True) - 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: - return "Could not find image" - return "Please use POST method" - - -@app.route("/segment", methods=["POST"]) +@app.route("/bridge/crack", methods=["POST"]) def segment(): if flask.request.method == "POST": if flask.request.files.get("image"): @@ -176,75 +138,5 @@ def run_inference_for_single_image(image, graph): return output_dict -@app.route('/', methods=["POST"]) -def index(): - if flask.request.method == "POST": - if flask.request.files.get("image"): - img_src = Image.open(flask.request.files["image"]) - # start crack detection - img_segment = prepare_img(img_src, "segment") - input_data = np.expand_dims(img_segment, axis=0) - input_data = np.float32(input_data) / 255.0 - tflite_interpreter_c.set_tensor(input_details_c[0]['index'], input_data) - tflite_interpreter_c.invoke() - result = tflite_interpreter_c.get_tensor(output_details_c[0]['index']) - result = result > 0.5 - result = result * 255 - mask = np.squeeze(result) - bg = np.asarray(img_segment).copy() - is_crack = False - for i in range(len(mask)): - for j in range(len(mask[i])): - if mask[i][j] > 0: - bg[i][j][0] = 0 - bg[i][j][1] = 0 - bg[i][j][2] = 255 - is_crack = True - - img = Image.fromarray(bg.astype("uint8")) - - # start pothole detection - image_np = load_image_into_numpy_array(img_src) - # image_np_expanded = np.expand_dims(image_np, axis=0) - output_dict = run_inference_for_single_image(image_np, detection_graph) - category_index = {0: {"name": "pothole"}, 1: {"name": "pothole"}} - _, is_pothole = vis_util.visualize_boxes_and_labels_on_image_array( - image_np, - output_dict['detection_boxes'], - output_dict['detection_classes'], - output_dict['detection_scores'], - category_index, - instance_masks=output_dict.get('detection_masks'), - use_normalized_coordinates=True, - line_thickness=8, - skip_scores=True, - skip_labels=True) - raw_bytes = io.BytesIO() - raw_src = io.BytesIO() - img.save(raw_bytes, "JPEG") - img_src.save(raw_src,"JPEG") - raw_bytes.seek(0) - raw_src.seek(0) - img_byte = raw_bytes.getvalue() - img_src_byte = raw_src.getvalue() - img_str = base64.b64encode(img_src_byte) - img_discern = base64.b64encode(img_byte) - - data = { - "code": 0, - "crack": is_crack, - "pothole": is_pothole, - "img_src": img_str.decode('utf-8'), - "img_discern": img_discern.decode('utf-8') - } - return jsonify(data) - else: - data = { - "code": 10001, - "msg": "Could not find image" - } - return jsonify(data) - return "Road Damage Detection" - if __name__ == "__main__": app.run()