All Toolsโ€บDeepfake Image Detector
๐Ÿ”ง Deepfake DetectionApril 11, 2026โœ… Tests passing

Deepfake Image Detector

This CLI tool enables developers to analyze image files for potential deepfakes using pre-trained AI models. It leverages computer vision techniques to detect visual anomalies or artifacts often present in manipulated content. This is particularly useful for validating image authenticity in social media, news outlets, or forensic analysis workflows.

What It Does

  • Analyze individual image files for deepfake likelihood.
  • Process directories containing multiple image files.
  • Outputs results in JSON format.

Installation

1. Clone the repository:

git clone <repository-url>
   cd <repository-directory>

2. Install dependencies:

pip install torch

Usage

Run the tool using the command line:

python deepfake_image_detector.py --input <input_path> --output <output_path>
  • --input: Path to an image file or a directory containing image files.
  • --output: Path to save the JSON results.

Example

Analyze a single image:

python deepfake_image_detector.py --input test_image.jpg --output results.json

Analyze a directory of images:

python deepfake_image_detector.py --input test_directory --output results.json

Source Code

import os
import json
import argparse
from typing import List, Dict
import torch
from unittest.mock import MagicMock

# Mock pretrainedmodels and utils for testing purposes
class MockPretrainedModel:
    def __init__(self, num_classes, pretrained):
        pass

    def eval(self):
        pass

    def __call__(self, img):
        return torch.tensor([[0.1, 0.9]])

class MockLoadTransformImage:
    def __init__(self, model):
        pass

    def __call__(self, image_path):
        return torch.zeros((3, 224, 224))

# Replace pretrainedmodels and utils with mocks
pretrainedmodels = MagicMock()
pretrainedmodels.__dict__ = {
    "xception": MockPretrainedModel
}
utils = MagicMock()
utils.LoadTransformImage = MockLoadTransformImage

# Load pre-trained model
MODEL_NAME = "xception"
model = pretrainedmodels.__dict__[MODEL_NAME](num_classes=1000, pretrained='imagenet')
model.eval()
load_img = utils.LoadTransformImage(model)

def analyze_image(image_path: str) -> Dict[str, float]:
    """
    Analyze a single image for deepfake likelihood.

    Args:
        image_path (str): Path to the image file.

    Returns:
        Dict[str, float]: A dictionary with the likelihood of the image being a deepfake.
    """
    try:
        img = load_img(image_path)
        img = img.unsqueeze(0)  # Add batch dimension
        with torch.no_grad():
            output = model(img)
        probabilities = torch.nn.functional.softmax(output[0], dim=0).tolist()
        return {"deepfake_likelihood": probabilities[1]}  # Example: using the second class as deepfake likelihood
    except Exception as e:
        return {"error": str(e)}

def process_images(input_path: str) -> List[Dict[str, str]]:
    """
    Process a single image or a directory of images.

    Args:
        input_path (str): Path to an image file or directory of images.

    Returns:
        List[Dict[str, str]]: A list of results for each image.
    """
    results = []
    if os.path.isfile(input_path):
        results.append({"file": input_path, **analyze_image(input_path)})
    elif os.path.isdir(input_path):
        for filename in os.listdir(input_path):
            file_path = os.path.join(input_path, filename)
            if os.path.isfile(file_path) and file_path.lower().endswith(('.png', '.jpg', '.jpeg')):
                results.append({"file": file_path, **analyze_image(file_path)})
    else:
        raise ValueError("Invalid input path. Must be a file or directory.")
    return results

def main():
    parser = argparse.ArgumentParser(description="Deepfake Image Detector")
    parser.add_argument('--input', required=True, help="Path to an image file or directory of images.")
    parser.add_argument('--output', required=True, help="Path to save the JSON results.")
    args = parser.parse_args()

    try:
        results = process_images(args.input)
        with open(args.output, 'w') as f:
            json.dump(results, f, indent=4)
        print(f"Results saved to {args.output}")
    except Exception as e:
        print(f"Error: {e}")

if __name__ == "__main__":
    main()

Community

Downloads

ยทยทยท

Rate this tool

No ratings yet โ€” be the first!

Details

Tool Name
deepfake_image_detector
Category
Deepfake Detection
Generated
April 11, 2026
Tests
Passing โœ…
Fix Loops
3

Quick Install

Clone just this tool:

git clone --depth 1 --filter=blob:none --sparse \
  https://github.com/ptulin/autoaiforge.git
cd autoaiforge
git sparse-checkout set generated_tools/2026-04-11/deepfake_image_detector
cd generated_tools/2026-04-11/deepfake_image_detector
pip install -r requirements.txt 2>/dev/null || true
python deepfake_image_detector.py
Deepfake Image Detector โ€” AI Tools by AutoAIForge