detector_test.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import numpy as np
  2. from PIL import Image
  3. from anonymizer.utils import Box
  4. from anonymizer.detection import Detector
  5. from anonymizer.detection import download_weights, get_weights_path
  6. def box_covers_box(covering_box: Box, covered_box: Box):
  7. return (covered_box.x_min > covering_box.x_min and covered_box.y_min > covering_box.y_min and
  8. covered_box.x_max < covering_box.x_max and covered_box.y_max < covering_box.y_max)
  9. def load_np_image(image_path):
  10. image = Image.open(image_path).convert('RGB')
  11. np_image = np.array(image)
  12. return np_image
  13. class TestDetector:
  14. @staticmethod
  15. def test_it_detects_obvious_faces(tmp_path):
  16. weights_directory = tmp_path / 'weights'
  17. face_weights_path = get_weights_path(weights_directory, kind='face')
  18. download_weights(weights_directory)
  19. detector = Detector(kind='face', weights_path=face_weights_path)
  20. np_image = load_np_image('./test/detection/face_test_image.jpg')
  21. left_face = Box(x_min=267, y_min=64, x_max=311, y_max=184, score=0.0, kind='face')
  22. right_face = Box(x_min=369, y_min=68, x_max=420, y_max=152, score=0.0, kind='face')
  23. boxes = detector.detect(np_image, detection_threshold=0.2)
  24. assert len(boxes) >= 2
  25. for box in boxes:
  26. assert box.score >= 0.2
  27. assert boxes[0].score >= 0.5 and boxes[1].score >= 0.5
  28. assert ((box_covers_box(boxes[0], left_face) and box_covers_box(boxes[1], right_face)) or
  29. (box_covers_box(boxes[1], left_face) and box_covers_box(boxes[0], right_face)))