From b47ac46e92c03d042b01187ec48b714e79476a17 Mon Sep 17 00:00:00 2001 From: luoyang <luoyang42@huawei.com> Date: Thu, 28 Oct 2021 15:18:08 +0800 Subject: [PATCH] Support bytes input for decode op --- mindspore/dataset/vision/c_transforms.py | 4 +++- tests/ut/python/dataset/test_eager_vision.py | 8 +++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/mindspore/dataset/vision/c_transforms.py b/mindspore/dataset/vision/c_transforms.py index bf67e01758c..133410ccccd 100644 --- a/mindspore/dataset/vision/c_transforms.py +++ b/mindspore/dataset/vision/c_transforms.py @@ -432,7 +432,9 @@ class Decode(ImageTensorOperation): Returns: img (NumPy), Decoded image. """ - if not isinstance(img, np.ndarray) or img.ndim != 1 or img.dtype.type is np.str_: + if isinstance(img, bytes): + img = np.frombuffer(img, np.uint8) + elif not isinstance(img, np.ndarray) or img.ndim != 1 or img.dtype.type is np.str_: raise TypeError( "Input should be an encoded image in 1-D NumPy format, got {}.".format(type(img))) return super().__call__(img) diff --git a/tests/ut/python/dataset/test_eager_vision.py b/tests/ut/python/dataset/test_eager_vision.py index 1d590679a4f..9ccbb637c69 100644 --- a/tests/ut/python/dataset/test_eager_vision.py +++ b/tests/ut/python/dataset/test_eager_vision.py @@ -25,9 +25,15 @@ def test_eager_decode(): img = C.Decode()(img) logger.info("Image.type: {}, Image.shape: {}".format(type(img), img.shape)) - assert img.shape == (2268, 4032, 3) + fp = open("../data/dataset/apple.jpg", "rb") + img2 = fp.read() + + img2 = C.Decode()(img2) + logger.info("Image.type: {}, Image.shape: {}".format(type(img2), img2.shape)) + assert img2.shape == (2268, 4032, 3) + def test_eager_resize(): img = cv2.imread("../data/dataset/apple.jpg")