orik-ss commited on
Commit
f560a14
Β·
1 Parent(s): d04bd0c

Added better visual

Browse files
Files changed (2) hide show
  1. app.py +7 -13
  2. ppe_compliance.py +69 -78
app.py CHANGED
@@ -157,11 +157,11 @@ with gr.Blocks(title="Small Object Detection") as app:
157
  with gr.Tab("PPE Compliance"):
158
 
159
  gr.Markdown(
160
- "A **D-FINE person detector** (same model as the Detect & Classify tab) and the "
161
- "**fine-tuned D-FINE-M PPE detector** run on the image. For each person, the PPE items "
162
- "worn are associated by body region, and any of the 6 required items "
163
- "(goggles, helmet, mask, shoes, vest, glove) that are missing are flagged. "
164
- "**Green** box = compliant, **red** box = violation (chip lists what's missing)."
165
  )
166
 
167
  with gr.Row():
@@ -190,11 +190,6 @@ with gr.Blocks(title="Small Object Detection") as app:
190
  label="PPE detection threshold",
191
  )
192
 
193
- region_checkbox = gr.Checkbox(
194
- value=True,
195
- label="Body-region check (only count PPE worn in the expected region)",
196
- )
197
-
198
  btn_ppe = gr.Button(
199
  "Run PPE Compliance",
200
  variant="primary",
@@ -214,13 +209,12 @@ with gr.Blocks(title="Small Object Detection") as app:
214
  )
215
 
216
  btn_ppe.click(
217
- fn=lambda image, p_thr, ppe_thr, region: run_ppe_compliance(
218
  image,
219
  person_threshold=float(p_thr),
220
  ppe_threshold=float(ppe_thr),
221
- use_region=bool(region),
222
  ),
223
- inputs=[inp_ppe, person_threshold_slider, ppe_threshold_slider, region_checkbox],
224
  outputs=[out_ppe_image, out_ppe_status],
225
  concurrency_limit=1,
226
  )
 
157
  with gr.Tab("PPE Compliance"):
158
 
159
  gr.Markdown(
160
+ "A **D-FINE person detector** (same model as the Detect & Classify tab) finds each "
161
+ "person, then the **fine-tuned D-FINE-M PPE detector** runs on each person crop. "
162
+ "Any of the 6 required items (goggles, helmet, mask, shoes, vest, glove) that are "
163
+ "missing for a person are flagged. **Green** box = compliant, **red** box = violation; "
164
+ "a per-person checklist (1 = present, 0 = missing) is drawn beside each box."
165
  )
166
 
167
  with gr.Row():
 
190
  label="PPE detection threshold",
191
  )
192
 
 
 
 
 
 
193
  btn_ppe = gr.Button(
194
  "Run PPE Compliance",
195
  variant="primary",
 
209
  )
210
 
211
  btn_ppe.click(
212
+ fn=lambda image, p_thr, ppe_thr: run_ppe_compliance(
213
  image,
214
  person_threshold=float(p_thr),
215
  ppe_threshold=float(ppe_thr),
 
216
  ),
217
+ inputs=[inp_ppe, person_threshold_slider, ppe_threshold_slider],
218
  outputs=[out_ppe_image, out_ppe_status],
219
  concurrency_limit=1,
220
  )
ppe_compliance.py CHANGED
@@ -13,12 +13,13 @@ items are MISSING (the "negation" logic):
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
@@ -33,18 +34,6 @@ DEVICE = "cpu"
33
 
34
  ALL_PPE = ["goggles", "helmet", "mask", "shoes", "vest", "glove"]
35
 
