diff --git a/docs/sources/user_guide/image/eyepad_align.ipynb b/docs/sources/user_guide/image/eyepad_align.ipynb index 576e012b4..51dd11cc2 100644 --- a/docs/sources/user_guide/image/eyepad_align.ipynb +++ b/docs/sources/user_guide/image/eyepad_align.ipynb @@ -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", @@ -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" ] }, @@ -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", @@ -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": [ - "" + "" ] }, "execution_count": 10, @@ -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", @@ -618,7 +648,7 @@ "\n", "
\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", @@ -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", diff --git a/mlxtend/image/eyepad_align.py b/mlxtend/image/eyepad_align.py index 9a5ee5e8a..8b7e38e85 100644 --- a/mlxtend/image/eyepad_align.py +++ b/mlxtend/image/eyepad_align.py @@ -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/ @@ -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. @@ -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 @@ -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]