This repository contains the code we used in our paper on defacing CT scans using the TotalSegmentator (v2.4.0). We tested its effectiveness in removing identifiable facial features, and it does 😊!
- First, get a non-commercial license for the TotalSegmentator and check the documentation.
- Activate the license by running the command you receive in the console (with your environment activated). The command will look something like this:
totalseg_set_license -l aca_XXXXXXXXX
- Install the requirements.txt (we tested it with Python 3.10.15)
- Python totaldeface.py
python totaldeface.py --input_dir --output_dir --totalsegmentator_licence
// If you don't add an input and output path we added an example from TCIA to showcase it and it should automatically download :)
For effective defacing, face masks should be bulky, not fine or detailed, to avoid re-identification. Sharp masks can be "refaced" if the mask can be differentiated from the normal CT background (including noise). The green mask is from the TotalSegmentator, and the red one from the CTA-DEFACE model.
![Defacing Example](https://private-user-images.githubusercontent.com/63144815/400242852-9070ac8e-f6b7-4233-b5fc-d5a64327c804.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MzkxMzM3NDMsIm5iZiI6MTczOTEzMzQ0MywicGF0aCI6Ii82MzE0NDgxNS80MDAyNDI4NTItOTA3MGFjOGUtZjZiNy00MjMzLWI1ZmMtZDVhNjQzMjdjODA0LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTAyMDklMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwMjA5VDIwMzcyM1omWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTk3ZjNkMThkNzJlOWQzMjE5ODY5ZmRiOTBmZDRkNTVjYzhkYWMxMTNmZjZmNDk3YWU2OGQxOTFlZjI5NmI1ZjImWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.yC9CNqtcU5MSA1LUN0yfmXQEOqBdK6-MBP9znOJA5RM)
For tasks where preserving more of the facial anatomy is important, finer masks may be more appropriate. In this case, you must ensure that the mask is replaced with background values (including noise) that cannot be distinguished from the real background or get a broader patient consent 😉
If you use any of the code or are interested in the defacing process, please read and/or cite our paper, as well as the original TotalSegmentator paper:
Lindholz, M., Ruppel, R., Schulze-Weddige, S., Baumgärtner, G. L., Schobert, I., Panten, A., ... & Penzkofer, T. (2025). Analyzing the TotalSegmentator for facial feature removal in head CT scans. *Radiography, 31*(1), 372-378.