Spaces:
Runtime error
Runtime error
UPDATED: Updated ELA image processing in app.py and ela.py
Browse files- Updated ELA image processing in app.py by replacing the previous ELA image generation with two passes: standard analysis and enhanced visibility.
- Updated genELA() function in ela.py to improve the:
- Replaced the previous implementation with a new implementation.
- Added arguments for quality, scale, contrast, linear, and grayscale.
- Compressed the input image using JPEG compression.
- Calculated the difference compressed images.
- Applied scaling to the difference image.
- Applied contrast adjustment to the resulting image.
- Added support for linear difference and grayscale output.
- Returned the processed ELA image.
- app.py +6 -4
- utils/ela.py +59 -17
app.py
CHANGED
|
@@ -277,11 +277,13 @@ def predict_image_with_html(img, confidence_threshold, augment_methods, rotate_d
|
|
| 277 |
gradient_image = gradient_processing(img_np) # Added gradient processing
|
| 278 |
minmax_image = minmax_preprocess(img_np) # Added MinMax processing
|
| 279 |
|
| 280 |
-
#
|
| 281 |
-
|
| 282 |
-
|
|
|
|
|
|
|
| 283 |
|
| 284 |
-
forensics_images = [img_pil,
|
| 285 |
|
| 286 |
html_content = generate_results_html(results)
|
| 287 |
return img_pil, forensics_images, html_content
|
|
|
|
| 277 |
gradient_image = gradient_processing(img_np) # Added gradient processing
|
| 278 |
minmax_image = minmax_preprocess(img_np) # Added MinMax processing
|
| 279 |
|
| 280 |
+
# First pass - standard analysis
|
| 281 |
+
ela1 = ELA(img_np, quality=75, scale=50, contrast=20, linear=False, grayscale=True)
|
| 282 |
+
|
| 283 |
+
# Second pass - enhanced visibility
|
| 284 |
+
ela2 = ELA(img_np, quality=75, scale=75, contrast=25, linear=False, grayscale=True)
|
| 285 |
|
| 286 |
+
forensics_images = [img_pil, ela1, ela2, gradient_image, minmax_image]
|
| 287 |
|
| 288 |
html_content = generate_results_html(results)
|
| 289 |
return img_pil, forensics_images, html_content
|
utils/ela.py
CHANGED
|
@@ -1,21 +1,63 @@
|
|
| 1 |
import numpy as np
|
| 2 |
-
import
|
| 3 |
-
from
|
| 4 |
-
from torchvision import transforms
|
| 5 |
|
| 6 |
-
def
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
temporary = Image.open(temp_path) # open up the re-saved image
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
for x in range(WIDTH): # row by row
|
| 17 |
-
for y in range(HEIGHT): # column by column
|
| 18 |
-
d[x, y] = tuple(k * scale for k in d[x, y]) # set the pixels to their x,y & color based on error
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import numpy as np
|
| 2 |
+
import cv2 as cv
|
| 3 |
+
from time import time
|
|
|
|
| 4 |
|
| 5 |
+
def compress_jpg(image, quality):
|
| 6 |
+
"""Compress image using JPEG compression."""
|
| 7 |
+
encode_param = [int(cv.IMWRITE_JPEG_QUALITY), quality]
|
| 8 |
+
_, buffer = cv.imencode('.jpg', image, encode_param)
|
| 9 |
+
return cv.imdecode(buffer, cv.IMREAD_COLOR)
|
|
|
|
| 10 |
|
| 11 |
+
def desaturate(image):
|
| 12 |
+
"""Convert image to grayscale."""
|
| 13 |
+
return cv.cvtColor(image, cv.COLOR_BGR2GRAY)
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
def create_lut(contrast, brightness):
|
| 16 |
+
"""Create lookup table for contrast and brightness adjustment."""
|
| 17 |
+
lut = np.arange(256, dtype=np.uint8)
|
| 18 |
+
lut = cv.LUT(lut, lut)
|
| 19 |
+
lut = cv.convertScaleAbs(lut, None, contrast/128, brightness)
|
| 20 |
+
return lut
|
| 21 |
+
|
| 22 |
+
def elapsed_time(start):
|
| 23 |
+
"""Calculate elapsed time since start."""
|
| 24 |
+
return f"{time() - start:.3f}s"
|
| 25 |
+
|
| 26 |
+
def genELA(img, quality=75, scale=50, contrast=20, linear=False, grayscale=False):
|
| 27 |
+
"""
|
| 28 |
+
Perform Error Level Analysis on an image.
|
| 29 |
+
|
| 30 |
+
Args:
|
| 31 |
+
img: Input image (numpy array)
|
| 32 |
+
quality: JPEG compression quality (1-100)
|
| 33 |
+
scale: Output multiplicative gain (1-100)
|
| 34 |
+
contrast: Output tonality compression (0-100)
|
| 35 |
+
linear: Whether to use linear difference
|
| 36 |
+
grayscale: Whether to output grayscale image
|
| 37 |
+
|
| 38 |
+
Returns:
|
| 39 |
+
Processed ELA image
|
| 40 |
+
"""
|
| 41 |
+
# Convert image to float32 and normalize
|
| 42 |
+
original = img.astype(np.float32) / 255
|
| 43 |
+
|
| 44 |
+
# Compress image
|
| 45 |
+
compressed = compress_jpg(img, quality)
|
| 46 |
+
compressed = compressed.astype(np.float32) / 255
|
| 47 |
+
|
| 48 |
+
# Calculate difference based on mode
|
| 49 |
+
if not linear:
|
| 50 |
+
difference = cv.absdiff(original, compressed)
|
| 51 |
+
ela = cv.convertScaleAbs(cv.sqrt(difference) * 255, None, scale / 20)
|
| 52 |
+
else:
|
| 53 |
+
ela = cv.convertScaleAbs(cv.subtract(compressed, img), None, scale)
|
| 54 |
+
|
| 55 |
+
# Apply contrast adjustment
|
| 56 |
+
contrast_value = int(contrast / 100 * 128)
|
| 57 |
+
ela = cv.LUT(ela, create_lut(contrast_value, contrast_value))
|
| 58 |
+
|
| 59 |
+
# Convert to grayscale if requested
|
| 60 |
+
if grayscale:
|
| 61 |
+
ela = desaturate(ela)
|
| 62 |
+
|
| 63 |
+
return ela
|