diff --git a/paddlehub/commands/tmpl/x_model.tmpl b/paddlehub/commands/tmpl/x_model.tmpl index 4d63a6f8c5d69b4d3b65a46d89f1238755dcb987..e9552fba3f584432a5ad22cb2c7ef288080cbfd4 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)