Spaces:
Running
Running
File size: 3,093 Bytes
0b8f50d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# 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.
### Worked By
Pujan Neupane
|