File size: 2,424 Bytes
64a94a9
 
034fc15
cfb7b57
 
034fc15
cfb7b57
 
 
 
 
 
 
 
 
034fc15
cfb7b57
 
 
 
 
034fc15
cfb7b57
 
 
 
 
 
034fc15
cfb7b57
 
 
034fc15
cfb7b57
 
3022c96
cfb7b57
 
 
3022c96
cfb7b57
 
 
 
 
 
 
3022c96
cfb7b57
 
 
 
 
 
 
 
 
 
 
3022c96
cfb7b57
 
 
034fc15
cfb7b57
 
034fc15
cfb7b57
 
 
 
034fc15
cfb7b57
 
 
 
6394c51
cfb7b57
 
 
 
64a94a9
 
19e6f0c
cfb7b57
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
import gradio as gr
from transformers import pipeline
from PIL import Image
import cv2
import numpy as np

# =========================
# Preprocessing function
# =========================
def preprocess_image(img_path):
    img = cv2.imread(img_path)
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    img = cv2.resize(img, (512, 512))  
    img = cv2.GaussianBlur(img, (3, 3), 0)  # Kurangi noise
    return Image.fromarray(img)

# =========================
# Load models
# =========================
detector_ai = pipeline("image-classification", model="umm-maybe/AI-image-detector")
detector_resnet = pipeline("image-classification", model="microsoft/resnet-50")

# =========================
# Detection Function
# =========================
def detect_image(img):
    # Preprocess
    img = preprocess_image(img)

    # Run AI detector
    ai_results = detector_ai(img)
    ai_dict = {res['label']: res['score'] for res in ai_results}

    human_score = ai_dict.get("human", 0)
    artificial_score = ai_dict.get("artificial", 0)

    # Run ResNet for natural object cross-check
    resnet_results = detector_resnet(img)
    top_resnet = resnet_results[0]  # ambil label dengan skor tertinggi

    # =========================
    # Decision Rule with Threshold
    # =========================
    if artificial_score > 0.75:
        verdict = "๐ŸŸฃ AI-generated"
    elif human_score > 0.65:
        verdict = "๐ŸŸข Foto Asli"
    else:
        verdict = "โš ๏ธ Tidak Pasti"

    # Extra check: kalau AI-detector bilang AI tapi ResNet yakin objek nyata
    if verdict == "๐ŸŸฃ AI-generated" and top_resnet['score'] > 0.70:
        verdict = "โš ๏ธ Tidak Pasti (deteksi objek nyata tinggi)"

    # =========================
    # Output
    # =========================
    summary = f"""
๐Ÿ“Š Ringkasan Deteksi:

๐Ÿ”น AI Detector:
   - Human: {human_score:.2%}
   - Artificial: {artificial_score:.2%}

๐Ÿ”น ResNet50:
   - Top Label: {top_resnet['label']} ({top_resnet['score']:.2%})

=== HASIL AKHIR ===
{verdict}
"""
    return summary

# =========================
# Gradio App
# =========================
demo = gr.Interface(
    fn=detect_image,
    inputs=gr.Image(type="filepath"),
    outputs="text",
    title="Deteksi AI vs Foto Asli",
    description="Upload foto untuk mendeteksi apakah gambar AI-generated atau Foto Asli. Menggunakan threshold + preprocessing."
)

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