├── .gitignore ├── LICENSE ├── MANIFEST.in ├── README.md ├── bin └── smartcrop ├── setup.py └── smartcrop ├── __init__.py └── cascades └── haarcascade_frontalface_default.xml /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | env/ 12 | build/ 13 | develop-eggs/ 14 | dist/ 15 | downloads/ 16 | eggs/ 17 | .eggs/ 18 | lib/ 19 | lib64/ 20 | parts/ 21 | sdist/ 22 | var/ 23 | wheels/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | 28 | # PyInstaller 29 | # Usually these files are written by a python script from a template 30 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 31 | *.manifest 32 | *.spec 33 | 34 | # Installer logs 35 | pip-log.txt 36 | pip-delete-this-directory.txt 37 | 38 | # Unit test / coverage reports 39 | htmlcov/ 40 | .tox/ 41 | .coverage 42 | .coverage.* 43 | .cache 44 | nosetests.xml 45 | coverage.xml 46 | *.cover 47 | .hypothesis/ 48 | 49 | # Translations 50 | *.mo 51 | *.pot 52 | 53 | # Django stuff: 54 | *.log 55 | local_settings.py 56 | 57 | # Flask stuff: 58 | instance/ 59 | .webassets-cache 60 | 61 | # Scrapy stuff: 62 | .scrapy 63 | 64 | # Sphinx documentation 65 | docs/_build/ 66 | 67 | # PyBuilder 68 | target/ 69 | 70 | # Jupyter Notebook 71 | .ipynb_checkpoints 72 | 73 | # pyenv 74 | .python-version 75 | 76 | # celery beat schedule file 77 | celerybeat-schedule 78 | 79 | # SageMath parsed files 80 | *.sage.py 81 | 82 | # dotenv 83 | .env 84 | 85 | # virtualenv 86 | .venv 87 | venv/ 88 | ENV/ 89 | 90 | # Spyder project settings 91 | .spyderproject 92 | .spyproject 93 | 94 | # Rope project settings 95 | .ropeproject 96 | 97 | # mkdocs documentation 98 | /site 99 | 100 | # mypy 101 | .mypy_cache/ 102 | 103 | .idea -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 EPIXELIC 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | include smartcrop/cascades/* -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # python-smart-crop 2 | 3 | Smart crops images uisng OpenCV 4 | 5 | Uses the algorithms described in https://github.com/thumbor/thumbor/wiki/Detection-algorithms but actually combining both methods. We try to detect faces on the image, then, in any case we detect features. We then combine both results with different weights, so face detection is, in this case 3,33 times stronger than feature detection. 6 | 7 | ## Installing 8 | 9 | Requires python-opencv, install the dependency on debian with `apt install python-opencv`. 10 | 11 | Install the command using PIP: `pip install git+https://github.com/epixelic/python-smart-crop` 12 | 13 | Tested on Debian 8 and Ubuntu WSL. 14 | 15 | Usage: `smartcrop -W 1140 -H 400 -i input.jpg -o output.jpg` 16 | 17 | See `smartcrop --help` 18 | -------------------------------------------------------------------------------- /bin/smartcrop: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import smartcrop 4 | smartcrop.main() 5 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | from setuptools import setup 2 | 3 | setup(name='smartcrop', 4 | version='0.4', 5 | description='OpenCV smart crop', 6 | url='https://github.com/epixelic/python-smart-crop', 7 | author='Josua Gonzalez', 8 | author_email='jgonzalez@epixelic.com', 9 | include_package_data=True, 10 | license='MIT', 11 | packages=['smartcrop'], 12 | scripts=['bin/smartcrop'], 13 | zip_safe=False) 14 | -------------------------------------------------------------------------------- /smartcrop/__init__.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | from __future__ import division 3 | 4 | import cv2 5 | import argparse 6 | import os 7 | import math 8 | 9 | # Algorithm parameters 10 | COMBINE_FACE_WEIGHT = 10 11 | COMBINE_FEATURE_WEIGHT = 10 12 | FEATURE_DETECT_MAX_CORNERS = 50 13 | FEATURE_DETECT_QUALITY_LEVEL = 0.1 14 | FEATURE_DETECT_MIN_DISTANCE = 10 15 | FACE_DETECT_REJECT_LEVELS = 1.3 16 | FACE_DETECT_LEVEL_WEIGHTS = 5 17 | 18 | cascade_path = os.path.dirname(__file__) + '/cascades/haarcascade_frontalface_default.xml' 19 | 20 | 21 | def center_from_faces(matrix): 22 | face_cascade = cv2.CascadeClassifier(cascade_path) 23 | faces = face_cascade.detectMultiScale(matrix, FACE_DETECT_REJECT_LEVELS, FACE_DETECT_LEVEL_WEIGHTS) 24 | 25 | x, y = (0, 0) 26 | weight = 0 27 | 28 | # iterate over our faces array 29 | for (x, y, w, h) in faces: 30 | print('Face detected at ', x, y, w, h) 31 | weight += w * h 32 | x += (x + w / 2) * w * h 33 | y += (y + h / 2) * w * h 34 | 35 | if len(faces) == 0: 36 | return False 37 | 38 | return { 39 | 'x': x / weight, 40 | 'y': y / weight, 41 | 'count': len(faces) 42 | } 43 | 44 | 45 | def center_from_good_features(matrix): 46 | x, y = (0, 0) 47 | weight = 0 48 | corners = cv2.goodFeaturesToTrack(matrix, FEATURE_DETECT_MAX_CORNERS, FEATURE_DETECT_QUALITY_LEVEL, 49 | FEATURE_DETECT_MIN_DISTANCE) 50 | 51 | for point in corners: 52 | weight += 1 53 | x += point[0][0] 54 | y += point[0][1] 55 | 56 | return { 57 | 'x': x / weight, 58 | 'y': y / weight, 59 | 'count': weight 60 | } 61 | 62 | 63 | def exact_crop(center, original_width, original_height, target_width, target_height): 64 | top = max(center['y'] - math.floor(target_height / 2), 0) 65 | offset_h = top + target_height 66 | if offset_h > original_height: 67 | # overflowing 68 | # print("Top side over by ", offsetH - original_height) 69 | top = top - (offset_h - original_height) 70 | top = max(top, 0) 71 | bottom = min(offset_h, original_height) 72 | 73 | left = max(center['x'] - math.floor(target_width / 2), 0) 74 | offset_w = left + target_width 75 | if offset_w > original_width: 76 | # overflowing 77 | # print("Left side over by ", offsetW - original_width) 78 | left = left - (offset_w - original_width) 79 | left = max(left, 0) 80 | right = min(left + target_width, original_width) 81 | 82 | return { 83 | 'left': left, 84 | 'right': right, 85 | 'top': top, 86 | 'bottom': bottom 87 | } 88 | 89 | 90 | def auto_resize(image, target_width, target_height): 91 | height, width, depth = image.shape 92 | 93 | ratio = target_width / width 94 | w, h = width * ratio, height * ratio 95 | p = 1 96 | 97 | # if there is still height or width to compensate, let's do it 98 | if w - target_width < 0 or h - target_height < 0: 99 | ratio = max(target_width / w, target_height / h) 100 | w, h = w * ratio, h * ratio 101 | p = 2 102 | 103 | image = cv2.resize(image, (int(w), int(h))) 104 | print("Image resized by", w - width, "*", h - height, "in", p, "pass(es)") 105 | 106 | return image 107 | 108 | 109 | def auto_center(matrix): 110 | face_center = center_from_faces(matrix) 111 | center = {'x': 0, 'y': 0} 112 | 113 | if not face_center: 114 | print('Using Good Feature Tracking method') 115 | center = center_from_good_features(matrix) 116 | else: 117 | print('Combining with Good Feature Tracking method') 118 | features_center = center_from_good_features(matrix) 119 | face_w = features_center['count'] * COMBINE_FACE_WEIGHT 120 | feat_w = features_center['count'] * COMBINE_FEATURE_WEIGHT 121 | t_w = face_w + feat_w 122 | center['x'] = (face_center['x'] * face_w + features_center['x'] * feat_w) / t_w 123 | center['y'] = (face_center['y'] * face_w + features_center['y'] * feat_w) / t_w 124 | 125 | print('Face center', face_center) 126 | print('Feat center', features_center) 127 | 128 | return center 129 | 130 | 131 | def smart_crop(image, target_width, target_height, destination, do_resize): 132 | # read grayscale image 133 | original = cv2.imread(image) 134 | 135 | if original is None: 136 | print("Could not read source image") 137 | exit(1) 138 | 139 | target_height = int(target_height) 140 | target_width = int(target_width) 141 | 142 | if do_resize: 143 | original = auto_resize(original, target_width, target_height) 144 | 145 | # build the grayscale image we will work onto 146 | matrix = cv2.cvtColor(original, cv2.COLOR_BGR2GRAY) 147 | height, width, depth = original.shape 148 | 149 | if target_height > height: 150 | print('Warning: target higher than image') 151 | 152 | if target_width > width: 153 | print('Warning: target wider than image') 154 | 155 | # center = center_from_faces(matrix) 156 | # 157 | # if not center: 158 | # print('Using Good Feature Tracking method') 159 | # center = center_from_good_features(matrix) 160 | center = auto_center(matrix) 161 | 162 | print('Found center at', center) 163 | 164 | crop_pos = exact_crop(center, width, height, target_width, target_height) 165 | print('Crop rectangle is', crop_pos) 166 | 167 | cropped = original[int(crop_pos['top']): int(crop_pos['bottom']), int(crop_pos['left']): int(crop_pos['right'])] 168 | cv2.imwrite(destination, cropped) 169 | 170 | 171 | def main(): 172 | ap = argparse.ArgumentParser() 173 | ap.add_argument("-W", "--width", required=True, help="Target width") 174 | ap.add_argument("-H", "--height", required=True, help="Target height") 175 | ap.add_argument("-i", "--image", required=True, help="Image to crop") 176 | ap.add_argument("-o", "--output", required=True, help="Output") 177 | ap.add_argument("-n", "--no-resize", required=False, default=False, action="store_true", 178 | help="Don't resize image before treating it") 179 | 180 | args = vars(ap.parse_args()) 181 | 182 | smart_crop(args["image"], args["width"], args["height"], args["output"], not args["no_resize"]) 183 | 184 | 185 | if __name__ == '__main__': 186 | main() 187 | --------------------------------------------------------------------------------