AI Gender Classifier is a Python CLI tool that scans a folder of images, uses DeepFace to predict visible gender presentation, and copies images into organized output folders.
It supports recursive folder scanning, image validation, DeepFace detector fallback, crash-safe processing, category-prefixed filenames, and a colorful terminal summary.
Important: this tool predicts model labels from image appearance. Do not use it for identity verification, access control, sensitive decisions, or assumptions about a person's actual gender identity.
- DeepFace gender analysis with
actions=["gender"] - OpenCV detector first, then RetinaFace fallback
- Recursive image scanning for nested folders
- Skips
classified_imageson reruns to prevent re-processing output - Crash-safe single-worker default to avoid TensorFlow/OpenCV native thread crashes
- Guarded threaded mode via
GENDER_CLASSIFIER_WORKERS - Thread-safe counters for reliable output numbering
- Overwrite-safe filenames like
male-1.jpg,female-1.jpg - Existing output detection so numbering continues after old files
- Image validation using Pillow before AI analysis
- Original files preserved because images are copied, not deleted
- No-human and error buckets for safer sorting
- Colorful terminal output with progress and final stats
- Optional auto-install of missing Python packages at runtime
| Layer | Tech |
|---|---|
| Language | Python 3.11+ |
| AI model wrapper | DeepFace |
| ML runtime | TensorFlow, tf-keras |
| Face detector fallback | RetinaFace |
| Image handling | Pillow, OpenCV |
| Terminal UI | Colorama, Emoji |
| Processing mode | Stable single-worker default; optional guarded ThreadPoolExecutor |
- Python 3.11+
- pip
- Internet connection on first run for model downloads
git clone <your-repo-url>
cd gender-classifier
python3 -m pip install -r requirements.txtThe script can also auto-install missing packages when it starts, but installing from requirements.txt first is still the cleanest setup.
python3 main.pyThen enter the folder path that contains your images:
Enter the folder path containing images:Confirm with:
yThe script creates a classified_images folder inside your selected input folder:
your-image-folder/
image-1.jpg
image-2.png
classified_images/
male/
male-1.jpg
male-2.jpg
female/
female-1.jpg
female-2.jpg
no_human/
no-human-1.jpg
errors/
error-1.jpgGenerated output files continue from existing numbers. For example, if female-7.jpg already exists, the next copied female image becomes female-8.jpg.
Input folder
|
v
Find supported images
|
v
Skip classified_images output folder
|
v
Validate image with Pillow
|
v
DeepFace analyze gender
|
+--> man -> classified_images/male/male-N.ext
+--> woman -> classified_images/female/female-N.ext
+--> none -> classified_images/no_human/no-human-N.ext
+--> error -> classified_images/errors/error-N.extSupported image formats:
.jpg, .jpeg, .png, .bmp, .tiff, .webpYou can edit these values in main.py:
SUPPORTED_FORMATS = (".jpg", ".jpeg", ".png", ".bmp", ".tiff", ".webp")
DEFAULT_WORKERS = 1The app defaults to one worker because DeepFace, TensorFlow, OpenCV, and RetinaFace can segfault under concurrent inference on some systems. To experiment with guarded threaded processing:
GENDER_CLASSIFIER_WORKERS=2 python3 main.pyFilename prefixes are also configurable:
CATEGORY_FILENAME_PREFIXES = {
"male": "male",
"female": "female",
"no_human": "no-human",
"error": "error",
}DeepFace downloads model weights into your home directory:
~/.deepface/weightsCommon files include:
gender_model_weights.h5
retinaface.h5
age_model_weights.h5If you delete these files, DeepFace will download them again on a future run.
| Command | Description |
|---|---|
python3 main.py |
Start the classifier |
python3 -m pip install -r requirements.txt |
Install dependencies |
python3 -m py_compile main.py |
Check Python syntax |
| Problem | Fix |
|---|---|
python: command not found |
Use python3 main.py |
| Package import error | Run python3 -m pip install -r requirements.txt |
| First run is slow | DeepFace may be downloading model weights |
| Segmentation fault during processing | Run the latest code; it defaults to crash-safe single-worker inference. Avoid raising GENDER_CLASSIFIER_WORKERS unless your local TensorFlow/OpenCV stack is stable. |
| No images found | Check supported extensions and folder path |
| Output images appear again on rerun | Make sure you are running the latest code; classified_images is skipped |
| TensorFlow warnings | Usually safe if classification still runs |
- Original images are preserved.
- Output files are copied into category folders.
classified_imagesis ignored during input scanning.- Model predictions can be wrong, especially for low-quality, stylized, occluded, or non-frontal images.
- Avoid using this tool for sensitive or high-stakes decisions.
Contributions are welcome.
- Fork the repository.
- Create a focused feature branch.
- Keep changes small and readable.
- Run
python3 -m py_compile main.py. - Open a pull request with a clear summary.
MIT License. See LICENSE.