webolavo commited on
Commit
ecc7dfc
·
verified ·
1 Parent(s): 55b966a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +100 -54
app.py CHANGED
@@ -38,9 +38,17 @@ PERSON_CLASS_SPECS: tuple[tuple[str, str, str, str], ...] = (
38
  ("female person", "person", "female", "person"),
39
  ("teenage girl", "person", "female", "person"),
40
  ("adolescent girl", "person", "female", "person"),
 
 
 
 
41
  ("adult man", "person", "male", "person"),
42
  ("man", "person", "male", "person"),
43
  ("male person", "person", "male", "person"),
 
 
 
 
44
  ("young child", "person", "child", "person"),
45
  ("baby", "person", "child", "person"),
46
  ("toddler", "person", "child", "person"),
@@ -84,6 +92,10 @@ def _build_class_specs() -> tuple[tuple[str, str, str, str], ...]:
84
  (
85
  ("woman's bare abdomen", "part", "female", "abdomen"),
86
  ("man's bare abdomen", "part", "male", "abdomen"),
 
 
 
 
87
  )
88
  )
89
  return tuple(specs)
@@ -146,9 +158,10 @@ OWNER_CONTAINMENT_THRESHOLD = float(
146
  )
147
  MIN_BOX_AREA_RATIO = float(os.getenv("MIN_BOX_AREA_RATIO", "0.0002"))
148
  IMAGE_SIZE = int(os.getenv("IMAGE_SIZE", "768"))
149
- TILE_IMAGE_SIZE = int(os.getenv("TILE_IMAGE_SIZE", "640"))
150
  TILE_OVERLAP = float(os.getenv("TILE_OVERLAP", "0.20"))
151
  MAX_TILES = int(os.getenv("MAX_TILES", "4"))
 
