LPX commited on
Commit
67883c3
·
1 Parent(s): 64eb1c4

✨ feat(image): add gradient processing in predict_image Function

Browse files

-Body Mainly:【main】+

- add gradient processing to predict image pipeline using new utils module
- introduce new gradient processing parameters for enhanced image analysis

✨ feat(utils): add gradient processing utility

- Body Process

-add gradient processing utility with various configuration options

- Body Methods
-add normalization, equalization, and LUT creation for gradient processing
-add spatial gradient calculation for image analysis
-implement blue mode options for gradient processing

- Body Mode
-images and dictionaries properly formatted according to function requirements
-utiilities imports updated for type relevance
-variables initialized for reuse and efficiency

πŸ”§ chore(utils): add python eol format

-update utils/gradiend.py for better visual formatting

Files changed (2) hide show
  1. app.py +7 -3
  2. utils/gradient.py +50 -0
app.py CHANGED
@@ -8,6 +8,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
@@ -230,12 +231,15 @@ def predict_image_with_html(img, confidence_threshold, augment_methods, rotate_d
230
  img_pil = img
231
  img_pil, results = predict_image(img_pil, confidence_threshold)
232
 
 
 
233
  # Generate ELA images with different presets
234
- ela_img_1 = ELA(img_pil, scale=77, alpha=0.66)
235
- ela_img_2 = ELA(img_pil, scale=100, alpha=0.8)
236
  ela_img_3 = ELA(img_pil, scale=50, alpha=0.5)
 
237
 
238
- ela_images = [ela_img_1, ela_img_2, ela_img_3]
239
 
240
  html_content = generate_results_html(results)
241
  return img_pil, ela_images, html_content
 
8
  import io
9
  import logging
10
  from utils.utils import softmax, augment_image, convert_pil_to_bytes, ELA
11
+ from utils.gradient import gradient_processing
12
 
13
 
14
  # Configure logging
 
231
  img_pil = img
232
  img_pil, results = predict_image(img_pil, confidence_threshold)
233
 
234
+ gradient_image = gradient_processing(img_pil) # Added gradient processing
235
+
236
  # Generate ELA images with different presets
237
+ ela_img_1 = ELA(img_pil, scale=100, alpha=0.66)
238
+ ela_img_2 = ELA(img_pil, scale=75, alpha=0.8)
239
  ela_img_3 = ELA(img_pil, scale=50, alpha=0.5)
240
+ ela_img_4 = ELA(img_pil, scale=25, alpha=0.85)
241
 
242
+ ela_images = [ela_img_1, ela_img_2, ela_img_3, ela_img_4, gradient_image]
243
 
244
  html_content = generate_results_html(results)
245
  return img_pil, ela_images, html_content
utils/gradient.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import cv2 as cv
3
+ from PIL import Image
4
+
5
+ def norm_mat(mat):
6
+ return cv.normalize(mat, None, 0, 255, cv.NORM_MINMAX).astype(np.uint8)
7
+
8
+ def equalize_img(img):
9
+ ycrcb = cv.cvtColor(img, cv.COLOR_BGR2YCrCb)
10
+ ycrcb[:, :, 0] = cv.equalizeHist(ycrcb[:, :, 0])
11
+ return cv.cvtColor(ycrcb, cv.COLOR_YCrCb2BGR)
12
+
13
+ def create_lut(intensity, gamma):
14
+ lut = np.zeros((256, 1, 3), dtype=np.uint8)
15
+ for i in range(256):
16
+ lut[i, 0, 0] = min(255, max(0, i + intensity))
17
+ lut[i, 0, 1] = min(255, max(0, i + intensity))
18
+ lut[i, 0, 2] = min(255, max(0, i + intensity))
19
+ return lut
20
+
21
+ def gradient_processing(image, intensity=95, blue_mode="Abs", invert=False, equalize=False):
22
+ image = np.array(image)
23
+ dx, dy = cv.spatialGradient(cv.cvtColor(image, cv.COLOR_BGR2GRAY))
24
+ intensity = int(intensity / 100 * 127)
25
+ if invert:
26
+ dx = (-dx).astype(np.float32)
27
+ dy = (-dy).astype(np.float32)
28
+ else:
29
+ dx = (+dx).astype(np.float32)
30
+ dy = (+dy).astype(np.float32)
31
+ dx_abs = np.abs(dx)
32
+ dy_abs = np.abs(dy)
33
+ red = ((dx / np.max(dx_abs) * 127) + 127).astype(np.uint8)
34
+ green = ((dy / np.max(dy_abs) * 127) + 127).astype(np.uint8)
35
+ if blue_mode == "None":
36
+ blue = np.zeros_like(red)
37
+ elif blue_mode == "Flat":
38
+ blue = np.full_like(red, 255)
39
+ elif blue_mode == "Abs":
40
+ blue = norm_mat(dx_abs + dy_abs)
41
+ elif blue_mode == "Norm":
42
+ blue = norm_mat(np.linalg.norm(cv.merge((red, green)), axis=2))
43
+ else:
44
+ blue = None
45
+ gradient = cv.merge([blue, green, red])
46
+ if equalize:
47
+ gradient = equalize_img(gradient)
48
+ elif intensity > 0:
49
+ gradient = cv.LUT(gradient, create_lut(intensity, intensity))
50
+ return Image.fromarray(gradient)