Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import numpy as np | |
| from PIL import Image | |
| import cv2 | |
| def simple_frequency_analysis(image): | |
| """Simple frequency analysis for demo""" | |
| if isinstance(image, Image.Image): | |
| img = np.array(image) | |
| else: | |
| img = image | |
| if len(img.shape) == 3: | |
| gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) | |
| else: | |
| gray = img | |
| # Simple Fourier transform | |
| f = np.fft.fft2(gray) | |
| fshift = np.fft.fftshift(f) | |
| magnitude = 20 * np.log(np.abs(fshift) + 1) | |
| # Simple fake detection (random for demo) | |
| is_fake = np.random.random() > 0.7 | |
| confidence = np.random.random() | |
| return is_fake, confidence, magnitude | |
| def analyze_image(image): | |
| """Simple image analysis""" | |
| is_fake, confidence, magnitude = simple_frequency_analysis(image) | |
| # Create visualization | |
| if isinstance(image, Image.Image): | |
| img_array = np.array(image) | |
| else: | |
| img_array = image | |
| # Add result overlay | |
| result_img = img_array.copy() | |
| text = "FAKE" if is_fake else "REAL" | |
| color = (255, 0, 0) if is_fake else (0, 255, 0) | |
| cv2.putText(result_img, text, (50, 50), | |
| cv2.FONT_HERSHEY_SIMPLEX, 1, color, 2) | |
| cv2.putText(result_img, f"Confidence: {confidence:.2%}", (50, 100), | |
| cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 255, 255), 2) | |
| # Convert back to PIL | |
| if len(result_img.shape) == 3 and result_img.shape[2] == 3: | |
| result_pil = Image.fromarray(cv2.cvtColor(result_img, cv2.COLOR_BGR2RGB)) | |
| else: | |
| result_pil = Image.fromarray(result_img) | |
| # Create frequency visualization | |
| magnitude_normalized = ((magnitude - magnitude.min()) / | |
| (magnitude.max() - magnitude.min()) * 255).astype(np.uint8) | |
| freq_pil = Image.fromarray(magnitude_normalized) | |
| # Report | |
| report = f""" | |
| ## Analysis Results | |
| **Verdict:** {'โ ๏ธ **DEEPFAKE DETECTED**' if is_fake else 'โ **LIKELY REAL**'} | |
| **Confidence:** {confidence:.2%} | |
| --- | |
| This demo shows: | |
| 1. Original image | |
| 2. Frequency spectrum analysis | |
| 3. Simple fake detection logic | |
| _Note: This is a demonstration. For accurate detection, use full system._ | |
| """ | |
| return result_pil, freq_pil, report | |
| # Create interface | |
| with gr.Blocks(title="Deepfake Detection Demo") as demo: | |
| gr.Markdown("# ๐ Deepfake Detection Demo") | |
| gr.Markdown("Upload an image to see how frequency analysis works") | |
| with gr.Row(): | |
| with gr.Column(): | |
| image_input = gr.Image(label="Upload Image", type="pil") | |
| analyze_btn = gr.Button("Analyze", variant="primary") | |
| with gr.Column(): | |
| result_output = gr.Image(label="Result", type="pil") | |
| freq_output = gr.Image(label="Frequency Spectrum", type="pil") | |
| report_output = gr.Markdown(label="Analysis Report") | |
| analyze_btn.click( | |
| fn=analyze_image, | |
| inputs=[image_input], | |
| outputs=[result_output, freq_output, report_output] | |
| ) | |
| gr.Markdown("---") | |
| gr.Markdown(""" | |
| ### How it works: | |
| 1. Upload an image (JPEG/PNG) | |
| 2. Click Analyze | |
| 3. View the frequency spectrum | |
| 4. See detection result | |
| ### This demo includes: | |
| - Frequency domain analysis | |
| - Simple fake detection logic | |
| - Visual feedback | |
| ### For full features: | |
| Run locally with all dependencies installed. | |
| """) | |
| if __name__ == "__main__": | |
| demo.launch() |