File size: 2,849 Bytes
eda917f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
"""
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()