anonymize.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. """
  2. Copyright 2018 understand.ai
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. """
  13. import argparse
  14. from anonymizer.anonymization import Anonymizer
  15. from anonymizer.detection import Detector, download_weights, get_weights_path
  16. from anonymizer.obfuscation import Obfuscator
  17. def parse_args():
  18. parser = argparse.ArgumentParser(
  19. description='Anonymize faces and license plates in a series of images.')
  20. parser.add_argument('--input', required=True,
  21. metavar='/path/to/input_folder',
  22. help='Path to a folder that contains the images that should be anonymized. '
  23. 'Images can be arbitrarily nested in subfolders and will still be found.')
  24. parser.add_argument('--image-output', required=True,
  25. metavar='/path/to/output_foler',
  26. help='Path to the folder the anonymized images should be written to. '
  27. 'Will mirror the folder structure of the input folder.')
  28. parser.add_argument('--weights', required=True,
  29. metavar='/path/to/weights_foler',
  30. help='Path to the folder where the weights are stored. If no weights with the '
  31. 'appropriate names are found they will be downloaded automatically.')
  32. parser.add_argument('--image-extensions', required=False, default='jpg,png',
  33. metavar='"jpg,png"',
  34. help='Comma-separated list of file types that will be anonymized')
  35. parser.add_argument('--face-threshold', type=float, required=False, default=0.3,
  36. metavar='0.3',
  37. help='Detection confidence needed to anonymize a detected face. '
  38. 'Must be in [0.001, 1.0]')
  39. parser.add_argument('--plate-threshold', type=float, required=False, default=0.3,
  40. metavar='0.3',
  41. help='Detection confidence needed to anonymize a detected license plate. '
  42. 'Must be in [0.001, 1.0]')
  43. parser.add_argument('--write-detections', dest='write_detections', action='store_true')
  44. parser.add_argument('--no-write-detections', dest='write_detections', action='store_false')
  45. parser.set_defaults(write_detections=True)
  46. parser.add_argument('--obfuscation-kernel', required=False, default='21,2,9',
  47. metavar='kernel_size,sigma,box_kernel_size',
  48. help='This parameter is used to change the way the blurring is done. '
  49. 'For blurring a gaussian kernel is used. The default size of the kernel is 21 pixels '
  50. 'and the default value for the standard deviation of the distribution is 2. '
  51. 'Higher values of the first parameter lead to slower transitions while blurring and '
  52. 'larger values of the second parameter lead to sharper edges and less blurring. '
  53. 'To make the transition from blurred areas to the non-blurred image smoother another '
  54. 'kernel is used which has a default size of 9. Larger values lead to a smoother '
  55. 'transition. Both kernel sizes must be odd numbers.')
  56. args = parser.parse_args()
  57. print(f'input: {args.input}')
  58. print(f'image-output: {args.image_output}')
  59. print(f'weights: {args.weights}')
  60. print(f'image-extensions: {args.image_extensions}')
  61. print(f'face-threshold: {args.face_threshold}')
  62. print(f'plate-threshold: {args.plate_threshold}')
  63. print(f'write-detections: {args.write_detections}')
  64. print(f'obfuscation-kernel: {args.obfuscation_kernel}')
  65. print()
  66. return args
  67. def main(input_path, image_output_path, weights_path, image_extensions, face_threshold, plate_threshold,
  68. write_json, obfuscation_parameters):
  69. download_weights(download_directory=weights_path)
  70. kernel_size, sigma, box_kernel_size = obfuscation_parameters.split(',')
  71. obfuscator = Obfuscator(kernel_size=int(kernel_size), sigma=float(sigma), box_kernel_size=int(box_kernel_size))
  72. detectors = {
  73. 'face': Detector(kind='face', weights_path=get_weights_path(weights_path, kind='face')),
  74. 'plate': Detector(kind='plate', weights_path=get_weights_path(weights_path, kind='plate'))
  75. }
  76. detection_thresholds = {
  77. 'face': face_threshold,
  78. 'plate': plate_threshold
  79. }
  80. anonymizer = Anonymizer(obfuscator=obfuscator, detectors=detectors)
  81. anonymizer.anonymize_images(input_path=input_path, output_path=image_output_path,
  82. detection_thresholds=detection_thresholds, file_types=image_extensions.split(','),
  83. write_json=write_json)
  84. if __name__ == '__main__':
  85. args = parse_args()
  86. main(input_path=args.input, image_output_path=args.image_output, weights_path=args.weights,
  87. image_extensions=args.image_extensions,
  88. face_threshold=args.face_threshold, plate_threshold=args.plate_threshold,
  89. write_json=args.write_detections, obfuscation_parameters=args.obfuscation_kernel)