Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,120 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import gradio as gr
|
3 |
+
import torch
|
4 |
+
from PIL import Image, ImageFilter, ImageDraw
|
5 |
+
from ultralytics import YOLO
|
6 |
+
|
7 |
+
MODEL_PATH = "./models/face_yolov8n_v2.pt"
|
8 |
+
MODEL = YOLO(MODEL_PATH)
|
9 |
+
|
10 |
+
def create_rounded_rectangle_mask(image:Image.Image, radius, alpha=255):
|
11 |
+
size = image.size
|
12 |
+
factor = 5 # Factor to increase the image size that I can later antialiaze the corners
|
13 |
+
radius = radius * factor
|
14 |
+
image = Image.new('RGBA', (size[0] * factor, size[1] * factor), (0, 0, 0, 0))
|
15 |
+
|
16 |
+
# create corner
|
17 |
+
corner = Image.new('RGBA', (radius, radius), (0, 0, 0, 0))
|
18 |
+
draw = ImageDraw.Draw(corner)
|
19 |
+
# added the fill = .. you only drew a line, no fill
|
20 |
+
draw.pieslice((0, 0, radius * 2, radius * 2), 180, 270, fill=(255, 255, 255, alpha))
|
21 |
+
|
22 |
+
# max_x, max_y
|
23 |
+
mx, my = (size[0] * factor, size[1] * factor)
|
24 |
+
|
25 |
+
# paste corner rotated as needed
|
26 |
+
# use corners alpha channel as mask
|
27 |
+
image.paste(corner, (0, 0), corner)
|
28 |
+
image.paste(corner.rotate(90), (0, my - radius), corner.rotate(90))
|
29 |
+
image.paste(corner.rotate(180), (mx - radius, my - radius), corner.rotate(180))
|
30 |
+
image.paste(corner.rotate(270), (mx - radius, 0), corner.rotate(270))
|
31 |
+
|
32 |
+
# draw both inner rects
|
33 |
+
draw = ImageDraw.Draw(image)
|
34 |
+
draw.rectangle([(radius, 0), (mx - radius, my)], fill=(255, 255, 255, alpha))
|
35 |
+
draw.rectangle([(0, radius), (mx, my - radius)], fill=(255, 255, 255, alpha))
|
36 |
+
return image.resize(size, Image.LANCZOS) # Smooth the corners
|
37 |
+
|
38 |
+
def max_rounding_radius(rect_coords):
|
39 |
+
# Extract the coordinates of the rectangle [x0, y0, x1, y1]
|
40 |
+
rect_coords = [coord-1 for coord in rect_coords]
|
41 |
+
x0, y0, x1, y1 = rect_coords
|
42 |
+
|
43 |
+
# Calculate the width and height of the rectangle
|
44 |
+
width = x1 - x0
|
45 |
+
height = y1 - y0
|
46 |
+
|
47 |
+
# Determine the smallest dimension (width or height)
|
48 |
+
min_dimension = min(width, height)
|
49 |
+
|
50 |
+
# Calculate the maximum radius as half of the smallest dimension
|
51 |
+
return min_dimension // 2
|
52 |
+
|
53 |
+
def generate_image(source_image:Image.Image, confidence=0.3, radius=50, blur_amount=10, margin=0):
|
54 |
+
if source_image is None:
|
55 |
+
return source_image
|
56 |
+
|
57 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
58 |
+
pred = MODEL(source_image, conf=confidence, device=device)
|
59 |
+
bboxes = pred[0].boxes.xyxy.cpu().numpy()
|
60 |
+
if bboxes.size == 0:
|
61 |
+
result = None
|
62 |
+
else:
|
63 |
+
bboxes = bboxes.tolist()
|
64 |
+
result = source_image.copy()
|
65 |
+
|
66 |
+
for bboxe in bboxes:
|
67 |
+
bboxe = [round(coord) for coord in bboxe]
|
68 |
+
|
69 |
+
mean_margin = margin // 2
|
70 |
+
new_bboxe = bboxe[0]-mean_margin, bboxe[1]-mean_margin, bboxe[2]+mean_margin, bboxe[3]+mean_margin
|
71 |
+
|
72 |
+
new_x0, new_y0, new_x1, new_y1 = new_bboxe
|
73 |
+
new_width = new_x1 - new_x0
|
74 |
+
new_height = new_y1 - new_y0
|
75 |
+
|
76 |
+
if not (new_width > source_image.width or new_height > source_image.height):
|
77 |
+
bboxe = new_bboxe
|
78 |
+
|
79 |
+
# Crop the region of interest
|
80 |
+
region = result.crop(bboxe)
|
81 |
+
|
82 |
+
final_radius = round(max_rounding_radius(bboxe)* (radius/100))
|
83 |
+
mask = create_rounded_rectangle_mask(region, final_radius)
|
84 |
+
|
85 |
+
# Apply blur filter to the cropped region
|
86 |
+
blurred_region = region.filter(ImageFilter.GaussianBlur(radius=blur_amount))
|
87 |
+
|
88 |
+
# Paste the blurred region back onto the original image
|
89 |
+
result.paste(blurred_region, bboxe, mask)
|
90 |
+
|
91 |
+
return result
|
92 |
+
|
93 |
+
css = """
|
94 |
+
img {
|
95 |
+
max-height: 500px;
|
96 |
+
object-fit: contain;
|
97 |
+
}
|
98 |
+
"""
|
99 |
+
|
100 |
+
with gr.Blocks(css=css) as FACE_2_BLUR:
|
101 |
+
with gr.Row():
|
102 |
+
with gr.Column():
|
103 |
+
image = gr.Image(label="Upload your image", type="pil")
|
104 |
+
generate_btn = gr.Button("Generate")
|
105 |
+
confidence = gr.Slider(label="Detection model confidence threshold.", value=0.3, minimum=0.0, maximum=1.0, step=0.01)
|
106 |
+
radius = gr.Slider(label="Edges radius in percentage.", value=50, minimum=0, maximum=100, step=1)
|
107 |
+
blur_amount = gr.Number(label="Controls the strength of the blur effect.", value=10, minimum=1, maximum=20, step=1)
|
108 |
+
margin = gr.Slider(label="Margin to add to the blurred box.", value=0, minimum=0, maximum=200)
|
109 |
+
|
110 |
+
with gr.Column():
|
111 |
+
image_out = gr.Image(label="Blurred face image", type="pil")
|
112 |
+
|
113 |
+
generate_btn.click(
|
114 |
+
fn=generate_image,
|
115 |
+
inputs=[image, confidence, radius, blur_amount, margin],
|
116 |
+
outputs=image_out,
|
117 |
+
api_name="generate_blurred"
|
118 |
+
)
|
119 |
+
|
120 |
+
FACE_2_BLUR.launch()
|