Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,12 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from PIL import Image
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
import cv2
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
-
|
| 9 |
def calculate_blur(image):
|
| 10 |
gray = np.array(image.convert("L"))
|
| 11 |
return cv2.Laplacian(gray, cv2.CV_64F).var()
|
|
@@ -14,19 +15,6 @@ def calculate_noise(image):
|
|
| 14 |
gray = np.array(image.convert("L"), dtype=np.float32)
|
| 15 |
return np.std(gray - np.mean(gray))
|
| 16 |
|
| 17 |
-
def has_camera_exif(image):
|
| 18 |
-
try:
|
| 19 |
-
exif = image._getexif()
|
| 20 |
-
if exif:
|
| 21 |
-
for tag, value in exif.items():
|
| 22 |
-
decoded = ExifTags.TAGS.get(tag, tag)
|
| 23 |
-
if decoded in ["Make", "Model"]:
|
| 24 |
-
return True
|
| 25 |
-
except:
|
| 26 |
-
return False
|
| 27 |
-
return False
|
| 28 |
-
|
| 29 |
-
# -------- FFT ANALYSIS --------
|
| 30 |
def high_freq_artifacts(image):
|
| 31 |
gray = np.array(image.convert("L"), dtype=np.float32)
|
| 32 |
f = np.fft.fft2(gray)
|
|
@@ -42,48 +30,29 @@ def high_freq_artifacts(image):
|
|
| 42 |
high_ratio = (total_energy - low_energy) / total_energy * 100
|
| 43 |
return high_ratio
|
| 44 |
|
| 45 |
-
# ----------------------------
|
| 46 |
-
# DETEKSI AI HYBRID ADAPTIF
|
| 47 |
-
# ----------------------------
|
| 48 |
def detect_image(image: Image.Image):
|
| 49 |
blur_score = calculate_blur(image)
|
| 50 |
noise_score = calculate_noise(image)
|
| 51 |
-
exif_present = has_camera_exif(image)
|
| 52 |
high_freq_score = high_freq_artifacts(image)
|
| 53 |
|
| 54 |
-
#
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
weighted_score = 0.5 * hf_norm + 0.5 * noise_norm
|
| 58 |
-
|
| 59 |
-
# Threshold tegas
|
| 60 |
-
is_ai = False
|
| 61 |
-
if not exif_present and weighted_score > 5 and blur_score < 150:
|
| 62 |
-
is_ai = True
|
| 63 |
-
|
| 64 |
-
final_result = "🤖 AI Detected" if is_ai else "✅ Foto Asli"
|
| 65 |
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
f"Blur Score: {blur_score:.2f}",
|
| 69 |
-
f"Noise Score: {noise_score:.2f}",
|
| 70 |
-
f"High-Freq Artifacts Score: {high_freq_score:.2f}",
|
| 71 |
-
f"Weighted Score: {weighted_score:.2f}",
|
| 72 |
-
f"Metadata Kamera: {'Ada' if exif_present else 'Tidak Ada'}"
|
| 73 |
-
]
|
| 74 |
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
# ----------------------------
|
| 80 |
-
with gr.Blocks(title="Hybrid AI Realistic Detector (Jejak Digital)") as demo:
|
| 81 |
-
gr.Markdown("Unggah gambar, sistem akan mendeteksi apakah gambar kemungkinan besar asli atau dihasilkan AI.")
|
| 82 |
with gr.Row():
|
| 83 |
img_input = gr.Image(type="pil", label="Unggah Gambar")
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
|
| 88 |
-
|
| 89 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from PIL import Image
|
| 3 |
+
from transformers import pipeline
|
| 4 |
import numpy as np
|
| 5 |
import cv2
|
| 6 |
|
| 7 |
+
# Muat model deteksi AI
|
| 8 |
+
classifier = pipeline("image-classification", model="umm-maybe/AI-image-detector")
|
| 9 |
+
|
| 10 |
def calculate_blur(image):
|
| 11 |
gray = np.array(image.convert("L"))
|
| 12 |
return cv2.Laplacian(gray, cv2.CV_64F).var()
|
|
|
|
| 15 |
gray = np.array(image.convert("L"), dtype=np.float32)
|
| 16 |
return np.std(gray - np.mean(gray))
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
def high_freq_artifacts(image):
|
| 19 |
gray = np.array(image.convert("L"), dtype=np.float32)
|
| 20 |
f = np.fft.fft2(gray)
|
|
|
|
| 30 |
high_ratio = (total_energy - low_energy) / total_energy * 100
|
| 31 |
return high_ratio
|
| 32 |
|
|
|
|
|
|
|
|
|
|
| 33 |
def detect_image(image: Image.Image):
|
| 34 |
blur_score = calculate_blur(image)
|
| 35 |
noise_score = calculate_noise(image)
|
|
|
|
| 36 |
high_freq_score = high_freq_artifacts(image)
|
| 37 |
|
| 38 |
+
# Prediksi menggunakan model deteksi AI
|
| 39 |
+
result = classifier(image)
|
| 40 |
+
ai_score = result[0]['score'] if result[0]['label'] == 'AI-generated' else 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
+
# Gabungkan skor
|
| 43 |
+
final_score = ai_score * 0.7 + (1 - blur_score / 1000) * 0.2 + (1 - noise_score / 255) * 0.1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
+
if final_score > 0.5:
|
| 46 |
+
return "🤖 Gambar ini kemungkinan besar dihasilkan oleh AI."
|
| 47 |
+
else:
|
| 48 |
+
return "✅ Gambar ini kemungkinan besar asli."
|
| 49 |
|
| 50 |
+
with gr.Blocks() as demo:
|
| 51 |
+
gr.Markdown("### Deteksi Gambar AI vs Asli")
|
|
|
|
|
|
|
|
|
|
| 52 |
with gr.Row():
|
| 53 |
img_input = gr.Image(type="pil", label="Unggah Gambar")
|
| 54 |
+
output = gr.Textbox(label="Hasil Deteksi")
|
| 55 |
+
btn = gr.Button("Deteksi")
|
| 56 |
+
btn.click(detect_image, inputs=img_input, outputs=output)
|
| 57 |
|
| 58 |
+
demo.launch()
|
|
|