LPX
commited on
Commit
·
c08bf6c
1
Parent(s):
86a7c4c
✨ feat(predict): add Error Level Analysis to image prediction
Browse files【Predict Image Function】
- integrate an extra transform using ELA (Error Level Analysis)
【User Interface】
- display ELA transformed image
【Settings】
- updated the visibility of settings slider
- app.py +10 -7
- utils/utils.py +19 -2
app.py
CHANGED
@@ -7,7 +7,7 @@ from PIL import Image
|
|
7 |
import numpy as np
|
8 |
import io
|
9 |
import logging
|
10 |
-
from utils.utils import softmax, augment_image, convert_pil_to_bytes
|
11 |
|
12 |
|
13 |
# Configure logging
|
@@ -219,8 +219,9 @@ def predict_image_with_html(img, confidence_threshold, augment_methods, rotate_d
|
|
219 |
else:
|
220 |
img_pil = img
|
221 |
img_pil, results = predict_image(img_pil, confidence_threshold)
|
|
|
222 |
html_content = generate_results_html(results)
|
223 |
-
return img_pil, html_content
|
224 |
|
225 |
with gr.Blocks() as iface:
|
226 |
with gr.Tab("AI Image Detection"):
|
@@ -231,20 +232,22 @@ with gr.Blocks() as iface:
|
|
231 |
image_input = gr.Image(label="Upload Image to Analyze", sources=['upload'], type='pil')
|
232 |
with gr.Accordion("Settings", open=False, elem_id="settings_accordion"):
|
233 |
augment_checkboxgroup = gr.CheckboxGroup(["rotate", "add_noise", "sharpen"], label="Augmentation Methods")
|
234 |
-
rotate_slider = gr.Slider(0,
|
235 |
-
noise_slider = gr.Slider(0,
|
236 |
-
sharpen_slider = gr.Slider(0,
|
237 |
-
confidence_slider = gr.Slider(0.0, 1.0, value=0.
|
238 |
inputs = [image_input, confidence_slider, augment_checkboxgroup, rotate_slider, noise_slider, sharpen_slider]
|
239 |
predict_button = gr.Button("Predict")
|
240 |
image_output = gr.Image(label="Processed Image", visible=True)
|
|
|
|
|
241 |
|
242 |
with gr.Column(scale=2):
|
243 |
with gr.Accordion("Project OpenSight - Model Evaluations & Playground", open=False, elem_id="project_accordion"):
|
244 |
gr.Markdown("## OpenSight is a SOTA gen. image detection model, in pre-release prep.\n\nThis HF Space is a temporary home for us and the public to evaluate the shortcomings of current open source models.\n\n<-- Feel free to play around by starting with an image as we prepare our formal announcement.")
|
245 |
# Custom HTML component to display results in 5 columns
|
246 |
results_html = gr.HTML(label="Model Predictions")
|
247 |
-
outputs = [image_output, results_html]
|
248 |
|
249 |
# Show/hide rotate slider based on selected augmentation method
|
250 |
augment_checkboxgroup.change(lambda methods: gr.update(visible="rotate" in methods), inputs=[augment_checkboxgroup], outputs=[rotate_slider])
|
|
|
7 |
import numpy as np
|
8 |
import io
|
9 |
import logging
|
10 |
+
from utils.utils import softmax, augment_image, convert_pil_to_bytes, ELA
|
11 |
|
12 |
|
13 |
# Configure logging
|
|
|
219 |
else:
|
220 |
img_pil = img
|
221 |
img_pil, results = predict_image(img_pil, confidence_threshold)
|
222 |
+
ela_img = ELA(img_pil) # Apply ELA to the image
|
223 |
html_content = generate_results_html(results)
|
224 |
+
return img_pil, ela_img, html_content
|
225 |
|
226 |
with gr.Blocks() as iface:
|
227 |
with gr.Tab("AI Image Detection"):
|
|
|
232 |
image_input = gr.Image(label="Upload Image to Analyze", sources=['upload'], type='pil')
|
233 |
with gr.Accordion("Settings", open=False, elem_id="settings_accordion"):
|
234 |
augment_checkboxgroup = gr.CheckboxGroup(["rotate", "add_noise", "sharpen"], label="Augmentation Methods")
|
235 |
+
rotate_slider = gr.Slider(0, 45, value=2, step=1, label="Rotate Degrees", visible=False)
|
236 |
+
noise_slider = gr.Slider(0, 50, value=4, step=1, label="Noise Level", visible=False)
|
237 |
+
sharpen_slider = gr.Slider(0, 50, value=11, step=1, label="Sharpen Strength", visible=False)
|
238 |
+
confidence_slider = gr.Slider(0.0, 1.0, value=0.75, step=0.05, label="Confidence Threshold")
|
239 |
inputs = [image_input, confidence_slider, augment_checkboxgroup, rotate_slider, noise_slider, sharpen_slider]
|
240 |
predict_button = gr.Button("Predict")
|
241 |
image_output = gr.Image(label="Processed Image", visible=True)
|
242 |
+
ela_image_output = gr.Image(label="ELA Processed Image", visible=True)
|
243 |
+
|
244 |
|
245 |
with gr.Column(scale=2):
|
246 |
with gr.Accordion("Project OpenSight - Model Evaluations & Playground", open=False, elem_id="project_accordion"):
|
247 |
gr.Markdown("## OpenSight is a SOTA gen. image detection model, in pre-release prep.\n\nThis HF Space is a temporary home for us and the public to evaluate the shortcomings of current open source models.\n\n<-- Feel free to play around by starting with an image as we prepare our formal announcement.")
|
248 |
# Custom HTML component to display results in 5 columns
|
249 |
results_html = gr.HTML(label="Model Predictions")
|
250 |
+
outputs = [image_output, ela_image_output, results_html]
|
251 |
|
252 |
# Show/hide rotate slider based on selected augmentation method
|
253 |
augment_checkboxgroup.change(lambda methods: gr.update(visible="rotate" in methods), inputs=[augment_checkboxgroup], outputs=[rotate_slider])
|
utils/utils.py
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
import numpy as np
|
2 |
import io
|
3 |
-
from PIL import Image, ImageFilter
|
4 |
from torchvision import transforms
|
5 |
|
6 |
def softmax(vector):
|
@@ -22,4 +22,21 @@ def convert_pil_to_bytes(image, format='JPEG'):
|
|
22 |
img_byte_arr = io.BytesIO()
|
23 |
image.save(img_byte_arr, format=format)
|
24 |
img_byte_arr = img_byte_arr.getvalue()
|
25 |
-
return img_byte_arr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import numpy as np
|
2 |
import io
|
3 |
+
from PIL import Image, ImageFilter, ImageChops
|
4 |
from torchvision import transforms
|
5 |
|
6 |
def softmax(vector):
|
|
|
22 |
img_byte_arr = io.BytesIO()
|
23 |
image.save(img_byte_arr, format=format)
|
24 |
img_byte_arr = img_byte_arr.getvalue()
|
25 |
+
return img_byte_arr
|
26 |
+
|
27 |
+
def ELA(img_pil, scale=77, alpha=0.66):
|
28 |
+
# Error Level Analysis for basic image forensics
|
29 |
+
original = img_pil.copy() # open up the input image
|
30 |
+
temp_path = 'temp.jpg' # temporary image name to save the ELA to
|
31 |
+
original.save(temp_path, quality=95) # re-save the image with a quality of 95%
|
32 |
+
temporary = Image.open(temp_path) # open up the re-saved image
|
33 |
+
|
34 |
+
diff = ImageChops.difference(original, temporary) # load in the images to look at pixel by pixel differences
|
35 |
+
d = diff.load() # load the image into a variable
|
36 |
+
WIDTH, HEIGHT = diff.size # set the size into a tuple
|
37 |
+
for x in range(WIDTH): # row by row
|
38 |
+
for y in range(HEIGHT): # column by column
|
39 |
+
d[x, y] = tuple(k * scale for k in d[x, y]) # set the pixels to their x,y & color based on error
|
40 |
+
|
41 |
+
new_img = ImageChops.blend(temporary, diff, alpha) # blend the original w/ the ELA @ a set alpha/transparency
|
42 |
+
return new_img
|