orik-ss commited on
Commit
d04bd0c
·
1 Parent(s): 16868d2

Running PPE on person image

Browse files
Files changed (1) hide show
  1. ppe_compliance.py +59 -8
ppe_compliance.py CHANGED
@@ -3,18 +3,22 @@
3
  Runs TWO detectors on the full image and reports, per person, which REQUIRED PPE
4
  items are MISSING (the "negation" logic):
5
 
6
- 1. PPE detector — the fine-tuned D-FINE-M (6 classes: goggles/helmet/mask/
7
- shoes/vest/glove), reused from ``ppe_pipeline``.
8
- 2. Person detector — the SAME HF-transformers D-FINE model the "Detect &
9
  Classify" tab uses (``ustc-community/dfine-*``), filtered to the COCO/
10
  Objects365 "person" class. We reuse that tab's already-loaded model when it
11
  matches, so the Space doesn't load a third detector.
 
 
 
 
 
12
 
13
  For each person, PPE boxes that fall inside (with body-region bands, so a helmet
14
  being *held* low isn't counted as *worn*) are associated to that person. A person
15
  missing any required item is a VIOLATION. Rendering: green box = OK, red box =
16
- violation, with a verdict chip ("#0 no helmet, glove"). Ported from
17
- ``ppe/scripts/compliance.py``.
 
18
  """
19
 
20
  import torch
@@ -44,6 +48,14 @@ REGION_BANDS = {
44
  # Person detector defaults to the same model the "Detect & Classify" tab uses.
45
  DEFAULT_PERSON_MODEL = "medium-obj2coco"
46
 
 
 
 
 
 
 
 
 
47
  _PERSON = None # (model_key, processor, model, person_ids)
48
 
49
 
@@ -91,6 +103,25 @@ def _contain_frac(inner, outer):
91
  return inter / area
92
 
93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94
  def _color_of(name):
95
  return PALETTE[ALL_PPE.index(name) % len(PALETTE)] if name in ALL_PPE else (150, 150, 150)
96
 
@@ -116,6 +147,8 @@ def run_ppe_compliance(
116
 
117
  im = image.convert("RGB") if isinstance(image, Image.Image) else Image.fromarray(image).convert("RGB")
118
 
 
 
119
  # 1) Person detector (reused HF D-FINE) — keep only 'person' above threshold.
120
  # Use the model's own device (it may be reused from the Detect & Classify tab,
121
  # which could be on GPU); run_dfine moves inputs to that device.
@@ -123,9 +156,27 @@ def run_ppe_compliance(
123
  pdev = str(next(mdl.parameters()).device)
124
  dets = run_dfine(im, proc, mdl, pdev, person_threshold)
125
  persons = [(d["conf"], d["box"]) for d in dets if d["cls"] in person_ids]
126
-
127
- # 2) PPE detector (fine-tuned D-FINE-M).
128
- ppe = detect_ppe_boxes(im, threshold=ppe_threshold)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
129
 
130
  # 3) Assign each PPE detection to the person it is most contained in.
131
  people = [{"score": s, "box": box, "present": {}} for s, box in persons]
 
3
  Runs TWO detectors on the full image and reports, per person, which REQUIRED PPE
4
  items are MISSING (the "negation" logic):
5
 
6
+ 1. Person detector — the SAME HF-transformers D-FINE model the "Detect &
 
 
7
  Classify" tab uses (``ustc-community/dfine-*``), filtered to the COCO/
8
  Objects365 "person" class. We reuse that tab's already-loaded model when it
9
  matches, so the Space doesn't load a third detector.
10
+ 2. PPE detector — the fine-tuned D-FINE-M (6 classes: goggles/helmet/mask/
11
+ shoes/vest/glove), reused from ``ppe_pipeline``. Rather than one full-frame
12
+ pass (which shrinks every worker in the 640x640 resize and misses small PPE
13
+ like vests), we run it on EACH person crop and map the boxes back. On this
14
+ image that lifted vest detections from 0 -> 4.
15
 
16
  For each person, PPE boxes that fall inside (with body-region bands, so a helmet
17
  being *held* low isn't counted as *worn*) are associated to that person. A person
18
  missing any required item is a VIOLATION. Rendering: green box = OK, red box =
19
+ violation, with a verdict chip ("#0 no helmet, glove"). The association /
20
+ negation / rendering logic is ported verbatim from ``ppe/scripts/compliance.py``;
21
+ only the source of PPE boxes changed (per-person crops instead of full frame).
22
  """
23
 
24
  import torch
 
48
  # Person detector defaults to the same model the "Detect & Classify" tab uses.
49
  DEFAULT_PERSON_MODEL = "medium-obj2coco"
50
 
51
+ # Per-person PPE detection: pad each person box before cropping so PPE near the
52
+ # edges isn't clipped; cap the number of crops so crowded frames stay responsive
53
+ # (each crop is one PPE forward pass, CPU on the Space).
54
+ PERSON_CROP_PAD = 0.15
55
+ MAX_PERSON_CROPS = 20
56
+ # Collapse the same physical item seen in two overlapping (padded) crops.
57
+ DEDUP_IOU = 0.55
58
+
59
  _PERSON = None # (model_key, processor, model, person_ids)
60
 
61
 
 
103
  return inter / area
104
 
105
 
106
+ def _box_iou(a, b):
107
+ ix1, iy1 = max(a[0], b[0]), max(a[1], b[1])
108
+ ix2, iy2 = min(a[2], b[2]), min(a[3], b[3])
109
+ inter = max(0.0, ix2 - ix1) * max(0.0, iy2 - iy1)
110
+ union = (a[2] - a[0]) * (a[3] - a[1]) + (b[2] - b[0]) * (b[3] - b[1]) - inter
111
+ return inter / union if union > 0 else 0.0
112
+
113
+
114
+ def _dedup_ppe(dets, iou=DEDUP_IOU):
115
+ """Drop near-duplicate detections of the SAME class (same item caught in two
116
+ overlapping person crops). Keep the highest-scoring box."""
117
+ kept = []
118
+ for name, s, box in sorted(dets, key=lambda d: -d[1]):
119
+ if any(n == name and _box_iou(box, kb) >= iou for n, _, kb in kept):
120
+ continue
121
+ kept.append((name, s, box))
122
+ return kept
123
+
124
+
125
  def _color_of(name):
126
  return PALETTE[ALL_PPE.index(name) % len(PALETTE)] if name in ALL_PPE else (150, 150, 150)
127
 
 
147
 
148
  im = image.convert("RGB") if isinstance(image, Image.Image) else Image.fromarray(image).convert("RGB")
149
 
150
+ W, H = im.size
151
+
152
  # 1) Person detector (reused HF D-FINE) — keep only 'person' above threshold.
153
  # Use the model's own device (it may be reused from the Detect & Classify tab,
154
  # which could be on GPU); run_dfine moves inputs to that device.
 
156
  pdev = str(next(mdl.parameters()).device)
157
  dets = run_dfine(im, proc, mdl, pdev, person_threshold)
158
  persons = [(d["conf"], d["box"]) for d in dets if d["cls"] in person_ids]
159
+ # Keep the most confident persons if the frame is crowded (cap PPE passes).
160
+ persons.sort(key=lambda ps: -ps[0])
161
+ persons = persons[:MAX_PERSON_CROPS]
162
+
163
+ # 2) PPE detector (fine-tuned D-FINE-M), run on EACH person crop so small PPE
164
+ # (vests, distant helmets) isn't lost to the 640x640 full-frame resize. Boxes
165
+ # are mapped back to full-image coords, then de-duplicated across crops.
166
+ ppe = []
167
+ for _conf, pb in persons:
168
+ x1, y1, x2, y2 = pb
169
+ pw, ph = x2 - x1, y2 - y1
170
+ cx1 = max(0, int(x1 - PERSON_CROP_PAD * pw))
171
+ cy1 = max(0, int(y1 - PERSON_CROP_PAD * ph))
172
+ cx2 = min(W, int(x2 + PERSON_CROP_PAD * pw))
173
+ cy2 = min(H, int(y2 + PERSON_CROP_PAD * ph))
174
+ if cx2 <= cx1 or cy2 <= cy1:
175
+ continue
176
+ crop = im.crop((cx1, cy1, cx2, cy2))
177
+ for name, s, b in detect_ppe_boxes(crop, threshold=ppe_threshold):
178
+ ppe.append((name, s, [b[0] + cx1, b[1] + cy1, b[2] + cx1, b[3] + cy1]))
179
+ ppe = _dedup_ppe(ppe)
180
 
181
  # 3) Assign each PPE detection to the person it is most contained in.
182
  people = [{"score": s, "box": box, "present": {}} for s, box in persons]