@@ -24,6 +24,13 @@ Loading pyramidbox_lite_server_mask successful.
24
24
这样就完成了一个口罩检测服务化API的部署,默认端口号为8866。
25
25
26
26
## Step2:测试图像生成在线API
27
+ 首先指定编码格式及引入需要的包:
28
+ ``` python
29
+ >> > import requests
30
+ >> > import json
31
+ >> > import base64
32
+ >> > import os
33
+ ```
27
34
我们用来测试的样例图片为:
28
35
29
36
<p align =" center " >
@@ -56,7 +63,7 @@ files = [("image", file_1), ("image", file_2)]
56
63
``` python
57
64
>> > # 指定检测方法为pyramidbox_lite_server_mask并发送post请求
58
65
>> > url = " http://127.0.0.1:8866/predict/image/pyramidbox_lite_server_mask"
59
- >> > r = requests.post(url = url, files = files)
66
+ >> > r = requests.post(url = url, files = files, data = { " visual_result " : " True " } )
60
67
```
61
68
我们可以打印接口返回结果:
62
69
``` python
@@ -67,63 +74,79 @@ files = [("image", file_1), ("image", file_2)]
67
74
" data" : [
68
75
{
69
76
" label" : " MASK" ,
70
- " left" : 455.5180733203888 ,
71
- " right" : 658.8289226293564 ,
72
- " top" : 186.38022020459175 ,
73
- " bottom" : 442.67284870147705 ,
74
- " confidence" : 0.92117363
77
+ " left" : 938.8167103528976 ,
78
+ " right" : 1126.8890985250473 ,
79
+ " top" : 335.8177453279495 ,
80
+ " bottom" : 586.0342741012573 ,
81
+ " confidence" : 0.9775171
75
82
},
76
83
{
77
- " label" : " MASK" ,
78
- " left" : 938.9076416492462 ,
79
- " right" : 1121.0804233551025 ,
80
- " top" : 326.9856423139572 ,
81
- " bottom" : 586.0468536615372 ,
82
- " confidence" : 0.997152
84
+ " label" : " NO MASK" ,
85
+ " left" : 1166.563014626503 ,
86
+ " right" : 1331.2186390161514 ,
87
+ " top" : 298.1251895427704 ,
88
+ " bottom" : 496.373051404953 ,
89
+ " confidence" : 0.6512484
83
90
},
84
91
{
85
- " label" : " NO MASK" ,
86
- " left" : 1166.189564704895 ,
87
- " right" : 1325.6211009025574 ,
88
- " top" : 295.55220007896423 ,
89
- " bottom" : 496.9406336545944 ,
90
- " confidence" : 0.9346678
92
+ " label" : " MASK" ,
93
+ " left" : 458.2292696237564 ,
94
+ " right" : 664.9880893230438 ,
95
+ " top" : 179.45007160305977 ,
96
+ " bottom" : 446.70506715774536 ,
97
+ " confidence" : 0.98069304
91
98
}
92
99
],
93
- " path" : " " ,
100
+ " path" : " family_mask.jpg " ,
94
101
" id" : 1
95
102
},
96
103
{
97
104
" data" : [
98
105
{
99
106
" label" : " MASK" ,
100
- " left" : 1346.7342281341553 ,
101
- " right" : 1593.7974529266357 ,
102
- " top" : 239.36296990513802 ,
103
- " bottom" : 574.6375751495361 ,
104
- " confidence" : 0.95378655
107
+ " left" : 1340.4194090366364 ,
108
+ " right" : 1595.8429119586945 ,
109
+ " top" : 251.97067219018936 ,
110
+ " bottom" : 584.6931987404823 ,
111
+ " confidence" : 0.9681898
105
112
},
106
113
{
107
114
" label" : " MASK" ,
108
- " left" : 840.5126552581787 ,
109
- " right" : 1083.8391423225403 ,
110
- " top" : 417.5169044137001 ,
111
- " bottom" : 733.8856244087219 ,
112
- " confidence" : 0.85434145
115
+ " left" : 839.8990581035614 ,
116
+ " right" : 1084.293223142624 ,
117
+ " top" : 446.8751857280731 ,
118
+ " bottom" : 758.4936121702194 ,
119
+ " confidence" : 0.9673422
120
+ },
121
+ {
122
+ " label" : " NO MASK" ,
123
+ " left" : 1145.4194769859314 ,
124
+ " right" : 1253.0083780288696 ,
125
+ " top" : 128.66552621126175 ,
126
+ " bottom" : 283.0486469864845 ,
127
+ " confidence" : 0.97426504
113
128
}
114
129
],
115
- " path" : " " ,
130
+ " path" : " woman_mask.jpg " ,
116
131
" id" : 2
117
132
}
118
133
]
119
134
```
120
135
根据结果可以看出准确识别了请求图片中的人脸位置及戴口罩确信度。
121
136
122
- pyramidbox_lite_server_mask返回的结果还包括标注检测框的图像的base64编码格式,经过转换可以得到生成图像,代码如下:
137
+ pyramidbox_lite_server_mask返回的结果还包括标注检测框的图像的base64编码格式,经过转换可以得到生成图像。
138
+
139
+ 我们建立一个用于保存结果图片的文件夹:
123
140
``` python
141
+ >> > if not os.path.exists(" output" ):
142
+ >> > os.mkdir(" output" )
143
+ ```
144
+ 然后将图片数据进行解码并保存,代码如下:
145
+ ``` python
146
+ >> > results = eval (r.json()[" results" ])
124
147
>> > for item in results:
125
- ... with open (output_path, " wb" ) as fp:
126
- ... fp.write(base64.b64decode(item[" base64" ].split(' ,' )[- 1 ]))
148
+ >> > with open (output_path, " wb" ) as fp:
149
+ >> > fp.write(base64.b64decode(item[" base64" ].split(' ,' )[- 1 ]))
127
150
```
128
151
查看指定输出文件夹,就能看到生成图像了,如图:
129
152
0 commit comments