it@M InnovationLab commited on
Commit
e76c1b2
1 Parent(s): 9ed8e63

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -51
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
- def detect_person(image: Image):
13
- result = seg_model(image, device="CPU")[0]
14
- masks = result.masks.data
15
- clss = result.boxes.cls
16
- person_indices = torch.where(clss == 0)
17
- person_masks = masks[person_indices]
 
 
 
18
  people_mask = torch.any(person_masks, dim=0).to(torch.uint8) * 255
19
- mask = transform(~people_mask)
20
- mask = mask.resize((image.width, image.height), resample=Image.Resampling.BILINEAR)
21
- return mask
22
-
23
- def detect_license_plate(image: Image):
24
- result = lp_model(image, imgsz=(image.height, image.width), device="cpu")[0]
25
- boxes = result.boxes.data[:, :4]
26
- mask = Image.new(mode="L", size=image.size, color=255)
27
- draw = ImageDraw.Draw(mask)
28
- for box in boxes:
29
- draw.rectangle(list(box), fill=0)
30
- return mask
31
-
32
-
33
- def detect_dummy(image: Image):
34
- return Image.new(mode="L", size=image.size, color=255)
35
-
36
-
37
- detectors = {
38
- "Person": detect_person,
39
- "License Plate": detect_license_plate
40
- }
41
-
42
- def test_gradio(image):
43
- masks = [detect_person(image), detect_license_plate(image)]
44
- combined = np.minimum.reduce([np.array(m) for m in masks])
45
- mask = Image.fromarray(combined)
46
- # Apply blur through mask
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
- # demo_live = gr.Interface(
54
- # fn=test_gradio,
55
- # inputs=gr.Image(source="webcam", type="pil", shape=(640, 480)),
56
- # outputs=gr.Image(type="pil")
57
- # )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
 
59
  demo_upload = gr.Interface(
60
- fn=test_gradio,
 
61
  inputs=gr.Image(type="pil"),
62
- outputs=gr.Image()
 
 
 
 
 
63
  )
64
 
65
- # demo = gr.TabbedInterface(
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()