152
  ENABLE_TILE_PASS = os.getenv("ENABLE_TILE_PASS", "true").lower() not in {
153
  "0",
154
  "false",
@@ -196,6 +209,7 @@ if (
196
  or TILE_IMAGE_SIZE <= 0
197
  or not 0.0 <= TILE_OVERLAP < 0.5
198
  or MAX_TILES <= 0
 
199
  or MAX_DETECTIONS <= 0
200
  ):
201
  raise RuntimeError("Invalid image-size, tiling, or detection-count configuration")
@@ -462,31 +476,11 @@ def _class_name(names: Any, class_id: int) -> str:
462
  return ""
463
 
464
 
465
- def _raw_detections(
466
- image: Image.Image,
467
- inference_size: int = IMAGE_SIZE,
468
  ) -> list[dict[str, Any]]:
469
- model: YOLOE = MODEL_DATA["model"]
470
- image_array = np.asarray(image)
471
- with torch.inference_mode():
472
- results = model.predict(
473
- source=image_array,
474
- imgsz=inference_size,
475
- conf=CANDIDATE_THRESHOLD,
476
- iou=MODEL_IOU_THRESHOLD,
477
- max_det=MAX_DETECTIONS,
478
- device="cpu",
479
- verbose=False,
480
- agnostic_nms=False,
481
- augment=False,
482
- rect=True,
483
- retina_masks=False,
484
- predictor=MultiLabelDetectionPredictor,
485
- )
486
-
487
- if not results or results[0].boxes is None:
488
  return []
489
- result = results[0]
490
  boxes = result.boxes
491
  if len(boxes) == 0:
492
  return []
@@ -524,6 +518,46 @@ def _raw_detections(
524
  return detections
525
 
526
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
527
  def _spatial_clusters(detections: list[dict[str, Any]]) -> list[list[dict[str, Any]]]:
528
  clusters: list[list[dict[str, Any]]] = []
529
  for detection in sorted(detections, key=lambda item: item["score"], reverse=True):
@@ -779,33 +813,39 @@ def _prioritize_tile_regions(
779
  return sorted(regions, key=priority)
780
 
781
 
782
- def _tile_detections(
783
- image: Image.Image, region: dict[str, int]
784
- ) -> list[dict[str, Any]]:
785
- tile = image.crop(
786
- (
787
- region["x_min"],
788
- region["y_min"],
789
- region["x_max"],
790
- region["y_max"],
791
- )
792
- )
793
- detections = _raw_detections(tile, TILE_IMAGE_SIZE)
794
- translated: list[dict[str, Any]] = []
795
- for detection in detections:
796
- box = detection["box"]
797
- translated.append(
798
- {
799
- **detection,
800
- "box": {
801
- "x_min": round(box["x_min"] + region["x_min"], 2),
802
- "y_min": round(box["y_min"] + region["y_min"], 2),
803
- "x_max": round(box["x_max"] + region["x_min"], 2),
804
- "y_max": round(box["y_max"] + region["y_min"], 2),
805
- },
806
- }
807
  )
808
- return translated
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
809
 
810
 
811
  def _has_confirmed_female_evidence(raw: list[dict[str, Any]]) -> bool:
@@ -876,9 +916,11 @@ def _run_yoloe(image: Image.Image, include_debug: bool = False) -> dict[str, Any
876
  analyzed_regions: list[dict[str, int]] = []
877
  if tile_pass_used:
878
  regions = _prioritize_tile_regions(_tile_regions(image), raw)
879
- for region in regions:
880
- analyzed_regions.append(region)
881
- tile_raw.extend(_tile_detections(image, region))
 
 
882
  raw = first_pass_raw + tile_raw
883
  if _has_confirmed_female_evidence(raw):
884
  break
@@ -950,9 +992,13 @@ def _run_yoloe(image: Image.Image, include_debug: bool = False) -> dict[str, Any
950
  },
951
  "full_image_size": IMAGE_SIZE,
952
  "tile_image_size": TILE_IMAGE_SIZE,
 
953
  "tile_overlap": TILE_OVERLAP,
954
  "tile_pass_used": tile_pass_used,
955
  "tiles_analyzed": len(analyzed_regions),
 
 
 
956
  "tile_regions": analyzed_regions,
957
  "persons": people,
958
  "body_parts": parts,
@@ -981,7 +1027,7 @@ async def lifespan(_: FastAPI):
981
  app = FastAPI(
982
  title="AI Shield - YOLOE-11-M Test",
983
  description="Fixed-vocabulary female-person and female body-part detector",
984
- version="0.3.0",
985
  lifespan=lifespan,
986
  )
987
 
 
38
  ("female person", "person", "female", "person"),
39
  ("teenage girl", "person", "female", "person"),
40
  ("adolescent girl", "person", "female", "person"),
41
+ ("small woman", "person", "female", "person"),
42
+ ("partially visible woman", "person", "female", "person"),
43
+ ("woman lying down", "person", "female", "person"),
44
+ ("woman taking a bath", "person", "female", "person"),
45
  ("adult man", "person", "male", "person"),
46
  ("man", "person", "male", "person"),
47
  ("male person", "person", "male", "person"),
48
+ ("small man", "person", "male", "person"),
49
+ ("partially visible man", "person", "male", "person"),
50
+ ("man lying down", "person", "male", "person"),
51
+ ("man taking a bath", "person", "male", "person"),
52
  ("young child", "person", "child", "person"),
53
  ("baby", "person", "child", "person"),
54
  ("toddler", "person", "child", "person"),
 
92
  (
93
  ("woman's bare abdomen", "part", "female", "abdomen"),
94
  ("man's bare abdomen", "part", "male", "abdomen"),
95
+ ("woman's upper body", "part", "female", "body"),
96
+ ("man's upper body", "part", "male", "body"),
97
+ ("woman's profile face", "part", "female", "face"),
98
+ ("man's profile face", "part", "male", "face"),
99
  )
100
  )
101
  return tuple(specs)
 
158
  )
159
  MIN_BOX_AREA_RATIO = float(os.getenv("MIN_BOX_AREA_RATIO", "0.0002"))
160
  IMAGE_SIZE = int(os.getenv("IMAGE_SIZE", "768"))
161
+ TILE_IMAGE_SIZE = int(os.getenv("TILE_IMAGE_SIZE", "512"))
162
  TILE_OVERLAP = float(os.getenv("TILE_OVERLAP", "0.20"))
163
  MAX_TILES = int(os.getenv("MAX_TILES", "4"))
164
+ TILE_BATCH_SIZE = int(os.getenv("TILE_BATCH_SIZE", "2"))
165
  ENABLE_TILE_PASS = os.getenv("ENABLE_TILE_PASS", "true").lower() not in {
166
  "0",
167
  "false",
 
209
  or TILE_IMAGE_SIZE <= 0
210
  or not 0.0 <= TILE_OVERLAP < 0.5
211
  or MAX_TILES <= 0
212
+ or TILE_BATCH_SIZE <= 0
213
  or MAX_DETECTIONS <= 0
214
  ):
215
  raise RuntimeError("Invalid image-size, tiling, or detection-count configuration")
 
476
  return ""
477
 
478
 
479
+ def _detections_from_result(
480
+ result: Any, image: Image.Image
 
481
  ) -> list[dict[str, Any]]:
482
+ if result.boxes is None:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
483
  return []
 
484
  boxes = result.boxes
485
  if len(boxes) == 0:
486
  return []
 
518
  return detections
519
 
520
 
521
+ def _raw_detections_batch(
522
+ images: list[Image.Image], inference_size: int
523
+ ) -> list[list[dict[str, Any]]]:
524
+ if not images:
525
+ return []
526
+ model: YOLOE = MODEL_DATA["model"]
527
+ sources = [np.asarray(image) for image in images]
528
+ with torch.inference_mode():
529
+ results = model.predict(
530
+ source=sources if len(sources) > 1 else sources[0],
531
+ imgsz=inference_size,
532
+ batch=len(sources),
533
+ conf=CANDIDATE_THRESHOLD,
534
+ iou=MODEL_IOU_THRESHOLD,
535
+ max_det=MAX_DETECTIONS,
536
+ device="cpu",
537
+ verbose=False,
538
+ agnostic_nms=False,
539
+ augment=False,
540
+ rect=True,
541
+ retina_masks=False,
542
+ predictor=MultiLabelDetectionPredictor,
543
+ )
544
+ if not results:
545
+ return [[] for _ in images]
546
+ parsed = [
547
+ _detections_from_result(result, image)
548
+ for result, image in zip(results, images)
549
+ ]
550
+ if len(parsed) < len(images):
551
+ parsed.extend([] for _ in range(len(images) - len(parsed)))
552
+ return parsed
553
+
554
+
555
+ def _raw_detections(
556
+ image: Image.Image, inference_size: int = IMAGE_SIZE
557
+ ) -> list[dict[str, Any]]:
558
+ return _raw_detections_batch([image], inference_size)[0]
559
+
560
+
561
  def _spatial_clusters(detections: list[dict[str, Any]]) -> list[list[dict[str, Any]]]:
562
  clusters: list[list[dict[str, Any]]] = []
563
  for detection in sorted(detections, key=lambda item: item["score"], reverse=True):
 
813
  return sorted(regions, key=priority)
814
 
815
 
816
+ def _tile_detections_batch(
817
+ image: Image.Image, regions: list[dict[str, int]]
818
+ ) -> list[list[dict[str, Any]]]:
819
+ tiles = [
820
+ image.crop(
821
+ (
822
+ region["x_min"],
823
+ region["y_min"],
824
+ region["x_max"],
825
+ region["y_max"],
826
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
827
  )
828
+ for region in regions
829
+ ]
830
+ tile_results = _raw_detections_batch(tiles, TILE_IMAGE_SIZE)
831
+ translated_results: list[list[dict[str, Any]]] = []
832
+ for detections, region in zip(tile_results, regions):
833
+ translated: list[dict[str, Any]] = []
834
+ for detection in detections:
835
+ box = detection["box"]
836
+ translated.append(
837
+ {
838
+ **detection,
839
+ "box": {
840
+ "x_min": round(box["x_min"] + region["x_min"], 2),
841
+ "y_min": round(box["y_min"] + region["y_min"], 2),
842
+ "x_max": round(box["x_max"] + region["x_min"], 2),
843
+ "y_max": round(box["y_max"] + region["y_min"], 2),
844
+ },
845
+ }
846
+ )
847
+ translated_results.append(translated)
848
+ return translated_results
849
 
850
 
851
  def _has_confirmed_female_evidence(raw: list[dict[str, Any]]) -> bool:
 
916
  analyzed_regions: list[dict[str, int]] = []
917
  if tile_pass_used:
918
  regions = _prioritize_tile_regions(_tile_regions(image), raw)
919
+ for start in range(0, len(regions), TILE_BATCH_SIZE):
920
+ region_batch = regions[start : start + TILE_BATCH_SIZE]
921
+ analyzed_regions.extend(region_batch)
922
+ for detections in _tile_detections_batch(image, region_batch):
923
+ tile_raw.extend(detections)
924
  raw = first_pass_raw + tile_raw
925
  if _has_confirmed_female_evidence(raw):
926
  break
 
992
  },
993
  "full_image_size": IMAGE_SIZE,
994
  "tile_image_size": TILE_IMAGE_SIZE,
995
+ "tile_batch_size": TILE_BATCH_SIZE,
996
  "tile_overlap": TILE_OVERLAP,
997
  "tile_pass_used": tile_pass_used,
998
  "tiles_analyzed": len(analyzed_regions),
999
+ "tile_batches_analyzed": math.ceil(
1000
+ len(analyzed_regions) / TILE_BATCH_SIZE
1001
+ ),
1002
  "tile_regions": analyzed_regions,
1003
  "persons": people,
1004
  "body_parts": parts,
 
1027
  app = FastAPI(
1028
  title="AI Shield - YOLOE-11-M Test",
1029
  description="Fixed-vocabulary female-person and female body-part detector",
1030
+ version="0.4.0",
1031
  lifespan=lifespan,
1032
  )
1033