36
- # Vertical band (fraction of person-box height from the top) where each PPE item
37
- # is expected. A detection only counts for a person if its center falls in the
38
- # band β€” this rejects e.g. a helmet a worker is *holding* (low) vs *wearing* (top).
39
- REGION_BANDS = {
40
- "helmet": (0.00, 0.35),
41
- "goggles": (0.00, 0.40),
42
- "mask": (0.05, 0.45),
43
- "vest": (0.20, 0.75),
44
- "glove": (0.30, 0.95),
45
- "shoes": (0.70, 1.00),
46
- }
47
-
48
  # Person detector defaults to the same model the "Detect & Classify" tab uses.
49
  DEFAULT_PERSON_MODEL = "medium-obj2coco"
50
 
@@ -132,7 +121,6 @@ def run_ppe_compliance(
132
  person_threshold=0.5,
133
  ppe_threshold=0.4,
134
  assoc=0.5,
135
- use_region=True,
136
  person_model=DEFAULT_PERSON_MODEL,
137
  min_side=960,
138
  ):
@@ -160,11 +148,17 @@ def run_ppe_compliance(
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))
@@ -175,29 +169,15 @@ def run_ppe_compliance(
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]
183
- for name, s, box in ppe:
184
- cy = 0.5 * (box[1] + box[3]) # PPE box vertical center
185
- best_i, best_f = -1, assoc
186
- for i, p in enumerate(people):
187
- f = _contain_frac(box, p["box"])
188
- if f < best_f:
189
  continue
190
- if use_region and name in REGION_BANDS:
191
- py1, py2 = p["box"][1], p["box"][3]
192
- frac = (cy - py1) / max(1e-6, py2 - py1) # where in the person it sits
193
- lo, hi = REGION_BANDS[name]
194
- if not (lo <= frac <= hi):
195
- continue # right person, wrong body region -> not "worn"
196
- best_i, best_f = i, f
197
- if best_i >= 0:
198
- cur = people[best_i]["present"].get(name)
199
  if cur is None or s > cur[0]:
200
- people[best_i]["present"][name] = (s, box)
 
201
 
202
  # 4) Verdicts + report.
203
  verdicts = []
