修改接口
This commit is contained in:
parent
dacf0bc15a
commit
793b0c6030
|
@ -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"]
|
||||
|
||||
|
||||
|
|
46
README.md
46
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```
|
||||
|
|
110
app/server.py
110
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()
|
||||
|
|
Loading…
Reference in New Issue