├── sample.jpg ├── sample_blurred.jpg ├── .vscode └── settings.json ├── README.md ├── LICENSE ├── .gitignore └── faceblur.py /sample.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/telesoho/faceblur/HEAD/sample.jpg -------------------------------------------------------------------------------- /sample_blurred.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/telesoho/faceblur/HEAD/sample_blurred.jpg -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "python.linting.pylintArgs": [ 3 | "--extension-pkg-whitelist=cv2" 4 | ] 5 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # faceblur 2 | Recognize and blur all faces in photo. 3 | 4 | ## requirement 5 | 6 | 1. [opencv](https://github.com/opencv/opencv) 7 | 1. [face recognition](https://github.com/ageitgey/face_recognition) 8 | 9 | ## Usage 10 | python faceblur [source image/source folder] [destination image/destination folder] 11 | 12 | Blur all faces in a photo or all photo in source folder. 13 | 14 | ## Blur Effect Sample 15 | Source image: 16 | 17 | ![](sample.jpg) 18 | 19 | Blurred image: 20 | 21 | ![](sample_blurred.jpg) -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 WenHong.Tan 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 | -------------------------------------------------------------------------------- /.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 | -------------------------------------------------------------------------------- /faceblur.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Recognize and blur all faces in photos. 3 | ''' 4 | import argparse 5 | import cv2 6 | import face_recognition 7 | import os 8 | import sys 9 | from shutil import copyfile 10 | 11 | def face_blur(src_img, dest_img, args): 12 | ''' 13 | Recognize and blur all faces in the source image file, then save as destination image file. 14 | ''' 15 | sys.stdout.write("%s:processing... \r" % (src_img)) 16 | sys.stdout.flush() 17 | 18 | # Initialize some variables 19 | face_locations = [] 20 | photo = face_recognition.load_image_file(src_img) 21 | # Resize image to 1/zoom_in size for faster face detection processing 22 | small_photo = cv2.resize(photo, (0, 0), fx=1/args.zoom_in, fy=1/args.zoom_in) 23 | 24 | # Find all the faces and face encodings in the current frame of video 25 | face_locations = face_recognition.face_locations(small_photo, model=args.model, number_of_times_to_upsample=args.upsampling) 26 | 27 | if face_locations: 28 | print("%s:There are %s faces at " % (src_img, len(face_locations)), face_locations) 29 | else: 30 | print('%s:There are no any face.' % (src_img)) 31 | if args.copy: 32 | copyfile(src_img, dest_img) 33 | print('Original photo has been saved in %s' % dest_img) 34 | return False 35 | 36 | #Blur all face 37 | photo = cv2.imread(src_img) 38 | for top, right, bottom, left in face_locations: 39 | # Scale back up face locations since the frame we detected in was scaled to 1/zoom_in size 40 | top *= args.zoom_in 41 | right *= args.zoom_in 42 | bottom *= args.zoom_in 43 | left *= args.zoom_in 44 | 45 | # Extract the region of the image that contains the face 46 | face_image = photo[top:bottom, left:right] 47 | 48 | # Blur the face image 49 | face_image = cv2.GaussianBlur(face_image, (args.blurr, args.blurr), 0) 50 | 51 | # Put the blurred face region back into the frame image 52 | photo[top:bottom, left:right] = face_image 53 | 54 | #Save image to file 55 | cv2.imwrite(dest_img, photo) 56 | 57 | print('Face blurred photo has been saved in %s' % dest_img) 58 | 59 | return True 60 | 61 | def blur_all_photo(src_dir, dest_dir, args): 62 | ''' 63 | Blur all faces in the source directory photos and copy them to destination directory 64 | ''' 65 | src_dir = os.path.abspath(src_dir) 66 | dest_dir = os.path.abspath(dest_dir) 67 | print('Search and blur human faces in %s''s photo.' % src_dir) 68 | for root, subdirs, files in os.walk(src_dir): 69 | root_relpath = os.path.relpath(root, src_dir) 70 | new_root_path = os.path.realpath(os.path.join(dest_dir, root_relpath)) 71 | os.makedirs(new_root_path, exist_ok=True) 72 | 73 | for filename in files: 74 | ext = os.path.splitext(filename)[1] 75 | if ext == '.jpg': 76 | srcfile_path = os.path.join(root, filename) 77 | destfile_path = os.path.join(new_root_path, os.path.basename(filename)) 78 | face_blur(srcfile_path, destfile_path, args) 79 | 80 | if __name__ == '__main__': 81 | parser = argparse.ArgumentParser( 82 | description='''Recognize and blur all faces in photo. faceblur v1.0.0 (c) telesoho.com''', 83 | formatter_class=argparse.ArgumentDefaultsHelpFormatter) 84 | parser.add_argument('src', help='source image or directory') 85 | parser.add_argument('dest', help='destination image or directory') 86 | parser.add_argument('-c', '--copy', action="store_true", default=False, help='copy src image to dest even if no faces have been detected') 87 | parser.add_argument('-m', '--model', default='cnn', choices=['cnn', 'hog'], help='recognition model, Convolutional Neural Network (CNN) or Histogram of Oriented Gradients (HOG)') 88 | parser.add_argument('-b', '--blurr', default=21, type=int, help='gaussian blurr kernel size (the neighbors to be considered), number must be odd') 89 | parser.add_argument('-u', '--upsampling', default=1, type=int, help='upsampling factor, higher values increase face recognition rate') 90 | parser.add_argument('-z', '--zoom_in', default=1, type=int, help='zoom in factor, higher values increase face detection speed') 91 | args = parser.parse_args() 92 | 93 | if args.blurr % 2 == 0: 94 | raise argparse.ArgumentTypeError("%s is not an odd int value" % args.blurr) 95 | 96 | if os.path.isfile(args.src): 97 | face_blur(args.src, args.dest, args) 98 | else: 99 | blur_all_photo(args.src, args.dest, args) 100 | --------------------------------------------------------------------------------