Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,62 +1,52 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
| 3 |
-
from PIL import Image
|
| 4 |
import numpy as np
|
|
|
|
| 5 |
|
| 6 |
# ----------------------------
|
| 7 |
-
#
|
| 8 |
# ----------------------------
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
| 11 |
|
| 12 |
# ----------------------------
|
| 13 |
-
# Fungsi deteksi
|
| 14 |
# ----------------------------
|
| 15 |
-
def
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
label2, score2 = res2['label'].lower(), res2['score']
|
| 27 |
-
label2_final = 'human' if 'person' in label2 or 'human' in label2 else 'ai'
|
| 28 |
-
results.append((label2_final, score2))
|
| 29 |
-
|
| 30 |
-
# ----------------------------
|
| 31 |
-
# Voting atau ambil confidence tertinggi
|
| 32 |
-
# ----------------------------
|
| 33 |
-
votes = [r[0] for r in results]
|
| 34 |
-
if votes[0] == votes[1]:
|
| 35 |
-
final_label = votes[0] # mayoritas sama
|
| 36 |
-
final_conf = np.mean([r[1] for r in results if r[0]==final_label])*100
|
| 37 |
-
else:
|
| 38 |
-
# ambil model dengan confidence tertinggi
|
| 39 |
-
if results[0][1] > results[1][1]:
|
| 40 |
-
final_label = results[0][0]
|
| 41 |
-
final_conf = results[0][1]*100
|
| 42 |
-
else:
|
| 43 |
-
final_label = results[1][0]
|
| 44 |
-
final_conf = results[1][1]*100
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
else:
|
| 52 |
-
final_result = f"π€ AI Detected
|
| 53 |
-
|
| 54 |
output = f"""
|
| 55 |
### Hasil Deteksi:
|
| 56 |
{final_result}
|
| 57 |
|
| 58 |
-
**
|
| 59 |
-
**
|
| 60 |
"""
|
| 61 |
return output
|
| 62 |
|
|
@@ -67,8 +57,8 @@ iface = gr.Interface(
|
|
| 67 |
fn=detect_image,
|
| 68 |
inputs=gr.Image(type="pil"),
|
| 69 |
outputs="markdown",
|
| 70 |
-
title="AI vs Foto Asli Detector",
|
| 71 |
-
description="Unggah gambar, sistem akan mendeteksi apakah gambar
|
| 72 |
)
|
| 73 |
|
| 74 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from PIL import Image, ImageStat, ExifTags
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
+
import cv2
|
| 5 |
|
| 6 |
# ----------------------------
|
| 7 |
+
# Fungsi hitung sharpness / blur
|
| 8 |
# ----------------------------
|
| 9 |
+
def calculate_blur(image):
|
| 10 |
+
image_cv = np.array(image.convert("L")) # konversi ke grayscale
|
| 11 |
+
laplacian_var = cv2.Laplacian(image_cv, cv2.CV_64F).var()
|
| 12 |
+
return laplacian_var
|
| 13 |
|
| 14 |
# ----------------------------
|
| 15 |
+
# Fungsi deteksi metadata
|
| 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 |
+
# ----------------------------
|
| 30 |
+
# Fungsi prediksi AI vs Real
|
| 31 |
+
# ----------------------------
|
| 32 |
+
def detect_image(image):
|
| 33 |
+
blur_score = calculate_blur(image)
|
| 34 |
+
exif_present = has_camera_exif(image)
|
| 35 |
+
|
| 36 |
+
# Threshold empiris (bisa disesuaikan)
|
| 37 |
+
# blur rendah + metadata β Foto Asli
|
| 38 |
+
# blur tinggi + tidak ada metadata β AI
|
| 39 |
+
if exif_present or blur_score > 100:
|
| 40 |
+
final_result = f"β
Foto Asli"
|
| 41 |
else:
|
| 42 |
+
final_result = f"π€ AI Detected"
|
| 43 |
+
|
| 44 |
output = f"""
|
| 45 |
### Hasil Deteksi:
|
| 46 |
{final_result}
|
| 47 |
|
| 48 |
+
**Blur Score:** {blur_score:.2f}
|
| 49 |
+
**Metadata Kamera:** {'Ada' if exif_present else 'Tidak Ada'}
|
| 50 |
"""
|
| 51 |
return output
|
| 52 |
|
|
|
|
| 57 |
fn=detect_image,
|
| 58 |
inputs=gr.Image(type="pil"),
|
| 59 |
outputs="markdown",
|
| 60 |
+
title="AI vs Foto Asli Detector (Gratis)",
|
| 61 |
+
description="Unggah gambar, sistem akan mendeteksi apakah gambar kemungkinan besar asli atau dihasilkan AI berdasarkan analisis blur dan metadata."
|
| 62 |
)
|
| 63 |
|
| 64 |
if __name__ == "__main__":
|