webolavo commited on
Commit
fa4d2f4
·
verified ·
1 Parent(s): d09a28f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +106 -16
app.py CHANGED
@@ -1,4 +1,4 @@
1
- import asyncio
2
  import io
3
  import os
4
  import socket
@@ -60,14 +60,17 @@ def _make_prompt() -> tuple[str, dict[str, tuple[int, int]]]:
60
 
61
  DETECTION_PROMPT, PHRASE_CHAR_SPANS = _make_prompt()
62
 
63
- # 0.20 keeps weak cropped body parts. Person/child margins prevent a close
64
- # male/female tie from becoming a confident owner classification.
65
  CANDIDATE_THRESHOLD = float(os.getenv("CANDIDATE_THRESHOLD", "0.15"))
66
  PERSON_THRESHOLD = float(os.getenv("PERSON_THRESHOLD", "0.25"))
67
  PART_THRESHOLD = float(os.getenv("PART_THRESHOLD", "0.20"))
68
  CHILD_THRESHOLD = float(os.getenv("CHILD_THRESHOLD", "0.25"))
69
  GENDER_MARGIN = float(os.getenv("GENDER_MARGIN", "0.05"))
70
  CHILD_MARGIN = float(os.getenv("CHILD_MARGIN", "0.05"))
 
 
 
71
  NMS_IOU_THRESHOLD = float(os.getenv("NMS_IOU_THRESHOLD", "0.75"))
72
  MATCH_IOU_THRESHOLD = float(os.getenv("MATCH_IOU_THRESHOLD", "0.25"))
73
  MATCH_CONTAINMENT_THRESHOLD = float(os.getenv("MATCH_CONTAINMENT_THRESHOLD", "0.80"))
@@ -84,6 +87,7 @@ for threshold_name, threshold_value in {
84
  "CHILD_THRESHOLD": CHILD_THRESHOLD,
85
  "GENDER_MARGIN": GENDER_MARGIN,
86
  "CHILD_MARGIN": CHILD_MARGIN,
 
87
  "NMS_IOU_THRESHOLD": NMS_IOU_THRESHOLD,
88
  "MATCH_IOU_THRESHOLD": MATCH_IOU_THRESHOLD,
89
  "MATCH_CONTAINMENT_THRESHOLD": MATCH_CONTAINMENT_THRESHOLD,
@@ -304,6 +308,69 @@ def _boxes_match(first: dict[str, float], second: dict[str, float]) -> bool:
304
  )
305
 
306
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
307
  def _nms(detections: list[dict[str, Any]]) -> list[dict[str, Any]]:
308
  kept: list[dict[str, Any]] = []
