From 297896455c29ae2c651a0dccf4288fde3abdaf91 Mon Sep 17 00:00:00 2001 From: Frank Liu Date: Mon, 22 Oct 2018 14:14:06 -0700 Subject: [PATCH] Add bytearray support back to imdecode (#12855, #12868) 1. Avoid raise exception when input is bytearray. 2. Avoid OpenCV crash for empty input. --- python/mxnet/image/image.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/python/mxnet/image/image.py b/python/mxnet/image/image.py index eee2ccf14a8e..b846700fb503 100644 --- a/python/mxnet/image/image.py +++ b/python/mxnet/image/image.py @@ -93,7 +93,7 @@ def imdecode(buf, *args, **kwargs): Parameters ---------- - buf : str/bytes or numpy.ndarray + buf : str/bytes/bytearray or numpy.ndarray Binary image data as string or numpy ndarray. flag : int, optional, default=1 1 for three channel color output. 0 for grayscale output. @@ -135,10 +135,15 @@ def imdecode(buf, *args, **kwargs): """ if not isinstance(buf, nd.NDArray): - if sys.version_info[0] == 3 and not isinstance(buf, (bytes, np.ndarray)): - raise ValueError('buf must be of type bytes or numpy.ndarray,' + if sys.version_info[0] == 3 and not isinstance(buf, (bytes, bytearray, np.ndarray)): + raise ValueError('buf must be of type bytes, bytearray or numpy.ndarray,' 'if you would like to input type str, please convert to bytes') buf = nd.array(np.frombuffer(buf, dtype=np.uint8), dtype=np.uint8) + + if len(buf) == 0: + # empty buf causes OpenCV crash. + raise ValueError("input buf cannot be empty.") + return _internal._cvimdecode(buf, *args, **kwargs)