@@ -221,49 +201,60 @@ def run_ppe_compliance(
221
  p["box"] = [v * scale for v in p["box"]]
222
  ppe = [(n, s, [v * scale for v in b]) for (n, s, b) in ppe]
223
 
224
- # 5) Render: translucent overlay + collision-avoided rounded label chips.
 
 
 
225
  W, H = im.size
226
  base = im.convert("RGBA")
227
  ov = Image.new("RGBA", (W, H), (0, 0, 0, 0))
228
  od = ImageDraw.Draw(ov)
229
- pf = _load_font(max(14, int(0.030 * max(W, H)))) # person verdict labels
230
- sf = _load_font(max(11, int(0.018 * max(W, H)))) # small PPE labels
231
- placed = [] # occupied label rects, to avoid overlaps
232
-
233
- def fits(r):
234
- return all(r[2] <= q[0] or r[0] >= q[2] or r[3] <= q[1] or r[1] >= q[3] for q in placed)
235
-
236
- def chip(x, y, text, font, bg, above=True):
237
- tb = od.textbbox((0, 0), text, font=font)
238
- tw, th = tb[2] - tb[0], tb[3] - tb[1]
239
- pad = 4
240
- rx = int(min(max(0, x), max(0, W - tw - 2 * pad)))
241
- ry = max(0, int(y - th - 2 * pad) if above else int(y))
242
- for _ in range(80):
243
- r = (rx, ry, rx + tw + 2 * pad, ry + th + 2 * pad)
244
- if fits(r) and r[3] <= H:
245
- break
246
- ry += th + 2 * pad + 2
247
- r = (rx, ry, rx + tw + 2 * pad, ry + th + 2 * pad)
248
- od.rounded_rectangle(r, radius=5, fill=bg + (230,))
249
- od.text((rx + pad, ry + pad - 1), text, font=font, fill=(255, 255, 255, 255))
250
- placed.append(r)
251
 
252
- # 1) thin PPE boxes (no inline text)
253
  for name, s, box in ppe:
254
  od.rectangle(box, outline=_color_of(name) + (255,), width=2)
255
 
256
- # 2) thick person boxes + verdict chips (placed first = priority)
257
  pw = max(3, int(0.006 * max(W, H)))
 
 
258
  for i, p, present, missing, ok in verdicts:
259
- c = (40, 200, 90) if ok else (235, 45, 55)
260
- x1, y1, x2, y2 = p["box"]
261
- od.rectangle([x1, y1, x2, y2], outline=c + (255,), width=pw)
262
- chip(x1, y1, (f"#{i} OK" if ok else f"#{i} no {', '.join(missing)}"), pf, c)
263
-
264
- # 3) small PPE tags, nudged so they never overlap the verdict chips
265
- for name, s, box in ppe:
266
- chip(box[0], box[1], f"{name} {s:.2f}", sf, _color_of(name))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
267
 
268
  out_im = Image.alpha_composite(base, ov).convert("RGB")
269
  return out_im, "\n".join(lines)
 
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
+ Each PPE detection is credited to the person whose crop produced it (gated by
17
+ containment in that person's box). A person missing any required item is a
18
+ VIOLATION. Rendering: green box = OK, red box = violation, with a compact
19
+ per-person checklist (1=present / 0=missing) drawn outside the box. Adapted from
20
+ ``ppe/scripts/compliance.py``; the original's body-region bands are dropped β€”
21
+ per-person cropping already localises PPE to the right worker, so the band only
22
+ hurt recall on bent/crouched poses.
23
  """
24
 
25
  import torch
 
34
 
35
  ALL_PPE = ["goggles", "helmet", "mask", "shoes", "vest", "glove"]
36
 
 
 
 
 
 
 
 
 
 
 
 
 
37
  # Person detector defaults to the same model the "Detect & Classify" tab uses.
38
  DEFAULT_PERSON_MODEL = "medium-obj2coco"
39
 
 
121
  person_threshold=0.5,
122
  ppe_threshold=0.4,
123
  assoc=0.5,
 
124
  person_model=DEFAULT_PERSON_MODEL,
125
  min_side=960,
126
  ):
 
148
  persons.sort(key=lambda ps: -ps[0])
149
  persons = persons[:MAX_PERSON_CROPS]
150
 
151
+ # 2+3) PPE detector (fine-tuned D-FINE-M), run on EACH person crop so small PPE
152
+ # (vests, distant helmets) isn't lost to the 640x640 full-frame resize, then
153
+ # attribute the detections to THAT person. We credit a detection to the person
154
+ # whose crop produced it (gated by containment in their own box),
155
+ # rather than re-assigning globally to whichever overlapping neighbour contains
156
+ # it most β€” that global step mislabelled a worker's own vest to a denser
157
+ # neighbour in crowded frames, so the vest was drawn but the wearer showed
158
+ # "no vest". Boxes are mapped back to full coords; the draw list is de-duped.
159
+ people = [{"score": conf, "box": pb, "present": {}} for conf, pb in persons]
160
+ ppe = [] # (name, score, full_box) β€” for drawing only
161
+ for i, (conf, pb) in enumerate(persons):
162
  x1, y1, x2, y2 = pb
163
  pw, ph = x2 - x1, y2 - y1
164
  cx1 = max(0, int(x1 - PERSON_CROP_PAD * pw))
 
169
  continue
170
  crop = im.crop((cx1, cy1, cx2, cy2))
171
  for name, s, b in detect_ppe_boxes(crop, threshold=ppe_threshold):
172
+ box = [b[0] + cx1, b[1] + cy1, b[2] + cx1, b[3] + cy1]
173
+ ppe.append((name, s, box))
174
+ # Attribute to this crop's person: must lie mostly inside their box.
175
+ if _contain_frac(box, pb) < assoc:
 
 
 
 
 
 
 
176
  continue
177
+ cur = people[i]["present"].get(name)
 
 
 
 
 
 
 
 
178
  if cur is None or s > cur[0]:
179
+ people[i]["present"][name] = (s, box)
180
+ ppe = _dedup_ppe(ppe)
181
 
182
  # 4) Verdicts + report.
183
  verdicts = []
 
201
  p["box"] = [v * scale for v in p["box"]]
202
  ppe = [(n, s, [v * scale for v in b]) for (n, s, b) in ppe]
203
 
204
+ # 5) Render: thin PPE boxes + a compact per-person CHECKLIST panel placed
205
+ # OUTSIDE the person box. One short row per required item ("vest 1" present /
206
+ # "vest 0" missing, green/red), stacked in a column β€” instead of one long
207
+ # "no goggles, mask, shoes, glove" sentence that collides in crowded frames.
208
  W, H = im.size
209
  base = im.convert("RGBA")
210
  ov = Image.new("RGBA", (W, H), (0, 0, 0, 0))
211
  od = ImageDraw.Draw(ov)
212
+ rf = _load_font(max(12, int(0.016 * max(W, H)))) # checklist rows
213
+ GREEN, RED = (40, 200, 90), (235, 45, 55)
214
+ placed = [] # occupied panel rects, to avoid overlaps
215
+
216
+ def overlaps(r):
217
+ return any(not (r[2] <= q[0] or r[0] >= q[2] or r[3] <= q[1] or r[1] >= q[3]) for q in placed)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
218
 
219
+ # thin PPE boxes (no inline text β€” presence is shown in the checklist)
220
  for name, s, box in ppe:
221
  od.rectangle(box, outline=_color_of(name) + (255,), width=2)
222
 
 
223
  pw = max(3, int(0.006 * max(W, H)))
224
+ pad = 5
225
+ line_h = rf.getbbox("Hg1")[3] + 4
226
  for i, p, present, missing, ok in verdicts:
227
+ col = GREEN if ok else RED
228
+ x1, y1, x2, y2 = [int(v) for v in p["box"]]
229
+ od.rectangle([x1, y1, x2, y2], outline=col + (255,), width=pw)
230
+
231
+ rows = [(f"#{i}", None)] + [(it, it in present) for it in ALL_PPE]
232
+ txts = [lab if pres is None else f"{lab} {1 if pres else 0}" for lab, pres in rows]
233
+ p_w = max(od.textbbox((0, 0), t, font=rf)[2] for t in txts) + 2 * pad
234
+ p_h = line_h * len(rows) + 2 * pad
235
+
236
+ # Prefer just right of the box; fall back to the left; keep inside frame.
237
+ rx = x2 + 6
238
+ if rx + p_w > W:
239
+ rx = x1 - p_w - 6
240
+ if rx < 0:
241
+ rx = min(x1 + 6, max(0, W - p_w))
242
+ ry = max(0, min(y1, H - p_h))
243
+ for _ in range(300): # nudge down, then to a fresh column, until it fits
244
+ if not overlaps((rx, ry, rx + p_w, ry + p_h)):
245
+ break
246
+ ry += line_h
247
+ if ry + p_h > H:
248
+ ry = max(0, min(y1, H - p_h))
249
+ rx = rx + p_w + 6
250
+ if rx + p_w > W:
251
+ rx = max(0, W - p_w)
252
+ placed.append((rx, ry, rx + p_w, ry + p_h))
253
+
254
+ od.rounded_rectangle((rx, ry, rx + p_w, ry + p_h), radius=5, fill=(0, 0, 0, 175))
255
+ for k, (lab, pres) in enumerate(rows):
256
+ tc = col if pres is None else (GREEN if pres else RED)
257
+ od.text((rx + pad, ry + pad + k * line_h), txts[k], font=rf, fill=tc + (255,))
258
 
259
  out_im = Image.alpha_composite(base, ov).convert("RGB")
260
  return out_im, "\n".join(lines)