未验证 提交 5e3ca2d5 编写于 作者: B buchongyu 提交者: GitHub

fix deeplabv3p_xception65_humanseg inference client bug (#1914)

上级 185ba23d
...@@ -131,34 +131,35 @@ ...@@ -131,34 +131,35 @@
- 配置好服务端,以下数行代码即可实现发送预测请求,获取预测结果 - 配置好服务端,以下数行代码即可实现发送预测请求,获取预测结果
```python - ```python
import requests import requests
import json import json
import cv2 import cv2
import base64 import base64
import numpy as np import numpy as np
def cv2_to_base64(image): def cv2_to_base64(image):
data = cv2.imencode('.jpg', image)[1] data = cv2.imencode('.jpg', image)[1]
return base64.b64encode(data.tostring()).decode('utf8') return base64.b64encode(data.tostring()).decode('utf8')
def base64_to_cv2(b64str): def base64_to_cv2(b64str):
data = base64.b64decode(b64str.encode('utf8')) data = base64.b64decode(b64str.encode('utf8'))
data = np.fromstring(data, np.uint8) data = np.fromstring(data, np.uint8)
data = cv2.imdecode(data, cv2.IMREAD_COLOR) data = cv2.imdecode(data, cv2.IMREAD_COLOR)
return data return data
# 发送HTTP请求 org_im = cv2.imread("/PATH/TO/IMAGE")
data = {'images':[cv2_to_base64(cv2.imread("/PATH/TO/IMAGE"))]} # 发送HTTP请求
headers = {"Content-type": "application/json"} data = {'images':[cv2_to_base64(org_im)]}
url = "http://127.0.0.1:8866/predict/deeplabv3p_xception65_humanseg" headers = {"Content-type": "application/json"}
r = requests.post(url=url, headers=headers, # 保存图片 url = "http://127.0.0.1:8866/predict/deeplabv3p_xception65_humanseg"
mask =cv2.cvtColor(base64_to_cv2(r.json()["results"][0]['data']), cv2.COLOR_BGR2GRAY) r = requests.post(url=url, headers=headers, data=json.dumps(data))# 保存图片
rgba = np.concatenate((org_im, np.expand_dims(mask, axis=2)), axis=2) mask =cv2.cvtColor(base64_to_cv2(r.json()["results"][0]['data']), cv2.COLOR_BGR2GRAY)
cv2.imwrite("segment_human_server.png", rgba) rgba = np.concatenate((org_im, np.expand_dims(mask, axis=2)), axis=2)
``` cv2.imwrite("segment_human_server.png", rgba)
```
## 五、更新历史 ## 五、更新历史
......
...@@ -128,34 +128,35 @@ ...@@ -128,34 +128,35 @@
- With a configured server, use the following lines of code to send the prediction request and obtain the result - With a configured server, use the following lines of code to send the prediction request and obtain the result
- ```python - ```python
import requests import requests
import json import json
import cv2 import cv2
import base64 import base64
import numpy as np import numpy as np
def cv2_to_base64(image): def cv2_to_base64(image):
data = cv2.imencode('.jpg', image)[1] data = cv2.imencode('.jpg', image)[1]
return base64.b64encode(data.tostring()).decode('utf8') return base64.b64encode(data.tostring()).decode('utf8')
def base64_to_cv2(b64str): def base64_to_cv2(b64str):
data = base64.b64decode(b64str.encode('utf8')) data = base64.b64decode(b64str.encode('utf8'))
data = np.fromstring(data, np.uint8) data = np.fromstring(data, np.uint8)
data = cv2.imdecode(data, cv2.IMREAD_COLOR) data = cv2.imdecode(data, cv2.IMREAD_COLOR)
return data return data
# Send an HTTP request org_im = cv2.imread("/PATH/TO/IMAGE")
data = {'images':[cv2_to_base64(cv2.imread("/PATH/TO/IMAGE"))]} # Send an HTTP request
headers = {"Content-type": "application/json"} data = {'images':[cv2_to_base64(org_im)]}
url = "http://127.0.0.1:8866/predict/deeplabv3p_xception65_humanseg" headers = {"Content-type": "application/json"}
r = requests.post(url=url, headers=headers, url = "http://127.0.0.1:8866/predict/deeplabv3p_xception65_humanseg"
mask =cv2.cvtColor(base64_to_cv2(r.json()["results"][0]['data']), cv2.COLOR_BGR2GRAY) r = requests.post(url=url, headers=headers, data=json.dumps(data))
rgba = np.concatenate((org_im, np.expand_dims(mask, axis=2)), axis=2) mask =cv2.cvtColor(base64_to_cv2(r.json()["results"][0]['data']), cv2.COLOR_BGR2GRAY)
cv2.imwrite("segment_human_server.png", rgba) rgba = np.concatenate((org_im, np.expand_dims(mask, axis=2)), axis=2)
``` cv2.imwrite("segment_human_server.png", rgba)
```
## V. Release Note ## V. Release Note
- 1.0.0 - 1.0.0
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册