Skip to content

Commit bf59fd8

Browse files
committed
OpenCV is now an optional dependency
Should contribute fixing Issue #99
1 parent 6ba86e2 commit bf59fd8

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

cdl/algorithms/image.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
from typing import Literal
1212

13-
import cv2
1413
import numpy as np
1514
import scipy.ndimage as spi
1615
import scipy.spatial as spt
@@ -720,6 +719,14 @@ def find_blobs_opencv(
720719
Returns:
721720
Coordinates of blobs
722721
"""
722+
723+
# Note:
724+
# Importing OpenCV inside the function in order to eventually raise an ImportError
725+
# when the function is called and OpenCV is not installed. This error will be
726+
# handled by DataLab and the user will be informed that OpenCV is required to use
727+
# this function.
728+
import cv2 # pylint: disable=import-outside-toplevel
729+
723730
params = cv2.SimpleBlobDetector_Params()
724731
if min_threshold is not None:
725732
params.minThreshold = min_threshold

cdl/tests/conftest.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
import os
1212

13-
import cv2
1413
import guidata
1514
import h5py
1615
import numpy
@@ -47,9 +46,14 @@ def pytest_report_header(config): # pylint: disable=unused-argument
4746
f"PythonQwt {qwt.__version__}, "
4847
f"{qtpy.API_NAME} {qtbindings_version} [Qt version: {qtpy.QT_VERSION}]",
4948
f"NumPy {numpy.__version__}, SciPy {scipy.__version__}, "
50-
f"h5py {h5py.__version__}, "
51-
f"scikit-image {skimage.__version__}, OpenCV {cv2.__version__}",
49+
f"h5py {h5py.__version__}, scikit-image {skimage.__version__}",
5250
]
51+
try:
52+
import cv2 # pylint: disable=import-outside-toplevel
53+
54+
infolist.append(f", OpenCV {cv2.__version__}")
55+
except ImportError:
56+
pass
5357
for vname in ("CDL_DATA", "PYTHONPATH", "DEBUG"):
5458
value = os.environ.get(vname, "")
5559
if value:

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ dependencies = [
4444
"NumPy >= 1.21",
4545
"SciPy >= 1.7",
4646
"scikit-image >= 0.18",
47-
"opencv-python-headless >= 4.5",
4847
"pandas >= 1.3",
4948
"PyWavelets >= 1.1",
5049
"psutil >= 5.5",
@@ -65,6 +64,7 @@ cdl-tests = "cdl.tests:run"
6564

6665
[project.optional-dependencies]
6766
qt = ["PyQt5"]
67+
opencv = ["opencv-python-headless >= 4.5"]
6868
dev = ["ruff", "pylint", "Coverage", "pyinstaller>=6.0"]
6969
doc = [
7070
"PyQt5",

0 commit comments

Comments
 (0)