feature_detection.rst 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 OpenCV to detect faces/features in an image when the image is uploaded. The detected features 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. Setup
  7. -----
  8. Feature detection requires OpenCV which can be a bit tricky to install as it's not currently pip-installable.
  9. Installing OpenCV on Debian/Ubuntu
  10. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  11. Debian and ubuntu provide an apt-get package called ``python-opencv``:
  12. .. code-block:: console
  13. $ sudo apt-get install python-opencv python-numpy
  14. This will install PyOpenCV into your site packages. If you are using a virtual environment, you need to make sure site packages are enabled or Wagtail will not be able to import PyOpenCV.
  15. Enabling site packages in the virtual environment
  16. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  17. If you are not using a virtual envionment, you can skip this step.
  18. Enabling site packages is different depending on whether you are using pyvenv (Python 3.3+ only) or virtualenv to manage your virtual environment.
  19. pyvenv
  20. ``````
  21. Go into your pyvenv directory and open the ``pyvenv.cfg`` file then set ``include-system-site-packages`` to ``true``.
  22. virtualenv
  23. ``````````
  24. Go into your virtualenv directory and delete a file called ``lib/python-x.x/no-global-site-packages.txt``.
  25. Testing the OpenCV installation
  26. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  27. You can test that OpenCV can be seen by Wagtail by opening up a python shell (with your virtual environment active) and typing:
  28. .. code-block:: python
  29. import cv
  30. If you don't see an ``ImportError``, it worked. (If you see the error ``libdc1394 error: Failed to initialize libdc1394``, this is harmless and can be ignored.)
  31. Switching on feature detection in Wagtail
  32. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  33. Once OpenCV is installed, you need to set the ``WAGTAILIMAGES_FEATURE_DETECTION_ENABLED`` setting to ``True``:
  34. .. code-block:: python
  35. # settings.py
  36. WAGTAILIMAGES_FEATURE_DETECTION_ENABLED = True
  37. Manually running feature detection
  38. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  39. Feature detection runs when new images are uploaded in to Wagtail. If you already have images in your Wagtail site and would like to run feature detection on them, you will have to run it manually.
  40. You can manually run feature detection on all images by running the following code in the python shell:
  41. .. code-block:: python
  42. from wagtail.wagtailimages.models import Image
  43. for image in Image.objects.all():
  44. if not image.has_focal_point():
  45. image.set_focal_point(image.get_suggested_focal_point())
  46. image.save()