Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from PIL import Image, ImageStat, ExifTags | |
| import numpy as np | |
| import cv2 | |
| # ---------------------------- | |
| # Fungsi hitung sharpness / blur | |
| # ---------------------------- | |
| def calculate_blur(image): | |
| image_cv = np.array(image.convert("L")) # konversi ke grayscale | |
| laplacian_var = cv2.Laplacian(image_cv, cv2.CV_64F).var() | |
| return laplacian_var | |
| # ---------------------------- | |
| # Fungsi deteksi metadata | |
| # ---------------------------- | |
| def has_camera_exif(image): | |
| try: | |
| exif = image._getexif() | |
| if exif: | |
| for tag, value in exif.items(): | |
| decoded = ExifTags.TAGS.get(tag, tag) | |
| if decoded in ["Make", "Model"]: | |
| return True | |
| except: | |
| return False | |
| return False | |
| # ---------------------------- | |
| # Fungsi prediksi AI vs Real | |
| # ---------------------------- | |
| def detect_image(image): | |
| blur_score = calculate_blur(image) | |
| exif_present = has_camera_exif(image) | |
| # Threshold empiris (bisa disesuaikan) | |
| # blur rendah + metadata β Foto Asli | |
| # blur tinggi + tidak ada metadata β AI | |
| if exif_present or blur_score > 100: | |
| final_result = f"β Foto Asli" | |
| else: | |
| final_result = f"π€ AI Detected" | |
| output = f""" | |
| ### Hasil Deteksi: | |
| {final_result} | |
| **Blur Score:** {blur_score:.2f} | |
| **Metadata Kamera:** {'Ada' if exif_present else 'Tidak Ada'} | |
| """ | |
| return output | |
| # ---------------------------- | |
| # Gradio Interface | |
| # ---------------------------- | |
| iface = gr.Interface( | |
| fn=detect_image, | |
| inputs=gr.Image(type="pil"), | |
| outputs="markdown", | |
| title="AI vs Foto Asli Detector (Gratis)", | |
| description="Unggah gambar, sistem akan mendeteksi apakah gambar kemungkinan besar asli atau dihasilkan AI berdasarkan analisis blur dan metadata." | |
| ) | |
| if __name__ == "__main__": | |
| iface.launch() | |