Update app.py
Browse files
app.py
CHANGED
|
@@ -27,7 +27,24 @@ PERSON_PHRASES = {
|
|
| 27 |
"male": ("man", "male person"),
|
| 28 |
"child": ("young child", "baby", "toddler", "little kid"),
|
| 29 |
}
|
| 30 |
-
PART_TYPES = (
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
PHRASE_SPECS: list[dict[str, str]] = []
|
| 33 |
for phrase in PERSON_PHRASES["female"]:
|
|
@@ -71,6 +88,17 @@ 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"))
|
|
@@ -88,6 +116,9 @@ for threshold_name, threshold_value in {
|
|
| 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,
|
|
@@ -517,35 +548,73 @@ def _run_grounding_dino(image: Image.Image) -> dict[str, Any]:
|
|
| 517 |
|
| 518 |
for part in body_parts:
|
| 519 |
owner = _best_matching_person(part["box"], persons)
|
| 520 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
|
|
|
| 535 |
part["attribution"] = owner_hint
|
| 536 |
part["attribution_source"] = f"ambiguous_{owner_hint}_person_hint"
|
| 537 |
-
elif
|
| 538 |
-
#
|
|
|
|
| 539 |
part["attribution"] = "female"
|
| 540 |
-
part["attribution_source"] =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 541 |
else:
|
| 542 |
part["attribution"] = "ambiguous"
|
| 543 |
-
part["attribution_source"] = "
|
| 544 |
-
elif
|
| 545 |
# Preserve cropped-body detection when no person box exists, but do
|
| 546 |
-
# not let
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 547 |
part["attribution"] = part["raw_gender"]
|
| 548 |
-
part["attribution_source"] = "
|
| 549 |
else:
|
| 550 |
part["attribution"] = "ambiguous"
|
| 551 |
part["attribution_source"] = "insufficient_part_gender_margin"
|
|
@@ -604,6 +673,9 @@ def _run_grounding_dino(image: Image.Image) -> dict[str, Any]:
|
|
| 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,7 +701,7 @@ async def lifespan(_: FastAPI):
|
|
| 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.
|
| 633 |
lifespan=lifespan,
|
| 634 |
)
|
| 635 |
|
|
|
|
| 27 |
"male": ("man", "male person"),
|
| 28 |
"child": ("young child", "baby", "toddler", "little kid"),
|
| 29 |
}
|
| 30 |
+
PART_TYPES = (
|
| 31 |
+
"face",
|
| 32 |
+
"hand",
|
| 33 |
+
"chest",
|
| 34 |
+
"torso",
|
| 35 |
+
"abdomen",
|
| 36 |
+
"stomach",
|
| 37 |
+
"waist",
|
| 38 |
+
"belly",
|
| 39 |
+
"arm",
|
| 40 |
+
"body",
|
| 41 |
+
"leg",
|
| 42 |
+
"thigh",
|
| 43 |
+
"foot",
|
| 44 |
+
)
|
| 45 |
+
CENTRAL_PART_TYPES = frozenset(
|
| 46 |
+
{"chest", "torso", "abdomen", "stomach", "waist", "belly", "body"}
|
| 47 |
+
)
|
| 48 |
|
| 49 |
PHRASE_SPECS: list[dict[str, str]] = []
|
| 50 |
for phrase in PERSON_PHRASES["female"]:
|
|
|
|
| 88 |
# A body part must also show a meaningful female/male separation before it can
|
| 89 |
# decide the image on its own. This does not add another model forward pass.
|
| 90 |
PART_GENDER_MARGIN = float(os.getenv("PART_GENDER_MARGIN", "0.05"))
|
| 91 |
+
# Central-body crops receive a slightly lower margin because they often lack a
|
| 92 |
+
# face or a complete person box.
|
| 93 |
+
CENTRAL_PART_GENDER_MARGIN = float(
|
| 94 |
+
os.getenv("CENTRAL_PART_GENDER_MARGIN", "0.04")
|
| 95 |
+
)
|
| 96 |
+
# "female hand" and "male hand" share a strong noun token, so their scores are
|
| 97 |
+
# often close. Retain a high female-hand score with a small positive separation.
|
| 98 |
+
STRONG_FEMALE_HAND_THRESHOLD = float(
|
| 99 |
+
os.getenv("STRONG_FEMALE_HAND_THRESHOLD", "0.40")
|
| 100 |
+
)
|
| 101 |
+
STRONG_FEMALE_HAND_MARGIN = float(os.getenv("STRONG_FEMALE_HAND_MARGIN", "0.01"))
|
| 102 |
NMS_IOU_THRESHOLD = float(os.getenv("NMS_IOU_THRESHOLD", "0.75"))
|
| 103 |
MATCH_IOU_THRESHOLD = float(os.getenv("MATCH_IOU_THRESHOLD", "0.25"))
|
| 104 |
MATCH_CONTAINMENT_THRESHOLD = float(os.getenv("MATCH_CONTAINMENT_THRESHOLD", "0.80"))
|
|
|
|
| 116 |
"GENDER_MARGIN": GENDER_MARGIN,
|
| 117 |
"CHILD_MARGIN": CHILD_MARGIN,
|
| 118 |
"PART_GENDER_MARGIN": PART_GENDER_MARGIN,
|
| 119 |
+
"CENTRAL_PART_GENDER_MARGIN": CENTRAL_PART_GENDER_MARGIN,
|
| 120 |
+
"STRONG_FEMALE_HAND_THRESHOLD": STRONG_FEMALE_HAND_THRESHOLD,
|
| 121 |
+
"STRONG_FEMALE_HAND_MARGIN": STRONG_FEMALE_HAND_MARGIN,
|
| 122 |
"NMS_IOU_THRESHOLD": NMS_IOU_THRESHOLD,
|
| 123 |
"MATCH_IOU_THRESHOLD": MATCH_IOU_THRESHOLD,
|
| 124 |
"MATCH_CONTAINMENT_THRESHOLD": MATCH_CONTAINMENT_THRESHOLD,
|
|
|
|
| 548 |
|
| 549 |
for part in body_parts:
|
| 550 |
owner = _best_matching_person(part["box"], persons)
|
| 551 |
+
standard_part_margin_is_clear = part["score_margin"] >= PART_GENDER_MARGIN
|
| 552 |
+
central_part_margin_is_clear = (
|
| 553 |
+
part["part"] in CENTRAL_PART_TYPES
|
| 554 |
+
and part["score_margin"] >= CENTRAL_PART_GENDER_MARGIN
|
| 555 |
+
)
|
| 556 |
+
part_margin_is_clear = (
|
| 557 |
+
standard_part_margin_is_clear or central_part_margin_is_clear
|
| 558 |
+
)
|
| 559 |
+
strong_female_hand = (
|
| 560 |
+
part["part"] == "hand"
|
| 561 |
+
and part["raw_gender"] == "female"
|
| 562 |
+
and part["female_score"] >= STRONG_FEMALE_HAND_THRESHOLD
|
| 563 |
+
and part["score_margin"] >= STRONG_FEMALE_HAND_MARGIN
|
| 564 |
+
)
|
| 565 |
+
female_part_is_clear = (
|
| 566 |
+
part["raw_gender"] == "female"
|
| 567 |
+
and (part_margin_is_clear or strong_female_hand)
|
| 568 |
+
)
|
| 569 |
|
| 570 |
if owner is not None:
|
| 571 |
owner_hint = owner["winner"]
|
| 572 |
+
owner_hint_threshold = (
|
| 573 |
+
CHILD_THRESHOLD if owner_hint == "child" else PERSON_THRESHOLD
|
| 574 |
+
)
|
| 575 |
+
owner_hint_is_strong = owner["score"] >= owner_hint_threshold
|
| 576 |
part["owner_gender"] = owner["gender"]
|
| 577 |
part["owner_winner"] = owner_hint
|
| 578 |
part["owner_confirmed"] = owner["confirmed"]
|
| 579 |
part["owner_score"] = owner["score"]
|
| 580 |
+
part["owner_hint_strong"] = owner_hint_is_strong
|
| 581 |
|
| 582 |
if owner["confirmed"]:
|
| 583 |
part["attribution"] = owner["gender"]
|
| 584 |
part["attribution_source"] = f"confirmed_{owner['gender']}_person_box"
|
| 585 |
+
elif owner_hint in {"male", "child"} and owner_hint_is_strong:
|
| 586 |
# An ambiguous whole-person result still provides stronger
|
| 587 |
+
# context when its winning score reached the normal threshold
|
| 588 |
+
# but failed only the required score margin.
|
| 589 |
part["attribution"] = owner_hint
|
| 590 |
part["attribution_source"] = f"ambiguous_{owner_hint}_person_hint"
|
| 591 |
+
elif female_part_is_clear:
|
| 592 |
+
# A weak, unconfirmed person box must not override a strong
|
| 593 |
+
# cropped female-part detection.
|
| 594 |
part["attribution"] = "female"
|
| 595 |
+
part["attribution_source"] = (
|
| 596 |
+
"female_hand_high_score"
|
| 597 |
+
if strong_female_hand and not part_margin_is_clear
|
| 598 |
+
else "female_part_gender_margin"
|
| 599 |
+
)
|
| 600 |
+
elif part_margin_is_clear:
|
| 601 |
+
part["attribution"] = part["raw_gender"]
|
| 602 |
+
part["attribution_source"] = "weak_person_part_gender_margin"
|
| 603 |
else:
|
| 604 |
part["attribution"] = "ambiguous"
|
| 605 |
+
part["attribution_source"] = "weak_person_and_part_margin"
|
| 606 |
+
elif female_part_is_clear:
|
| 607 |
# Preserve cropped-body detection when no person box exists, but do
|
| 608 |
+
# not let an ordinary low-score near-tie decide the whole image.
|
| 609 |
+
part["attribution"] = "female"
|
| 610 |
+
part["attribution_source"] = (
|
| 611 |
+
"unowned_female_hand_high_score"
|
| 612 |
+
if strong_female_hand and not part_margin_is_clear
|
| 613 |
+
else "unowned_female_part_gender_margin"
|
| 614 |
+
)
|
| 615 |
+
elif part_margin_is_clear:
|
| 616 |
part["attribution"] = part["raw_gender"]
|
| 617 |
+
part["attribution_source"] = "unowned_part_gender_margin"
|
| 618 |
else:
|
| 619 |
part["attribution"] = "ambiguous"
|
| 620 |
part["attribution_source"] = "insufficient_part_gender_margin"
|
|
|
|
| 673 |
"gender_margin": GENDER_MARGIN,
|
| 674 |
"child_margin": CHILD_MARGIN,
|
| 675 |
"part_gender_margin": PART_GENDER_MARGIN,
|
| 676 |
+
"central_part_gender_margin": CENTRAL_PART_GENDER_MARGIN,
|
| 677 |
+
"strong_female_hand_threshold": STRONG_FEMALE_HAND_THRESHOLD,
|
| 678 |
+
"strong_female_hand_margin": STRONG_FEMALE_HAND_MARGIN,
|
| 679 |
"nms_iou_threshold": NMS_IOU_THRESHOLD,
|
| 680 |
"match_iou_threshold": MATCH_IOU_THRESHOLD,
|
| 681 |
"match_containment_threshold": MATCH_CONTAINMENT_THRESHOLD,
|
|
|
|
| 701 |
app = FastAPI(
|
| 702 |
title="AI Shield - Grounding DINO Base",
|
| 703 |
description="Single-pass contrastive female-person and female body-part detector",
|
| 704 |
+
version="1.5.0",
|
| 705 |
lifespan=lifespan,
|
| 706 |
)
|
| 707 |
|