Update app.py
Browse files
app.py
CHANGED
|
@@ -45,6 +45,41 @@ PART_TYPES = (
|
|
| 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"]:
|
|
@@ -52,12 +87,14 @@ for phrase in PERSON_PHRASES["female"]:
|
|
| 52 |
for phrase in PERSON_PHRASES["male"]:
|
| 53 |
PHRASE_SPECS.append({"key": f"person:male:{phrase}", "phrase": phrase})
|
| 54 |
for part_type in PART_TYPES:
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
| 61 |
for phrase in PERSON_PHRASES["child"]:
|
| 62 |
PHRASE_SPECS.append({"key": f"person:child:{phrase}", "phrase": phrase})
|
| 63 |
|
|
@@ -93,6 +130,12 @@ PART_GENDER_MARGIN = float(os.getenv("PART_GENDER_MARGIN", "0.05"))
|
|
| 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(
|
|
@@ -117,6 +160,8 @@ for threshold_name, threshold_value in {
|
|
| 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,
|
|
@@ -518,12 +563,20 @@ def _run_grounding_dino(image: Image.Image) -> dict[str, Any]:
|
|
| 518 |
|
| 519 |
body_parts: list[dict[str, Any]] = []
|
| 520 |
for part_type in PART_TYPES:
|
| 521 |
-
|
| 522 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 523 |
part_candidates: list[dict[str, Any]] = []
|
| 524 |
for query_index in range(normalized_boxes.shape[0]):
|
| 525 |
-
female_score =
|
| 526 |
-
|
|
|
|
|
|
|
| 527 |
score = max(female_score, male_score)
|
| 528 |
if score < PART_THRESHOLD:
|
| 529 |
continue
|
|
@@ -562,10 +615,21 @@ def _run_grounding_dino(image: Image.Image) -> dict[str, Any]:
|
|
| 562 |
and part["female_score"] >= STRONG_FEMALE_HAND_THRESHOLD
|
| 563 |
and part["score_margin"] >= STRONG_FEMALE_HAND_MARGIN
|
| 564 |
)
|
| 565 |
-
|
| 566 |
-
part["
|
| 567 |
-
and
|
|
|
|
|
|
|
| 568 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 569 |
|
| 570 |
if owner is not None:
|
| 571 |
owner_hint = owner["winner"]
|
|
@@ -592,11 +656,7 @@ def _run_grounding_dino(image: Image.Image) -> dict[str, Any]:
|
|
| 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"
|
|
@@ -608,9 +668,7 @@ def _run_grounding_dino(image: Image.Image) -> dict[str, Any]:
|
|
| 608 |
# not let an ordinary low-score near-tie decide the whole image.
|
| 609 |
part["attribution"] = "female"
|
| 610 |
part["attribution_source"] = (
|
| 611 |
-
"
|
| 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"]
|
|
@@ -674,6 +732,8 @@ def _run_grounding_dino(image: Image.Image) -> dict[str, Any]:
|
|
| 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,
|
|
@@ -701,7 +761,7 @@ async def lifespan(_: FastAPI):
|
|
| 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.
|
| 705 |
lifespan=lifespan,
|
| 706 |
)
|
| 707 |
|
|
|
|
| 45 |
CENTRAL_PART_TYPES = frozenset(
|
| 46 |
{"chest", "torso", "abdomen", "stomach", "waist", "belly", "body"}
|
| 47 |
)
|
| 48 |
+
PART_PHRASES: dict[str, dict[str, tuple[str, ...]]] = {
|
| 49 |
+
part_type: {
|
| 50 |
+
"female": (f"female {part_type}",),
|
| 51 |
+
"male": (f"male {part_type}",),
|
| 52 |
+
}
|
| 53 |
+
for part_type in PART_TYPES
|
| 54 |
+
}
|
| 55 |
+
# Natural caption-style alternatives improve grounding of printed or tightly
|
| 56 |
+
# cropped midriffs. Scores for aliases are combined inside the same forward pass.
|
| 57 |
+
PART_PHRASES["abdomen"] = {
|
| 58 |
+
"female": (
|
| 59 |
+
"female abdomen",
|
| 60 |
+
"woman's abdomen",
|
| 61 |
+
"woman's bare abdomen",
|
| 62 |
+
"female midriff",
|
| 63 |
+
),
|
| 64 |
+
"male": (
|
| 65 |
+
"male abdomen",
|
| 66 |
+
"man's abdomen",
|
| 67 |
+
"man's bare abdomen",
|
| 68 |
+
"male midriff",
|
| 69 |
+
),
|
| 70 |
+
}
|
| 71 |
+
PART_PHRASES["stomach"] = {
|
| 72 |
+
"female": ("female stomach", "woman's stomach"),
|
| 73 |
+
"male": ("male stomach", "man's stomach"),
|
| 74 |
+
}
|
| 75 |
+
PART_PHRASES["waist"] = {
|
| 76 |
+
"female": ("female waist", "woman's waist"),
|
| 77 |
+
"male": ("male waist", "man's waist"),
|
| 78 |
+
}
|
| 79 |
+
PART_PHRASES["belly"] = {
|
| 80 |
+
"female": ("female belly", "woman's belly"),
|
| 81 |
+
"male": ("male belly", "man's belly"),
|
| 82 |
+
}
|
| 83 |
|
| 84 |
PHRASE_SPECS: list[dict[str, str]] = []
|
| 85 |
for phrase in PERSON_PHRASES["female"]:
|
|
|
|
| 87 |
for phrase in PERSON_PHRASES["male"]:
|
| 88 |
PHRASE_SPECS.append({"key": f"person:male:{phrase}", "phrase": phrase})
|
| 89 |
for part_type in PART_TYPES:
|
| 90 |
+
for gender in ("female", "male"):
|
| 91 |
+
for phrase_index, phrase in enumerate(PART_PHRASES[part_type][gender]):
|
| 92 |
+
PHRASE_SPECS.append(
|
| 93 |
+
{
|
| 94 |
+
"key": f"part:{part_type}:{gender}:{phrase_index}",
|
| 95 |
+
"phrase": phrase,
|
| 96 |
+
}
|
| 97 |
+
)
|
| 98 |
for phrase in PERSON_PHRASES["child"]:
|
| 99 |
PHRASE_SPECS.append({"key": f"person:child:{phrase}", "phrase": phrase})
|
| 100 |
|
|
|
|
| 130 |
CENTRAL_PART_GENDER_MARGIN = float(
|
| 131 |
os.getenv("CENTRAL_PART_GENDER_MARGIN", "0.04")
|
| 132 |
)
|
| 133 |
+
STRONG_FEMALE_CENTRAL_THRESHOLD = float(
|
| 134 |
+
os.getenv("STRONG_FEMALE_CENTRAL_THRESHOLD", "0.24")
|
| 135 |
+
)
|
| 136 |
+
STRONG_FEMALE_CENTRAL_MARGIN = float(
|
| 137 |
+
os.getenv("STRONG_FEMALE_CENTRAL_MARGIN", "0.01")
|
| 138 |
+
)
|
| 139 |
# "female hand" and "male hand" share a strong noun token, so their scores are
|
| 140 |
# often close. Retain a high female-hand score with a small positive separation.
|
| 141 |
STRONG_FEMALE_HAND_THRESHOLD = float(
|
|
|
|
| 160 |
"CHILD_MARGIN": CHILD_MARGIN,
|
| 161 |
"PART_GENDER_MARGIN": PART_GENDER_MARGIN,
|
| 162 |
"CENTRAL_PART_GENDER_MARGIN": CENTRAL_PART_GENDER_MARGIN,
|
| 163 |
+
"STRONG_FEMALE_CENTRAL_THRESHOLD": STRONG_FEMALE_CENTRAL_THRESHOLD,
|
| 164 |
+
"STRONG_FEMALE_CENTRAL_MARGIN": STRONG_FEMALE_CENTRAL_MARGIN,
|
| 165 |
"STRONG_FEMALE_HAND_THRESHOLD": STRONG_FEMALE_HAND_THRESHOLD,
|
| 166 |
"STRONG_FEMALE_HAND_MARGIN": STRONG_FEMALE_HAND_MARGIN,
|
| 167 |
"NMS_IOU_THRESHOLD": NMS_IOU_THRESHOLD,
|
|
|
|
| 563 |
|
| 564 |
body_parts: list[dict[str, Any]] = []
|
| 565 |
for part_type in PART_TYPES:
|
| 566 |
+
female_keys = [
|
| 567 |
+
f"part:{part_type}:female:{phrase_index}"
|
| 568 |
+
for phrase_index in range(len(PART_PHRASES[part_type]["female"]))
|
| 569 |
+
]
|
| 570 |
+
male_keys = [
|
| 571 |
+
f"part:{part_type}:male:{phrase_index}"
|
| 572 |
+
for phrase_index in range(len(PART_PHRASES[part_type]["male"]))
|
| 573 |
+
]
|
| 574 |
part_candidates: list[dict[str, Any]] = []
|
| 575 |
for query_index in range(normalized_boxes.shape[0]):
|
| 576 |
+
female_score = _max_phrase_score(
|
| 577 |
+
phrase_scores, female_keys, query_index
|
| 578 |
+
)
|
| 579 |
+
male_score = _max_phrase_score(phrase_scores, male_keys, query_index)
|
| 580 |
score = max(female_score, male_score)
|
| 581 |
if score < PART_THRESHOLD:
|
| 582 |
continue
|
|
|
|
| 615 |
and part["female_score"] >= STRONG_FEMALE_HAND_THRESHOLD
|
| 616 |
and part["score_margin"] >= STRONG_FEMALE_HAND_MARGIN
|
| 617 |
)
|
| 618 |
+
strong_female_central = (
|
| 619 |
+
part["part"] in CENTRAL_PART_TYPES
|
| 620 |
+
and part["raw_gender"] == "female"
|
| 621 |
+
and part["female_score"] >= STRONG_FEMALE_CENTRAL_THRESHOLD
|
| 622 |
+
and part["score_margin"] >= STRONG_FEMALE_CENTRAL_MARGIN
|
| 623 |
)
|
| 624 |
+
female_evidence_kind: str | None = None
|
| 625 |
+
if part["raw_gender"] == "female":
|
| 626 |
+
if part_margin_is_clear:
|
| 627 |
+
female_evidence_kind = "gender_margin"
|
| 628 |
+
elif strong_female_hand:
|
| 629 |
+
female_evidence_kind = "hand_high_score"
|
| 630 |
+
elif strong_female_central:
|
| 631 |
+
female_evidence_kind = "central_high_score"
|
| 632 |
+
female_part_is_clear = female_evidence_kind is not None
|
| 633 |
|
| 634 |
if owner is not None:
|
| 635 |
owner_hint = owner["winner"]
|
|
|
|
| 656 |
# A weak, unconfirmed person box must not override a strong
|
| 657 |
# cropped female-part detection.
|
| 658 |
part["attribution"] = "female"
|
| 659 |
+
part["attribution_source"] = f"female_part_{female_evidence_kind}"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 660 |
elif part_margin_is_clear:
|
| 661 |
part["attribution"] = part["raw_gender"]
|
| 662 |
part["attribution_source"] = "weak_person_part_gender_margin"
|
|
|
|
| 668 |
# not let an ordinary low-score near-tie decide the whole image.
|
| 669 |
part["attribution"] = "female"
|
| 670 |
part["attribution_source"] = (
|
| 671 |
+
f"unowned_female_part_{female_evidence_kind}"
|
|
|
|
|
|
|
| 672 |
)
|
| 673 |
elif part_margin_is_clear:
|
| 674 |
part["attribution"] = part["raw_gender"]
|
|
|
|
| 732 |
"child_margin": CHILD_MARGIN,
|
| 733 |
"part_gender_margin": PART_GENDER_MARGIN,
|
| 734 |
"central_part_gender_margin": CENTRAL_PART_GENDER_MARGIN,
|
| 735 |
+
"strong_female_central_threshold": STRONG_FEMALE_CENTRAL_THRESHOLD,
|
| 736 |
+
"strong_female_central_margin": STRONG_FEMALE_CENTRAL_MARGIN,
|
| 737 |
"strong_female_hand_threshold": STRONG_FEMALE_HAND_THRESHOLD,
|
| 738 |
"strong_female_hand_margin": STRONG_FEMALE_HAND_MARGIN,
|
| 739 |
"nms_iou_threshold": NMS_IOU_THRESHOLD,
|
|
|
|
| 761 |
app = FastAPI(
|
| 762 |
title="AI Shield - Grounding DINO Base",
|
| 763 |
description="Single-pass contrastive female-person and female body-part detector",
|
| 764 |
+
version="1.6.0",
|
| 765 |
lifespan=lifespan,
|
| 766 |
)
|
| 767 |
|