anonymizer_test.py 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import numpy as np
  2. from PIL import Image
  3. from anonymizer.utils import Box
  4. from anonymizer.anonymization import Anonymizer
  5. def load_np_image(image_path):
  6. image = Image.open(image_path).convert('RGB')
  7. np_image = np.array(image)
  8. return np_image
  9. class MockObfuscator:
  10. def obfuscate(self, image, boxes):
  11. obfuscated_image = np.copy(image)
  12. for box in boxes:
  13. obfuscated_image[int(box.y_min):int(box.y_max), int(box.x_min):int(box.x_max), :] = 0.0
  14. return obfuscated_image
  15. class MockDetector:
  16. def __init__(self, detected_boxes):
  17. self.detected_boxes = detected_boxes
  18. def detect(self, image, detection_threshold):
  19. return self.detected_boxes
  20. class TestAnonymizer:
  21. @staticmethod
  22. def test_it_anonymizes_a_single_image():
  23. np.random.seed(42) # to avoid flaky tests
  24. input_image = np.random.rand(128, 64, 3) # height, width, channels
  25. obfuscator = MockObfuscator()
  26. mock_detector = MockDetector([Box(y_min=0, x_min=10, y_max=20, x_max=30, score=0.5, kind=''),
  27. Box(y_min=100, x_min=10, y_max=120, x_max=30, score=0.9, kind='')])
  28. expected_anonymized_image = np.copy(input_image)
  29. expected_anonymized_image[0:20, 10:30] = 0.0
  30. expected_anonymized_image[100:120, 10:30] = 0.0
  31. anonymizer = Anonymizer(detectors={'face': mock_detector}, obfuscator=obfuscator)
  32. anonymized_image, detected_boxes = anonymizer.anonymize_image(input_image, detection_thresholds={'face': 0.1})
  33. assert np.all(np.isclose(expected_anonymized_image, anonymized_image))
  34. assert detected_boxes == [Box(y_min=0, x_min=10, y_max=20, x_max=30, score=0.5, kind=''),
  35. Box(y_min=100, x_min=10, y_max=120, x_max=30, score=0.9, kind='')]
  36. @staticmethod
  37. def test_it_anonymizes_multiple_images(tmp_path):
  38. np.random.seed(42) # to avoid flaky tests
  39. input_images = [np.random.rand(128, 64, 3), np.random.rand(128, 64, 3), np.random.rand(128, 64, 3)]
  40. obfuscator = MockObfuscator()
  41. mock_detector = MockDetector([Box(y_min=0, x_min=10, y_max=20, x_max=30, score=0.5, kind=''),
  42. Box(y_min=100, x_min=10, y_max=120, x_max=30, score=0.9, kind='')])
  43. expected_anonymized_images = list(map(np.copy, input_images))
  44. for i, _ in enumerate(expected_anonymized_images):
  45. expected_anonymized_images[i] = (expected_anonymized_images[i] * 255).astype(np.uint8)
  46. expected_anonymized_images[i][0:20, 10:30] = 0
  47. expected_anonymized_images[i][100:120, 10:30] = 0
  48. # write input images to disk
  49. input_path = tmp_path / 'input'
  50. input_path.mkdir()
  51. output_path = tmp_path / 'output'
  52. for i, input_image in enumerate(input_images):
  53. image_path = input_path / f'{i}.png'
  54. pil_image = Image.fromarray((input_image * 255).astype(np.uint8), mode='RGB')
  55. pil_image.save(image_path)
  56. anonymizer = Anonymizer(detectors={'face': mock_detector}, obfuscator=obfuscator)
  57. anonymizer.anonymize_images(str(input_path), output_path=str(output_path), detection_thresholds={'face': 0.1},
  58. file_types=['jpg', 'png'], write_json=False)
  59. anonymized_images = []
  60. for image_path in sorted(output_path.glob('**/*.png')):
  61. anonymized_images.append(load_np_image(image_path))
  62. for i, expected_anonymized_image in enumerate(expected_anonymized_images):
  63. assert np.all(np.isclose(expected_anonymized_image, anonymized_images[i]))