Skip to content

Commit 253f930

Browse files
committed
Move imread and friends to datasets. Fixes #81
1 parent 4290690 commit 253f930

File tree

3 files changed

+67
-70
lines changed

3 files changed

+67
-70
lines changed

DeepFried2/datasets/images.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import numpy as _np
2+
3+
4+
try:
5+
import cv2 as _cv2
6+
7+
def imread(fname, dtype=_np.float32):
8+
im = _cv2.imread(fname, flags=_cv2.IMREAD_UNCHANGED)
9+
if im is None:
10+
raise IOError("Couldn't open image file {}".format(fname))
11+
return im.astype(dtype)
12+
13+
def imresize(im, h, w):
14+
if im.shape[:2] == (h, w):
15+
return im
16+
17+
# Use AREA interpolation as soon as one dimension gets smaller to avoid moiree.
18+
inter = _cv2.INTER_AREA if im.shape[0] < h or im.shape[1] < w else _cv2.INTER_LINEAR
19+
return _cv2.resize(im, (w,h), interpolation=inter)
20+
21+
except ImportError:
22+
23+
try:
24+
# This is what scipy's imread does lazily.
25+
from PIL import Image as _Image
26+
27+
def imread(fname, dtype=_np.float32):
28+
# This does what CV_LOAD_IMAGE_ANYDEPTH does by default.
29+
return _np.array(_Image.open(fname), dtype=dtype)
30+
31+
def imresize(im, h, w):
32+
if im.shape[:2] == (h, w):
33+
return im
34+
35+
# This has problems re-reading a numpy array if it's not 8-bit anymore.
36+
assert im.max() > 1, "PIL has problems resizing images after they've been changed to e.g. [0-1] range. Either install OpenCV or resize right after reading the image."
37+
img = _Image.fromarray(im.astype(_np.uint8))
38+
return _np.array(img.resize((w,h), _Image.BILINEAR), dtype=im.dtype)
39+
40+
except ImportError:
41+
42+
def imread(fname, dtype=None):
43+
raise ImportError(
44+
"Neither OpenCV nor the Python Imaging Library (PIL) is "
45+
"installed. Please install either for loading images."
46+
)
47+
48+
def imresize(im, h, w):
49+
raise ImportError(
50+
"Neither OpenCV nor the Python Imaging Library (PIL) is "
51+
"installed. Please install either for resizing images."
52+
)
53+
54+
55+
def imresizecrop(img, size):
56+
assert not isinstance(size, tuple), "For now, `size` needs to be a single integer, i.e. it's being squared."
57+
58+
# First, resize the smallest side to `size`.
59+
img = imresize(img, h=img.shape[0]*size//min(img.shape[:2]),
60+
w=img.shape[1]*size//min(img.shape[:2]))
61+
62+
# Then, crop-out the central part of the largest side.
63+
return img[(img.shape[0]-size)//2:img.shape[0]-(img.shape[0]-size)//2,
64+
(img.shape[1]-size)//2:img.shape[1]-(img.shape[1]-size)//2,
65+
:]

examples/Pretrained/run_vgg.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
import sys, os, json, argparse, collections
44
import numpy as np
55
import DeepFried2 as df
6+
from DeepFried2.datasets.images import imread, imresizecrop
67

7-
from examples.utils import printnow, imread, imresizecrop
8+
from examples.utils import printnow
89

910

1011
if __name__ == "__main__":

examples/utils.py

Lines changed: 0 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import sys as _sys
2-
import numpy as _np
32

43

54
def printnow(fmt, *a, **kw):
@@ -40,71 +39,3 @@ def finish(self):
4039

4140
def make_progressbar(prefix, data_size):
4241
return SimpleProgressBar(data_size, prefix + ", processed {i} of {tot} ({pct:.2%})")
43-
44-
45-
# Reading and resizing images
46-
#############################
47-
48-
49-
try:
50-
import cv2 as _cv2
51-
52-
def imread(fname, dtype=_np.float32):
53-
im = _cv2.imread(fname, flags=_cv2.IMREAD_UNCHANGED)
54-
if im is None:
55-
raise IOError("Couldn't open image file {}".format(fname))
56-
return im.astype(dtype)
57-
58-
def imresize(im, h, w):
59-
if im.shape[:2] == (h, w):
60-
return im
61-
62-
# Use AREA interpolation as soon as one dimension gets smaller to avoid moiree.
63-
inter = _cv2.INTER_AREA if im.shape[0] < h or im.shape[1] < w else _cv2.INTER_LINEAR
64-
return _cv2.resize(im, (w,h), interpolation=inter)
65-
66-
except ImportError:
67-
68-
try:
69-
# This is what scipy's imread does lazily.
70-
from PIL import Image as _Image
71-
72-
def imread(fname, dtype=_np.float32):
73-
# This does what CV_LOAD_IMAGE_ANYDEPTH does by default.
74-
return _np.array(_Image.open(fname), dtype=dtype)
75-
76-
def imresize(im, h, w):
77-
if im.shape[:2] == (h, w):
78-
return im
79-
80-
# This has problems re-reading a numpy array if it's not 8-bit anymore.
81-
assert im.max() > 1, "PIL has problems resizing images after they've been changed to e.g. [0-1] range. Either install OpenCV or resize right after reading the image."
82-
img = _Image.fromarray(im.astype(_np.uint8))
83-
return _np.array(img.resize((w,h), _Image.BILINEAR), dtype=im.dtype)
84-
85-
except ImportError:
86-
87-
def imread(fname, dtype=None):
88-
raise ImportError(
89-
"Neither OpenCV nor the Python Imaging Library (PIL) is "
90-
"installed. Please install either for loading images."
91-
)
92-
93-
def imresize(im, h, w):
94-
raise ImportError(
95-
"Neither OpenCV nor the Python Imaging Library (PIL) is "
96-
"installed. Please install either for resizing images."
97-
)
98-
99-
100-
def imresizecrop(img, size):
101-
assert not isinstance(size, tuple), "For now, `size` needs to be a single integer, i.e. it's being squared."
102-
103-
# First, resize the smallest side to `size`.
104-
img = imresize(img, h=img.shape[0]*size//min(img.shape[:2]),
105-
w=img.shape[1]*size//min(img.shape[:2]))
106-
107-
# Then, crop-out the central part of the largest side.
108-
return img[(img.shape[0]-size)//2:img.shape[0]-(img.shape[0]-size)//2,
109-
(img.shape[1]-size)//2:img.shape[1]-(img.shape[1]-size)//2,
110-
:]

0 commit comments

Comments
 (0)