Skip to content

Commit cbf65e3

Browse files
rijobrowyli
andauthored
import cv2 inside class (#5659)
The line `optional_import("cv2")` causes the security light of my macOS to light up in green, even for the lowest level command e.g., `import monai`. I was worried I had a virus until I realised what was causing it! To avoid worrying other users, I suggest we only import when a user is using the `VideoDataset`. ### Types of changes <!--- Put an `x` in all the boxes that apply, and remove the not applicable items --> - [x] Non-breaking change (fix or new feature that would not break existing functionality). Signed-off-by: Richard Brown <33289025+rijobro@users.noreply.github.com> Signed-off-by: Wenqi Li <wenqil@nvidia.com> Co-authored-by: Wenqi Li <wenqil@nvidia.com> Co-authored-by: Wenqi Li <831580+wyli@users.noreply.github.com>
1 parent e457a5a commit cbf65e3

2 files changed

Lines changed: 16 additions & 9 deletions

File tree

monai/data/dataset.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,16 +1091,14 @@ def is_started(self):
10911091
Check whether the replacement thread is already started.
10921092
10931093
"""
1094-
if self._replace_mgr is None:
1095-
return False
1096-
return self._replace_mgr.is_alive()
1094+
return False if self._replace_mgr is None else self._replace_mgr.is_alive()
10971095

10981096
def start(self):
10991097
"""
11001098
Start the background thread to replace training items for every epoch.
11011099
11021100
"""
1103-
if self._replace_mgr is None or not self.is_started():
1101+
if not self.is_started():
11041102
self._restart()
11051103

11061104
def _restart(self):
@@ -1141,8 +1139,7 @@ def update_cache(self):
11411139
If the cache has been shutdown before, need to restart the `_replace_mgr` thread.
11421140
11431141
"""
1144-
if not self._replace_mgr.is_alive():
1145-
self._restart()
1142+
self.start()
11461143

11471144
# make sure update is done
11481145
while not self._try_update_cache():
@@ -1173,7 +1170,7 @@ def shutdown(self):
11731170
# wait until replace mgr is done the current round
11741171
while not self._try_shutdown():
11751172
time.sleep(0.01)
1176-
self._replace_mgr.join()
1173+
self._replace_mgr.join(300)
11771174

11781175
def _replace_cache_thread(self, index: int):
11791176
"""

monai/data/video_dataset.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,21 @@
2020
from monai.utils.enums import ColorOrder
2121
from monai.utils.module import optional_import
2222

23+
__all__ = ["VideoDataset", "VideoFileDataset", "CameraDataset"]
24+
2325
if TYPE_CHECKING:
2426
import cv2
2527

2628
has_cv2 = True
2729
else:
28-
cv2, has_cv2 = optional_import("cv2")
30+
cv2, has_cv2 = None, None
2931

30-
__all__ = ["VideoDataset", "VideoFileDataset", "CameraDataset"]
32+
33+
def import_cv():
34+
"""Import cv2. Put it inside a function to avoid webcam lights blinking on ``import monai``."""
35+
global cv2
36+
global has_cv2
37+
cv2, has_cv2 = optional_import("cv2")
3138

3239

3340
class SuppressStderr:
@@ -50,6 +57,9 @@ def __exit__(self, *_):
5057

5158

5259
class VideoDataset:
60+
# import inside class to avoid webcam blinking on ``import monai``.
61+
import_cv()
62+
5363
def __init__(
5464
self,
5565
video_source: Union[str, int],

0 commit comments

Comments
 (0)