A program to determine whether a face in an image is real or AI-generated (deepfake) using gradient and covariance matrix analysis.
The program implements the method described in task.md:
- Luminance Conversion: The image is converted to grayscale (luminance)
- Gradient Computation: Horizontal (Gx) and vertical (Gy) brightness gradients are computed
- Covariance Matrix: A matrix M is created from all gradient vectors and the covariance matrix C = (1/N) * M^T * M is computed
- Feature Extraction: Statistical features are extracted from the covariance matrix (eigenvalues, trace, determinant, etc.)
- Classification: Machine learning or heuristic methods are used to determine if the image is real
pip install -r requirements.txtpython face_real_fake_detector.py path/to/image.jpgpython face_real_fake_detector.py path/to/image.jpg --no-face-detectionFor better accuracy, you can train the model on a dataset:
python face_real_fake_detector.py dummy.jpg --train --real-dir ./real_faces --fake-dir ./fake_facesAfter training, the model will use the trained classifier for more accurate predictions.
face_real_fake_detector.py- main script withFaceRealFakeDetectorclassrequirements.txt- Python dependenciestask.md- method descriptionimage.png- method diagramGRADIENTS.md- detailed explanation of gradient analysis
- Face Detection: Haar Cascade is used to detect faces in the image
- Gradient Analysis: Real images have more structured and consistent gradients that correspond to the physical structure of objects
- Statistical Analysis: The covariance matrix of gradients reveals patterns that differ between real and generated images
- Classification: Extracted features are used to determine whether the image is real or fake
- Without training, the model uses a heuristic method with low confidence
- For best results, it is recommended to train the model on labeled data
- The method works best on face images with good lighting and resolution