How to Deface Faces in Pictures and Videos With Free CLI Tool

Linux TLDR
Last Updated:
Reading time: 3 minutes

Deface is a free and open-source command-line tool that allows you to automatically blur human faces in pictures or videos without relying on any external editing software or online service.

For videos, it first detects all human faces frame by frame and then applies your selected blur anonymization filter to each detected face region, but note that it will strip the audio by default.

This tool is specially built for defacing human faces, but out of curiosity, I tried feeding animals, anime characters, and groups of people, and you can see the result below.

As you can see, it performs well with human faces but not with animals and cartoons; during testing with various pictures, it occasionally functions if the face geometry resembles a human, but most of the time it won’t do anything.

However, it was just my curiosity to experiment with faces other than human and share those curious individuals like me, but in reality, no one is going to deface animal or cartoon characters, and for human faces, it works like a charm.

So, read this article to learn how to install this tool on your system and use it to deface your next solo or group photo without relying on any editing software or online services.

Tutorial Details

DescriptionDeface: Anonymize Human Faces
Difficulty LevelLow
Root or Sudo PrivilegesNo
OS CompatibilityLinux, Windows, and macOS
PrerequisitesPython and PIP
Internet RequiredYes (for installation)

How to Install Deface on Linux, Windows, and macOS

The β€œdeface” is available for all popular operating systems, such as Linux, Windows, and macOS, but it requires Python with PIP installed on those systems as a prerequisite.

So, if you already have them installed, then execute the following command to install it.

$ python3 -m pip install deface

Once the installation is finished, the β€œdeface” command will become accessible.

How to Deface Faces in Pictures and Videos Using Deface

To demonstrate the usage of the β€œdeface” command, we will deface faces for a group of people from the picture and video using different anonymization filters or replace them with custom pictures.

Deface Faces From a Picture

To deface human faces in a picture, you can use the β€œdeface” command by specifying the path or filename of the picture, the β€œ--replacewith” option with anonymization filter mode (e.g., β€œblurβ€œ, β€œsolidβ€œ, β€œmosaicβ€œ), and lastly, the β€œ-o” option specifying the output filename.

The following is an example of defacing a group of human faces with all the filter modes.

#Anonymize the faces in blur filter mode.
$ deface input.jpg --replacewith blur -o output.jpg

#Anonymize the faces in solid filter mode.
$ deface input.jpg --replacewith solid -o output.jpg

#Anonymize the faces in mosaic filter mode.
$ deface input.jpg --replacewith mosaic -o output.jpg

The following is an output of the defaced pictures:

Deface Faces With A Custom Picture

Despite the anonymization filter mode, you can deface faces with any custom image by using the filter mode β€œimgβ€œ, specifying the β€œ--replaceimg” option with a path or filename of the picture that will replace the faces.

For example, the following command will replace all the human faces in the β€œinput.jpg” picture with the β€œemoji.png” picture.

$ deface input.jpg --replacewith img --replaceimg emoji.png -o output.jpg

Output:

replacing the faces with custom picture

Deface Faces From a Video

Defacing faces from a video can also be achieved using the same command by simply replacing the picture with your video file. Note that, by default, it will remove the audio from the video. To keep the audio file, you have to use the β€œ--keep-audio” or β€œ-k” option.

Here’s a command-line example of defacing a video file without audio, with audio, and replacing the faces with a custom image.

#Anonymize the faces from the input file with no audio output.
$ deface input.mp4 --replacewith blur -o output.mp4

#Anonymize the faces from the input file with audio output.
$ deface input.mp4 --replacewith blur --keep-audio -o output.mp4

#Anonymize the faces from the input file with a custom image.
$ deface input.mp4 --replacewith blur img --replaceimg emoji.png -o output.mp4

The following is an output of the defaced video created using the first command:

Final Word

I’ll conclude this article, but you can dig deeper and experiment with various options, such as scaling, drawing scores, thresholds, etc., by checking the help section using the β€œdeface -h” command.

Now, if you have any questions or queries related to the topic, do let me know in the comment section.

Till then, peace!

Join The Conversation

Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.