Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,8 +12,7 @@ def calculate_blur(image):
|
|
| 12 |
|
| 13 |
def calculate_noise(image):
|
| 14 |
gray = np.array(image.convert("L"), dtype=np.float32)
|
| 15 |
-
|
| 16 |
-
return noise_std
|
| 17 |
|
| 18 |
def has_camera_exif(image):
|
| 19 |
try:
|
|
@@ -33,10 +32,8 @@ def high_freq_artifacts(image):
|
|
| 33 |
f = np.fft.fft2(gray)
|
| 34 |
fshift = np.fft.fftshift(f)
|
| 35 |
magnitude_spectrum = np.abs(fshift)
|
| 36 |
-
# rasio energi high frequency vs total
|
| 37 |
rows, cols = gray.shape
|
| 38 |
crow, ccol = rows // 2 , cols // 2
|
| 39 |
-
# buat mask low freq
|
| 40 |
r = min(crow, ccol) // 4
|
| 41 |
mask = np.zeros((rows, cols))
|
| 42 |
mask[crow-r:crow+r, ccol-r:ccol+r] = 1
|
|
@@ -46,45 +43,36 @@ def high_freq_artifacts(image):
|
|
| 46 |
return high_ratio
|
| 47 |
|
| 48 |
# ----------------------------
|
| 49 |
-
# DETEKSI AI HYBRID
|
| 50 |
# ----------------------------
|
| 51 |
def detect_image(image: Image.Image):
|
| 52 |
-
output_lines = []
|
| 53 |
-
|
| 54 |
blur_score = calculate_blur(image)
|
| 55 |
noise_score = calculate_noise(image)
|
| 56 |
exif_present = has_camera_exif(image)
|
| 57 |
high_freq_score = high_freq_artifacts(image)
|
| 58 |
|
| 59 |
-
#
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
score += 20
|
| 65 |
-
if not exif_present:
|
| 66 |
-
score += 10
|
| 67 |
-
if high_freq_score > 25: # banyak high freq artifacts = AI
|
| 68 |
-
score += 50
|
| 69 |
|
| 70 |
-
if
|
| 71 |
-
final_result = "🤖 AI Detected"
|
| 72 |
-
else:
|
| 73 |
-
final_result = "✅ Foto Asli"
|
| 74 |
|
| 75 |
-
output_lines
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
|
|
|
| 81 |
|
| 82 |
return "\n".join(output_lines)
|
| 83 |
|
| 84 |
# ----------------------------
|
| 85 |
# GRADIO INTERFACE
|
| 86 |
# ----------------------------
|
| 87 |
-
with gr.Blocks(title="Hybrid AI Realistic Detector (
|
| 88 |
gr.Markdown("Unggah gambar, sistem akan mendeteksi apakah gambar kemungkinan besar asli atau dihasilkan AI.")
|
| 89 |
with gr.Row():
|
| 90 |
img_input = gr.Image(type="pil", label="Unggah Gambar")
|
|
|
|
| 12 |
|
| 13 |
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:
|
|
|
|
| 32 |
f = np.fft.fft2(gray)
|
| 33 |
fshift = np.fft.fftshift(f)
|
| 34 |
magnitude_spectrum = np.abs(fshift)
|
|
|
|
| 35 |
rows, cols = gray.shape
|
| 36 |
crow, ccol = rows // 2 , cols // 2
|
|
|
|
| 37 |
r = min(crow, ccol) // 4
|
| 38 |
mask = np.zeros((rows, cols))
|
| 39 |
mask[crow-r:crow+r, ccol-r:ccol+r] = 1
|
|
|
|
| 43 |
return high_ratio
|
| 44 |
|
| 45 |
# ----------------------------
|
| 46 |
+
# DETEKSI AI HYBRID SENSITIF
|
| 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 |
+
# ---------- Aturan tegas & sensitif ----------
|
| 55 |
+
# Foto asli aman jika memiliki metadata atau noise > 10
|
| 56 |
+
is_ai = False
|
| 57 |
+
if (not exif_present) and (noise_score < 8) and (blur_score < 120) and (high_freq_score > 20):
|
| 58 |
+
is_ai = True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
+
final_result = "🤖 AI Detected" if is_ai else "✅ Foto Asli"
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
+
output_lines = [
|
| 63 |
+
f"### Hasil Deteksi:\n{final_result}",
|
| 64 |
+
f"Blur Score: {blur_score:.2f}",
|
| 65 |
+
f"Noise Score: {noise_score:.2f}",
|
| 66 |
+
f"High-Freq Artifacts Score: {high_freq_score:.2f}",
|
| 67 |
+
f"Metadata Kamera: {'Ada' if exif_present else 'Tidak Ada'}"
|
| 68 |
+
]
|
| 69 |
|
| 70 |
return "\n".join(output_lines)
|
| 71 |
|
| 72 |
# ----------------------------
|
| 73 |
# GRADIO INTERFACE
|
| 74 |
# ----------------------------
|
| 75 |
+
with gr.Blocks(title="Hybrid AI Realistic Detector (Sensitif & Tegas)") as demo:
|
| 76 |
gr.Markdown("Unggah gambar, sistem akan mendeteksi apakah gambar kemungkinan besar asli atau dihasilkan AI.")
|
| 77 |
with gr.Row():
|
| 78 |
img_input = gr.Image(type="pil", label="Unggah Gambar")
|