Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,22 +5,25 @@ import numpy as np
|
|
| 5 |
import cv2
|
| 6 |
|
| 7 |
# ----------------------------
|
| 8 |
-
#
|
| 9 |
# ----------------------------
|
| 10 |
try:
|
| 11 |
-
|
| 12 |
-
except
|
| 13 |
-
|
| 14 |
-
print("HF AI-detector gagal dimuat
|
| 15 |
|
|
|
|
|
|
|
|
|
|
| 16 |
try:
|
| 17 |
general_model = pipeline("image-classification", model="google/vit-base-patch16-224")
|
| 18 |
-
except
|
| 19 |
general_model = None
|
| 20 |
-
print("General classifier gagal dimuat
|
| 21 |
|
| 22 |
# ----------------------------
|
| 23 |
-
#
|
| 24 |
# ----------------------------
|
| 25 |
def calculate_blur(image):
|
| 26 |
gray = np.array(image.convert("L"))
|
|
@@ -44,28 +47,26 @@ def has_camera_exif(image):
|
|
| 44 |
return False
|
| 45 |
|
| 46 |
# ----------------------------
|
| 47 |
-
#
|
| 48 |
# ----------------------------
|
| 49 |
def detect_image(image):
|
| 50 |
output_lines = []
|
| 51 |
|
| 52 |
-
#
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
if
|
| 57 |
try:
|
| 58 |
-
result =
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
if any(x in
|
| 62 |
-
|
| 63 |
-
elif "human" in hf_label.lower():
|
| 64 |
-
hf_score = 100 - hf_conf # semakin besar human β kecil skor AI
|
| 65 |
except:
|
| 66 |
-
|
| 67 |
|
| 68 |
-
#
|
| 69 |
general_score = 0
|
| 70 |
general_label = "N/A"
|
| 71 |
general_conf = 0
|
|
@@ -74,38 +75,42 @@ def detect_image(image):
|
|
| 74 |
result2 = general_model(image)
|
| 75 |
general_label = result2[0]['label']
|
| 76 |
general_conf = result2[0]['score'] * 100
|
| 77 |
-
if any(x in general_label.lower() for x in ["anime","cartoon","illustration","
|
| 78 |
general_score = general_conf
|
| 79 |
-
else:
|
| 80 |
-
general_score = 100 - general_conf
|
| 81 |
except:
|
| 82 |
general_score = 0
|
| 83 |
|
| 84 |
-
#
|
| 85 |
blur_score = calculate_blur(image)
|
| 86 |
noise_score = calculate_noise(image)
|
| 87 |
exif_present = has_camera_exif(image)
|
| 88 |
-
|
| 89 |
-
if blur_score
|
| 90 |
-
|
| 91 |
-
if
|
| 92 |
-
|
|
|
|
|
|
|
| 93 |
|
| 94 |
-
#
|
| 95 |
-
|
| 96 |
-
|
|
|
|
| 97 |
|
| 98 |
-
|
| 99 |
-
if weighted_score >= 95:
|
| 100 |
-
final_result = "π€ Gambar ini hasil AI"
|
| 101 |
-
elif weighted_score <= 5:
|
| 102 |
final_result = "β
Gambar ini asli"
|
|
|
|
|
|
|
| 103 |
else:
|
| 104 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
|
|
|
|
| 106 |
output_lines.append(f"### Hasil Deteksi:\n{final_result}")
|
| 107 |
-
output_lines.append(f"
|
| 108 |
-
output_lines.append(f"HF AI-detector: {hf_label} ({hf_conf:.2f}%)")
|
| 109 |
output_lines.append(f"General Model: {general_label} ({general_conf:.2f}%)")
|
| 110 |
output_lines.append(f"Blur Score: {blur_score:.2f}")
|
| 111 |
output_lines.append(f"Noise Score: {noise_score:.2f}")
|
|
@@ -121,7 +126,7 @@ iface = gr.Interface(
|
|
| 121 |
inputs=gr.Image(type="pil"),
|
| 122 |
outputs="markdown",
|
| 123 |
title="AI vs Foto Asli Detector (Hybrid)",
|
| 124 |
-
description="Unggah gambar, sistem akan mendeteksi
|
| 125 |
)
|
| 126 |
|
| 127 |
if __name__ == "__main__":
|
|
|
|
| 5 |
import cv2
|
| 6 |
|
| 7 |
# ----------------------------
|
| 8 |
+
# Model AI-detector (gratis)
|
| 9 |
# ----------------------------
|
| 10 |
try:
|
| 11 |
+
ai_detector = pipeline("image-classification", model="umm-maybe/AI-image-detector")
|
| 12 |
+
except:
|
| 13 |
+
ai_detector = None
|
| 14 |
+
print("HF AI-detector gagal dimuat")
|
| 15 |
|
| 16 |
+
# ----------------------------
|
| 17 |
+
# Model General (backup)
|
| 18 |
+
# ----------------------------
|
| 19 |
try:
|
| 20 |
general_model = pipeline("image-classification", model="google/vit-base-patch16-224")
|
| 21 |
+
except:
|
| 22 |
general_model = None
|
| 23 |
+
print("General classifier gagal dimuat")
|
| 24 |
|
| 25 |
# ----------------------------
|
| 26 |
+
# Analisis kamera / lokal
|
| 27 |
# ----------------------------
|
| 28 |
def calculate_blur(image):
|
| 29 |
gray = np.array(image.convert("L"))
|
|
|
|
| 47 |
return False
|
| 48 |
|
| 49 |
# ----------------------------
|
| 50 |
+
# Deteksi hybrid
|
| 51 |
# ----------------------------
|
| 52 |
def detect_image(image):
|
| 53 |
output_lines = []
|
| 54 |
|
| 55 |
+
# ---- AI-detector ----
|
| 56 |
+
ai_score = 0
|
| 57 |
+
ai_label = "N/A"
|
| 58 |
+
ai_conf = 0
|
| 59 |
+
if ai_detector:
|
| 60 |
try:
|
| 61 |
+
result = ai_detector(image)
|
| 62 |
+
ai_label = result[0]['label']
|
| 63 |
+
ai_conf = result[0]['score'] * 100
|
| 64 |
+
if any(x in ai_label.lower() for x in ["fake", "ai", "artificial"]):
|
| 65 |
+
ai_score = ai_conf
|
|
|
|
|
|
|
| 66 |
except:
|
| 67 |
+
ai_score = 0
|
| 68 |
|
| 69 |
+
# ---- General model ----
|
| 70 |
general_score = 0
|
| 71 |
general_label = "N/A"
|
| 72 |
general_conf = 0
|
|
|
|
| 75 |
result2 = general_model(image)
|
| 76 |
general_label = result2[0]['label']
|
| 77 |
general_conf = result2[0]['score'] * 100
|
| 78 |
+
if any(x in general_label.lower() for x in ["anime","cartoon","illustration","maya","3d"]):
|
| 79 |
general_score = general_conf
|
|
|
|
|
|
|
| 80 |
except:
|
| 81 |
general_score = 0
|
| 82 |
|
| 83 |
+
# ---- Analisis kamera ----
|
| 84 |
blur_score = calculate_blur(image)
|
| 85 |
noise_score = calculate_noise(image)
|
| 86 |
exif_present = has_camera_exif(image)
|
| 87 |
+
camera_score = 0
|
| 88 |
+
if blur_score > 100: # Foto asli biasanya lebih fokus
|
| 89 |
+
camera_score += 40
|
| 90 |
+
if noise_score > 15: # Sensor noise alami
|
| 91 |
+
camera_score += 30
|
| 92 |
+
if exif_present: # Ada EXIF
|
| 93 |
+
camera_score += 30
|
| 94 |
|
| 95 |
+
# ---- Weighted hybrid score ----
|
| 96 |
+
weighted_ai = ai_score * 0.6 + general_score * 0.2
|
| 97 |
+
weighted_camera = camera_score
|
| 98 |
+
final_score = weighted_camera - weighted_ai # Positif = asli, Negatif = AI
|
| 99 |
|
| 100 |
+
if final_score >= 95:
|
|
|
|
|
|
|
|
|
|
| 101 |
final_result = "β
Gambar ini asli"
|
| 102 |
+
elif final_score <= -95:
|
| 103 |
+
final_result = "π€ Gambar ini hasil AI"
|
| 104 |
else:
|
| 105 |
+
# Persentase
|
| 106 |
+
if final_score > 0:
|
| 107 |
+
final_result = f"β
Gambar ini {min(round(final_score,2),100)}% asli"
|
| 108 |
+
else:
|
| 109 |
+
final_result = f"π€ Gambar ini {min(round(-final_score,2),100)}% hasil AI"
|
| 110 |
|
| 111 |
+
# ---- Output ----
|
| 112 |
output_lines.append(f"### Hasil Deteksi:\n{final_result}")
|
| 113 |
+
output_lines.append(f"AI-detector: {ai_label} ({ai_conf:.2f}%)")
|
|
|
|
| 114 |
output_lines.append(f"General Model: {general_label} ({general_conf:.2f}%)")
|
| 115 |
output_lines.append(f"Blur Score: {blur_score:.2f}")
|
| 116 |
output_lines.append(f"Noise Score: {noise_score:.2f}")
|
|
|
|
| 126 |
inputs=gr.Image(type="pil"),
|
| 127 |
outputs="markdown",
|
| 128 |
title="AI vs Foto Asli Detector (Hybrid)",
|
| 129 |
+
description="Unggah gambar, sistem akan mendeteksi apakah gambar asli atau hasil AI (menggunakan kombinasi AI-detector dan jejak kamera)."
|
| 130 |
)
|
| 131 |
|
| 132 |
if __name__ == "__main__":
|