309
  for detection in sorted(
@@ -396,6 +463,7 @@ def _run_grounding_dino(image: Image.Image) -> dict[str, Any]:
396
  "label": winner if confirmed else "ambiguous person",
397
  "kind": "person",
398
  "gender": winner if confirmed else "ambiguous",
 
399
  "score": round(winner_score, 4),
400
  "female_score": round(scores["female"], 4),
401
  "male_score": round(scores["male"], 4),
@@ -448,18 +516,39 @@ def _run_grounding_dino(image: Image.Image) -> dict[str, Any]:
448
  body_parts.extend(_nms(part_candidates))
449
 
450
  for part in body_parts:
451
- if any(_boxes_match(part["box"], owner["box"]) for owner in confirmed_females):
452
- part["attribution"] = "female"
453
- part["attribution_source"] = "female_person_box"
454
- elif any(_boxes_match(part["box"], owner["box"]) for owner in confirmed_males):
455
- part["attribution"] = "male"
456
- part["attribution_source"] = "male_person_box"
457
- elif any(_boxes_match(part["box"], owner["box"]) for owner in confirmed_children):
458
- part["attribution"] = "child"
459
- part["attribution_source"] = "young_child_box"
460
- else:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
461
  part["attribution"] = part["raw_gender"]
462
- part["attribution_source"] = "contrastive_part_score"
 
 
 
463
 
464
  body_parts.sort(key=lambda item: item["score"], reverse=True)
465
  female_parts = [item for item in body_parts if item["attribution"] == "female"]
@@ -514,6 +603,7 @@ def _run_grounding_dino(image: Image.Image) -> dict[str, Any]:
514
  "child_threshold": CHILD_THRESHOLD,
515
  "gender_margin": GENDER_MARGIN,
516
  "child_margin": CHILD_MARGIN,
 
517
  "nms_iou_threshold": NMS_IOU_THRESHOLD,
518
  "match_iou_threshold": MATCH_IOU_THRESHOLD,
519
  "match_containment_threshold": MATCH_CONTAINMENT_THRESHOLD,
@@ -539,7 +629,7 @@ async def lifespan(_: FastAPI):
539
  app = FastAPI(
540
  title="AI Shield - Grounding DINO Base",
541
  description="Single-pass contrastive female-person and female body-part detector",
542
- version="1.3.0",
543
  lifespan=lifespan,
544
  )
545
 
@@ -594,4 +684,4 @@ async def analyze(request: Request) -> dict[str, Any]:
594
  @app.post("/analyze-file")
595
  async def analyze_file(file: UploadFile = File(...)) -> dict[str, Any]:
596
  image = await _read_uploaded_image(file)
597
- return await _analyze_image(image)
 
1
+
2
  import io
3
  import os
4
  import socket
 
60
 
61
  DETECTION_PROMPT, PHRASE_CHAR_SPANS = _make_prompt()
62
 
63
+ # Low candidate/part thresholds retain weak cropped detections. The margins
64
+ # prevent close text-score ties from becoming confident classifications.
65
  CANDIDATE_THRESHOLD = float(os.getenv("CANDIDATE_THRESHOLD", "0.15"))
66
  PERSON_THRESHOLD = float(os.getenv("PERSON_THRESHOLD", "0.25"))
67
  PART_THRESHOLD = float(os.getenv("PART_THRESHOLD", "0.20"))
68
  CHILD_THRESHOLD = float(os.getenv("CHILD_THRESHOLD", "0.25"))
69
  GENDER_MARGIN = float(os.getenv("GENDER_MARGIN", "0.05"))
70
  CHILD_MARGIN = float(os.getenv("CHILD_MARGIN", "0.05"))
71
+ # A body part must also show a meaningful female/male separation before it can
72
+ # decide the image on its own. This does not add another model forward pass.
73
+ PART_GENDER_MARGIN = float(os.getenv("PART_GENDER_MARGIN", "0.05"))
74
  NMS_IOU_THRESHOLD = float(os.getenv("NMS_IOU_THRESHOLD", "0.75"))
75
  MATCH_IOU_THRESHOLD = float(os.getenv("MATCH_IOU_THRESHOLD", "0.25"))
76
  MATCH_CONTAINMENT_THRESHOLD = float(os.getenv("MATCH_CONTAINMENT_THRESHOLD", "0.80"))
 
87
  "CHILD_THRESHOLD": CHILD_THRESHOLD,
88
  "GENDER_MARGIN": GENDER_MARGIN,
89
  "CHILD_MARGIN": CHILD_MARGIN,
90
+ "PART_GENDER_MARGIN": PART_GENDER_MARGIN,
91
  "NMS_IOU_THRESHOLD": NMS_IOU_THRESHOLD,
92
  "MATCH_IOU_THRESHOLD": MATCH_IOU_THRESHOLD,
93
  "MATCH_CONTAINMENT_THRESHOLD": MATCH_CONTAINMENT_THRESHOLD,
 
308
  )
309
 
310
 
311
+ def _box_area(box: dict[str, float]) -> float:
312
+ return max(0.0, box["x_max"] - box["x_min"]) * max(
313
+ 0.0, box["y_max"] - box["y_min"]
314
+ )
315
+
316
+
317
+ def _part_containment(
318
+ part_box: dict[str, float], person_box: dict[str, float]
319
+ ) -> float:
320
+ intersection_width = max(
321
+ 0.0,
322
+ min(part_box["x_max"], person_box["x_max"])
323
+ - max(part_box["x_min"], person_box["x_min"]),
324
+ )
325
+ intersection_height = max(
326
+ 0.0,
327
+ min(part_box["y_max"], person_box["y_max"])
328
+ - max(part_box["y_min"], person_box["y_min"]),
329
+ )
330
+ part_area = _box_area(part_box)
331
+ return (
332
+ intersection_width * intersection_height / part_area
333
+ if part_area > 0.0
334
+ else 0.0
335
+ )
336
+
337
+
338
+ def _best_matching_person(
339
+ part_box: dict[str, float], persons: list[dict[str, Any]]
340
+ ) -> dict[str, Any] | None:
341
+ matches = [person for person in persons if _boxes_match(part_box, person["box"])]
342
+ if not matches:
343
+ return None
344
+
345
+ # Keep matches that contain approximately the same amount of the part, then
346
+ # discard overly broad group boxes when a more specific person box exists.
347
+ # Within that local cluster, a confirmed person is more reliable than an
348
+ # ambiguous duplicate box around the same individual.
349
+ max_containment = max(
350
+ _part_containment(part_box, person["box"]) for person in matches
351
+ )
352
+ close_matches = [
353
+ person
354
+ for person in matches
355
+ if _part_containment(part_box, person["box"]) >= max_containment - 0.05
356
+ ]
357
+ smallest_area = min(_box_area(person["box"]) for person in close_matches)
358
+ specific_matches = [
359
+ person
360
+ for person in close_matches
361
+ if _box_area(person["box"]) <= smallest_area * 4.0
362
+ ]
363
+ return max(
364
+ specific_matches,
365
+ key=lambda person: (
366
+ person["confirmed"],
367
+ _part_containment(part_box, person["box"]),
368
+ -_box_area(person["box"]),
369
+ person["score"],
370
+ ),
371
+ )
372
+
373
+
374
  def _nms(detections: list[dict[str, Any]]) -> list[dict[str, Any]]:
