Skip to content

Commit d914b22

Browse files
committed
remove redundant import checking
utils.py used to have a defensive import checking in place even for required dependencies. Since the pyproject.toml now explicitly states required dependencies, import errors surface if dependencies are missing. -> removed the try/except blocks around these imports
1 parent 17a3a52 commit d914b22

1 file changed

Lines changed: 16 additions & 63 deletions

File tree

dlclive/utils.py

Lines changed: 16 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -7,39 +7,15 @@
77

88
import warnings
99
from pathlib import Path
10-
import numpy as np
11-
import urllib.request
1210
import urllib.error
11+
import urllib.request
1312

14-
from dlclive.exceptions import DLCLiveWarning
15-
from dlclive.engine import Engine
16-
17-
try:
18-
import skimage
19-
20-
SK_IM = True
21-
except ImportError as e:
22-
SK_IM = False
23-
24-
try:
25-
import cv2
26-
27-
OPEN_CV = True
28-
except ImportError as e:
29-
from PIL import Image
30-
31-
OPEN_CV = False
32-
warnings.warn(
33-
"OpenCV is not installed. Using pillow for image processing, which is slower.",
34-
DLCLiveWarning,
35-
)
36-
37-
try:
38-
from tqdm import tqdm
13+
import cv2
14+
import numpy as np
15+
from tqdm import tqdm
3916

40-
has_tqdm = True
41-
except ImportError:
42-
has_tqdm = False
17+
from dlclive.engine import Engine
18+
from dlclive.exceptions import DLCLiveWarning
4319

4420

4521
def convert_to_ubyte(frame: np.ndarray) -> np.ndarray:
@@ -56,15 +32,11 @@ def convert_to_ubyte(frame: np.ndarray) -> np.ndarray:
5632
:class: `numpy.ndarray`
5733
image converted to uint8
5834
"""
59-
60-
if SK_IM:
61-
return skimage.img_as_ubyte(frame)
62-
else:
63-
return _img_as_ubyte_np(frame)
35+
return _img_as_ubyte_np(frame)
6436

6537

6638
def resize_frame(frame: np.ndarray, resize=None) -> np.ndarray:
67-
"""Resizes an image. Uses OpenCV if installed, otherwise, uses pillow
39+
"""Resizes an image using OpenCV.
6840
6941
Parameters
7042
----------
@@ -75,21 +47,14 @@ def resize_frame(frame: np.ndarray, resize=None) -> np.ndarray:
7547
if (resize is not None) and (resize != 1):
7648
new_x = int(frame.shape[0] * resize)
7749
new_y = int(frame.shape[1] * resize)
78-
79-
if OPEN_CV:
80-
return cv2.resize(frame, (new_y, new_x))
81-
82-
else:
83-
img = Image.fromarray(frame)
84-
img = img.resize((new_y, new_x))
85-
return np.asarray(img)
50+
return cv2.resize(frame, (new_y, new_x))
8651

8752
else:
8853
return frame
8954

9055

9156
def img_to_rgb(frame: np.ndarray) -> np.ndarray:
92-
"""Convert an image to RGB. Uses OpenCV is installed, otherwise uses pillow.
57+
"""Convert an image to RGB using OpenCV.
9358
9459
Parameters
9560
----------
@@ -112,39 +77,27 @@ def img_to_rgb(frame: np.ndarray) -> np.ndarray:
11277

11378

11479
def gray_to_rgb(frame: np.ndarray) -> np.ndarray:
115-
"""Convert an image from grayscale to RGB. Uses OpenCV is installed, otherwise uses pillow.
80+
"""Convert an image from grayscale to RGB using OpenCV.
11681
11782
Parameters
11883
----------
11984
frame : :class:`numpy.ndarray
12085
an image as a numpy array
12186
"""
12287

123-
if OPEN_CV:
124-
return cv2.cvtColor(frame, cv2.COLOR_GRAY2RGB)
125-
126-
else:
127-
img = Image.fromarray(frame)
128-
img = img.convert("RGB")
129-
return np.asarray(img)
88+
return cv2.cvtColor(frame, cv2.COLOR_GRAY2RGB)
13089

13190

13291
def bgr_to_rgb(frame: np.ndarray) -> np.ndarray:
133-
"""Convert an image from BGR to RGB. Uses OpenCV is installed, otherwise uses pillow.
92+
"""Convert an image from BGR to RGB using OpenCV.
13493
13594
Parameters
13695
----------
13796
frame : :class:`numpy.ndarray
13897
an image as a numpy array
13998
"""
14099

141-
if OPEN_CV:
142-
return cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
143-
144-
else:
145-
img = Image.fromarray(frame)
146-
img = img.convert("RGB")
147-
return np.asarray(img)
100+
return cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
148101

149102

150103
def _img_as_ubyte_np(frame: np.ndarray) -> np.ndarray:
@@ -279,8 +232,8 @@ def download_file(url: str, filepath: str, chunk_size: int = 8192) -> None:
279232
# Get file size if available
280233
total_size = int(response.headers.get('Content-Length', 0))
281234

282-
# Create progress bar if tqdm is available
283-
if has_tqdm and total_size > 0:
235+
# Create progress bar if file size is known
236+
if total_size > 0:
284237
pbar = tqdm(total=total_size, unit='B', unit_scale=True, desc="Downloading")
285238
else:
286239
pbar = None

0 commit comments

Comments
 (0)