Skip to content

Commit

Permalink
Adds a pre-check for image dimensions to EyepadAlign.fit_directory (#492
Browse files Browse the repository at this point in the history
)

* adds a precheck for image dimensions

* pep8 fixes
  • Loading branch information
rasbt authored Jan 19, 2019
1 parent 29826e3 commit ea9e9db
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 15 deletions.
54 changes: 46 additions & 8 deletions docs/sources/user_guide/image/eyepad_align.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,20 @@
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Pre-Checking directory for consistent image dimensions...\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"0% [#########] 100% | ETA: 00:00:00"
]
},
{
"name": "stdout",
"output_type": "stream",
Expand All @@ -189,13 +203,15 @@
"name": "stderr",
"output_type": "stream",
"text": [
"\n",
"Total time elapsed: 00:00:00\n",
"0% [##### ] 100% | ETA: 00:00:00/Users/sebastian/code/mlxtend/mlxtend/image/extract_face_landmarks.py:61: UserWarning: No face detected.\n",
" warnings.warn('No face detected.')\n",
"/Users/sebastian/code/mlxtend/mlxtend/image/eyepad_align.py:160: UserWarning: No face detected in image 000004.jpg. Image ignored.\n",
"/Users/sebastian/code/mlxtend/mlxtend/image/eyepad_align.py:185: UserWarning: No face detected in image 000004.jpg. Image ignored.\n",
" % f)\n",
"0% [#########] 100% | ETA: 00:00:00\n",
"Total time elapsed: 00:00:00\n",
"/Users/sebastian/code/mlxtend/mlxtend/image/eyepad_align.py:160: UserWarning: No face detected in image 000003.jpg. Image ignored.\n",
"/Users/sebastian/code/mlxtend/mlxtend/image/eyepad_align.py:185: UserWarning: No face detected in image 000003.jpg. Image ignored.\n",
" % f)\n"
]
},
Expand Down Expand Up @@ -433,6 +449,20 @@
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Pre-Checking directory for consistent image dimensions...\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"0% [#########] 100% | ETA: 00:00:00"
]
},
{
"name": "stdout",
"output_type": "stream",
Expand All @@ -444,20 +474,22 @@
"name": "stderr",
"output_type": "stream",
"text": [
"\n",
"Total time elapsed: 00:00:00\n",
"0% [##### ] 100% | ETA: 00:00:00/Users/sebastian/code/mlxtend/mlxtend/image/extract_face_landmarks.py:61: UserWarning: No face detected.\n",
" warnings.warn('No face detected.')\n",
"/Users/sebastian/code/mlxtend/mlxtend/image/eyepad_align.py:160: UserWarning: No face detected in image 000004.jpg. Image ignored.\n",
"/Users/sebastian/code/mlxtend/mlxtend/image/eyepad_align.py:185: UserWarning: No face detected in image 000004.jpg. Image ignored.\n",
" % f)\n",
"0% [#########] 100% | ETA: 00:00:00\n",
"Total time elapsed: 00:00:00\n",
"/Users/sebastian/code/mlxtend/mlxtend/image/eyepad_align.py:160: UserWarning: No face detected in image 000003.jpg. Image ignored.\n",
"/Users/sebastian/code/mlxtend/mlxtend/image/eyepad_align.py:185: UserWarning: No face detected in image 000003.jpg. Image ignored.\n",
" % f)\n"
]
},
{
"data": {
"text/plain": [
"<mlxtend.image.eyepad_align.EyepadAlign at 0x1c169076a0>"
"<mlxtend.image.eyepad_align.EyepadAlign at 0x1c1839ba90>"
]
},
"execution_count": 10,
Expand Down Expand Up @@ -604,8 +636,6 @@
"- `target_width_` : the width of the transformed output image.\n",
"\n",
"\n",
" file_extension str (default='.jpg'): File extension of the image files.\n",
"\n",
"For more usage examples, please see\n",
"[http://rasbt.github.io/mlxtend/user_guide/image/EyepadAlign/](http://rasbt.github.io/mlxtend/user_guide/image/EyepadAlign/)\n",
"\n",
Expand All @@ -618,7 +648,7 @@
"\n",
"<hr>\n",
"\n",
"*fit_directory(target_img_dir, target_height, target_width, file_extension='.jpg')*\n",
"*fit_directory(target_img_dir, target_height, target_width, file_extension='.jpg', pre_check=True)*\n",
"\n",
"Calculates the average landmarks for all face images\n",
"in a directory which will then be set as the target landmark set.\n",
Expand All @@ -639,6 +669,14 @@
"\n",
" Expected image width of the images in the directory\n",
"\n",
" file_extension str (default='.jpg'): File extension of the image files.\n",
"\n",
" pre_check Bool (default=True): Checks that each image has the dimensions\n",
" specificed via `target_height` and `target_width` on the whole\n",
" directory first to identify potential issues that are recommended\n",
" to be fixed before proceeding. Raises a warning for each image if\n",
" dimensions differ from the ones specified and expected.\n",
"\n",
"**Returns**\n",
"\n",
"- `self` : object\n",
Expand Down
40 changes: 33 additions & 7 deletions mlxtend/image/eyepad_align.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ class EyepadAlign(object):
target_width_ : the width of the transformed output image.
file_extension str (default='.jpg'): File extension of the image files.
For more usage examples, please see
http://rasbt.github.io/mlxtend/user_guide/image/EyepadAlign/
Expand Down Expand Up @@ -94,7 +92,8 @@ def fit_image(self, target_image):
return self

def fit_directory(self, target_img_dir, target_height,
target_width, file_extension='.jpg'):
target_width, file_extension='.jpg',
pre_check=True):
"""
Calculates the average landmarks for all face images
in a directory which will then be set as the target landmark set.
Expand All @@ -110,6 +109,15 @@ def fit_directory(self, target_img_dir, target_height,
target_width : int
Expected image width of the images in the directory
file_extension str (default='.jpg'): File extension of the image files.
pre_check Bool (default=True): Checks that each image has the
dimensions specificed via `target_height`
and `target_width` on the whole directory first to identify
potential issues that are recommended
to be fixed before proceeding. Raises a warning for each image if
dimensions differ from the ones specified and expected.
Returns
-------
self : object
Expand All @@ -128,17 +136,35 @@ def fit_directory(self, target_img_dir, target_height,
raise ValueError('No images found in %s with extension %s.'
% (target_img_dir, file_extension))

if self.verbose >= 1:
print("Fitting the average facial landmarks "
"for %d face images " % (len(file_list)))
landmarks_list = []

if pre_check:
if self.verbose >= 1:
print('Pre-Checking directory for'
' consistent image dimensions...')
pbar = ProgBar(len(file_list))
for f in file_list:
img = read_image(filename=f, path=target_img_dir)
if self.verbose >= 1:
pbar.update()
if (img.shape[0] != self.target_height_
or img.shape[1] != self.target_width_):
warnings.warn('Image %s has '
'dimensions %d x %d '
'instead of %d x %d.'
% (f, img.shape[0],
img.shape[1],
self.target_height_,
self.target_width_))

if self.verbose >= 1:
print("Fitting the average facial landmarks "
"for %d face images " % (len(file_list)))
pbar = ProgBar(len(file_list))
for f in file_list:
img = read_image(filename=f, path=target_img_dir)
if self.verbose >= 1:
pbar.update()
img = read_image(filename=f, path=target_img_dir)

if self.target_width_ != img.shape[1]:
width_ratio = self.target_width_ / img.shape[1]
Expand Down

0 comments on commit ea9e9db

Please sign in to comment.