|
|
| # Fast Fourier Transform (FFT) Detector |
|
|
| ```python |
| def run_fft(image: Image.Image, threshold: float = 0.92) -> bool: |
| ``` |
|
|
| ## **Overview** |
|
|
| The `run_fft` function performs a frequency domain analysis on an image using the **Fast Fourier Transform (FFT)** to detect possible **AI generation or digital manipulation**. It leverages the fact that artificially generated or heavily edited images often exhibit a distinct high-frequency pattern. |
|
|
| --- |
|
|
| ## **Parameters** |
|
|
| | Parameter | Type | Description | |
| | ----------- | ----------------- | --------------------------------------------------------------------------------------- | |
| | `image` | `PIL.Image.Image` | Input image to analyze. It will be converted to grayscale and resized. | |
| | `threshold` | `float` | Proportion threshold of high-frequency components to flag the image. Default is `0.92`. | |
|
|
| --- |
|
|
| ## **Returns** |
|
|
| | Type | Description | |
| | ------ | ---------------------------------------------------------------------- | |
| | `bool` | `True` if image is likely AI-generated/manipulated; otherwise `False`. | |
|
|
| --- |
|
|
| ## **Step-by-Step Explanation** |
|
|
| ### 1. **Grayscale Conversion** |
|
|
| All images are converted to grayscale: |
|
|
| ```python |
| gray_image = image.convert("L") |
| ``` |
|
|
| ### 2. **Resize** |
|
|
| The image is resized to a fixed $512 \times 512$ for uniformity: |
|
|
| ```python |
| resized_image = gray_image.resize((512, 512)) |
| ``` |
|
|
| ### 3. **FFT Calculation** |
|
|
| Compute the 2D Discrete Fourier Transform: |
|
|
| $$ |
| F(u, v) = \sum_{x=0}^{M-1} \sum_{y=0}^{N-1} f(x, y) \cdot e^{-2\pi i \left( \frac{ux}{M} + \frac{vy}{N} \right)} |
| $$ |
|
|
| ```python |
| fft_result = fft2(image_array) |
| ``` |
|
|
| ### 4. **Shift Zero Frequency to Center** |
|
|
| Use `fftshift` to center the zero-frequency component: |
|
|
| ```python |
| fft_shifted = fftshift(fft_result) |
| ``` |
|
|
| ### 5. **Magnitude Spectrum** |
|
|
| $$ |
| |F(u, v)| = \sqrt{\Re^2 + \Im^2} |
| $$ |
|
|
| ```python |
| magnitude_spectrum = np.abs(fft_shifted) |
| ``` |
|
|
| ### 6. **Normalization** |
|
|
| Normalize the spectrum to avoid scale issues: |
|
|
| $$ |
| \text{Normalized}(u,v) = \frac{|F(u,v)|}{\max(|F(u,v)|)} |
| $$ |
|
|
| ```python |
| normalized_spectrum = magnitude_spectrum / max_magnitude |
| ``` |
|
|
| ### 7. **High-Frequency Detection** |
|
|
| High-frequency components are defined as: |
|
|
| $$ |
| \text{Mask}(u,v) = |
| \begin{cases} |
| 1 & \text{if } \text{Normalized}(u,v) > 0.5 \\ |
| 0 & \text{otherwise} |
| \end{cases} |
| $$ |
|
|
| ```python |
| high_freq_mask = normalized_spectrum > 0.5 |
| ``` |
|
|
| ### 8. **Proportion Calculation** |
|
|
| $$ |
| \text{Ratio} = \frac{\sum \text{Mask}}{\text{Total pixels}} |
| $$ |
|
|
| ```python |
| high_freq_ratio = np.sum(high_freq_mask) / normalized_spectrum.size |
| ``` |
|
|
| ### 9. **Threshold Decision** |
|
|
| If the ratio exceeds the threshold: |
|
|
| $$ |
| \text{is\_fake} = (\text{Ratio} > \text{Threshold}) |
| $$ |
| |
| ```python |
| is_fake = high_freq_ratio > threshold |
| ``` |
| |
| it is implemented in the api |
| |
| ### Running Locally |
| |
| Just put the function in a notebook or script file and run it with your image. It works well for basic images. |
| |
| |
| [🔙 Back to Main README](../README.md) |
| |