From e1e5c19e1799c32e0f6f1e7df77ce2fcc1728128 Mon Sep 17 00:00:00 2001 From: will-jl944 Date: Mon, 27 Sep 2021 10:09:35 +0800 Subject: [PATCH] compatible with paddlex 2.0 (#1628) --- paddlehub/commands/tmpl/x_model.tmpl | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/paddlehub/commands/tmpl/x_model.tmpl b/paddlehub/commands/tmpl/x_model.tmpl index 4d63a6f8..e9552fba 100644 --- a/paddlehub/commands/tmpl/x_model.tmpl +++ b/paddlehub/commands/tmpl/x_model.tmpl @@ -14,17 +14,30 @@ from paddlehub.module.module import moduleinfo, runnable, serving def base64_to_cv2(b64str): data = base64.b64decode(b64str.encode('utf8')) - data = np.fromstring(data, np.uint8) + data = np.frombuffer(data, np.uint8) data = cv2.imdecode(data, cv2.IMREAD_COLOR) return data +def base64_to_np(b64tuple): + data, shape = b64tuple + data = base64.b64decode(data.encode('utf8')) + data = np.frombuffer(data, np.float32).reshape(shape) + return data + + def cv2_to_base64(image): # return base64.b64encode(image) data = cv2.imencode('.jpg', image)[1] return base64.b64encode(data.tostring()).decode('utf8') +def np_to_base64(array): + shape = array.shape + data = base64.b64encode(array).decode('utf8') + return data, shape + + def read_images(paths): images = [] for path in paths: @@ -83,7 +96,9 @@ class MODULE(hub.Module): if isinstance(result, dict): # result_new = dict() for key, value in result.items(): - if isinstance(value, np.ndarray): + if key == 'score_map': + result[key] = np_to_base64(value) + elif isinstance(value, np.ndarray): result[key] = cv2_to_base64(value) elif isinstance(value, np.generic): result[key] = np.asscalar(value) @@ -91,7 +106,9 @@ class MODULE(hub.Module): elif isinstance(result, list): for index in range(len(result)): for key, value in result[index].items(): - if isinstance(value, np.ndarray): + if key == 'score_map': + result[index][key] = np_to_base64(value) + elif isinstance(value, np.ndarray): result[index][key] = cv2_to_base64(value) elif isinstance(value, np.generic): result[index][key] = np.asscalar(value) -- GitLab