-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add more documentation and util to filter invalid images
- Loading branch information
Showing
6 changed files
with
102 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
from .image_features import image_features | ||
from .image_features import image_features | ||
from .utils import filter_invalid_images |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import multiprocessing | ||
import pretrainedmodels.utils as utils | ||
from tqdm import tqdm | ||
|
||
load_img = utils.LoadImage() | ||
|
||
|
||
def _is_valid_img(img_path): | ||
try: | ||
load_img(img_path) | ||
return True | ||
except Exception: | ||
return False | ||
|
||
|
||
def filter_invalid_images(img_paths, num_workers=4, progress=False): | ||
"""Filter invalid images before computing expensive features.""" | ||
with multiprocessing.Pool(num_workers) as p: | ||
if progress: | ||
load_works = list(tqdm( | ||
p.imap(_is_valid_img, img_paths), | ||
total=len(img_paths), | ||
desc="Filtering invalid images")) | ||
else: | ||
load_works = p.map(_is_valid_img, img_paths) | ||
|
||
img_paths = [ | ||
img_path for img_path, is_loadable in | ||
zip(img_paths, load_works) if is_loadable | ||
] | ||
return img_paths |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import numpy as np | ||
import os | ||
from image_features.utils import filter_invalid_images | ||
|
||
|
||
data_path = os.path.join(os.path.dirname(__file__), 'data') | ||
|
||
|
||
def test_image_features(): | ||
example_imgs = [ | ||
os.path.join(data_path, 'example_image.jpg'), | ||
os.path.join(data_path, 'example_image_2.JPG'), | ||
os.path.join(data_path, 'example_image_corrupted.JPG') | ||
] | ||
|
||
valid_imgs = filter_invalid_images(example_imgs) | ||
assert valid_imgs == example_imgs[:2] | ||
|
||
if __name__ == '__main__': | ||
test_image_features() |