Support bytes input for decode op

This commit is contained in:
luoyang 2021-10-28 15:18:08 +08:00
parent c3ce9c56c6
commit b47ac46e92
2 changed files with 10 additions and 2 deletions

View File

@ -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)

View File

@ -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")