375
  kept: list[dict[str, Any]] = []
376
  for detection in sorted(
 
463
  "label": winner if confirmed else "ambiguous person",
464
  "kind": "person",
465
  "gender": winner if confirmed else "ambiguous",
466
+ "winner": winner,
467
  "score": round(winner_score, 4),
468
  "female_score": round(scores["female"], 4),
469
  "male_score": round(scores["male"], 4),
 
516
  body_parts.extend(_nms(part_candidates))
517
 
518
  for part in body_parts:
519
+ owner = _best_matching_person(part["box"], persons)
520
+ part_margin_is_clear = part["score_margin"] >= PART_GENDER_MARGIN
521
+
522
+ if owner is not None:
523
+ owner_hint = owner["winner"]
524
+ part["owner_gender"] = owner["gender"]
525
+ part["owner_winner"] = owner_hint
526
+ part["owner_confirmed"] = owner["confirmed"]
527
+ part["owner_score"] = owner["score"]
528
+
529
+ if owner["confirmed"]:
530
+ part["attribution"] = owner["gender"]
531
+ part["attribution_source"] = f"confirmed_{owner['gender']}_person_box"
532
+ elif owner_hint in {"male", "child"}:
533
+ # An ambiguous whole-person result still provides stronger
534
+ # context than a standalone gender guess for a hand or face.
535
+ part["attribution"] = owner_hint
536
+ part["attribution_source"] = f"ambiguous_{owner_hint}_person_hint"
537
+ elif part["raw_gender"] == "female" and part_margin_is_clear:
538
+ # The whole person and the body part both lean female.
539
+ part["attribution"] = "female"
540
+ part["attribution_source"] = "ambiguous_female_person_and_part_margin"
541
+ else:
542
+ part["attribution"] = "ambiguous"
543
+ part["attribution_source"] = "ambiguous_person_or_part_margin"
544
+ elif part_margin_is_clear:
545
+ # Preserve cropped-body detection when no person box exists, but do
546
+ # not let a near-tie such as 0.250 vs 0.246 decide the whole image.
547
  part["attribution"] = part["raw_gender"]
548
+ part["attribution_source"] = "contrastive_part_score_with_margin"
549
+ else:
550
+ part["attribution"] = "ambiguous"
551
+ part["attribution_source"] = "insufficient_part_gender_margin"
552
 
553
  body_parts.sort(key=lambda item: item["score"], reverse=True)
554
  female_parts = [item for item in body_parts if item["attribution"] == "female"]
 
603
  "child_threshold": CHILD_THRESHOLD,
604
  "gender_margin": GENDER_MARGIN,
605
  "child_margin": CHILD_MARGIN,
606
+ "part_gender_margin": PART_GENDER_MARGIN,
607
  "nms_iou_threshold": NMS_IOU_THRESHOLD,
608
  "match_iou_threshold": MATCH_IOU_THRESHOLD,
609
  "match_containment_threshold": MATCH_CONTAINMENT_THRESHOLD,
 
629
  app = FastAPI(
630
  title="AI Shield - Grounding DINO Base",
631
  description="Single-pass contrastive female-person and female body-part detector",
632
+ version="1.4.0",
633
  lifespan=lifespan,
634
  )
635
 
 
684
  @app.post("/analyze-file")
685
  async def analyze_file(file: UploadFile = File(...)) -> dict[str, Any]:
686
  image = await _read_uploaded_image(file)
687
+ return await _analyze_image(image)