Spaces:
Running
Running
it@M InnovationLab
commited on
Commit
•
e76c1b2
1
Parent(s):
9ed8e63
Update app.py
Browse files
app.py
CHANGED
@@ -9,67 +9,81 @@ transform = torchvision.transforms.ToPILImage()
|
|
9 |
seg_model = YOLO("yolov8m-seg.pt")
|
10 |
lp_model = YOLO("yolov8m_lp.pt")
|
11 |
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
|
|
|
|
|
|
18 |
people_mask = torch.any(person_masks, dim=0).to(torch.uint8) * 255
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
blurred = image.filter(ImageFilter.GaussianBlur(30))
|
48 |
anonymized = Image.composite(image, blurred, mask)
|
49 |
## TODO: Tempfile statt einem generischen File
|
50 |
anonymized.save("anon.JPG")
|
51 |
return "anon.JPG"
|
52 |
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
|
59 |
demo_upload = gr.Interface(
|
60 |
-
|
|
|
61 |
inputs=gr.Image(type="pil"),
|
62 |
-
outputs=gr.Image()
|
|
|
|
|
|
|
|
|
|
|
63 |
)
|
64 |
|
65 |
-
|
66 |
-
# interface_list=[demo_live, demo_upload],
|
67 |
-
# tab_names=["Webcam", "Bild hochladen"],
|
68 |
-
# title="Image Anonymizer"
|
69 |
-
# )
|
70 |
-
|
71 |
-
# print(__name__)
|
72 |
-
# demo_upload.launch(server_name="localhost", server_port=8080)
|
73 |
-
# demo.launch(server_name="localhost", server_port=8080)
|
74 |
-
|
75 |
demo_upload.launch()
|
|
|
9 |
seg_model = YOLO("yolov8m-seg.pt")
|
10 |
lp_model = YOLO("yolov8m_lp.pt")
|
11 |
|
12 |
+
|
13 |
+
def detect(image):
|
14 |
+
seg_result = seg_model(image, device="CPU")[0]
|
15 |
+
seg_masks = seg_result.masks.data
|
16 |
+
seg_clss = seg_result.boxes.cls
|
17 |
+
seg_boxes = seg_result.boxes.data
|
18 |
+
|
19 |
+
person_indices = torch.where(seg_clss == 0)
|
20 |
+
person_masks = seg_masks[person_indices]
|
21 |
people_mask = torch.any(person_masks, dim=0).to(torch.uint8) * 255
|
22 |
+
people_mask = transform(~people_mask)
|
23 |
+
people_mask = people_mask.resize((image.width, image.height), resample=Image.Resampling.BILINEAR)
|
24 |
+
|
25 |
+
vehicle_classes = [2, 3, 5, 7]
|
26 |
+
license_plates = list()
|
27 |
+
|
28 |
+
for seg_box in seg_boxes:
|
29 |
+
if seg_box[5] in vehicle_classes:
|
30 |
+
vehicle_box = seg_box[:4].to(torch.int32)
|
31 |
+
vehicle_crop = image.crop(vehicle_box.tolist())
|
32 |
+
lp_result = lp_model(vehicle_crop, imgsz=(vehicle_crop.height, vehicle_crop.width), device="cpu")[0]
|
33 |
+
lp_boxes = lp_result.boxes.data[:, :4]
|
34 |
+
vehicle_offset = torch.cat((vehicle_box[:2], vehicle_box[:2]))
|
35 |
+
for lp_box in lp_boxes:
|
36 |
+
license_plates.append(torch.add(lp_box, vehicle_offset))
|
37 |
+
|
38 |
+
lp_mask = Image.new(mode="L", size=image.size, color=255)
|
39 |
+
draw = ImageDraw.Draw(lp_mask)
|
40 |
+
|
41 |
+
for license_plate in license_plates:
|
42 |
+
draw.rectangle(license_plate.tolist(), fill = 0)
|
43 |
+
|
44 |
+
combined_mask = Image.fromarray(np.minimum.reduce([np.array(m) for m in [people_mask, lp_mask]]))
|
45 |
+
return combined_mask
|
46 |
+
|
47 |
+
|
48 |
+
def test_comb(image):
|
49 |
+
mask = detect(image)
|
50 |
blurred = image.filter(ImageFilter.GaussianBlur(30))
|
51 |
anonymized = Image.composite(image, blurred, mask)
|
52 |
## TODO: Tempfile statt einem generischen File
|
53 |
anonymized.save("anon.JPG")
|
54 |
return "anon.JPG"
|
55 |
|
56 |
+
|
57 |
+
css = """
|
58 |
+
P { text-align: center }
|
59 |
+
H3 { text-align: center }
|
60 |
+
"""
|
61 |
+
|
62 |
+
description = """
|
63 |
+
### ML-Prototyp zur Anonymisierung von Bildern
|
64 |
+
Es werden Personen sowie Kennzeichen zensiert.
|
65 |
+
Große Bilder können einige Zeit benötigen.
|
66 |
+
"""
|
67 |
+
|
68 |
+
article = """
|
69 |
+
Nutzt YOLOv8-Modelle zur Erkennung / Segmentierung der Bilder.
|
70 |
+
|
71 |
+
Code: https://huggingface.co/spaces/it-at-m/image-anonymizer/tree/main
|
72 |
+
|
73 |
+
Ein Prototyp des it@M InnovationLab (itm.innolab@muenchen.de)
|
74 |
+
"""
|
75 |
|
76 |
demo_upload = gr.Interface(
|
77 |
+
title="Image Anonymizer",
|
78 |
+
fn=test_comb,
|
79 |
inputs=gr.Image(type="pil"),
|
80 |
+
outputs=gr.Image(),
|
81 |
+
allow_flagging="never",
|
82 |
+
examples="examples",
|
83 |
+
description=description,
|
84 |
+
article=article,
|
85 |
+
css=css
|
86 |
)
|
87 |
|
88 |
+
demo_upload.queue(concurrency_count=1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
89 |
demo_upload.launch()
|