File size: 1,931 Bytes
8c30b41
3289e9d
 
f658766
f23fa83
923a637
3289e9d
 
 
f034887
 
 
 
 
505b320
0738ed7
f034887
458c1f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
269f417
f23fa83
f034887
458c1f5
 
3289e9d
 
 
f034887
3289e9d
 
923a637
3289e9d
 
 
 
923a637
3289e9d
 
269f417
 
3289e9d
 
 
8c30b41
3289e9d
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
import gradio as gr
from PIL import Image
from transformers import pipeline
import numpy as np
import cv2

# Muat model deteksi AI
classifier = pipeline("image-classification", model="umm-maybe/AI-image-detector")

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)
    return np.std(gray - np.mean(gray))

def high_freq_artifacts(image):
    gray = np.array(image.convert("L"), dtype=np.float32)
    f = np.fft.fft2(gray)
    fshift = np.fft.fftshift(f)
    magnitude_spectrum = np.abs(fshift)
    rows, cols = gray.shape
    crow, ccol = rows // 2 , cols // 2
    r = min(crow, ccol) // 4
    mask = np.zeros((rows, cols))
    mask[crow-r:crow+r, ccol-r:ccol+r] = 1
    low_energy = np.sum(magnitude_spectrum * mask)
    total_energy = np.sum(magnitude_spectrum)
    high_ratio = (total_energy - low_energy) / total_energy * 100
    return high_ratio

def detect_image(image: Image.Image):
    blur_score = calculate_blur(image)
    noise_score = calculate_noise(image)
    high_freq_score = high_freq_artifacts(image)

    # Prediksi menggunakan model deteksi AI
    result = classifier(image)
    ai_score = result[0]['score'] if result[0]['label'] == 'AI-generated' else 0

    # Gabungkan skor
    final_score = ai_score * 0.7 + (1 - blur_score / 1000) * 0.2 + (1 - noise_score / 255) * 0.1

    if final_score > 0.5:
        return "🤖 Gambar ini kemungkinan besar dihasilkan oleh AI."
    else:
        return "✅ Gambar ini kemungkinan besar asli."

with gr.Blocks() as demo:
    gr.Markdown("### Deteksi Gambar AI vs Asli")
    with gr.Row():
        img_input = gr.Image(type="pil", label="Unggah Gambar")
        output = gr.Textbox(label="Hasil Deteksi")
    btn = gr.Button("Deteksi")
    btn.click(detect_image, inputs=img_input, outputs=output)

demo.launch()