File size: 3,943 Bytes
8c30b41
3289e9d
13ae792
 
 
19e6f0c
13ae792
cf51b4c
13ae792
 
cf51b4c
 
 
 
19e6f0c
13ae792
 
cf51b4c
13ae792
cf51b4c
19e6f0c
13ae792
cf51b4c
13ae792
 
 
 
 
 
 
 
 
 
 
19e6f0c
13ae792
 
 
 
 
 
 
 
 
 
 
cf51b4c
13ae792
 
cf51b4c
 
 
 
 
13ae792
cf51b4c
 
 
 
 
 
 
13ae792
cf51b4c
13ae792
cf51b4c
13ae792
975d67c
 
13ae792
 
 
975d67c
 
b178e5a
975d67c
13ae792
 
 
cf51b4c
13ae792
 
 
cf51b4c
 
 
 
 
 
 
 
 
 
13ae792
cf51b4c
 
13ae792
cf51b4c
 
 
 
 
13ae792
cf51b4c
13ae792
cf51b4c
 
 
 
 
 
 
 
 
19e6f0c
975d67c
19e6f0c
13ae792
 
 
19e6f0c
 
 
 
13ae792
cf51b4c
19e6f0c
923a637
19e6f0c
 
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import gradio as gr
from transformers import pipeline
from PIL import Image, ExifTags
import numpy as np
import cv2

# ----------------------------
# MODEL
# ----------------------------
try:
    hf_detector = pipeline("image-classification", model="umm-maybe/AI-image-detector")
except Exception as e:
    hf_detector = None
    print("HF AI-detector gagal dimuat:", e)

try:
    general_model = pipeline("image-classification", model="google/vit-base-patch16-224")
except Exception as e:
    general_model = None
    print("General classifier gagal dimuat:", e)

# ----------------------------
# ANALISIS LOKAL
# ----------------------------
def calculate_blur(image):
    gray = np.array(image.convert("L"))
    return cv2.Laplacian(gray, cv2.CV_64F).var()

def calculate_noise(image):
    gray = np.array(image.convert("L"), dtype=np.float32)
    noise_std = np.std(gray - np.mean(gray))
    return noise_std

def has_camera_exif(image):
    try:
        exif = image._getexif()
        if exif:
            for tag, value in exif.items():
                decoded = ExifTags.TAGS.get(tag, tag)
                if decoded in ["Make", "Model"]:
                    return True
    except:
        return False
    return False

# ----------------------------
# DETEKSI HYBRID
# ----------------------------
def detect_image(image):
    # Prediksi HF AI-detector
    hf_score = 0
    hf_label = "N/A"
    hf_conf = 0
    if hf_detector:
        try:
            result = hf_detector(image)
            hf_label = result[0]['label']
            hf_conf = result[0]['score'] * 100
            if any(x in hf_label.lower() for x in ["fake", "ai", "artificial"]):
                hf_score = hf_conf
            else:
                hf_score = 100 - hf_conf  # jika human, kurangi
        except:
            hf_score = 0

    # Prediksi general model
    general_score = 0
    general_label = "N/A"
    general_conf = 0
    if general_model:
        try:
            result2 = general_model(image)
            general_label = result2[0]['label']
            general_conf = result2[0]['score'] * 100
            if any(x in general_label.lower() for x in ["anime","cartoon","illustration","maya","3d"]):
                general_score = general_conf
        except:
            general_score = 0

    # Analisis lokal
    blur_score = calculate_blur(image)
    noise_score = calculate_noise(image)
    exif_present = has_camera_exif(image)
    local_score = 0

    # Blur rendah atau noise rendah → kemungkinan AI
    if blur_score < 100 or noise_score < 10:
        local_score += 50
    # Tidak ada metadata kamera → kemungkinan AI
    if not exif_present:
        local_score += 40
    else:
        local_score -= 20  # ada metadata kamera → lebih asli

    # Weighted hybrid
    weighted_score = hf_score*0.4 + general_score*0.2 + local_score*0.4

    # Konversi ke persen AI / Asli
    if weighted_score >= 95:
        final_result = "🖼️ Gambar ini hasil AI (100%)"
    elif weighted_score <= 5:
        final_result = "🖼️ Gambar ini asli (100%)"
    else:
        final_result = f"🖼️ Gambar ini {round(weighted_score,2)}% AI / {round(100-weighted_score,2)}% Asli"

    # Output
    output_lines = [
        f"### Hasil Deteksi:\n{final_result}",
        f"HF AI-detector: {hf_label} ({hf_conf:.2f}%)",
        f"General Model: {general_label} ({general_conf:.2f}%)",
        f"Blur Score: {blur_score:.2f}",
        f"Noise Score: {noise_score:.2f}",
        f"Metadata Kamera: {'Ada' if exif_present else 'Tidak Ada'}"
    ]

    return "\n".join(output_lines)

# ----------------------------
# Gradio Interface
# ----------------------------
iface = gr.Interface(
    fn=detect_image,
    inputs=gr.Image(type="pil"),
    outputs="markdown",
    title="AI vs Foto Asli Detector (Hybrid)",
    description="Unggah gambar, sistem akan mendeteksi persentase AI vs Asli."
)

if __name__ == "__main__":
    iface.launch()