it@M InnovationLab commited on
Commit
3c61106
1 Parent(s): 9af0a64

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -9
app.py CHANGED
@@ -22,12 +22,13 @@ def detect(image):
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
  imgsz = (vehicle_crop.height, vehicle_crop.width) if vehicle_crop.width < 640 and vehicle_crop.height < 640 else (640, 640)
33
  lp_result = lp_model(vehicle_crop, imgsz=imgsz, device="cpu")[0]
@@ -37,22 +38,31 @@ def detect(image):
37
  license_plates.append(torch.add(lp_box, vehicle_offset))
38
 
39
  lp_mask = Image.new(mode="L", size=image.size, color=255)
40
- draw = ImageDraw.Draw(lp_mask)
41
 
42
  for license_plate in license_plates:
43
- draw.rectangle(license_plate.tolist(), fill = 0)
 
 
 
 
 
 
 
44
 
 
45
  combined_mask = Image.fromarray(np.minimum.reduce([np.array(m) for m in [people_mask, lp_mask]]))
46
- return combined_mask
47
 
48
 
49
  def test_comb(image):
50
- mask = detect(image)
51
  blurred = image.filter(ImageFilter.GaussianBlur(30))
52
  anonymized = Image.composite(image, blurred, mask)
53
  ## TODO: Tempfile statt einem generischen File
54
  anonymized.save("anon.JPG")
55
- return "anon.JPG"
 
56
 
57
 
58
  css = """
@@ -77,8 +87,8 @@ Ein Prototyp des it@M InnovationLab (itm.innolab@muenchen.de)
77
  demo_upload = gr.Interface(
78
  title="Image Anonymizer",
79
  fn=test_comb,
80
- inputs=gr.Image(type="pil"),
81
- outputs=gr.Image(),
82
  allow_flagging="never",
83
  examples="examples",
84
  description=description,
 
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] # Classes: car (2), motorcycle (3), bus (5) and truck (7)
26
  license_plates = list()
27
+ vehicle_boxes = []
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_boxes.append(vehicle_box)
32
  vehicle_crop = image.crop(vehicle_box.tolist())
33
  imgsz = (vehicle_crop.height, vehicle_crop.width) if vehicle_crop.width < 640 and vehicle_crop.height < 640 else (640, 640)
34
  lp_result = lp_model(vehicle_crop, imgsz=imgsz, device="cpu")[0]
 
38
  license_plates.append(torch.add(lp_box, vehicle_offset))
39
 
40
  lp_mask = Image.new(mode="L", size=image.size, color=255)
41
+ lp_draw = ImageDraw.Draw(lp_mask)
42
 
43
  for license_plate in license_plates:
44
+ lp_draw.rectangle(license_plate.tolist(), fill = 0)
45
+
46
+ vehicle_mask = Image.new(mode="L", size=image.size, color=255)
47
+ vehicle_draw = ImageDraw.Draw(vehicle_mask)
48
+ for vehicle_box in vehicle_boxes:
49
+ vehicle_draw.rectangle(vehicle_box.tolist(), outline = 0, width=5)
50
+
51
+
52
 
53
+ #TODO: move combination to caller function
54
  combined_mask = Image.fromarray(np.minimum.reduce([np.array(m) for m in [people_mask, lp_mask]]))
55
+ return combined_mask, people_mask, lp_mask, vehicle_mask
56
 
57
 
58
  def test_comb(image):
59
+ mask, people_mask, lp_mask, vm = detect(image)
60
  blurred = image.filter(ImageFilter.GaussianBlur(30))
61
  anonymized = Image.composite(image, blurred, mask)
62
  ## TODO: Tempfile statt einem generischen File
63
  anonymized.save("anon.JPG")
64
+ annotation_list = [(1 - np.asarray(people_mask) / 255, "Person"), (1 - np.asarray(vm) / 255, "Fahrzeug"), (1 - np.asarray(lp_mask) / 255, "Kennzeichen")]
65
+ return "anon.JPG", (image, annotation_list)
66
 
67
 
68
  css = """
 
87
  demo_upload = gr.Interface(
88
  title="Image Anonymizer",
89
  fn=test_comb,
90
+ inputs=gr.Image(type="pil", label="Zu anonymisierendes Bild"),
91
+ outputs=[gr.Image(label="Anonymisiertes Bild"), gr.AnnotatedImage(label="Erkannte Regionen")],
92
  allow_flagging="never",
93
  examples="examples",
94
  description=description,