# AI : Faceswap CLI : tools sort
```
python tools.py sort -i FOLDER [-o OUTDIR] -s ... -g ...
```
## Options
```
-i|--input DIR Input directory of aligned faces.
-o|--output DIR Output directory for sorted aligned faces.
If not provided and 'keep' is selected then a new folder called 'sorted' will be created within the input folder to house the output.
If not provided and 'keep' is not selected then the images will be sorted in-place, overwriting the original contents of the 'input_dir'.
-B|--batch-mode If selected then the input_dir should be a parent folder containing multiple folders of faces you wish to sort.
The faces will be output to separate sub-folders in the output_dir
-s|--sort-by {none,blur,blur-fft,distance,face,face-cnn,face-cnn-dissim,yaw,pitch,roll,hist,hist-dissim,color-black,color-gray,color-luma,color-green,color-orange,size}
Choose how images are sorted.
Selecting a sort method gives the images a new filename based on the order the image appears within the given method.
- none : Don't sort the images.
When a 'group-by' method is selected, selecting 'none' means that the files will be moved/copied into their respective bins, but the files will keep their original filenames.
Selecting 'none' for both 'sort-by' and 'group-by' will do nothing.
- blur : Sort faces by blurriness.
- blur-fft : Sort faces by fft filtered blurriness.
- color-black : Sort images by their number of black pixels.
Useful when faces are near borders and a large part of the image is black.
- color-gray : Sort images by the average intensity of the converted grayscale color channel.
- color-green : Sort images by the average intensity of the converted Cg color channel.
Green images will be ranked first and red images will be last.
- color-luma : Sort images by the average intensity of the converted Y color channel.
Bright lighting and oversaturated images will be ranked first.
- color-orange : Sort images by the average intensity of the converted Co color channel.
Orange images will be ranked first and blue images will be last.
- distance : Sort faces by the estimated distance of the alignments from an 'average' face.
This can be useful for eliminating misaligned faces. Sorts from most like an average face to least like an average face.
- face : Sort faces using VGG Face2 by face similarity.
This uses a pairwise clustering algorithm to check the distances between 512 features on every face in your set and order them appropriately.
- face-cnn : Sort faces by their landmarks.
- face-cnn-dissim : Sort Like 'face-cnn' but sorts by dissimilarity.
- hist : Sort faces by their color histogram.
- hist-dissim : Sort Like 'hist' but sorts by dissimilarity.
- pitch : Sort faces by Pitch (rotation up and down).
- roll : Sort faces by Roll (rotation). Aligned faces should have a roll value close to zero.
The further the Roll value from zero the higher liklihood the face is misaligned.
- size : Sort images by their size in the original frame.
Faces further from the camera and from lower resolution sources will be sorted first, whilst faces closer to the camera and from higher resolution sources will be sorted last.
- yaw : Sort faces by Yaw (rotation left to right).
Default: face
-g|--group-by {none,blur,blur-fft,distance,face,face-cnn,face-cnn-dissim,yaw,pitch,roll,hist,hist-dissim,color-black,color-gray,color-luma,color-green,color-orange,size}
Selecting a group by method will move/copy files into numbered bins based on the selected method.
- none : Don't bin the images. Folders will be sorted by the selected 'sort-by' but will not be binned, instead they will be sorted into a single folder.
Selecting 'none' for both 'sort-by' and 'group-by' will do nothing.
- blur : Group faces by blurriness. Adjust the '-b' ('--bins') parameter to control the number of bins for grouping.
The minimum and maximum values are taken for the chosen sort metric.
The bins are then populated with the results from the group sorting.
- blur-fft : Group faces by fft filtered blurriness.
Adjust the '-b' ('--bins') parameter to control the number of bins for grouping.
The minimum and maximum values are taken for the chosen sort metric.
The bins are then populated with the results from the group sorting.
- color-black : Group images by their number of black pixels.
Useful when faces are near borders and a large part of the image is black.
Adjust the '-b' ('--bins') parameter to control the number of bins for grouping.
Each image is allocated to a bin by the percentage of color pixels that appear in the image.
- color-gray : Group images by the average intensity of the converted grayscale color channel.
Adjust the '-b' ('--bins') parameter to control the number of bins for grouping.
Each image is allocated to a bin by the percentage of color pixels that appear in the image.
- color-green : Group images by the average intensity of the converted Cg color channel.
Green images will be ranked first and red images will be last.
Adjust the '-b' ('--bins') parameter to control the number of bins for grouping.
Each image is allocated to a bin by the percentage of color pixels that appear in the image.
- color-luma : Group images by the average intensity of the converted Y color channel.
Bright lighting and oversaturated images will be ranked first.
Adjust the '-b' ('--bins') parameter to control the number of bins for grouping.
Each image is allocated to a bin by the percentage of color pixels that appear in the image.
- color-orange : Group images by the average intensity of the converted Co color channel.
Orange images will be ranked first and blue images will be last.
Adjust the '-b' ('--bins') parameter to control the number of bins for grouping.
Each image is allocated to a bin by the percentage of color pixels that appear in the image.
- distance : Group faces by the estimated distance of the alignments from an 'average' face.
This can be useful for eliminating misaligned faces.
Sorts from most like an average face to least like an average face.
Adjust the '-b' ('--bins') parameter to control the number of bins for grouping.
The minimum and maximum values are taken for the chosen sort metric.
The bins are then populated with the results from the group sorting.
- face : Group faces using VGG Face2 by face similarity.
This uses a pairwise clustering algorithm to check the distances between 512 features on every face in your set and order them appropriately.
Adjust the '-t' ('--threshold') parameter to control the strength of grouping.
- face-cnn : Group faces by their landmarks. Adjust the '-t' ('--threshold') parameter to control the strength of grouping.
- face-cnn-dissim : Group Like 'face-cnn' but sorts by dissimilarity. Adjust the '-t' ('--threshold') parameter to control the strength of grouping.
- hist : Group faces by their color histogram. Adjust the '-t' ('--threshold') parameter to control the strength of grouping.
- hist-dissim : Group Like 'hist' but sorts by dissimilarity. Adjust the '-t' ('--threshold') parameter to control the strength of grouping.
- pitch : Group faces by Pitch (rotation up and down). Adjust the '-b' ('--bins') parameter to control the number of bins for grouping.
Each image is allocated to a bin by the number of degrees the face is orientated from center.
- roll : Group faces by Roll (rotation). Aligned faces should have a roll value close to zero.
The further the Roll value from zero the higher liklihood the face is misaligned.
Adjust the '-b' ('--bins') parameter to control the number of bins for grouping.
Each image is allocated to a bin by the number of degrees the face is orientated from center.
- size : Group images by their size in the original frame.
Faces further from the camera and from lower resolution sources will be sorted first, whilst faces closer to the camera and from higher resolution sources will be sorted last.
Adjust the '-b' ('--bins') parameter to control the number of bins for grouping.
The minimum and maximum values are taken for the chosen sort metric.
The bins are then populated with the results from the group sorting.
- yaw : Group faces by Yaw (rotation left to right).
Adjust the '-b' ('--bins') parameter to control the number of bins for grouping.
Each image is allocated to a bin by the number of degrees the face is orientated from center.
Default: none
-k|--keep Whether to keep the original files in their original location.
Choosing a 'sort-by' method means that the files have to be renamed.
Selecting 'keep' means that the original files will be kept, and the renamed files will be created in the specified output folder.
Unselecting keep means that the original files will be moved and renamed based on the selected sort/group criteria.
-t|--threshold VAL Float value. Minimum threshold to use for grouping comparison with 'face-cnn' 'hist' and 'face' methods.
The lower the value the more discriminating the grouping is.
Leaving -1.0 will allow Faceswap to choose the default value.
- For 'face-cnn' 7.2 should be enough, with 4 being very discriminating.
- For 'hist' 0.3 should be enough, with 0.2 being very discriminating.
- For 'face' between 0.1 (more bins) to 0.5 (fewer bins) should be about right.
Be careful setting a value that's too extrene in a directory with many images, as this could result in a lot of folders being created.
Defaults: face-cnn 7.2, hist 0.3, face 0.25
-b|--bins NUM Integer value. Used to control the number of bins created for grouping by:
any 'blur' methods, 'color' methods or 'face metric' methods ('distance', 'size') and 'orientation; methods ('yaw', 'pitch').
For any other grouping methods see the '-t' ('--threshold') option.
- For 'face metric' methods the bins are filled, according the the distribution of faces between the minimum and maximum chosen metric.
- For 'color' methods the number of bins represents the divider of the percentage of colored pixels.
Eg. For a bin number of '5': The first folder will have the faces with 0% to 20% colored pixels, second 21% to 40%, etc.
Any empty bins will be deleted, so you may end up with fewer bins than selected.
- For 'blur' methods folder 0 will be the least blurry, while the last folder will be the blurriest.
- For 'orientation' methods the number of bins is dictated by how much 180 degrees is divided.
Eg. If 18 is selected, then each folder will be a 10 degree increment.
Folder 0 will contain faces looking the most to the left/down whereas the last folder will contain the faces looking the most to the right/up.
NB: Some bins may be empty if faces do not fit the criteria.
Default value: 5
-l|--log-changes Logs file renaming changes if grouping by renaming, or it logs the file copying/movement if grouping by folders.
If no log file is specified with '--log-file', then a 'sort_log.json' file will be created in the input directory.
-f|--log-file PATH Specify a log file to use for saving the renaming or grouping information.
If specified extension isn't 'json' or 'yaml', then json will be used as the serializer, with the supplied filename.
Default: sort_log.json
```