[Guide] Applying Original Faceswap Metadata Headers to PNGs That Have Had Them Removed/Broken After Post-Processing

Want to know about the Faceswap's Face Extraction process? Got tips, ideas or just want to learn about how it all works? Then this is the place for you


Forum rules

Read the FAQs and search the forum before posting a new topic.

This forum is for discussing tips and understanding the process involved for Extracting and preparing face sets for training a model in Faceswap.

If you have found a bug are having issues with the Extraction process not working, then you should post in the Extract Support forum.

Please mark any answers that fixed your problems so others can find the solutions.

Post Reply
User avatar
ianstephens
Posts: 117
Joined: Sun Feb 14, 2021 7:20 pm
Has thanked: 12 times
Been thanked: 15 times

[Guide] Applying Original Faceswap Metadata Headers to PNGs That Have Had Them Removed/Broken After Post-Processing

Post by ianstephens »

This is a simple guide to import the correct metadata headers/PNG chunk information that Faceswap generates during extraction and bakes into the PNG files.

Intro
No matter what we used, any image editing software would screw up and adulterate the original metadata headers that Faceswap added to the PNG files after extraction.

This metadata contains important information such as masking, alignments, and more. Originally, it would exist in a separate alignments file but with the recent Faceswap updates, all of this data is now baked and included inside the extracted PNG file - which is indeed more convenient and easy to use.

We like to color correct our images after extraction purely because we can achieve the best color when the image has been made smaller and there are no other color/lighting distractions from a full-size image or video frame.

Prerequisites
You will need to have knowledge of using (or at least be familiar with) using the command line.

Commands in this guide are based on a nix-based system (specifically macOS).

You must also have ExifTool installed/compiled on your machine (Windows, macOS, Linux):
https://exiftool.org/

You must also have an original extracted dataset with original Faceswap metadata included.

Simple Guide

So you've extracted all of your face images from your input video or set of photos. They look great, but you'd like to color correct, adjust brightness or lighting, or even make them more vivid before you start training to ensure you get the best training set possible.

It's important you do not change the image structure in any way - all of the alignments/masks match the extracted image so you may only adjust color and other settings - but do not change/crop/resize any of the images - that's important.

Perform these first steps:

  • Run the usual extraction for your training set.

  • Do the usual cleanup/order by face/remove the junk and ensure this is the set you are happy to train with.

  • Create two copies of that final training set folder - for example let's use "original" and "fixed" one will be the originals and the second is the one you are going to work with for fixing up.

  • Perform your fixup on the images inside the "fixed" folder - either manually or using automation/batch processing.

Chances are, your editing software (we didn't come across any that didn't) has stripped/modified/broken the Faceswap data included in the originals. These fixed/edited images are now useless for training.

Fix them by doing the following

Create a custom ExifTool config

First, you need to create a custom config file for ExifTool. Out of the box, ExifTool will not re-write headers not included inside its codebase. The "Faceswap" header is not included in standards.

Create a custom config file for ExifTool. A sample file that you can use to create your own can be found at:
https://exiftool.org/config.html

For simplicity, let's use the example config above.

Head on down to:

  1. PNG:NewPngTag1

Code: Select all

 # new PNG tags are added to the PNG::TextualData table:
    'Image::ExifTool::PNG::TextualData' => {
        # Example 12.  PNG:NewPngTag1
        NewPngTag1 => { },
        # Example 13.  PNG:NewPngTag2
        NewPngTag2 => { },
        # Example 14.  PNG:NewPngTag3
        NewPngTag3 => { },
    },

Simply change that above code block to:

Code: Select all

 # new PNG tags are added to the PNG::TextualData table:
    'Image::ExifTool::PNG::TextualData' => {
        # Example 12.  PNG:NewPngTag1
         faceswap => { iTXt => 1 },
        # Example 13.  PNG:NewPngTag2
        NewPngTag2 => { },
        # Example 14.  PNG:NewPngTag3
        NewPngTag3 => { },
    },

Notice the "Faceswap" PNG TextualData being assigned.

Save the config file and you may then use it to work with the Faceswap header data. You may add the config to your ENV variables but for simplicity, we simply call it from the command line when using ExifTool. Let's say we saved the config file to "~/exiftool.conf".

Enter into your "fixed" folder (the folder containing all of the fixed-up images):

Enter the following command:

The original folder containing the original extracted image is "original" and the folder containing the fixed-up images is "fixed"

  • Code: Select all

    exiftool -config ~/exiftool.conf -all= -tagsfromfile ~/original/%f.png -all:all ~/fixed -overwrite_original

The command above will essentially cycle through the entire dataset doing the following:

  • Remove useless header data your image software has added and strip the file of ALL metadata (let's start fresh).

  • Extract from the original extracted image and insert the Faceswap metadata into the PNG header (chunk) in the correct format into the new fixed-up image.

  • Add any other existing metadata Faceswap has added inside standards from the original extracted image to the new fixed-up image.

I hope this helps somebody who had the same issues as us. Like I said, before the dev team moved the data into the image file headers this wasn't an issue but hopefully this easy fix above will be useful going forward.

Any additional questions, adjustments or if you get stuck, I pop in here often and will reply as well as I can.

Last edited by ianstephens on Sun Aug 29, 2021 9:16 am, edited 1 time in total.

Tags:
User avatar
torzdf
Posts: 2651
Joined: Fri Jul 12, 2019 12:53 am
Answers: 159
Has thanked: 129 times
Been thanked: 622 times

Re: [Guide] Applying Original Faceswap Metadata Headers to PNGs That Have Had Them Removed/Broken After Post-Processing

Post by torzdf »

Thanks for this useful and thorough guide.

Pinned.

My word is final

Post Reply