Repository navigation
Expand file tree
/
Copy pathweb_interface.py
More file actions
178 lines (160 loc) · 6.67 KB
/
Copy pathweb_interface.py
File metadata and controls
178 lines (160 loc) · 6.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import gc
import sys
import os
from multiprocessing import Pool
import torch
sys.path.append(os.getcwd() + "/yolov5")
sys.path.append(os.getcwd() + "/SegFormer")
from glob import glob
from flask import Flask, request, jsonify, make_response
from main_bolt_loosen import recog_img_bolt_state
from main_piezometer_reading import recog_img_reading
from main_oil_level import recog_img_oil_level
app = Flask(__name__)
app.config.from_object(__name__)
app.config["JSON_AS_ASCII"] = False
@app.route("/api/bolt_loosen", methods=["POST"])
def bolt_loosen():
data = request.json
keys = list(data.keys())
error_warn_info = ""
if "img_root" in keys:
img_root = data["img_root"]
else:
error_warn_info += "错误: 缺少参数img_root. "
if "save_path" in keys:
save_path = data["save_path"]
else:
error_warn_info += "警告: 缺少参数save_path,使用默认值./tmp_results/bolt. "
save_path = "./tmp_results/bolt"
if "thresh_angle" in keys:
thresh_angle = float(data["thresh_angle"])
else:
error_warn_info += "警告: 缺少参数thresh_angle,使用默认值5. "
thresh_angle = 8
if "thresh_dist" in keys:
thresh_dist = float(data["thresh_dist"])
else:
error_warn_info += "警告: 缺少参数thresh_dist,使用默认值5. "
thresh_dist = 8
if "device" in keys:
device = data["device"]
else:
error_warn_info += "警告: 缺少参数device,使用默认值cuda. "
device = "cuda"
if "show_temp" in keys:
show_temp = bool(data["show_temp"])
else:
error_warn_info += "警告: 缺少参数show_temp,使用默认值,False. "
show_temp = False
res_dict = {}
res_dict["error_warn_info"] = error_warn_info
if "错误" in error_warn_info:
res_dict["results"] = []
else:
abso_img_paths = sorted(glob("%s/*" % img_root))
pool = Pool(1)
results = pool.apply(recog_img_bolt_state, args=(abso_img_paths, save_path, thresh_angle,
thresh_dist, device, show_temp))
pool.close()
pool.join()
res_dict["results"] = results
response = make_response(jsonify(res_dict))
response.headers["Content-Type"] = "application/json;charset=UTF-8"
gc.collect()
return response
@app.route("/api/piezometer_reading", methods=["POST"])
def piezometer_reading():
data = request.json
keys = list(data.keys())
error_warn_info = ""
if "img_root" in keys:
img_root = data["img_root"]
else:
error_warn_info += "错误: 缺少参数img_root. "
if "save_path" in keys:
save_path = data["save_path"]
else:
error_warn_info += "警告: 缺少参数save_path,使用默认值./tmp_results/piezometer. "
save_path = "./tmp_results/piezometer"
if "kernel_size" in keys:
kernel_size = float(data["kernel_size"])
else:
error_warn_info += "警告: kernel_size,使用默认值1. "
kernel_size = 1
if "device" in keys:
device = data["device"]
else:
error_warn_info += "警告: 缺少参数device,使用默认值cuda. "
device = "cuda"
if "show_temp" in keys:
show_temp = bool(data["show_temp"])
else:
error_warn_info += "警告: 缺少参数show_temp,使用默认值,False. "
show_temp = False
res_dict = {}
res_dict["error_warn_info"] = error_warn_info
if "错误" in error_warn_info:
res_dict["results"] = []
else:
abso_img_paths = sorted(glob("%s/*" % img_root))
pool = Pool(1)
results = pool.apply(recog_img_reading, args=(abso_img_paths, save_path, kernel_size,
device, show_temp))
pool.close()
pool.join()
res_dict["results"] = results
response = make_response(jsonify(res_dict))
response.headers["Content-Type"] = "application/json;charset=UTF-8"
return response
@app.route("/api/oil_level_reading", methods=["POST"])
def oil_level_reading():
data = request.json
keys = list(data.keys())
error_warn_info = ""
if "img_root" in keys:
img_root = data["img_root"]
else:
error_warn_info += "错误: 缺少参数img_root. "
if "save_path" in keys:
save_path = data["save_path"]
else:
error_warn_info += "警告: 缺少参数save_path,使用默认值./tmp_results/oil_level. "
save_path = "./tmp_results/oil_level"
if "device" in keys:
device = data["device"]
else:
error_warn_info += "警告: 缺少参数device,使用默认值cuda. "
device = "cuda"
if "show_temp" in keys:
show_temp = bool(data["show_temp"])
else:
error_warn_info += "警告: 缺少参数show_temp,使用默认值,False. "
show_temp = False
res_dict = {}
res_dict["error_warn_info"] = error_warn_info
if "错误" in error_warn_info:
res_dict["results"] = []
else:
abso_img_paths = sorted(glob("%s/*" % img_root))
pool = Pool(1)
results = pool.apply(recog_img_oil_level, args=(abso_img_paths, save_path, device, show_temp))
pool.close()
pool.join()
res_dict["results"] = results
response = make_response(jsonify(res_dict))
response.headers["Content-Type"] = "application/json;charset=UTF-8"
return response
if __name__ == '__main__':
"""
1. 启动服务器
cd 至代码根目录
python web_interface.py
2. 客户端接口调用
螺丝松动api调用: curl -X POST http://10.10.3.99:9999/api/bolt_loosen -d "{\"img_root\": \"/home/dykj/work/IndustryQualityTesting/test_imgs/bolt1\", \"save_path\": \"/home/dykj/work/IndustryQualityTesting/tmp_save/bolt1\", \"thresh_angle\": 8, \"thresh_dist\": 8, \"device\": \"cuda\", \"show_temp\": 1}" -H "Content-Type:application/json"
表计读数api调用: curl -X POST http://10.10.3.99:9999/api/piezometer_reading -d "{\"img_root\": \"/home/dykj/work/IndustryQualityTesting/test_imgs/piezometer1\", \"save_path\": \"/home/dykj/work/IndustryQualityTesting/tmp_save/piezometer1\", \"kernel_size\": 1, \"device\": \"cuda\", \"show_temp\": 1}" -H "Content-Type:application/json"
油液读数api调用: curl -X POST http://10.10.3.99:9999/api/oil_level_reading -d "{\"img_root\": \"/home/dykj/work/IndustryQualityTesting/test_imgs/oil_level1\", \"save_path\": \"/home/dykj/work/IndustryQualityTesting/tmp_save/oil_level1\", \"device\": \"cuda\", \"show_temp\": 1}" -H "Content-Type:application/json"
0: False, 1: True
"""
torch.multiprocessing.set_start_method('spawn')
app.run(host="0.0.0.0", port=9999, debug=True)