Skip to content

Commit 44240d4

Browse files
committed
fix bug to avoid warning once import paddle.fluid
1 parent 482d297 commit 44240d4

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

python/paddle/dataset/image.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,6 @@
3636
try:
3737
import cv2
3838
except ImportError:
39-
import sys
40-
sys.stderr.write(
41-
'''Warning with paddle image module: opencv-python should be imported,
42-
or paddle image module could NOT work; please install opencv-python first.'''
43-
)
4439
cv2 = None
4540
import os
4641
import tarfile
@@ -53,6 +48,18 @@
5348
]
5449

5550

51+
def _check_cv2():
52+
if cv2 is None:
53+
import sys
54+
sys.stderr.write(
55+
'''Warning with paddle image module: opencv-python should be imported,
56+
or paddle image module could NOT work; please install opencv-python first.'''
57+
)
58+
return False
59+
else:
60+
return True
61+
62+
5663
def batch_images_from_tar(data_file,
5764
dataset_name,
5865
img2label,
@@ -134,7 +141,7 @@ def load_image_bytes(bytes, is_color=True):
134141
load and return a gray image.
135142
:type is_color: bool
136143
"""
137-
assert cv2 is not None
144+
assert _check_cv2() is True
138145

139146
flag = 1 if is_color else 0
140147
file_bytes = np.asarray(bytearray(bytes), dtype=np.uint8)
@@ -159,7 +166,7 @@ def load_image(file, is_color=True):
159166
load and return a gray image.
160167
:type is_color: bool
161168
"""
162-
assert cv2 is not None
169+
assert _check_cv2() is True
163170

164171
# cv2.IMAGE_COLOR for OpenCV3
165172
# cv2.CV_LOAD_IMAGE_COLOR for older OpenCV Version
@@ -188,7 +195,7 @@ def resize_short(im, size):
188195
:param size: the shorter edge size of image after resizing.
189196
:type size: int
190197
"""
191-
assert cv2 is not None
198+
assert _check_cv2() is True
192199

193200
h, w = im.shape[:2]
194201
h_new, w_new = size, size

0 commit comments

Comments
 (0)