test_face_analyser.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import subprocess
  2. import pytest
  3. from facefusion import face_classifier, face_detector, face_landmarker, face_recognizer, state_manager
  4. from facefusion.download import conditional_download
  5. from facefusion.face_analyser import get_many_faces, get_one_face
  6. from facefusion.typing import Face
  7. from facefusion.vision import read_static_image
  8. from .helper import get_test_example_file, get_test_examples_directory
  9. @pytest.fixture(scope = 'module', autouse = True)
  10. def before_all() -> None:
  11. conditional_download(get_test_examples_directory(),
  12. [
  13. 'https://github.com/facefusion/facefusion-assets/releases/download/examples-3.0.0/source.jpg'
  14. ])
  15. subprocess.run([ 'ffmpeg', '-i', get_test_example_file('source.jpg'), '-vf', 'crop=iw*0.8:ih*0.8', get_test_example_file('source-80crop.jpg') ])
  16. subprocess.run([ 'ffmpeg', '-i', get_test_example_file('source.jpg'), '-vf', 'crop=iw*0.7:ih*0.7', get_test_example_file('source-70crop.jpg') ])
  17. subprocess.run([ 'ffmpeg', '-i', get_test_example_file('source.jpg'), '-vf', 'crop=iw*0.6:ih*0.6', get_test_example_file('source-60crop.jpg') ])
  18. state_manager.init_item('execution_device_id', 0)
  19. state_manager.init_item('execution_providers', [ 'cpu' ])
  20. state_manager.init_item('face_detector_angles', [ 0 ])
  21. state_manager.init_item('face_detector_model', 'many')
  22. state_manager.init_item('face_detector_score', 0.5)
  23. state_manager.init_item('face_landmarker_model', 'many')
  24. state_manager.init_item('face_landmarker_score', 0.5)
  25. face_classifier.pre_check()
  26. face_landmarker.pre_check()
  27. face_recognizer.pre_check()
  28. @pytest.fixture(autouse = True)
  29. def before_each() -> None:
  30. face_classifier.clear_inference_pool()
  31. face_detector.clear_inference_pool()
  32. face_landmarker.clear_inference_pool()
  33. face_recognizer.clear_inference_pool()
  34. def test_get_one_face_with_retinaface() -> None:
  35. state_manager.init_item('face_detector_model', 'retinaface')
  36. state_manager.init_item('face_detector_size', '320x320')
  37. face_detector.pre_check()
  38. source_paths =\
  39. [
  40. get_test_example_file('source.jpg'),
  41. get_test_example_file('source-80crop.jpg'),
  42. get_test_example_file('source-70crop.jpg'),
  43. get_test_example_file('source-60crop.jpg')
  44. ]
  45. for source_path in source_paths:
  46. source_frame = read_static_image(source_path)
  47. many_faces = get_many_faces([ source_frame ])
  48. face = get_one_face(many_faces)
  49. assert isinstance(face, Face)
  50. def test_get_one_face_with_scrfd() -> None:
  51. state_manager.init_item('face_detector_model', 'scrfd')
  52. state_manager.init_item('face_detector_size', '640x640')
  53. face_detector.pre_check()
  54. source_paths =\
  55. [
  56. get_test_example_file('source.jpg'),
  57. get_test_example_file('source-80crop.jpg'),
  58. get_test_example_file('source-70crop.jpg'),
  59. get_test_example_file('source-60crop.jpg')
  60. ]
  61. for source_path in source_paths:
  62. source_frame = read_static_image(source_path)
  63. many_faces = get_many_faces([ source_frame ])
  64. face = get_one_face(many_faces)
  65. assert isinstance(face, Face)
  66. def test_get_one_face_with_yoloface() -> None:
  67. state_manager.init_item('face_detector_model', 'yoloface')
  68. state_manager.init_item('face_detector_size', '640x640')
  69. face_detector.pre_check()
  70. source_paths =\
  71. [
  72. get_test_example_file('source.jpg'),
  73. get_test_example_file('source-80crop.jpg'),
  74. get_test_example_file('source-70crop.jpg'),
  75. get_test_example_file('source-60crop.jpg')
  76. ]
  77. for source_path in source_paths:
  78. source_frame = read_static_image(source_path)
  79. many_faces = get_many_faces([ source_frame ])
  80. face = get_one_face(many_faces)
  81. assert isinstance(face, Face)
  82. def test_get_many_faces() -> None:
  83. source_path = get_test_example_file('source.jpg')
  84. source_frame = read_static_image(source_path)
  85. many_faces = get_many_faces([ source_frame, source_frame, source_frame ])
  86. assert isinstance(many_faces[0], Face)
  87. assert isinstance(many_faces[1], Face)
  88. assert isinstance(many_faces[2], Face)