forked from onnx/onnx
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use Pillow to implement ImageDecoder (onnx#5555)
Use Pillow to replace opencv to implement ImageDecoder to remove dependency on the heavy opencv. Code change made with help from Copilot Chat. ### Motivation and Context Fixes onnx#5545 --------- Signed-off-by: Justin Chu <[email protected]> Co-authored-by: Chun-Wei Chen <[email protected]>
- Loading branch information
1 parent
bd60a39
commit 7f81ffb
Showing
36 changed files
with
94 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file modified
BIN
+0 Bytes
(100%)
onnx/backend/test/data/node/test_image_decoder_decode_bmp_rgb/model.onnx
Binary file not shown.
Binary file modified
BIN
-1.69 KB
(65%)
onnx/backend/test/data/node/test_image_decoder_decode_bmp_rgb/test_data_set_0/input_0.pb
Binary file not shown.
Binary file modified
BIN
-1.69 KB
(64%)
onnx/backend/test/data/node/test_image_decoder_decode_bmp_rgb/test_data_set_0/output_0.pb
Binary file not shown.
Binary file modified
BIN
+0 Bytes
(100%)
onnx/backend/test/data/node/test_image_decoder_decode_jpeg2k_rgb/model.onnx
Binary file not shown.
Binary file modified
BIN
+681 Bytes
(160%)
onnx/backend/test/data/node/test_image_decoder_decode_jpeg2k_rgb/test_data_set_0/input_0.pb
Binary file not shown.
Binary file modified
BIN
-1.69 KB
(64%)
onnx/backend/test/data/node/test_image_decoder_decode_jpeg2k_rgb/test_data_set_0/output_0.pb
Binary file not shown.
Binary file modified
BIN
+0 Bytes
(100%)
onnx/backend/test/data/node/test_image_decoder_decode_jpeg_bgr/model.onnx
Binary file not shown.
Binary file modified
BIN
-1.12 KB
(48%)
onnx/backend/test/data/node/test_image_decoder_decode_jpeg_bgr/test_data_set_0/input_0.pb
Binary file not shown.
Binary file modified
BIN
-1.69 KB
(64%)
onnx/backend/test/data/node/test_image_decoder_decode_jpeg_bgr/test_data_set_0/output_0.pb
Binary file not shown.
Binary file modified
BIN
+0 Bytes
(100%)
onnx/backend/test/data/node/test_image_decoder_decode_jpeg_grayscale/model.onnx
Binary file not shown.
Binary file modified
BIN
-1.12 KB
(48%)
...ackend/test/data/node/test_image_decoder_decode_jpeg_grayscale/test_data_set_0/input_0.pb
Binary file not shown.
11 changes: 10 additions & 1 deletion
11
...ckend/test/data/node/test_image_decoder_decode_jpeg_grayscale/test_data_set_0/output_0.pb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file modified
BIN
+0 Bytes
(100%)
onnx/backend/test/data/node/test_image_decoder_decode_jpeg_rgb/model.onnx
Binary file not shown.
Binary file modified
BIN
-1.12 KB
(48%)
onnx/backend/test/data/node/test_image_decoder_decode_jpeg_rgb/test_data_set_0/input_0.pb
Binary file not shown.
Binary file modified
BIN
-1.69 KB
(64%)
onnx/backend/test/data/node/test_image_decoder_decode_jpeg_rgb/test_data_set_0/output_0.pb
Binary file not shown.
Binary file modified
BIN
+0 Bytes
(100%)
onnx/backend/test/data/node/test_image_decoder_decode_png_rgb/model.onnx
Binary file not shown.
Binary file modified
BIN
-1.02 KB
(24%)
onnx/backend/test/data/node/test_image_decoder_decode_png_rgb/test_data_set_0/input_0.pb
Binary file not shown.
Binary file modified
BIN
-1.69 KB
(64%)
onnx/backend/test/data/node/test_image_decoder_decode_png_rgb/test_data_set_0/output_0.pb
Binary file not shown.
Binary file modified
BIN
+0 Bytes
(100%)
onnx/backend/test/data/node/test_image_decoder_decode_pnm_rgb/model.onnx
Binary file not shown.
Binary file modified
BIN
-1.69 KB
(64%)
onnx/backend/test/data/node/test_image_decoder_decode_pnm_rgb/test_data_set_0/input_0.pb
Binary file not shown.
Binary file modified
BIN
-1.69 KB
(64%)
onnx/backend/test/data/node/test_image_decoder_decode_pnm_rgb/test_data_set_0/output_0.pb
Binary file not shown.
Binary file modified
BIN
+0 Bytes
(100%)
onnx/backend/test/data/node/test_image_decoder_decode_tiff_rgb/model.onnx
Binary file not shown.
Binary file modified
BIN
+1.98 KB
(270%)
onnx/backend/test/data/node/test_image_decoder_decode_tiff_rgb/test_data_set_0/input_0.pb
Binary file not shown.
Binary file modified
BIN
-1.69 KB
(64%)
onnx/backend/test/data/node/test_image_decoder_decode_tiff_rgb/test_data_set_0/output_0.pb
Binary file not shown.
Binary file modified
BIN
+0 Bytes
(100%)
onnx/backend/test/data/node/test_image_decoder_decode_webp_rgb/model.onnx
Binary file not shown.
Binary file modified
BIN
+192 Bytes
(150%)
onnx/backend/test/data/node/test_image_decoder_decode_webp_rgb/test_data_set_0/input_0.pb
Binary file not shown.
Binary file modified
BIN
-1.69 KB
(64%)
onnx/backend/test/data/node/test_image_decoder_decode_webp_rgb/test_data_set_0/output_0.pb
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,33 @@ | ||
# Copyright (c) ONNX Project Contributors | ||
|
||
# SPDX-License-Identifier: Apache-2.0 | ||
# pylint: disable=C0123,C3001,R0912,R0913,R0914,R1730,W0221,W0613 | ||
|
||
from __future__ import annotations | ||
|
||
import io | ||
|
||
import numpy as np | ||
|
||
from onnx.reference.op_run import OpRun | ||
|
||
|
||
class ImageDecoder(OpRun): | ||
def _run( # type: ignore | ||
self, | ||
encoded, | ||
pixel_format="RGB", | ||
): | ||
def _run(self, encoded: np.ndarray, pixel_format="RGB") -> tuple[np.ndarray]: # type: ignore | ||
try: | ||
# pylint: disable=import-outside-toplevel` | ||
import cv2 | ||
import PIL.Image # pylint: disable=import-outside-toplevel | ||
except ImportError as e: | ||
raise ImportError( | ||
"opencv-python must be installed to use the reference implementation of the ImageDecoder operator" | ||
"Pillow must be installed to use the reference implementation of the ImageDecoder operator" | ||
) from e | ||
decoded = None | ||
img = PIL.Image.open(io.BytesIO(encoded.tobytes())) | ||
if pixel_format == "BGR": | ||
decoded = cv2.imdecode(encoded, cv2.IMREAD_COLOR) | ||
decoded = np.array(img)[:, :, ::-1] | ||
elif pixel_format == "RGB": | ||
decoded = cv2.imdecode(encoded, cv2.IMREAD_COLOR) | ||
decoded = cv2.cvtColor(decoded, cv2.COLOR_BGR2RGB) | ||
decoded = np.array(img) | ||
elif pixel_format == "Grayscale": | ||
decoded = cv2.imdecode(encoded, cv2.IMREAD_GRAYSCALE) | ||
img = img.convert("L") | ||
decoded = np.array(img) | ||
decoded = np.expand_dims(decoded, axis=2) # (H, W) to (H, W, 1) | ||
else: | ||
raise RuntimeError(f"pixel_format={pixel_format!r} is not supported.") | ||
raise ValueError(f"pixel_format={pixel_format!r} is not supported.") | ||
return (decoded,) |
Oops, something went wrong.