.. _background_removal:

=======================
Face background removal
=======================

Background removal should be performed in two steps:

#. Call the :ref:`FaceAnalyser.SegmentBackground<id3_face_face_analyser_segment_background_class_method>` method to create a segmentation mask.
#. Call the :ref:`FaceAnalyser.applyMask<id3_face_face_analyser_apply_mask_class_method>` method to apply the segmentation mask and replace the background by a specified color.

.. important:: 
    The FaceBackgroundSegmenter1A model is required to suppress the background. *(Before 9.11.0, the model was FaceSegmenter1A)*


If the face is approximately centered and in the foreground of the image, directly use the BackgroundSegmentation functions.

Alpha mask
==========
.. image:: img/alpha_mask.jpg
  :width: 30%

Segmented image
===============
.. image:: img/background_removal.jpg
  :width: 30%

Example
=======

The example below demonstrates how to remove the background on a face image.

.. tab-set::

  .. tab-item:: Python
    :sync: Python

    .. literalinclude:: /../samples/sample.py
      :language: python
      :start-after: [face_background_removal]
      :end-before: [face_background_removal]
      :dedent: 4


Deal with faces not centered
============================

If the face is not the main subject of the photo, quite small or on the side, we recommand to first run a :ref:`Face detection<face_detection>` step and coarsely crop the image around the face

.. tab-set::

  .. tab-item:: Python
    :sync: Python

    .. literalinclude:: /../samples/sample.py
      :language: python
      :start-after: [face_background_removal_crop]
      :end-before: [face_background_removal_crop]
      :dedent: 4



See also
========
- :ref:`id3_face_face_analyser_class`
- :ref:`id3_face_face_analyser_segment_face_class_method`
- :ref:`id3_face_face_analyser_apply_mask_class_method`
- :ref:`id3_face_face_analyser_apply_alpha_mask_class_method`
