Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,76 +5,70 @@ import numpy as np
|
|
| 5 |
import cv2
|
| 6 |
import io
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
detector = pipeline("image-classification", model="
|
|
|
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
exif = img._getexif()
|
| 20 |
-
if exif is not None:
|
| 21 |
-
for tag, value in exif.items():
|
| 22 |
-
tag_name = ExifTags.TAGS.get(tag, tag)
|
| 23 |
-
if "Model" in tag_name or "Make" in tag_name:
|
| 24 |
-
return True
|
| 25 |
-
except:
|
| 26 |
-
pass
|
| 27 |
-
return False
|
| 28 |
|
| 29 |
-
#
|
| 30 |
-
|
| 31 |
-
# Resize ke 224x224 untuk model
|
| 32 |
-
img_resized = img.resize((224,224))
|
| 33 |
-
|
| 34 |
-
# Prediksi AI-detector HuggingFace
|
| 35 |
-
preds = detector(img_resized)
|
| 36 |
-
ai_score = 0
|
| 37 |
-
for p in preds:
|
| 38 |
-
if "artificial" in p['label'].lower() or "fake" in p['label'].lower():
|
| 39 |
-
ai_score += p['score'] * 100
|
| 40 |
-
elif "human" in p['label'].lower() or "real" in p['label'].lower():
|
| 41 |
-
ai_score += (1 - p['score']) * 100
|
| 42 |
-
|
| 43 |
-
ai_score = max(0, min(100, ai_score)) # jaga range
|
| 44 |
-
real_score = 100 - ai_score
|
| 45 |
|
| 46 |
-
#
|
| 47 |
-
|
| 48 |
-
if
|
| 49 |
-
real_score += 30
|
| 50 |
-
ai_score -= 30
|
| 51 |
|
| 52 |
-
#
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
ai_score -= 20
|
| 57 |
|
| 58 |
-
# Normalisasi
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
# Final Output
|
| 63 |
-
if real_score == 100:
|
| 64 |
-
label = "🖼️ Gambar ini ASLI 100%"
|
| 65 |
-
elif ai_score == 100:
|
| 66 |
-
label = "🖼️ Gambar ini HASIL AI 100%"
|
| 67 |
else:
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
-
return
|
| 71 |
|
| 72 |
-
# Gradio UI
|
| 73 |
demo = gr.Interface(
|
| 74 |
-
fn=
|
| 75 |
inputs=gr.Image(type="pil"),
|
| 76 |
-
outputs=
|
| 77 |
-
title="
|
|
|
|
| 78 |
)
|
| 79 |
|
| 80 |
if __name__ == "__main__":
|
|
|
|
| 5 |
import cv2
|
| 6 |
import io
|
| 7 |
|
| 8 |
+
# Load HuggingFace pipeline
|
| 9 |
+
detector = pipeline("image-classification", model="falconsai/nsfw_image_detection")
|
| 10 |
+
# ganti model ke yang support binary classification real vs ai kalau mau training
|
| 11 |
|
| 12 |
+
def analyze_image(image):
|
| 13 |
+
# Konversi ke format OpenCV
|
| 14 |
+
img_bytes = io.BytesIO()
|
| 15 |
+
image.save(img_bytes, format="PNG")
|
| 16 |
+
img_bytes = np.frombuffer(img_bytes.getvalue(), dtype=np.uint8)
|
| 17 |
+
cv_img = cv2.imdecode(img_bytes, cv2.IMREAD_COLOR)
|
| 18 |
|
| 19 |
+
# Blur score (laplacian variance)
|
| 20 |
+
gray = cv2.cvtColor(cv_img, cv2.COLOR_BGR2GRAY)
|
| 21 |
+
blur_score = cv2.Laplacian(gray, cv2.CV_64F).var()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
# Noise score (std dev)
|
| 24 |
+
noise_score = np.std(gray)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
# Metadata Kamera
|
| 27 |
+
meta = image.getexif()
|
| 28 |
+
meta_info = "Ada" if meta else "Tidak Ada"
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
# Prediksi AI vs Asli (pakai HF pipeline dummy dulu)
|
| 31 |
+
preds = detector(image)
|
| 32 |
+
label = preds[0]["label"].lower()
|
| 33 |
+
score = preds[0]["score"]
|
|
|
|
| 34 |
|
| 35 |
+
# Normalisasi ke persentase AI vs Asli
|
| 36 |
+
if "artificial" in label or "fake" in label or "ai" in label:
|
| 37 |
+
ai_prob = score * 100
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
else:
|
| 39 |
+
ai_prob = (1 - score) * 100
|
| 40 |
+
real_prob = 100 - ai_prob
|
| 41 |
+
|
| 42 |
+
# Koreksi dengan metadata → jika metadata kamera ada, naikkan skor real
|
| 43 |
+
if meta_info == "Ada":
|
| 44 |
+
real_prob += 15
|
| 45 |
+
ai_prob -= 15
|
| 46 |
+
|
| 47 |
+
# Koreksi blur/noise → foto asli biasanya lebih natural
|
| 48 |
+
if blur_score > 500 and noise_score > 20:
|
| 49 |
+
real_prob += 5
|
| 50 |
+
|
| 51 |
+
# Clamp ke 0-100
|
| 52 |
+
ai_prob = max(0, min(100, ai_prob))
|
| 53 |
+
real_prob = max(0, min(100, real_prob))
|
| 54 |
+
|
| 55 |
+
# Output
|
| 56 |
+
hasil = f"""🖼️ Hasil Deteksi:
|
| 57 |
+
{ai_prob:.2f}% AI / {real_prob:.2f}% Asli
|
| 58 |
+
|
| 59 |
+
Blur Score: {blur_score:.2f}
|
| 60 |
+
Noise Score: {noise_score:.2f}
|
| 61 |
+
Metadata Kamera: {meta_info}
|
| 62 |
+
"""
|
| 63 |
|
| 64 |
+
return hasil
|
| 65 |
|
|
|
|
| 66 |
demo = gr.Interface(
|
| 67 |
+
fn=analyze_image,
|
| 68 |
inputs=gr.Image(type="pil"),
|
| 69 |
+
outputs="text",
|
| 70 |
+
title="AI vs Real Image Detector",
|
| 71 |
+
description="Deteksi apakah gambar AI-generated atau asli. Gratis, tanpa API berbayar."
|
| 72 |
)
|
| 73 |
|
| 74 |
if __name__ == "__main__":
|