amirgame197 commited on
Commit
ed1578c
·
verified ·
1 Parent(s): fedfb6e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -18
app.py CHANGED
@@ -1,31 +1,22 @@
1
  import gradio as gr
2
  from nudenet import NudeDetector
3
  import cv2
 
4
 
5
- def blur_image(image_path, detections, parts_to_blur):
6
  image = cv2.imread(image_path)
7
 
8
  for detection in detections:
9
  label = detection['class']
10
  if label in parts_to_blur:
11
  x, y, width, height = map(int, detection['box'])
12
- x1, y1 = x, y
13
- x2, y2 = x + width, y + height
14
 
15
- x1 = max(0, x1)
16
- y1 = max(0, y1)
17
- x2 = min(image.shape[1], x2)
18
- y2 = min(image.shape[0], y2)
19
-
20
- print(f"Blurring region: {x1, y1, x2, y2} for label: {label}")
21
- roi = image[y1:y2, x1:x2]
22
-
23
- if roi.size == 0:
24
- print(f"Skipping empty ROI for detection: {detection}")
25
- continue
26
-
27
- roi = cv2.GaussianBlur(roi, (51, 51), 30)
28
- image[y1:y2, x1:x2] = roi
29
 
30
  blurred_image_path = 'blurred_' + image_path.split('/')[-1]
31
  cv2.imwrite(blurred_image_path, image)
@@ -41,7 +32,7 @@ def process(input_img):
41
  'MALE_BREAST_EXPOSED', 'ANUS_EXPOSED'
42
  ]
43
 
44
- blurred_image_path = blur_image(input_img, detections, parts_to_blur)
45
  return blurred_image_path
46
 
47
  iface = gr.Interface(process, gr.components.Image(type='filepath'), gr.components.Image(type="filepath"))
 
1
  import gradio as gr
2
  from nudenet import NudeDetector
3
  import cv2
4
+ import numpy as np
5
 
6
+ def circular_blur(image_path, detections, parts_to_blur):
7
  image = cv2.imread(image_path)
8
 
9
  for detection in detections:
10
  label = detection['class']
11
  if label in parts_to_blur:
12
  x, y, width, height = map(int, detection['box'])
13
+ center_x, center_y = x + width // 2, y + height // 2
14
+ radius = int(min(width, height) / 2)
15
 
16
+ mask = np.zeros_like(image)
17
+ cv2.circle(mask, (center_x, center_y), radius, (255, 255, 255), -1)
18
+ blurred_image = cv2.GaussianBlur(image, (51, 51), 30)
19
+ image = np.where(mask == 255, blurred_image, image)
 
 
 
 
 
 
 
 
 
 
20
 
21
  blurred_image_path = 'blurred_' + image_path.split('/')[-1]
22
  cv2.imwrite(blurred_image_path, image)
 
32
  'MALE_BREAST_EXPOSED', 'ANUS_EXPOSED'
33
  ]
34
 
35
+ blurred_image_path = circular_blur(input_img, detections, parts_to_blur)
36
  return blurred_image_path
37
 
38
  iface = gr.Interface(process, gr.components.Image(type='filepath'), gr.components.Image(type="filepath"))