File size: 2,140 Bytes
8c30b41
3289e9d
39141aa
7ad75f4
cc2b650
 
19e6f0c
ae9db09
 
 
af7913c
ae9db09
 
 
 
 
 
39141aa
ae9db09
 
 
af7913c
ae9db09
 
cc2b650
ae9db09
 
 
cc2b650
ae9db09
 
 
 
cc2b650
ae9db09
 
 
cc2b650
ae9db09
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cc2b650
ae9db09
cc2b650
 
ae9db09
19e6f0c
ae9db09
 
 
19e6f0c
923a637
19e6f0c
cc2b650
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
import gradio as gr
from transformers import pipeline
from PIL import Image, ExifTags
import numpy as np
import cv2
import io

# Load HuggingFace pipeline
detector = pipeline("image-classification", model="falconsai/nsfw_image_detection")  
# ganti model ke yang support binary classification real vs ai kalau mau training

def analyze_image(image):
    # Konversi ke format OpenCV
    img_bytes = io.BytesIO()
    image.save(img_bytes, format="PNG")
    img_bytes = np.frombuffer(img_bytes.getvalue(), dtype=np.uint8)
    cv_img = cv2.imdecode(img_bytes, cv2.IMREAD_COLOR)

    # Blur score (laplacian variance)
    gray = cv2.cvtColor(cv_img, cv2.COLOR_BGR2GRAY)
    blur_score = cv2.Laplacian(gray, cv2.CV_64F).var()

    # Noise score (std dev)
    noise_score = np.std(gray)

    # Metadata Kamera
    meta = image.getexif()
    meta_info = "Ada" if meta else "Tidak Ada"

    # Prediksi AI vs Asli (pakai HF pipeline dummy dulu)
    preds = detector(image)
    label = preds[0]["label"].lower()
    score = preds[0]["score"]

    # Normalisasi ke persentase AI vs Asli
    if "artificial" in label or "fake" in label or "ai" in label:
        ai_prob = score * 100
    else:
        ai_prob = (1 - score) * 100
    real_prob = 100 - ai_prob

    # Koreksi dengan metadata → jika metadata kamera ada, naikkan skor real
    if meta_info == "Ada":
        real_prob += 15
        ai_prob -= 15

    # Koreksi blur/noise → foto asli biasanya lebih natural
    if blur_score > 500 and noise_score > 20:
        real_prob += 5

    # Clamp ke 0-100
    ai_prob = max(0, min(100, ai_prob))
    real_prob = max(0, min(100, real_prob))

    # Output
    hasil = f"""🖼️ Hasil Deteksi:
    {ai_prob:.2f}% AI / {real_prob:.2f}% Asli

    Blur Score: {blur_score:.2f}
    Noise Score: {noise_score:.2f}
    Metadata Kamera: {meta_info}
    """

    return hasil

demo = gr.Interface(
    fn=analyze_image,
    inputs=gr.Image(type="pil"),
    outputs="text",
    title="AI vs Real Image Detector",
    description="Deteksi apakah gambar AI-generated atau asli. Gratis, tanpa API berbayar."
)

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