Spaces:
Build error
Build error
Francesco Pochetti
commited on
Commit
·
767fc76
1
Parent(s):
572447c
moving to self trained face segmentation model
Browse files- .DS_Store +0 -0
- app.py +51 -51
- images/{crowd.jpeg → crowd2.jpeg} +0 -0
- images/girls.jpeg +0 -0
- images/kid.jpeg +0 -0
- model/model.pt +3 -0
.DS_Store
ADDED
Binary file (6.15 kB). View file
|
|
app.py
CHANGED
@@ -1,67 +1,67 @@
|
|
1 |
import cv2
|
2 |
import gradio as gr
|
3 |
-
from
|
|
|
4 |
import numpy as np
|
5 |
import torch
|
6 |
-
import kornia as K
|
7 |
-
from kornia.contrib import FaceDetector, FaceDetectorResult
|
8 |
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
image.
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
return
|
53 |
|
54 |
content_image_input = gr.inputs.Image(label="Content Image", type="pil")
|
|
|
55 |
|
56 |
description="Privacy first! Upload an image of a groupf of people and blur their faces automatically."
|
57 |
article="""
|
58 |
-
Demo built on top of
|
59 |
-
<a href='https://github.com/
|
60 |
"""
|
61 |
-
examples = [["./images/
|
62 |
|
63 |
app_interface = gr.Interface(fn=run,
|
64 |
-
inputs=[content_image_input],
|
65 |
outputs="image",
|
66 |
title="Blurry Faces",
|
67 |
description=description,
|
|
|
1 |
import cv2
|
2 |
import gradio as gr
|
3 |
+
from typing import Union, Tuple
|
4 |
+
from PIL import Image, ImageOps
|
5 |
import numpy as np
|
6 |
import torch
|
|
|
|
|
7 |
|
8 |
+
model = torch.jit.load('./model/model.pt').eval()
|
9 |
+
|
10 |
+
def resize_with_padding(img: Image.Image, expected_size: Tuple[int, int]) -> Image.Image:
|
11 |
+
img.thumbnail((expected_size[0], expected_size[1]))
|
12 |
+
delta_width = expected_size[0] - img.size[0]
|
13 |
+
delta_height = expected_size[1] - img.size[1]
|
14 |
+
pad_width = delta_width // 2
|
15 |
+
pad_height = delta_height // 2
|
16 |
+
padding = (pad_width, pad_height, delta_width - pad_width, delta_height - pad_height)
|
17 |
+
return ImageOps.expand(img, padding), padding
|
18 |
+
|
19 |
+
def preprocess_image(img: Image.Image, size: int = 512) -> Tuple[Image.Image, torch.tensor, Tuple[int]]:
|
20 |
+
pil_img, padding = resize_with_padding(img, (size, size))
|
21 |
+
|
22 |
+
img = (np.array(pil_img).astype(np.float32) / 255) - np.array([0.485, 0.456, 0.406], dtype=np.float32).reshape(1, 1, 3)
|
23 |
+
img = img / np.array([0.229, 0.224, 0.225], dtype=np.float32).reshape(1, 1, 3)
|
24 |
+
img = np.transpose(img, (2, 0, 1))
|
25 |
+
|
26 |
+
return pil_img, torch.tensor(img[None]), padding
|
27 |
+
|
28 |
+
def soft_blur_with_mask(image: Image.Image, mask: torch.tensor, padding: Tuple[int]) -> Image.Image:
|
29 |
+
image = np.array(image)
|
30 |
+
# Create a blurred copy of the original image.
|
31 |
+
blurred_image = cv2.GaussianBlur(image, (221, 221), sigmaX=20, sigmaY=20)
|
32 |
+
image_height, image_width = image.shape[:2]
|
33 |
+
mask = cv2.resize(mask.astype(np.uint8), (image_width, image_height), interpolation=cv2.INTER_NEAREST)
|
34 |
+
# Blurring the mask itself to get a softer mask with no firm edges
|
35 |
+
mask = cv2.GaussianBlur(mask.astype(np.float32), (11, 11), 10, 10)[:, :, None]
|
36 |
+
|
37 |
+
# Take the blurred image where the mask it positive, and the original image where the image is original
|
38 |
+
image = (mask * blurred_image + (1.0 - mask) * image)
|
39 |
+
pad_w, pad_h, _, _ = padding
|
40 |
+
img_w, img_h, _ = image.shape
|
41 |
+
image = image[(pad_h):(img_h-pad_h), (pad_w):(img_w-pad_w), :]
|
42 |
+
return Image.fromarray(image.astype(np.uint8))
|
43 |
+
|
44 |
+
def run(image, size):
|
45 |
+
pil_image, torch_image, padding = preprocess_image(image, size=size)
|
46 |
+
|
47 |
+
with torch.inference_mode():
|
48 |
+
mask = model(torch_image)
|
49 |
+
mask = mask.argmax(dim=1).numpy().squeeze()
|
50 |
+
|
51 |
+
return soft_blur_with_mask(pil_image, mask, padding)
|
52 |
|
53 |
content_image_input = gr.inputs.Image(label="Content Image", type="pil")
|
54 |
+
model_image_size = gr.inputs.Radio([256, 384, 512, 1024], type="value", default=512, label="Inference size")
|
55 |
|
56 |
description="Privacy first! Upload an image of a groupf of people and blur their faces automatically."
|
57 |
article="""
|
58 |
+
Demo built on top of a face segmentation model trained from scratch with IceVision on the
|
59 |
+
<a href='https://github.com/microsoft/FaceSynthetics' target='_blank'>FaceSynthetics</a> dataset.
|
60 |
"""
|
61 |
+
examples = [["./images/girls.jpeg", 384], ["./images/kid.jpeg", 256], ["./images/family.jpeg", 512], ["./images/crowd1.jpeg", 1024], ["./images/crowd2.jpeg", 1024]]
|
62 |
|
63 |
app_interface = gr.Interface(fn=run,
|
64 |
+
inputs=[content_image_input, model_image_size],
|
65 |
outputs="image",
|
66 |
title="Blurry Faces",
|
67 |
description=description,
|
images/{crowd.jpeg → crowd2.jpeg}
RENAMED
File without changes
|
images/girls.jpeg
ADDED
![]() |
images/kid.jpeg
ADDED
![]() |
model/model.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:1e310f25944aaf0a35a334798e72aca4494dd19f3785225042017743ecd37757
|
3 |
+
size 165321408
|