deepfake-detector / example.py
yaya36095's picture
Upload 8 files
eda917f verified
"""
Example usage of the Enhanced AI Image Detector
"""
import os
import sys
import argparse
from ai_detector import EnhancedAIDetector
import json
def main():
# Parse command line arguments
parser = argparse.ArgumentParser(description='Detect if an image is AI-generated')
parser.add_argument('image_path', type=str, help='Path to the image to analyze')
parser.add_argument('--output', type=str, help='Path to save the results as JSON', default=None)
parser.add_argument('--detailed', action='store_true', help='Show detailed analysis results')
args = parser.parse_args()
# Check if image exists
if not os.path.exists(args.image_path):
print(f"Error: Image not found at {args.image_path}")
sys.exit(1)
# Initialize the detector
detector = EnhancedAIDetector()
try:
# Analyze the image
print(f"Analyzing image: {args.image_path}")
result = detector.analyze_image(args.image_path)
# Display the result
if result["is_ai_generated"]:
print("\n🤖 This image is likely AI-generated")
print(f"Confidence score: {result['overall_score']:.2f}")
else:
print("\n📷 This image is likely authentic")
print(f"Confidence score: {1 - result['overall_score']:.2f}")
# Display detailed analysis if requested
if args.detailed:
print("\nDetailed Analysis:")
print(f"Noise analysis score: {result['noise_analysis']['score']:.2f} " +
("(suspicious)" if result['noise_analysis']['is_suspicious'] else "(normal)"))
print(f"Texture analysis score: {result['texture_analysis']['score']:.2f} " +
("(suspicious)" if result['texture_analysis']['is_suspicious'] else "(normal)"))
print(f"Color analysis score: {result['color_analysis']['score']:.2f} " +
("(suspicious)" if result['color_analysis']['is_suspicious'] else "(normal)"))
print(f"Edge analysis score: {result['edge_analysis']['score']:.2f} " +
("(suspicious)" if result['edge_analysis']['is_suspicious'] else "(normal)"))
if "face_analysis" in result:
print(f"Face analysis score: {result['face_analysis']['score']:.2f} " +
("(suspicious)" if result['face_analysis']['is_suspicious'] else "(normal)"))
# Save results to JSON if requested
if args.output:
with open(args.output, 'w', encoding='utf-8') as f:
json.dump(result, f, ensure_ascii=False, indent=2)
print(f"\nResults saved to {args.output}")
except Exception as e:
print(f"Error analyzing image: {str(e)}")
sys.exit(1)
if __name__ == "__main__":
main()