|
| 1 | +import numpy as np |
| 2 | +from PIL import Image |
| 3 | + |
| 4 | + |
| 5 | +def detect_deepfake_artifacts(image_path): |
| 6 | + """ |
| 7 | + Detects common deepfake/GAN artifacts: |
| 8 | + - Blurry boundaries between face and background |
| 9 | + - Unnatural skin texture |
| 10 | + - Inconsistent lighting |
| 11 | + - Face warping artifacts |
| 12 | +
|
| 13 | + Args: |
| 14 | + image_path (str): Path to image file |
| 15 | +
|
| 16 | + Returns: |
| 17 | + dict: Deepfake artifact detection results |
| 18 | + """ |
| 19 | + try: |
| 20 | + img = Image.open(image_path).convert('RGB') |
| 21 | + img_array = np.array(img, dtype=np.float32) |
| 22 | + |
| 23 | + # Check for common GAN artifacts |
| 24 | + # 1. Frequency anomalies (GANs produce artifacts in specific frequency bands) |
| 25 | + from scipy.fft import fft2, fftshift |
| 26 | + |
| 27 | + gray = np.mean(img_array, axis=2) |
| 28 | + fft_result = fft2(gray) |
| 29 | + magnitude = np.abs(fftshift(fft_result)) |
| 30 | + |
| 31 | + # Analyze specific frequency bands used by GANs |
| 32 | + h, w = magnitude.shape |
| 33 | + center_h, center_w = h // 2, w // 2 |
| 34 | + |
| 35 | + # High frequency analysis (often shows GAN artifacts) |
| 36 | + high_freq_ring = magnitude[center_h - |
| 37 | + 20:center_h+20, center_w-20:center_w+20] |
| 38 | + high_freq_variance = np.var(high_freq_ring) |
| 39 | + |
| 40 | + # 2. Texture consistency check |
| 41 | + # GANs often produce subtle texture inconsistencies |
| 42 | + r, g, b = img_array[:, :, 0], img_array[:, :, 1], img_array[:, :, 2] |
| 43 | + |
| 44 | + # Channel correlation |
| 45 | + rg_correlation = np.corrcoef(r.flatten(), g.flatten())[0, 1] |
| 46 | + rb_correlation = np.corrcoef(r.flatten(), b.flatten())[0, 1] |
| 47 | + gb_correlation = np.corrcoef(g.flatten(), b.flatten())[0, 1] |
| 48 | + |
| 49 | + avg_channel_correlation = np.mean( |
| 50 | + [rg_correlation, rb_correlation, gb_correlation]) |
| 51 | + |
| 52 | + # 3. Boundary blur detection |
| 53 | + # Calculate gradient magnitude |
| 54 | + from scipy.ndimage import sobel |
| 55 | + gradient_x = sobel(gray, axis=1) |
| 56 | + gradient_y = sobel(gray, axis=0) |
| 57 | + gradient_magnitude = np.sqrt(gradient_x**2 + gradient_y**2) |
| 58 | + |
| 59 | + # High gradient at edges is natural; too uniform suggests blurring |
| 60 | + edge_sharpness = np.std(gradient_magnitude) |
| 61 | + |
| 62 | + result = { |
| 63 | + "status": "analysis_complete", |
| 64 | + "method": "GAN/Deepfake Artifact Detection", |
| 65 | + "image_size": img_array.shape, |
| 66 | + "artifacts": { |
| 67 | + "frequency_anomaly_score": float(high_freq_variance), |
| 68 | + "channel_correlation_score": float(avg_channel_correlation), |
| 69 | + "edge_sharpness_score": float(edge_sharpness) |
| 70 | + }, |
| 71 | + "interpretation": "Scores help identify GAN-generated or deepfake artifacts", |
| 72 | + "note": "This is a simplified heuristic detector; professional deepfake detection requires deep learning models" |
| 73 | + } |
| 74 | + |
| 75 | + return result |
| 76 | + |
| 77 | + except Exception as e: |
| 78 | + return {"error": str(e), "status": "analysis_failed"} |
| 79 | + |
| 80 | + |
| 81 | +def detect_gan_fingerprint(image_path): |
| 82 | + """ |
| 83 | + Detects specific fingerprints left by popular GAN architectures (StyleGAN, ProGAN, etc). |
| 84 | +
|
| 85 | + Args: |
| 86 | + image_path (str): Path to image file |
| 87 | +
|
| 88 | + Returns: |
| 89 | + dict: GAN fingerprint detection results |
| 90 | + """ |
| 91 | + try: |
| 92 | + img = Image.open(image_path).convert('RGB') |
| 93 | + img_array = np.array(img, dtype=np.float32) |
| 94 | + |
| 95 | + # Analyze spectral properties unique to GANs |
| 96 | + from scipy.fft import fft2, fftshift |
| 97 | + |
| 98 | + gray = np.mean(img_array, axis=2) |
| 99 | + fft_result = fft2(gray) |
| 100 | + magnitude = np.abs(fftshift(fft_result)) |
| 101 | + |
| 102 | + # Look for characteristic "blob" patterns in frequency domain |
| 103 | + h, w = magnitude.shape |
| 104 | + center_h, center_w = h // 2, w // 2 |
| 105 | + |
| 106 | + # Radial frequency analysis |
| 107 | + y, x = np.ogrid[:h, :w] |
| 108 | + distance = np.sqrt((y - center_h)**2 + (x - center_w)**2) |
| 109 | + |
| 110 | + radial_profile = [] |
| 111 | + for r in range(1, min(center_h, center_w), 10): |
| 112 | + mask = (distance >= r) & (distance < r + 10) |
| 113 | + radial_profile.append(np.mean(magnitude[mask])) |
| 114 | + |
| 115 | + # GANs produce characteristic radial patterns |
| 116 | + radial_variance = np.var(radial_profile) |
| 117 | + |
| 118 | + result = { |
| 119 | + "status": "analysis_complete", |
| 120 | + "method": "GAN Fingerprint Detection (Spectral Analysis)", |
| 121 | + "radial_frequency_variance": float(radial_variance), |
| 122 | + "potential_gan_likelihood": "Medium" if radial_variance > 1000 else "Low", |
| 123 | + "note": "Requires deep learning model for accurate detection; this is pattern-based heuristic" |
| 124 | + } |
| 125 | + |
| 126 | + return result |
| 127 | + |
| 128 | + except Exception as e: |
| 129 | + return {"error": str(e), "status": "analysis_failed"} |
0 commit comments