feature_detection.rst 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. .. _image_feature_detection:
  2. Feature Detection
  3. =================
  4. Wagtail has the ability to automatically detect faces and features inside your images and crop the images to those features.
  5. Feature detection uses third-party tools to detect faces/features in an image when the image is uploaded. The detected features are stored internally as a focal point in the ``focal_point_{x, y, width, height}`` fields on the ``Image`` model. These fields are used by the ``fill`` image filter when an image is rendered in a template to crop the image.
  6. Installation
  7. ------------
  8. Two third-party tools are known to work with Wagtail: One based on OpenCV_ for general feature detection and one based on Rustface_ for face detection.
  9. .. _OpenCV: https://opencv.org/
  10. .. _Rustface: https://github.com/torchbox/rustface-py/
  11. OpenCV on Debian/Ubuntu
  12. ^^^^^^^^^^^^^^^^^^^^^^^
  13. Feature detection requires OpenCV_ which can be a bit tricky to install as it's not currently pip-installable.
  14. There is more than one way to install these components, but in each case you will need to test that both OpenCV itself *and* the Python interface have been correctly installed.
  15. Install ``opencv-python``
  16. `````````````````````````
  17. `opencv-python <https://pypi.org/project/opencv-python/>`_ is available on PyPI.
  18. It includes a Python interface to OpenCV, as well as the statically-built OpenCV binaries themselves.
  19. To install:
  20. .. code-block:: console
  21. $ pip install opencv-python
  22. Depending on what else is installed on your system, this may be all that is required. On lighter-weight Linux systems, you may need to identify and install missing system libraries (for example, a slim version of Debian Stretch requires ``libsm6 libxrender1 libxext6`` to be installed with ``apt``).
  23. Install a system-level package
  24. ``````````````````````````````
  25. A system-level package can take care of all of the required components. Check what is available for your operating system. For example, `python-opencv <https://packages.debian.org/stretch/python-opencv>`_ is available for Debian; it installs OpenCV itself, and sets up Python bindings.
  26. However, it may make incorrect assumptions about how you're using Python (for example, which version you're using) - test as described below.
  27. Testing the installation
  28. ````````````````````````
  29. Test the installation::
  30. python3
  31. >>> import cv2
  32. An error such as::
  33. ImportError: libSM.so.6: cannot open shared object file: No such file or directory
  34. indicates that a required system library (in this case ``libsm6``) has not been installed.
  35. On the other hand,
  36. ::
  37. ModuleNotFoundError: No module named 'cv2'
  38. means that the Python components have not been set up correctly in your Python environment.
  39. If you don't get an import error, installation has probably been successful.
  40. Rustface
  41. ^^^^^^^^
  42. Rustface_ is Python library with prebuilt wheel files provided for Linux and macOS. Although implemented in Rust it is pip-installable:
  43. .. code-block:: console
  44. $ pip install wheel
  45. $ pip install rustface
  46. Registering with Willow
  47. ```````````````````````
  48. Rustface provides a plug-in that needs to be registered with Willow_.
  49. This should be done somewhere that gets run on application startup:
  50. .. code-block:: python
  51. from willow.registry import registry
  52. import rustface.willow
  53. registry.register_plugin(rustface.willow)
  54. For example, in an app's AppConfig.ready_.
  55. .. _Willow: https://github.com/wagtail/Willow
  56. .. _AppConfig.ready: https://docs.djangoproject.com/en/2.2/ref/applications/#django.apps.AppConfig.ready
  57. Cropping
  58. --------
  59. The face detection algorithm produces a focal area that is tightly cropped to the face rather than the whole head.
  60. For images with a single face this can be okay in some cases, e.g. thumbnails, it might be overly tight for "headshots".
  61. Image renditions can encompass more of the head by reducing the crop percentage (``-c<percentage>``), at the end of the resize-rule, down to as low as 0%:
  62. .. code-block:: html+django
  63. {% image page.photo fill-200x200-c0 %}
  64. Switching on feature detection in Wagtail
  65. -----------------------------------------
  66. Once installed, you need to set the ``WAGTAILIMAGES_FEATURE_DETECTION_ENABLED`` setting to ``True`` to automatically detect faces/features whenever a new image is uploaded in to Wagtail or when an image without a focal point is saved (this is done via a pre-save signal handler):
  67. .. code-block:: python
  68. # settings.py
  69. WAGTAILIMAGES_FEATURE_DETECTION_ENABLED = True
  70. Manually running feature detection
  71. ----------------------------------
  72. If you already have images in your Wagtail site and would like to run feature detection on them, or you want to apply feature detection selectively when the ``WAGTAILIMAGES_FEATURE_DETECTION_ENABLED`` is set to ``False`` you can run it manually using the `get_suggested_focal_point()` method on the ``Image`` model.
  73. For example, you can manually run feature detection on all images by running the following code in the python shell:
  74. .. code-block:: python
  75. from wagtail.images import get_image_model
  76. Image = get_image_model()
  77. for image in Image.objects.all():
  78. if not image.has_focal_point():
  79. image.set_focal_point(image.get_suggested_focal_point())
  80. image.save()