File size: 2,399 Bytes
8c30b41
3289e9d
39cfaa0
 
f0ff41c
 
 
 
958932b
f0ff41c
 
 
958932b
f0ff41c
 
 
 
 
 
 
 
 
958932b
f0ff41c
 
 
 
 
39cfaa0
f0ff41c
 
39cfaa0
 
 
f0ff41c
 
 
39cfaa0
f0ff41c
 
39cfaa0
f0ff41c
 
 
 
 
 
39cfaa0
 
 
f0ff41c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19e6f0c
f0ff41c
 
 
19e6f0c
923a637
19e6f0c
f0ff41c
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
import gradio as gr
from transformers import pipeline
from PIL import Image, ExifTags
import cv2
import numpy as np

# Model Hugging Face untuk AI vs Human
detector = pipeline("image-classification", model="microsoft/resnet-50")

def analyze_image(image):
    # Convert ke format OpenCV
    img_cv = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)

    # ---- 1. Prediksi dengan model HF ----
    preds = detector(image)
    ai_score = 0.0
    human_score = 0.0
    for p in preds:
        if "artificial" in p["label"].lower() or "ai" in p["label"].lower():
            ai_score += p["score"]
        else:
            human_score += p["score"]

    # Normalisasi biar total 1
    total = ai_score + human_score
    if total > 0:
        ai_score /= total
        human_score /= total

    # ---- 2. Analisis blur/noise ----
    gray = cv2.cvtColor(img_cv, cv2.COLOR_BGR2GRAY)
    blur_score = cv2.Laplacian(gray, cv2.CV_64F).var()
    noise_score = np.std(gray)

    # Semakin kecil blur/noise โ†’ semakin cenderung AI
    blur_factor = 1.0 if blur_score < 100 else 0.0
    noise_factor = 1.0 if noise_score < 20 else 0.0

    # ---- 3. Cek metadata kamera ----
    has_metadata = False
    try:
        exif = image._getexif()
        if exif is not None:
            for tag, value in exif.items():
                decoded = ExifTags.TAGS.get(tag, tag)
                if decoded in ["Make", "Model"]:
                    has_metadata = True
    except:
        pass

    metadata_factor = 0.0 if has_metadata else 1.0

    # ---- 4. Ensemble skor ----
    final_score = (
        0.6 * ai_score +
        0.2 * blur_factor +
        0.1 * noise_factor +
        0.1 * metadata_factor
    )

    result = "AI Generated" if final_score > 0.5 else "Foto Asli"

    # ---- 5. Hasil detail ----
    details = f"""
    ๐Ÿ” Hasil Deteksi:
    {result}

    Skor Model AI-detector: {ai_score:.2f}
    Skor Human: {human_score:.2f}
    Blur Score: {blur_score:.2f}
    Noise Score: {noise_score:.2f}
    Metadata Kamera: {"Ada" if has_metadata else "Tidak Ada"}
    Final Score: {final_score:.2f}
    """
    return details

# Gradio UI
demo = gr.Interface(
    fn=analyze_image,
    inputs=gr.Image(type="pil"),
    outputs="text",
    title="AI vs Real Image Detector",
    description="Upload foto untuk mendeteksi apakah gambar asli atau hasil AI."
)

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