andevs commited on
Commit
7cfb24c
·
verified ·
1 Parent(s): 95d5e33

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +255 -631
app.py CHANGED
@@ -1,20 +1,24 @@
1
  #!/usr/bin/env python3
2
- # app.py - Background Remover v32.6.1
3
  #
4
- # Changes in this version:
5
- # • TIER_0_FLAT routes to flood_fill (classical) not u2netp (neural).
6
- # Restores the 1-2 second processing time for flat images like flags,
7
- # logos, and products on white. flood_fill is ~200ms vs u2netp's ~10s.
8
- # • Router rewritten with complexity score restored (from v32.6.0).
9
- # • Router uses _skin_score (2500 pixels at 50×50) instead of the
10
- # ContentClassifier's unreliable 25-pixel sample (from v32.6.0).
11
- # • Tier tracking in every image response (from v32.6.0).
12
- # • /api/debug/route endpoint for 50ms router inspection (from v32.6.0).
13
- # • All 4 models preloaded and pre-warmed (from v32.5.0).
14
- # • RSS-based memory rejection (from v32.5.0).
15
- # • In-memory bulk zip (from v32.5.0).
16
- # • Alpha cache TTL 60s + background purge (from v32.5.0).
17
- # • SpooledTemporaryFile patched to keep uploads in RAM (from v32.5.0).
 
 
 
 
18
 
19
  # ============================================================
20
  # FORCE IN-MEMORY UPLOAD PARSING (before fastapi import)
@@ -57,7 +61,7 @@ from concurrent.futures import ThreadPoolExecutor
57
  import psutil
58
 
59
  # ============================================================
60
- # ONNX RUNTIME
61
  # ============================================================
62
  try:
63
  import onnxruntime as _ort
@@ -67,7 +71,7 @@ try:
67
  if 'sess_options' not in kwargs and 'so' not in kwargs:
68
  try:
69
  so = _ort.SessionOptions()
70
- so.intra_op_num_threads = 2
71
  so.inter_op_num_threads = 1
72
  so.enable_cpu_mem_arena = False
73
  so.enable_mem_pattern = False
@@ -78,7 +82,7 @@ try:
78
  return _orig_session_init(self, *args, **kwargs)
79
 
80
  _ort.InferenceSession.__init__ = _patched_session_init
81
- print("ONNX Runtime patched: arena=OFF, mem_pattern=OFF, threads=2/1")
82
  except Exception as e:
83
  print(f"Could not patch ONNX Runtime: {e}")
84
 
@@ -105,12 +109,9 @@ logger = logging.getLogger(__name__)
105
  # MEMORY UTILITIES
106
  # ============================================================
107
  def trim_memory():
108
- try:
109
- import torch
110
- if hasattr(torch, 'cuda') and torch.cuda.is_available():
111
- torch.cuda.empty_cache()
112
- except Exception:
113
- pass
114
  try:
115
  libc = ctypes.CDLL("libc.so.6")
116
  libc.malloc_trim(0)
@@ -181,6 +182,17 @@ def get_container_memory() -> Dict:
181
  }
182
 
183
 
 
 
 
 
 
 
 
 
 
 
 
184
  # ============================================================
185
  # CORE CLASSES
186
  # ============================================================
@@ -225,11 +237,7 @@ class ModelManager:
225
  self.use_counts[name] = 1
226
  gc.collect()
227
  trim_memory()
228
- mem = get_container_memory()
229
- logger.info(
230
- f"Loaded {name} in {self.load_times[name]:.1f}s. "
231
- f"RSS {mem['process_rss_mb']}MB ({mem['rss_percent']:.1f}%)"
232
- )
233
  return session
234
  except Exception as e:
235
  logger.warning(f"Loader failed for {name}: {e}")
@@ -492,7 +500,7 @@ class ContentClassifier:
492
  # ENVIRONMENT
493
  # ============================================================
494
  os.environ["U2NET_HOME"] = "/tmp/u2net_models"
495
- os.environ["OMP_NUM_THREADS"] = "2"
496
  os.environ["CUDA_VISIBLE_DEVICES"] = ""
497
  os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
498
  os.environ["PYTHONUNBUFFERED"] = "1"
@@ -614,35 +622,39 @@ BIREFNET_MAX_DIM = 512
614
 
615
 
616
  # ============================================================
617
- # MODEL CONFIG
618
  # ============================================================
619
  PRELOAD_MODELS = [
620
- ("silueta", "~400 MB — video only"),
621
- ("u2netp", "~300 MB — easy/flat"),
622
- ("isnet-general-use", "~800 MB — human subjects"),
623
- ("birefnet-general-lite", "~2 GB — complex non-human"),
624
  ]
625
 
626
- WARMUP_RESOLUTIONS = {
627
- 'silueta': (640, 640),
628
- 'u2netp': (512, 512),
629
- 'isnet-general-use': (640, 640),
630
- 'birefnet-general-lite': (512, 512),
631
  }
632
 
633
- LAZY_MODELS = {
634
- "u2net": "~176 MB disk / ~800 MB RAM",
635
- "u2net_human_seg": "~176 MB disk / ~800 MB RAM",
636
- "isnet-anime": "~176 MB disk / ~800 MB RAM",
637
- "birefnet-general": "~900 MB disk / ~2.5 GB RAM",
638
- "u2net_cloth_seg": "~176 MB disk / ~800 MB RAM",
639
- "sam": "~375 MB disk / ~1 GB RAM",
 
 
640
  }
641
 
642
- VALID_REMBG_MODELS = {
643
- 'u2net', 'u2netp', 'u2net_human_seg', 'u2net_cloth_seg',
644
- 'silueta', 'isnet-general-use', 'isnet-anime',
645
- 'birefnet-general', 'birefnet-general-lite', 'sam',
 
 
 
 
646
  }
647
 
648
  MODEL_ALIASES = {
@@ -680,10 +692,11 @@ NEURAL_CONFIGS = {
680
  'sam': {'fg': 240, 'bg': 10, 'erode': 10},
681
  }
682
 
 
683
  MODEL_RES_CAPS = {
684
  'u2netp': 512, 'silueta': 640, 'u2net': 640,
685
- 'u2net_human_seg': 640, 'birefnet-general-lite': 640,
686
- 'birefnet-general': 640, 'isnet-general-use': 640,
687
  'isnet-anime': 640, 'u2net_cloth_seg': 512, 'sam': 512,
688
  }
689
 
@@ -714,7 +727,7 @@ def _load_session(name: str):
714
  from rembg import new_session
715
  resolved = MODEL_ALIASES.get(name, name)
716
  if resolved not in VALID_REMBG_MODELS:
717
- logger.warning(f"'{resolved}' not valid. Using u2netp.")
718
  resolved = 'u2netp'
719
  logger.info(f"Loading {resolved}...")
720
  return new_session(resolved)
@@ -836,9 +849,6 @@ def process_with_neural(image, session, model_name, force_no_matting=False):
836
 
837
 
838
  def process_graphic_classical(image):
839
- """Classical OpenCV pipeline for flat images. ~200ms.
840
- Returns None on failure (mask too small or too large) so the
841
- caller falls back to a neural model."""
842
  try:
843
  if image.mode != 'RGBA':
844
  image = image.convert('RGBA')
@@ -929,11 +939,7 @@ def preload_all_models():
929
  logger.warning(f" {name} failed")
930
  except Exception as e:
931
  logger.warning(f" {name}: {e}")
932
- mem = get_container_memory()
933
- logger.info(
934
- f"Preload done. {len(model_manager.models)} models. "
935
- f"RSS {mem['process_rss_mb']}MB ({mem['rss_percent']:.1f}%)"
936
- )
937
 
938
 
939
  def initialize_models():
@@ -969,7 +975,7 @@ def _prewarm_models():
969
  logger.warning(f" {name}: not loaded, skip warmup")
970
  continue
971
 
972
- size = WARMUP_RESOLUTIONS.get(name, (512, 512))
973
 
974
  try:
975
  img = Image.new('RGB', size, (245, 245, 245))
@@ -1004,17 +1010,16 @@ def _prewarm_models():
1004
  gc.collect()
1005
  trim_memory()
1006
 
1007
- mem = get_container_memory()
1008
  warmed = sum(1 for w in model_manager.warmed.values() if w)
1009
- logger.info(
1010
- f"Warmup done: {warmed}/{len(PRELOAD_MODELS)} warm. "
1011
- f"RSS {mem['process_rss_mb']}MB ({mem['rss_percent']:.1f}%)"
1012
- )
1013
 
1014
 
1015
  logger.info("=" * 60)
1016
- logger.info("Background Remover v32.6.1")
1017
- logger.info("4 models preloaded, pre-warmed, TIER_0_FLAT → flood_fill")
 
 
1018
  logger.info("=" * 60)
1019
 
1020
  preload_all_models()
@@ -1022,16 +1027,10 @@ initialize_models()
1022
  _prewarm_models()
1023
  _start_cache_purge_thread()
1024
 
1025
- mem = get_container_memory()
1026
- logger.info(f"AI: {AI_AVAILABLE} | Default: {default_model_name}")
1027
- logger.info(
1028
- f"RSS: {mem['process_rss_mb']}MB / {mem['total_mb']}MB "
1029
- f"({mem['rss_percent']:.1f}%)"
1030
- )
1031
  logger.info(f"Video: FORCE={VIDEO_FORCE_MODEL}, MAX_BATCH={VIDEO_MAX_BATCH}")
1032
  logger.info(f"Video RAM reject: {VIDEO_RAM_REJECT_PERCENT}% (process RSS)")
1033
  logger.info(f"Alpha cache: TTL {ALPHA_CACHE_TTL_SECONDS}s, purge every {ALPHA_PURGE_INTERVAL}s")
1034
- logger.info(f"Router: flat→flood_fill · human→isnet · small→u2netp · complex→birefnet")
1035
  logger.info("=" * 60)
1036
 
1037
 
@@ -1057,8 +1056,19 @@ def get_session_for_model(model_name):
1057
 
1058
 
1059
  # ============================================================
1060
- # ROUTER
1061
  # ============================================================
 
 
 
 
 
 
 
 
 
 
 
1062
  def detect_hair_complexity(image):
1063
  try:
1064
  gray = image.convert('L').resize((100, 100), Image.Resampling.LANCZOS)
@@ -1079,167 +1089,119 @@ def detect_hair_complexity(image):
1079
 
1080
 
1081
  def _skin_score(rgb):
1082
- if rgb is None or rgb.size == 0:
1083
- return 0.0
1084
- r = rgb[:, :, 0].astype(np.int32)
1085
- g = rgb[:, :, 1].astype(np.int32)
1086
- b = rgb[:, :, 2].astype(np.int32)
1087
- warm = (r > 50) & (g > 25) & (b > 15) & (r > g) & (r > b)
1088
- not_too_red = (r - g) < 100
1089
- not_grey = (np.abs(r - g) + np.abs(g - b)) > 15
1090
- not_white = r < 250
1091
- not_black = r > 40
1092
- mask = warm & not_too_red & not_grey & not_white & not_black
1093
- return float(mask.sum()) / float(r.shape[0] * r.shape[1])
1094
 
1095
 
1096
  def analyze_image_complexity(image):
1097
- """Priority router with complexity score and tier tracking.
1098
-
1099
- Tier order:
1100
- TIER_0_FLAT → flood_fill (200ms, classical OpenCV)
1101
- TIER_1_HUMAN → isnet (2-6s)
1102
- TIER_2_SMALL → u2netp (1-2s)
1103
- TIER_3_COMPLEX → birefnet (15-25s)
1104
- TIER_4_FALLBACK → u2netp (1-2s)
1105
- """
1106
  try:
 
 
 
 
 
 
 
 
 
 
 
 
 
1107
  cc = content_classifier.classify(image)
1108
- sig = cc.get('signals', {}) or {}
1109
  cc_cat = cc.get('category', 'general_photo')
1110
- cc_color = float(sig.get('color_complexity', 0.5))
1111
- cc_border = float(sig.get('border_uniformity', 0))
1112
- cc_edge = float(sig.get('edge_density', 0))
1113
- cc_texture = float(sig.get('texture', 0))
1114
-
1115
- # Direct skin score at 50×50 (2500 pixels). More reliable than
1116
- # the ContentClassifier's 25-pixel sample for partial occlusion.
1117
- small_50 = image.copy()
1118
- small_50.thumbnail((50, 50), Image.Resampling.LANCZOS)
1119
- rgb_50 = np.array(small_50.convert('RGB'))
1120
- skin_score = _skin_score(rgb_50)
1121
-
1122
- # Hair complexity
1123
- hc = detect_hair_complexity(image)
1124
-
1125
- # Complexity score
1126
- small_10 = image.copy()
1127
- small_10.thumbnail((10, 10), Image.Resampling.LANCZOS)
1128
- gray_10 = np.array(small_10.convert('L'))
1129
- rgb_10 = np.array(small_10.convert('RGB'))
1130
- var = float(np.var(gray_10))
1131
- gx = np.gradient(gray_10.astype(np.float32), axis=1)
1132
- gy = np.gradient(gray_10.astype(np.float32), axis=0)
1133
- em = np.sqrt(gx ** 2 + gy ** 2)
1134
- edge_10 = float(np.sum(em > 10) / max(1, em.size))
1135
- colors = len(np.unique(rgb_10.reshape(-1, 3), axis=0))
1136
-
1137
- is_human_signal = (
1138
- skin_score > 0.05
1139
- or cc_cat in ('human', 'human_hair')
1140
- or hc['route'] in ('wispy_hair', 'complex_hair')
1141
- )
1142
 
1143
  complexity = (
1144
  min(100, var / 5000 * 100) * 0.20
1145
- + edge_10 * 100 * 0.20
1146
  + min(100, colors / 5 * 100) * 0.15
1147
- + (15 if is_human_signal else 0)
1148
  + hc['complexity_score'] * 0.30
1149
  )
1150
 
1151
- def _route(proc, cat, strat, tier, reason, extra=None):
1152
- r = {
 
 
 
 
 
 
 
 
 
 
 
 
1153
  'processor': proc, 'model': proc, 'model_id': proc,
1154
- 'category': cat, 'strategy': strat, 'tier': tier,
1155
- 'reason': reason,
1156
- 'score': round(complexity, 1),
1157
- 'is_human': is_human_signal,
1158
- 'is_graphic': cat in ('flat', 'simple_graphic', 'logo_icon'),
1159
  'hair_complexity': hc,
1160
  'cc_category': cc_cat, 'cc_confidence': 0.7,
1161
- 'signals': {
1162
- 'skin_score': round(skin_score, 3),
1163
- 'complexity': round(complexity, 1),
1164
- 'color': round(cc_color, 3),
1165
- 'border': round(cc_border, 3),
1166
- 'edge': round(cc_edge, 3),
1167
- 'texture': round(cc_texture, 3),
1168
- 'hair_route': hc['route'],
1169
- 'hair_score': round(hc['complexity_score'], 1),
1170
- 'variance': round(var, 1),
1171
- 'edge_10': round(edge_10, 3),
1172
- 'colors': colors,
1173
- },
1174
  }
1175
- if extra:
1176
- r.update(extra)
1177
- return r
1178
-
1179
- # TIER 0: FLAT → flood_fill (classical, ~200ms)
1180
- is_flat = (
1181
- not is_human_signal
1182
- and cc_texture < 0.35
1183
- and (
1184
- complexity < 25
1185
- or cc_cat in ('simple_graphic', 'logo_icon', 'product_white_bg')
1186
- or (cc_color < 0.15 and cc_border > 0.8)
1187
- )
1188
  )
1189
- if is_flat:
1190
- return _route(
1191
- 'flood_fill', 'flat', 'classical', 'TIER_0_FLAT',
1192
- f'flat (cx={complexity:.1f}, tex={cc_texture:.2f}, '
1193
- f'col={cc_color:.2f}, bord={cc_border:.2f}, cat={cc_cat})'
1194
- )
1195
 
1196
- # TIER 1: HUMAN → isnet
1197
- if is_human_signal:
1198
- hair_label = 'human_hair' if hc['route'] != 'simple_hair' else 'human'
1199
- return _route(
1200
- 'isnet-general-use', hair_label, 'neural', 'TIER_1_HUMAN',
1201
- f'human (skin={skin_score:.2f}, cc={cc_cat}, '
1202
- f'hair={hc["route"]}, cx={complexity:.1f})',
1203
- {'is_human': True}
1204
- )
1205
 
1206
- # TIER 2: SMALL → u2netp
1207
- if max(image.size) < 400:
1208
- return _route(
1209
- 'u2netp', 'small', 'neural', 'TIER_2_SMALL',
1210
- f'small ({max(image.size)}px)'
1211
- )
1212
 
1213
- # TIER 3: COMPLEX → birefnet
1214
- is_complex = (
1215
- cc_edge > 0.05
1216
- or cc_color > 0.35
1217
- or cc_texture > 0.4
1218
- or complexity > 45
1219
  )
1220
- if is_complex:
1221
- return _route(
1222
- 'birefnet-general-lite', 'complex', 'neural', 'TIER_3_COMPLEX',
1223
- f'complex (edge={cc_edge:.2f}, col={cc_color:.2f}, '
1224
- f'tex={cc_texture:.2f}, cx={complexity:.1f})'
1225
- )
1226
 
1227
- # TIER 4: FALLBACK
1228
- return _route(
1229
- 'u2netp', 'general', 'neural', 'TIER_4_FALLBACK',
1230
- f'fallback (cx={complexity:.1f})'
1231
- )
1232
 
 
 
 
 
 
 
 
 
 
 
1233
  except Exception as e:
1234
  logger.warning(f"Router failed: {e}")
 
1235
  return {
1236
- 'processor': 'u2netp', 'model': 'u2netp', 'model_id': 'u2netp',
1237
  'category': 'error', 'strategy': 'neural', 'tier': 'ERROR',
1238
  'reason': f'router-error: {e}', 'score': 0,
1239
  'is_human': False, 'is_graphic': False,
1240
  'hair_complexity': {'route': 'simple_hair'},
1241
  'cc_category': 'general_photo', 'cc_confidence': 0.5,
1242
- 'signals': {},
1243
  }
1244
 
1245
 
@@ -1279,7 +1241,7 @@ def detect_fragments(image):
1279
 
1280
 
1281
  # ============================================================
1282
- # IMAGE PIPELINE
1283
  # ============================================================
1284
  def process_image_sync(image_data, model_name=None, transparent=True,
1285
  max_size=1024, quality=95, preserve_hair=True,
@@ -1313,10 +1275,10 @@ def process_image_sync(image_data, model_name=None, transparent=True,
1313
  'needs_reprocess': False, 'router_enabled': False,
1314
  'router_score': 0.0, 'router_reason': 'already transparent',
1315
  'router_tier': 'ALPHA_PASSTHROUGH',
1316
- 'router_signals': {},
1317
  'router_category': 'alpha_passthrough', 'router_confidence': 1.0,
1318
  'mask_confidence': 1.0, 'mask_issues': [], 'hair_route': '',
1319
- 'hair_refined': False, 'diagnostics': []}
 
1320
 
1321
  if model_name:
1322
  resolved = MODEL_ALIASES.get(model_name, model_name)
@@ -1347,8 +1309,7 @@ def process_image_sync(image_data, model_name=None, transparent=True,
1347
  result = process_graphic_classical(image)
1348
  dt = time.time() - t0
1349
  diagnostics.append({
1350
- 'model_tried': selected,
1351
- 'attempt': 'classical',
1352
  'result': 'ok' if result is not None else 'None',
1353
  'elapsed': round(dt, 3),
1354
  })
@@ -1395,6 +1356,9 @@ def process_image_sync(image_data, model_name=None, transparent=True,
1395
  else:
1396
  frag = detect_fragments(result)
1397
 
 
 
 
1398
  qs = frag['quality_score']
1399
  if qs >= 90: ql = 'Excellent'
1400
  elif qs >= 75: ql = 'Good'
@@ -1425,18 +1389,22 @@ def process_image_sync(image_data, model_name=None, transparent=True,
1425
  'router_score': route_analysis.get('score', 0) if route_analysis else 0,
1426
  'router_reason': route_analysis.get('reason', '') if route_analysis else '',
1427
  'router_tier': route_analysis.get('tier', '') if route_analysis else '',
1428
- 'router_signals': route_analysis.get('signals', {}) if route_analysis else {},
1429
  'router_category': cc_category,
1430
  'router_confidence': cc_confidence,
1431
  'mask_confidence': mask_quality.get('confidence', 0),
1432
  'mask_issues': mask_quality.get('issues', []),
1433
  'hair_route': (route_analysis.get('hair_complexity', {}) or {}).get('route', '') if route_analysis else '',
1434
- 'hair_refined': hair_refined, 'diagnostics': diagnostics}
 
1435
  except HTTPException:
1436
  raise
1437
  except Exception as e:
1438
  logger.error(f"Image processing error: {e}")
1439
  raise
 
 
 
 
1440
 
1441
 
1442
  # ============================================================
@@ -1502,10 +1470,8 @@ def _process_frames_sync(raw_frames, resolved_model, transparent,
1502
  img.convert("RGBA").save(buf, format="PNG", compress_level=3)
1503
  results.append({
1504
  "data": base64.b64encode(buf.getvalue()).decode("ascii"),
1505
- "format": "png",
1506
- "size": buf.tell(),
1507
- "fallback": True,
1508
- "error": "all_models_failed",
1509
  })
1510
  continue
1511
 
@@ -1536,16 +1502,17 @@ def _process_frames_sync(raw_frames, resolved_model, transparent,
1536
 
1537
  results.append({
1538
  "data": base64.b64encode(buf.getvalue()).decode("ascii"),
1539
- "format": fmt,
1540
- "size": buf.tell(),
1541
- "model": used_model,
1542
- "smoothed": blended,
1543
  })
1544
 
1545
  del arr
1546
  except Exception as e:
1547
  logger.error(f"[frames-batch] frame {idx} failed: {e}")
1548
  results.append({"data": "", "format": "png", "error": str(e)[:120]})
 
 
 
1549
 
1550
  return results
1551
 
@@ -1553,7 +1520,7 @@ def _process_frames_sync(raw_frames, resolved_model, transparent,
1553
  # ============================================================
1554
  # FASTAPI
1555
  # ============================================================
1556
- app = FastAPI(title="Background Remover", version="32.6.1")
1557
  app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_credentials=True,
1558
  allow_methods=["*"], allow_headers=["*"])
1559
 
@@ -1574,7 +1541,7 @@ async def rate_limit_middleware(request, call_next):
1574
  if request.url.path in (
1575
  "/api/health", "/", "/docs", "/redoc", "/openapi.json",
1576
  "/api/warmup", "/api/debug/env", "/api/debug/memory", "/api/debug/tmp",
1577
- "/api/debug/route",
1578
  "/api/process/frames-batch",
1579
  "/api/frames/session/reset",
1580
  "/api/frames/session/stats",
@@ -1623,11 +1590,19 @@ async def process_image_api(
1623
  if len(contents) > 30 * 1024 * 1024: raise HTTPException(400, "Too large")
1624
  if not AI_AVAILABLE: raise HTTPException(503, "AI unavailable")
1625
 
 
1626
  user_model = None
 
1627
  if model:
1628
  resolved = MODEL_ALIASES.get(model, model)
1629
  if resolved in VALID_REMBG_MODELS or resolved in CLASSICAL_PROCESSORS:
1630
  user_model = resolved
 
 
 
 
 
 
1631
 
1632
  loop = asyncio.get_event_loop()
1633
  pf = functools.partial(process_image_sync, image_data=contents,
@@ -1641,16 +1616,6 @@ async def process_image_api(
1641
  pt = time.time() - t0
1642
  fn = Path(file.filename).stem
1643
 
1644
- sig = result_data.get('router_signals', {}) or {}
1645
- sig_str = ''
1646
- if sig:
1647
- sig_str = (
1648
- f"skin={sig.get('skin_score', 0)},"
1649
- f"cx={sig.get('complexity', 0)},"
1650
- f"tex={sig.get('texture', 0)},"
1651
- f"col={sig.get('color', 0)}"
1652
- )
1653
-
1654
  headers = {
1655
  'X-Processing-Time': f'{pt:.3f}',
1656
  'X-Model-Used': result_data['model'],
@@ -1665,11 +1630,12 @@ async def process_image_api(
1665
  'X-Router-Score': f"{result_data.get('router_score', 0):.1f}",
1666
  'X-Router-Category': result_data.get('router_category', ''),
1667
  'X-Router-Reason': result_data.get('router_reason', ''),
1668
- 'X-Router-Signals': sig_str,
1669
  'X-Hair-Route': result_data.get('hair_route', ''),
1670
  'X-Hair-Refined': str(result_data.get('hair_refined', False)).lower(),
1671
  'Content-Disposition': f"attachment; filename=processed_{fn}.{result_data['extension']}",
1672
  }
 
 
1673
  return StreamingResponse(io.BytesIO(result_data['data']),
1674
  media_type=result_data['media_type'],
1675
  headers=headers)
@@ -1680,9 +1646,41 @@ async def process_image_api(
1680
  raise HTTPException(500, f"Processing failed: {str(e)[:200]}")
1681
 
1682
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1683
  @app.post("/api/debug/route")
1684
  async def debug_route(file: UploadFile = File(...)):
1685
- """Run ONLY the router. No model inference. ~50ms."""
1686
  try:
1687
  contents = await file.read()
1688
  if not contents:
@@ -1693,11 +1691,17 @@ async def debug_route(file: UploadFile = File(...)):
1693
 
1694
  analysis = analyze_image_complexity(image)
1695
 
 
 
 
 
 
 
 
 
 
 
1696
  cc = content_classifier.classify(image)
1697
- small_50 = image.copy()
1698
- small_50.thumbnail((50, 50), Image.Resampling.LANCZOS)
1699
- rgb_50 = np.array(small_50.convert('RGB'))
1700
- direct_skin = _skin_score(rgb_50)
1701
 
1702
  return {
1703
  'decision': {
@@ -1708,19 +1712,16 @@ async def debug_route(file: UploadFile = File(...)):
1708
  'complexity': analysis.get('score'),
1709
  'strategy': analysis.get('strategy'),
1710
  },
1711
- 'signals': analysis.get('signals', {}),
1712
- 'comparison': {
1713
- 'router_skin_score': analysis.get('signals', {}).get('skin_score'),
1714
- 'direct_skin_score_50x50': round(direct_skin, 3),
1715
- 'classifier_skin_score_25px': round(
1716
- cc.get('signals', {}).get('skin_score', 0), 3
1717
- ),
1718
- 'classifier_category': cc.get('category'),
1719
- },
1720
- 'image': {
1721
- 'size': list(image.size),
1722
- 'mode': image.mode,
1723
  },
 
 
1724
  }
1725
  except HTTPException:
1726
  raise
@@ -1754,6 +1755,7 @@ async def process_bulk_images(
1754
  resolved = MODEL_ALIASES.get(model, model)
1755
  if resolved in VALID_REMBG_MODELS or resolved in CLASSICAL_PROCESSORS:
1756
  user_model = resolved
 
1757
 
1758
  zb = io.BytesIO()
1759
  used_names = set()
@@ -1770,382 +1772,4 @@ async def process_bulk_images(
1770
  max_size=max_size, quality=95,
1771
  preserve_hair=preserve_hair,
1772
  use_alpha_matting=use_alpha_matting,
1773
- border_pixels=border_pixels, sharpen=sharpen,
1774
- )
1775
- scores.append(d['quality_score'])
1776
- stem = Path(f.filename).stem or "image"
1777
- out_stem = f"{stem}_remova"
1778
- if out_stem in used_names:
1779
- n = 2
1780
- while f"{out_stem}_{n}" in used_names: n += 1
1781
- out_stem = f"{out_stem}_{n}"
1782
- used_names.add(out_stem)
1783
- out_name = f"{out_stem}.{d['extension']}"
1784
- zf.writestr(out_name, d['data'])
1785
- count += 1
1786
- except Exception as e:
1787
- logger.error(f"Failed {f.filename}: {e}")
1788
-
1789
- if count == 0: raise HTTPException(500, "No images processed")
1790
-
1791
- zb.seek(0)
1792
- pt = time.time() - t0
1793
- avg = sum(scores) / len(scores) if scores else 0
1794
- headers = {
1795
- 'X-Processing-Time': f'{pt:.3f}',
1796
- 'X-Model-Used': 'smart_routing',
1797
- 'X-Image-Count': str(count),
1798
- 'X-Avg-Quality-Score': f'{avg:.1f}',
1799
- 'Content-Disposition': f'attachment; filename=processed_images_{int(time.time())}.zip',
1800
- }
1801
- return StreamingResponse(zb, media_type='application/zip', headers=headers)
1802
- except HTTPException:
1803
- raise
1804
- except Exception as e:
1805
- logger.error(f"Bulk error: {e}")
1806
- raise HTTPException(500, str(e)[:200])
1807
-
1808
-
1809
- @app.post("/api/process/frames-batch")
1810
- async def process_frames_batch(
1811
- files: List[UploadFile] = File(...),
1812
- model: str = Form(DEFAULT_VIDEO_MODEL),
1813
- transparent: bool = Form(True),
1814
- session_id: Optional[str] = Query(None),
1815
- smooth: bool = Query(True),
1816
- ):
1817
- if len(files) > VIDEO_MAX_BATCH:
1818
- raise HTTPException(400, f"Max {VIDEO_MAX_BATCH} frames per batch")
1819
-
1820
- mem = get_container_memory()
1821
- if mem['rss_percent'] > VIDEO_RAM_REJECT_PERCENT:
1822
- logger.warning(
1823
- f"[frames-batch] RSS {mem['rss_percent']:.1f}% > "
1824
- f"{VIDEO_RAM_REJECT_PERCENT}%"
1825
- )
1826
- return JSONResponse(
1827
- status_code=503,
1828
- content={
1829
- 'error': 'Server memory high. Retry shortly.',
1830
- 'rss_percent': round(mem['rss_percent'], 1),
1831
- 'rss_mb': mem['process_rss_mb'],
1832
- 'container_percent': round(mem['percent'], 1),
1833
- 'retry_in': 10,
1834
- 'cold': True,
1835
- },
1836
- headers={'Retry-After': '10', 'Cache-Control': 'no-store'},
1837
- )
1838
-
1839
- raw_frames = []
1840
- for f in files:
1841
- try:
1842
- raw_frames.append(await f.read())
1843
- except Exception as e:
1844
- logger.warning(f"[frames-batch] read failed: {e}")
1845
- raw_frames.append(b"")
1846
-
1847
- loop = asyncio.get_event_loop()
1848
-
1849
- def _worker():
1850
- with _video_semaphore:
1851
- return _process_frames_sync(
1852
- raw_frames, VIDEO_FORCE_MODEL, transparent, session_id, smooth,
1853
- )
1854
-
1855
- try:
1856
- results = await loop.run_in_executor(cpu_executor, _worker)
1857
- except Exception as e:
1858
- logger.error(f"[frames-batch] worker crashed: {e}")
1859
- raise HTTPException(500, f"Frame processing failed: {str(e)[:200]}")
1860
- finally:
1861
- gc.collect()
1862
- trim_memory()
1863
-
1864
- used = next((r.get("model") for r in results if r.get("model")), VIDEO_FORCE_MODEL)
1865
- smoothed_count = sum(1 for r in results if r.get("smoothed"))
1866
- return {
1867
- "frames": results,
1868
- "count": len(results),
1869
- "model": used,
1870
- "transparent": transparent,
1871
- "session_id": session_id if (smooth and session_id) else None,
1872
- "smoothed_count": smoothed_count,
1873
- "smooth_weight": ALPHA_SMOOTH_WEIGHT if smoothed_count else 0.0,
1874
- }
1875
-
1876
-
1877
- @app.post("/api/frames/session/reset")
1878
- async def reset_frame_session(session_id: str = Query(...)):
1879
- existed = _reset_session(session_id)
1880
- return {"ok": True, "session_id": session_id, "existed": existed}
1881
-
1882
-
1883
- @app.get("/api/frames/session/stats")
1884
- async def frame_session_stats():
1885
- return _alpha_cache_stats()
1886
-
1887
-
1888
- @app.post("/api/editor/auto-crop")
1889
- async def editor_auto_crop(file: UploadFile = File(...), padding: int = Query(0, ge=0, le=20)):
1890
- try:
1891
- contents = await file.read()
1892
- img = Image.open(io.BytesIO(contents)).convert('RGBA')
1893
- arr = np.array(img)
1894
- alpha = arr[:, :, 3]
1895
- ys, xs = np.where(alpha > 20)
1896
- if len(ys) == 0:
1897
- return JSONResponse({'success': False, 'error': 'No foreground found'})
1898
- x1, x2 = int(xs.min()), int(xs.max()) + 1
1899
- y1, y2 = int(ys.min()), int(ys.max()) + 1
1900
- w, h = img.size
1901
- if padding > 0:
1902
- pw = int((x2 - x1) * padding / 100)
1903
- ph = int((y2 - y1) * padding / 100)
1904
- x1 = max(0, x1 - pw); y1 = max(0, y1 - ph)
1905
- x2 = min(w, x2 + pw); y2 = min(h, y2 + ph)
1906
- return {'success': True, 'x': x1, 'y': y1,
1907
- 'width': x2 - x1, 'height': y2 - y1,
1908
- 'original_width': w, 'original_height': h}
1909
- except Exception as e:
1910
- logger.error(f"auto-crop failed: {e}")
1911
- return JSONResponse({'success': False, 'error': str(e)}, status_code=500)
1912
-
1913
-
1914
- @app.post("/api/editor/inpaint")
1915
- async def editor_inpaint(file: UploadFile = File(...), mask: UploadFile = File(...),
1916
- method: str = Query('telea'), dilate: int = Query(3)):
1917
- try:
1918
- contents = await file.read()
1919
- mask_contents = await mask.read()
1920
- pil = Image.open(io.BytesIO(contents)).convert('RGBA')
1921
- arr = np.array(pil)
1922
- rgb = arr[:, :, :3].copy()
1923
- alpha = arr[:, :, 3].copy()
1924
- h, w = rgb.shape[:2]
1925
- mask_pil = Image.open(io.BytesIO(mask_contents)).convert('L')
1926
- if mask_pil.size != pil.size:
1927
- mask_pil = mask_pil.resize(pil.size, Image.Resampling.LANCZOS)
1928
- mask_arr = np.array(mask_pil)
1929
- if dilate > 0:
1930
- mask_arr = cv2.dilate(mask_arr, np.ones((dilate, dilate), np.uint8), iterations=1)
1931
- if method == 'ns':
1932
- inpainted = cv2.inpaint(rgb, mask_arr, 3, cv2.INPAINT_NS)
1933
- else:
1934
- inpainted = cv2.inpaint(rgb, mask_arr, 3, cv2.INPAINT_TELEA)
1935
- mask_float = mask_arr.astype(np.float32) / 255.0
1936
- from scipy.ndimage import gaussian_filter
1937
- mask_blur = gaussian_filter(mask_float, sigma=1.5)
1938
- mask_3ch = mask_blur[:, :, None]
1939
- blended = rgb * (1 - mask_3ch) + inpainted * mask_3ch
1940
- blended = np.clip(blended, 0, 255).astype(np.uint8)
1941
- result = np.zeros((h, w, 4), dtype=np.uint8)
1942
- result[:, :, :3] = blended
1943
- result[:, :, 3] = alpha
1944
- out = Image.fromarray(result, 'RGBA')
1945
- buf = io.BytesIO()
1946
- out.save(buf, format='PNG', optimize=True)
1947
- buf.seek(0)
1948
- return StreamingResponse(buf, media_type='image/png')
1949
- except Exception as e:
1950
- logger.error(f"inpaint failed: {e}")
1951
- raise HTTPException(500, f"Inpainting failed: {str(e)[:200]}")
1952
-
1953
-
1954
- @app.get("/api/warmup")
1955
- async def warmup():
1956
- return {"status": "warm", "ts": datetime.now().isoformat()}
1957
-
1958
-
1959
- @app.get("/api/debug/memory")
1960
- async def debug_memory():
1961
- mem = get_container_memory()
1962
- stats = model_manager.get_stats()
1963
- return {
1964
- 'container': mem,
1965
- 'process_rss_mb': mem['process_rss_mb'],
1966
- 'rss_percent': round(mem['rss_percent'], 1),
1967
- 'models_loaded': stats['loaded_models'],
1968
- 'models_count': stats['model_count'],
1969
- 'models_warmed': stats['warmed'],
1970
- 'use_counts': stats['use_counts'],
1971
- 'protected': stats['protected'],
1972
- 'alpha_cache': _alpha_cache_stats(),
1973
- 'video_reject_pct': VIDEO_RAM_REJECT_PERCENT,
1974
- 'torch_imported': 'torch' in sys.modules,
1975
- }
1976
-
1977
-
1978
- @app.get("/api/debug/tmp")
1979
- async def debug_tmp():
1980
- items = []
1981
- try:
1982
- for root, dirs, files in os.walk('/tmp'):
1983
- for f in files:
1984
- p = os.path.join(root, f)
1985
- try:
1986
- items.append({
1987
- 'path': p,
1988
- 'size_mb': round(os.path.getsize(p) / 1048576, 2),
1989
- 'mtime': os.path.getmtime(p),
1990
- })
1991
- except Exception:
1992
- pass
1993
- except Exception:
1994
- pass
1995
- return {
1996
- 'count': len(items),
1997
- 'total_mb': round(sum(i['size_mb'] for i in items), 2),
1998
- 'items': sorted(items, key=lambda x: -x['size_mb'])[:50],
1999
- }
2000
-
2001
-
2002
- @app.get("/api/debug/env")
2003
- async def debug_env():
2004
- mem = get_container_memory()
2005
- return {
2006
- 'total_mb': mem['total_mb'],
2007
- 'used_mb': mem['used_mb'],
2008
- 'available_mb': mem['available_mb'],
2009
- 'container_percent': round(mem['percent'], 1),
2010
- 'process_rss_mb': mem['process_rss_mb'],
2011
- 'rss_percent': round(mem['rss_percent'], 1),
2012
- 'memory_source': mem['source'],
2013
- 'models_loaded': list(model_manager.models.keys()),
2014
- 'models_warmed': dict(model_manager.warmed),
2015
- 'remove_func_ready': remove_func is not None,
2016
- 'ai_available': AI_AVAILABLE,
2017
- 'default_model': default_model_name,
2018
- 'video_force_model': VIDEO_FORCE_MODEL,
2019
- 'video_ram_reject_percent': VIDEO_RAM_REJECT_PERCENT,
2020
- 'video_max_batch': VIDEO_MAX_BATCH,
2021
- 'preload_models': [n for n, _ in PRELOAD_MODELS],
2022
- 'lazy_models': sorted(LAZY_MODELS.keys()),
2023
- 'upload_parser': 'in-memory (SpooledTemporaryFile patched to 200MB)',
2024
- 'router': 'v32.6.1 — flood_fill for flat, complexity score, tier tracking',
2025
- }
2026
-
2027
-
2028
- @app.get("/")
2029
- async def home():
2030
- stats = model_manager.get_stats()
2031
- mem = get_container_memory()
2032
- warmed = sum(1 for w in stats['warmed'].values() if w)
2033
- return HTMLResponse(f"""
2034
- <!DOCTYPE html><html><head><meta charset="UTF-8">
2035
- <title>Background Remover v32.6.1</title>
2036
- <style>body{{font-family:system-ui;background:#0A0A0A;
2037
- color:#fff;min-height:100vh;display:flex;align-items:center;justify-content:center;
2038
- margin:0;padding:20px}}.c{{max-width:900px;background:#111;
2039
- border-radius:20px;padding:40px;text-align:center;
2040
- border:1px solid #222}}.s{{padding:15px;border-radius:12px;
2041
- margin:20px 0;background:#1a1a1a}}a{{color:#7ED957}}</style>
2042
- </head><body><div class="c">
2043
- <h1>Background Remover v32.6.1</h1>
2044
- <div class="s">AI online. {len(stats['loaded_models'])} models ({warmed} warmed).
2045
- RSS {mem['rss_percent']:.0f}% ({mem['process_rss_mb']}/{mem['total_mb']}MB)</div>
2046
- <p>Router: flat→flood_fill (200ms) · human→isnet · small→u2netp · complex→birefnet</p>
2047
- <p>Video: silueta · max {VIDEO_MAX_BATCH} frames/batch · reject at {VIDEO_RAM_REJECT_PERCENT}%</p>
2048
- <p>Privacy: no user data on disk. Bulk in-memory zip. Uploads never touch /tmp.</p>
2049
- <p><a href="/docs">API Docs</a> ·
2050
- <a href="/api/debug/memory">Memory</a> ·
2051
- <a href="/api/debug/tmp">Disk Audit</a> ·
2052
- <a href="/api/debug/env">Env</a></p>
2053
- <p style="margin-top:20px;color:#888;font-size:12px">
2054
- Debug a routing decision: POST an image to /api/debug/route
2055
- </p>
2056
- </div></body></html>
2057
- """)
2058
-
2059
-
2060
- @app.get("/api/health")
2061
- async def health_check():
2062
- stats = model_manager.get_stats()
2063
- mem = get_container_memory()
2064
-
2065
- preload_names = [n for n, _ in PRELOAD_MODELS]
2066
- all_loaded = all(n in stats['loaded_models'] for n in preload_names)
2067
- all_warmed = all(stats['warmed'].get(n, False) for n in preload_names)
2068
-
2069
- ram_ok = mem['rss_percent'] < (VIDEO_RAM_REJECT_PERCENT - 5)
2070
-
2071
- if not AI_AVAILABLE:
2072
- status = 'error'
2073
- elif not all_loaded:
2074
- status = 'degraded'
2075
- elif not all_warmed:
2076
- status = 'warming'
2077
- elif not ram_ok:
2078
- status = 'degraded'
2079
- else:
2080
- status = 'healthy'
2081
-
2082
- return {
2083
- 'status': status,
2084
- 'ai_available': AI_AVAILABLE,
2085
- 'models_loaded': stats['loaded_models'],
2086
- 'models_count': stats['model_count'],
2087
- 'models_warmed': stats['warmed'],
2088
- 'all_loaded': all_loaded,
2089
- 'all_warmed': all_warmed,
2090
- 'protected_models': stats['protected'],
2091
- 'rss_percent': round(mem['rss_percent'], 1),
2092
- 'rss_mb': mem['process_rss_mb'],
2093
- 'container_percent': round(mem['percent'], 1),
2094
- 'memory_total_mb': mem['total_mb'],
2095
- 'memory_source': mem['source'],
2096
- 'video_ram_reject_percent': VIDEO_RAM_REJECT_PERCENT,
2097
- 'video_max_batch': VIDEO_MAX_BATCH,
2098
- 'video_force_model': VIDEO_FORCE_MODEL,
2099
- 'version': '32.6.1',
2100
- 'preload_models': preload_names,
2101
- 'lazy_models': sorted(LAZY_MODELS.keys()),
2102
- 'alpha_cache_ttl': ALPHA_CACHE_TTL_SECONDS,
2103
- 'alpha_purge_interval': ALPHA_PURGE_INTERVAL,
2104
- 'alpha_smoothing': _alpha_cache_stats(),
2105
- 'router_version': 'v32.6.1',
2106
- 'debug_endpoints': [
2107
- '/api/debug/memory',
2108
- '/api/debug/tmp',
2109
- '/api/debug/env',
2110
- '/api/debug/route (POST image)',
2111
- ],
2112
- }
2113
-
2114
-
2115
- @app.exception_handler(HTTPException)
2116
- async def http_exc(request, exc):
2117
- return JSONResponse(status_code=exc.status_code,
2118
- content={'error': exc.detail, 'status_code': exc.status_code})
2119
-
2120
-
2121
- @app.exception_handler(Exception)
2122
- async def gen_exc(request, exc):
2123
- logger.error(f"Unhandled: {exc}")
2124
- return JSONResponse(status_code=500,
2125
- content={'error': 'Internal error', 'detail': str(exc)[:200]})
2126
-
2127
-
2128
- @app.on_event("startup")
2129
- async def startup():
2130
- logger.info("=" * 60)
2131
- logger.info("v32.6.1 ready")
2132
- logger.info("Router: flat→flood_fill (200ms) · human→isnet · small→u2netp · complex→birefnet")
2133
- logger.info("Preload: silueta + u2netp + isnet + birefnet (all 4, all warm)")
2134
- logger.info(f"Video: RSS reject at {VIDEO_RAM_REJECT_PERCENT}%")
2135
- logger.info(f"Alpha cache: {ALPHA_CACHE_TTL_SECONDS}s TTL, {ALPHA_PURGE_INTERVAL}s purge")
2136
- logger.info("Privacy: bulk in-memory, uploads in-memory, no disk writes")
2137
- logger.info("Debug: /api/debug/route for fast router inspection")
2138
- logger.info("=" * 60)
2139
-
2140
-
2141
- @app.on_event("shutdown")
2142
- async def shutdown():
2143
- cpu_executor.wait_for_shutdown = None
2144
- cpu_executor.shutdown(wait=True)
2145
-
2146
-
2147
- if __name__ == '__main__':
2148
- import uvicorn
2149
- uvicorn.run('app:app', host='0.0.0.0',
2150
- port=int(os.environ.get('PORT', 7860)),
2151
- timeout_keep_alive=300, log_level='info', reload=False)
 
1
  #!/usr/bin/env python3
2
+ # app.py - Background Remover v32.6.3
3
  #
4
+ # RAM optimizations vs v32.6.2:
5
+ # • ONNX intra_op_threads = 1 (was 2) — matches Dockerfile, no duplicated buffers
6
+ # • No torch import in trim_memory — saves ~300-500 MB if torch was pulled in
7
+ # • Pre-warm at 128×128 for isnet + birefnet (was 640/512) — tiny buffers
8
+ # • Pre-warm at 384×384 for silueta + u2netp
9
+ # • Birefnet capped at 512 (was 640), isnet capped at 576 (was 640)
10
+ # • Explicit gc.collect() + trim_memory() in process_image_sync finally
11
+ # • Per-stage RSS logging so you can see where RAM goes
12
+ # • /api/models lists active vs dormant with current load state
13
+ # • /api/debug/memory shows per-model RSS breakdown
14
+ #
15
+ # Model access policy:
16
+ # ACTIVE (queryable): silueta, u2netp, isnet-general-use, birefnet-general-lite
17
+ # DORMANT (in code, not queryable): u2net, u2net_human_seg, isnet-anime,
18
+ # birefnet-general, u2net_cloth_seg, sam
19
+ # Dormant requests silently reroute through the complexity router.
20
+ #
21
+ # Router is byte-for-byte from app(3).py.
22
 
23
  # ============================================================
24
  # FORCE IN-MEMORY UPLOAD PARSING (before fastapi import)
 
61
  import psutil
62
 
63
  # ============================================================
64
+ # ONNX RUNTIME — single-threaded to match Dockerfile env
65
  # ============================================================
66
  try:
67
  import onnxruntime as _ort
 
71
  if 'sess_options' not in kwargs and 'so' not in kwargs:
72
  try:
73
  so = _ort.SessionOptions()
74
+ so.intra_op_num_threads = 1 # was 2 — matches OMP_NUM_THREADS=1
75
  so.inter_op_num_threads = 1
76
  so.enable_cpu_mem_arena = False
77
  so.enable_mem_pattern = False
 
82
  return _orig_session_init(self, *args, **kwargs)
83
 
84
  _ort.InferenceSession.__init__ = _patched_session_init
85
+ print("ONNX Runtime patched: arena=OFF, mem_pattern=OFF, threads=1/1")
86
  except Exception as e:
87
  print(f"Could not patch ONNX Runtime: {e}")
88
 
 
109
  # MEMORY UTILITIES
110
  # ============================================================
111
  def trim_memory():
112
+ """Release freed heap pages back to the OS.
113
+ No torch import — if torch was somehow pulled in, importing it here
114
+ would load ~300-500 MB into the process just to call empty_cache()."""
 
 
 
115
  try:
116
  libc = ctypes.CDLL("libc.so.6")
117
  libc.malloc_trim(0)
 
182
  }
183
 
184
 
185
+ def _log_rss(label: str):
186
+ """Log RSS at a checkpoint so you can see where memory goes."""
187
+ try:
188
+ rss = get_process_rss_mb()
189
+ limit = get_container_limit_mb()
190
+ pct = (rss / limit * 100) if limit > 0 else 0
191
+ logger.info(f"[RSS] {label}: {rss} MB ({pct:.1f}%)")
192
+ except Exception:
193
+ pass
194
+
195
+
196
  # ============================================================
197
  # CORE CLASSES
198
  # ============================================================
 
237
  self.use_counts[name] = 1
238
  gc.collect()
239
  trim_memory()
240
+ _log_rss(f"after loading {name}")
 
 
 
 
241
  return session
242
  except Exception as e:
243
  logger.warning(f"Loader failed for {name}: {e}")
 
500
  # ENVIRONMENT
501
  # ============================================================
502
  os.environ["U2NET_HOME"] = "/tmp/u2net_models"
503
+ os.environ["OMP_NUM_THREADS"] = "1" # match Dockerfile
504
  os.environ["CUDA_VISIBLE_DEVICES"] = ""
505
  os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
506
  os.environ["PYTHONUNBUFFERED"] = "1"
 
622
 
623
 
624
  # ============================================================
625
+ # MODEL CONFIG — 4 ACTIVE, 6 DORMANT
626
  # ============================================================
627
  PRELOAD_MODELS = [
628
+ ("silueta", "video only"),
629
+ ("u2netp", "easy/flat"),
630
+ ("isnet-general-use", "human subjects"),
631
+ ("birefnet-general-lite", "complex non-human"),
632
  ]
633
 
634
+ # Active: queryable via ?model=, selected by router, preloaded.
635
+ VALID_REMBG_MODELS = {
636
+ 'silueta', 'u2netp', 'isnet-general-use', 'birefnet-general-lite',
 
 
637
  }
638
 
639
+ # Dormant: still in the code, weights on disk, but NOT queryable.
640
+ # Requests for these reroute through the complexity router.
641
+ DORMANT_MODELS = {
642
+ 'u2net': "~176 MB disk / ~800 MB RAM",
643
+ 'u2net_human_seg': "~176 MB disk / ~800 MB RAM",
644
+ 'isnet-anime': "~176 MB disk / ~800 MB RAM",
645
+ 'birefnet-general': "~900 MB disk / ~2.5 GB RAM",
646
+ 'u2net_cloth_seg': "~176 MB disk / ~800 MB RAM",
647
+ 'sam': "~375 MB disk / ~1 GB RAM",
648
  }
649
 
650
+ # Warm resolution: small for heavy models — graph optimization done,
651
+ # working buffers tiny. First real inference is fast (buffers expand
652
+ # on demand in a single step, kernels are already compiled).
653
+ WARMUP_RESOLUTIONS = {
654
+ 'silueta': (384, 384),
655
+ 'u2netp': (384, 384),
656
+ 'isnet-general-use': (128, 128), # tiny warm only
657
+ 'birefnet-general-lite': (128, 128), # tiny warm only
658
  }
659
 
660
  MODEL_ALIASES = {
 
692
  'sam': {'fg': 240, 'bg': 10, 'erode': 10},
693
  }
694
 
695
+ # CHANGED: birefnet 640→512 (RAM win), isnet 640→576 (mild)
696
  MODEL_RES_CAPS = {
697
  'u2netp': 512, 'silueta': 640, 'u2net': 640,
698
+ 'u2net_human_seg': 640, 'birefnet-general-lite': 512,
699
+ 'birefnet-general': 512, 'isnet-general-use': 576,
700
  'isnet-anime': 640, 'u2net_cloth_seg': 512, 'sam': 512,
701
  }
702
 
 
727
  from rembg import new_session
728
  resolved = MODEL_ALIASES.get(name, name)
729
  if resolved not in VALID_REMBG_MODELS:
730
+ logger.warning(f"'{resolved}' is dormant or invalid. Using u2netp.")
731
  resolved = 'u2netp'
732
  logger.info(f"Loading {resolved}...")
733
  return new_session(resolved)
 
849
 
850
 
851
  def process_graphic_classical(image):
 
 
 
852
  try:
853
  if image.mode != 'RGBA':
854
  image = image.convert('RGBA')
 
939
  logger.warning(f" {name} failed")
940
  except Exception as e:
941
  logger.warning(f" {name}: {e}")
942
+ _log_rss("preload done")
 
 
 
 
943
 
944
 
945
  def initialize_models():
 
975
  logger.warning(f" {name}: not loaded, skip warmup")
976
  continue
977
 
978
+ size = WARMUP_RESOLUTIONS.get(name, (128, 128))
979
 
980
  try:
981
  img = Image.new('RGB', size, (245, 245, 245))
 
1010
  gc.collect()
1011
  trim_memory()
1012
 
 
1013
  warmed = sum(1 for w in model_manager.warmed.values() if w)
1014
+ logger.info(f"Warmup done: {warmed}/{len(PRELOAD_MODELS)} warm.")
1015
+ _log_rss("warmup done")
 
 
1016
 
1017
 
1018
  logger.info("=" * 60)
1019
+ logger.info("Background Remover v32.6.3")
1020
+ logger.info(f"Active: {sorted(VALID_REMBG_MODELS)}")
1021
+ logger.info(f"Dormant: {sorted(DORMANT_MODELS.keys())}")
1022
+ logger.info("Router: flat→flood_fill · human→isnet · small→u2netp · complex→birefnet")
1023
  logger.info("=" * 60)
1024
 
1025
  preload_all_models()
 
1027
  _prewarm_models()
1028
  _start_cache_purge_thread()
1029
 
1030
+ _log_rss("boot complete")
 
 
 
 
 
1031
  logger.info(f"Video: FORCE={VIDEO_FORCE_MODEL}, MAX_BATCH={VIDEO_MAX_BATCH}")
1032
  logger.info(f"Video RAM reject: {VIDEO_RAM_REJECT_PERCENT}% (process RSS)")
1033
  logger.info(f"Alpha cache: TTL {ALPHA_CACHE_TTL_SECONDS}s, purge every {ALPHA_PURGE_INTERVAL}s")
 
1034
  logger.info("=" * 60)
1035
 
1036
 
 
1056
 
1057
 
1058
  # ============================================================
1059
+ # ROUTER — verbatim from app(3).py
1060
  # ============================================================
1061
+ def _preferred_quality_model():
1062
+ loaded = model_manager.get_stats().get('loaded_models', [])
1063
+ if 'isnet-general-use' in loaded:
1064
+ return 'isnet-general-use'
1065
+ if 'silueta' in loaded:
1066
+ return 'silueta'
1067
+ if 'birefnet-general-lite' in loaded:
1068
+ return 'birefnet-general-lite'
1069
+ return 'u2netp'
1070
+
1071
+
1072
  def detect_hair_complexity(image):
1073
  try:
1074
  gray = image.convert('L').resize((100, 100), Image.Resampling.LANCZOS)
 
1089
 
1090
 
1091
  def _skin_score(rgb):
1092
+ pixels = rgb.reshape(-1, 3)
1093
+ step = max(1, len(pixels) // 100)
1094
+ sample = pixels[::step]
1095
+ skin = sum(1 for r, g, b in sample
1096
+ if r > 60 and g > 40 and b > 20 and r > g and r > b
1097
+ and abs(r - g) < 60 and abs(r - b) < 60)
1098
+ return skin / len(sample) if len(sample) else 0
 
 
 
 
 
1099
 
1100
 
1101
  def analyze_image_complexity(image):
1102
+ """Router — verbatim from app(3).py."""
 
 
 
 
 
 
 
 
1103
  try:
1104
+ small = image.copy()
1105
+ small.thumbnail((10, 10), Image.Resampling.LANCZOS)
1106
+ gray = np.array(small.convert('L'))
1107
+ rgb = np.array(small.convert('RGB'))
1108
+ var = float(np.var(gray))
1109
+ gx = np.gradient(gray.astype(np.float32), axis=1)
1110
+ gy = np.gradient(gray.astype(np.float32), axis=0)
1111
+ em = np.sqrt(gx ** 2 + gy ** 2)
1112
+ edge = float(np.sum(em > 10) / 100)
1113
+ colors = len(np.unique(rgb.reshape(-1, 3), axis=0))
1114
+ is_graphic = colors < 15 and edge > 0.08
1115
+ is_human = _skin_score(rgb) > 0.1
1116
+ hc = detect_hair_complexity(image)
1117
  cc = content_classifier.classify(image)
 
1118
  cc_cat = cc.get('category', 'general_photo')
1119
+ cc_signals = cc.get('signals', {}) or {}
1120
+ cc_edge = float(cc_signals.get('edge_density', 0.0))
1121
+ cc_border = float(cc_signals.get('border_uniformity', 0.0))
1122
+ cc_colorcx = float(cc_signals.get('color_complexity', 0.5))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1123
 
1124
  complexity = (
1125
  min(100, var / 5000 * 100) * 0.20
1126
+ + edge * 100 * 0.20
1127
  + min(100, colors / 5 * 100) * 0.15
1128
+ + (15 if is_human else 0)
1129
  + hc['complexity_score'] * 0.30
1130
  )
1131
 
1132
+ loaded = model_manager.get_stats().get('loaded_models', [])
1133
+ has_birefnet = 'birefnet-general-lite' in loaded
1134
+ has_silueta = 'silueta' in loaded
1135
+ quality = _preferred_quality_model()
1136
+
1137
+ tier_map = {
1138
+ 'vehicle': 'VEHICLE', 'simple_graphic': 'GRAPHIC',
1139
+ 'very_hard': 'VERY_HARD', 'human_hair': 'HUMAN_HAIR',
1140
+ 'human': 'HUMAN', 'complex': 'COMPLEX',
1141
+ 'medium': 'MEDIUM', 'simple': 'SIMPLE', 'error': 'ERROR',
1142
+ }
1143
+
1144
+ def _route(proc, cat, strat, reason):
1145
+ return {
1146
  'processor': proc, 'model': proc, 'model_id': proc,
1147
+ 'category': cat, 'strategy': strat, 'reason': reason,
1148
+ 'tier': tier_map.get(cat, ''),
1149
+ 'score': complexity,
1150
+ 'is_human': is_human, 'is_graphic': is_graphic,
 
1151
  'hair_complexity': hc,
1152
  'cc_category': cc_cat, 'cc_confidence': 0.7,
 
 
 
 
 
 
 
 
 
 
 
 
 
1153
  }
1154
+
1155
+ is_vehicle_like = (
1156
+ not is_human
1157
+ and cc_cat in ('general_photo', 'product_white_bg', 'complex')
1158
+ and cc_edge > 0.04
 
 
 
 
 
 
 
 
1159
  )
 
 
 
 
 
 
1160
 
1161
+ if is_vehicle_like:
1162
+ if has_birefnet:
1163
+ return _route(
1164
+ 'birefnet-general-lite', 'vehicle', 'neural',
1165
+ f"vehicle/object (cc={cc_cat}, edge={cc_edge:.2f}, "
1166
+ f"border={cc_border:.2f}, cx={cc_colorcx:.2f})"
1167
+ )
1168
+ if has_silueta:
1169
+ return _route('silueta', 'vehicle', 'neural', 'vehicle (silueta fallback)')
1170
 
1171
+ if is_graphic and not is_vehicle_like:
1172
+ return _route('flood_fill', 'simple_graphic', 'classical', 'simple graphic')
 
 
 
 
1173
 
1174
+ is_very_hard = (
1175
+ max(image.size) > 2400
1176
+ or complexity > 78
1177
+ or (is_human and hc['route'] == 'wispy_hair' and complexity > 55)
 
 
1178
  )
1179
+ if is_very_hard and has_birefnet:
1180
+ return _route('birefnet-general-lite', 'very_hard', 'neural', 'very hard')
 
 
 
 
1181
 
1182
+ if is_human and hc['route'] in ('wispy_hair', 'complex_hair'):
1183
+ return _route(quality, 'human_hair', 'neural', f'human+{hc["route"]}')
 
 
 
1184
 
1185
+ if is_human:
1186
+ return _route(quality, 'human', 'neural', 'human')
1187
+
1188
+ if complexity > 55:
1189
+ return _route(quality, 'complex', 'neural', 'complex')
1190
+
1191
+ if complexity > 25:
1192
+ return _route(quality, 'medium', 'neural', 'medium')
1193
+
1194
+ return _route('u2netp', 'simple', 'neural', 'simple')
1195
  except Exception as e:
1196
  logger.warning(f"Router failed: {e}")
1197
+ m = _preferred_quality_model()
1198
  return {
1199
+ 'processor': m, 'model': m, 'model_id': m,
1200
  'category': 'error', 'strategy': 'neural', 'tier': 'ERROR',
1201
  'reason': f'router-error: {e}', 'score': 0,
1202
  'is_human': False, 'is_graphic': False,
1203
  'hair_complexity': {'route': 'simple_hair'},
1204
  'cc_category': 'general_photo', 'cc_confidence': 0.5,
 
1205
  }
1206
 
1207
 
 
1241
 
1242
 
1243
  # ============================================================
1244
+ # IMAGE PIPELINE — with explicit cleanup finally
1245
  # ============================================================
1246
  def process_image_sync(image_data, model_name=None, transparent=True,
1247
  max_size=1024, quality=95, preserve_hair=True,
 
1275
  'needs_reprocess': False, 'router_enabled': False,
1276
  'router_score': 0.0, 'router_reason': 'already transparent',
1277
  'router_tier': 'ALPHA_PASSTHROUGH',
 
1278
  'router_category': 'alpha_passthrough', 'router_confidence': 1.0,
1279
  'mask_confidence': 1.0, 'mask_issues': [], 'hair_route': '',
1280
+ 'hair_refined': False, 'diagnostics': [],
1281
+ 'dormant_rerouted': None}
1282
 
1283
  if model_name:
1284
  resolved = MODEL_ALIASES.get(model_name, model_name)
 
1309
  result = process_graphic_classical(image)
1310
  dt = time.time() - t0
1311
  diagnostics.append({
1312
+ 'model_tried': selected, 'attempt': 'classical',
 
1313
  'result': 'ok' if result is not None else 'None',
1314
  'elapsed': round(dt, 3),
1315
  })
 
1356
  else:
1357
  frag = detect_fragments(result)
1358
 
1359
+ if result.size != original_size:
1360
+ result = result.resize(original_size, Image.Resampling.LANCZOS)
1361
+
1362
  qs = frag['quality_score']
1363
  if qs >= 90: ql = 'Excellent'
1364
  elif qs >= 75: ql = 'Good'
 
1389
  'router_score': route_analysis.get('score', 0) if route_analysis else 0,
1390
  'router_reason': route_analysis.get('reason', '') if route_analysis else '',
1391
  'router_tier': route_analysis.get('tier', '') if route_analysis else '',
 
1392
  'router_category': cc_category,
1393
  'router_confidence': cc_confidence,
1394
  'mask_confidence': mask_quality.get('confidence', 0),
1395
  'mask_issues': mask_quality.get('issues', []),
1396
  'hair_route': (route_analysis.get('hair_complexity', {}) or {}).get('route', '') if route_analysis else '',
1397
+ 'hair_refined': hair_refined, 'diagnostics': diagnostics,
1398
+ 'dormant_rerouted': None}
1399
  except HTTPException:
1400
  raise
1401
  except Exception as e:
1402
  logger.error(f"Image processing error: {e}")
1403
  raise
1404
+ finally:
1405
+ # Release local references and ask glibc to return freed pages
1406
+ gc.collect()
1407
+ trim_memory()
1408
 
1409
 
1410
  # ============================================================
 
1470
  img.convert("RGBA").save(buf, format="PNG", compress_level=3)
1471
  results.append({
1472
  "data": base64.b64encode(buf.getvalue()).decode("ascii"),
1473
+ "format": "png", "size": buf.tell(),
1474
+ "fallback": True, "error": "all_models_failed",
 
 
1475
  })
1476
  continue
1477
 
 
1502
 
1503
  results.append({
1504
  "data": base64.b64encode(buf.getvalue()).decode("ascii"),
1505
+ "format": fmt, "size": buf.tell(),
1506
+ "model": used_model, "smoothed": blended,
 
 
1507
  })
1508
 
1509
  del arr
1510
  except Exception as e:
1511
  logger.error(f"[frames-batch] frame {idx} failed: {e}")
1512
  results.append({"data": "", "format": "png", "error": str(e)[:120]})
1513
+ finally:
1514
+ gc.collect()
1515
+ trim_memory()
1516
 
1517
  return results
1518
 
 
1520
  # ============================================================
1521
  # FASTAPI
1522
  # ============================================================
1523
+ app = FastAPI(title="Background Remover", version="32.6.3")
1524
  app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_credentials=True,
1525
  allow_methods=["*"], allow_headers=["*"])
1526
 
 
1541
  if request.url.path in (
1542
  "/api/health", "/", "/docs", "/redoc", "/openapi.json",
1543
  "/api/warmup", "/api/debug/env", "/api/debug/memory", "/api/debug/tmp",
1544
+ "/api/debug/route", "/api/models",
1545
  "/api/process/frames-batch",
1546
  "/api/frames/session/reset",
1547
  "/api/frames/session/stats",
 
1590
  if len(contents) > 30 * 1024 * 1024: raise HTTPException(400, "Too large")
1591
  if not AI_AVAILABLE: raise HTTPException(503, "AI unavailable")
1592
 
1593
+ # DORMANT MODEL ROUTING
1594
  user_model = None
1595
+ dormant_requested = None
1596
  if model:
1597
  resolved = MODEL_ALIASES.get(model, model)
1598
  if resolved in VALID_REMBG_MODELS or resolved in CLASSICAL_PROCESSORS:
1599
  user_model = resolved
1600
+ elif resolved in DORMANT_MODELS:
1601
+ logger.info(f"Dormant '{resolved}' requested — using router instead")
1602
+ dormant_requested = resolved
1603
+ user_model = None
1604
+ else:
1605
+ user_model = None
1606
 
1607
  loop = asyncio.get_event_loop()
1608
  pf = functools.partial(process_image_sync, image_data=contents,
 
1616
  pt = time.time() - t0
1617
  fn = Path(file.filename).stem
1618
 
 
 
 
 
 
 
 
 
 
 
1619
  headers = {
1620
  'X-Processing-Time': f'{pt:.3f}',
1621
  'X-Model-Used': result_data['model'],
 
1630
  'X-Router-Score': f"{result_data.get('router_score', 0):.1f}",
1631
  'X-Router-Category': result_data.get('router_category', ''),
1632
  'X-Router-Reason': result_data.get('router_reason', ''),
 
1633
  'X-Hair-Route': result_data.get('hair_route', ''),
1634
  'X-Hair-Refined': str(result_data.get('hair_refined', False)).lower(),
1635
  'Content-Disposition': f"attachment; filename=processed_{fn}.{result_data['extension']}",
1636
  }
1637
+ if dormant_requested:
1638
+ headers['X-Dormant-Rerouted'] = f"{dormant_requested}->{result_data['model_id']}"
1639
  return StreamingResponse(io.BytesIO(result_data['data']),
1640
  media_type=result_data['media_type'],
1641
  headers=headers)
 
1646
  raise HTTPException(500, f"Processing failed: {str(e)[:200]}")
1647
 
1648
 
1649
+ @app.get("/api/models")
1650
+ async def list_models_endpoint():
1651
+ """Which models are active vs dormant, plus current load state."""
1652
+ stats = model_manager.get_stats()
1653
+ loaded = set(stats['loaded_models'])
1654
+ return {
1655
+ 'active': [
1656
+ {
1657
+ 'id': n, 'name': DISPLAY_NAMES.get(n, n),
1658
+ 'loaded': n in loaded,
1659
+ 'warmed': stats['warmed'].get(n, False),
1660
+ 'queryable': True,
1661
+ }
1662
+ for n, _ in PRELOAD_MODELS
1663
+ ],
1664
+ 'dormant': [
1665
+ {
1666
+ 'id': n, 'size_hint': size,
1667
+ 'name': DISPLAY_NAMES.get(n, n),
1668
+ 'loaded': n in loaded,
1669
+ 'queryable': False,
1670
+ 'note': 'Requests reroute through complexity router',
1671
+ }
1672
+ for n, size in DORMANT_MODELS.items()
1673
+ ],
1674
+ 'classical': [
1675
+ {'id': 'flood_fill', 'name': 'Flood Fill', 'queryable': True},
1676
+ ],
1677
+ 'loaded_now': sorted(loaded),
1678
+ 'max_slots': model_manager.max_models,
1679
+ }
1680
+
1681
+
1682
  @app.post("/api/debug/route")
1683
  async def debug_route(file: UploadFile = File(...)):
 
1684
  try:
1685
  contents = await file.read()
1686
  if not contents:
 
1691
 
1692
  analysis = analyze_image_complexity(image)
1693
 
1694
+ small = image.copy()
1695
+ small.thumbnail((10, 10), Image.Resampling.LANCZOS)
1696
+ gray = np.array(small.convert('L'))
1697
+ rgb = np.array(small.convert('RGB'))
1698
+ gx = np.gradient(gray.astype(np.float32), axis=1)
1699
+ gy = np.gradient(gray.astype(np.float32), axis=0)
1700
+ em = np.sqrt(gx ** 2 + gy ** 2)
1701
+ edge_10 = float(np.sum(em > 10) / 100)
1702
+ colors_10 = int(len(np.unique(rgb.reshape(-1, 3), axis=0)))
1703
+
1704
  cc = content_classifier.classify(image)
 
 
 
 
1705
 
1706
  return {
1707
  'decision': {
 
1712
  'complexity': analysis.get('score'),
1713
  'strategy': analysis.get('strategy'),
1714
  },
1715
+ 'signals': {
1716
+ 'is_human': analysis.get('is_human'),
1717
+ 'is_graphic': analysis.get('is_graphic'),
1718
+ 'hair_route': (analysis.get('hair_complexity') or {}).get('route', ''),
1719
+ 'hair_score': round((analysis.get('hair_complexity') or {}).get('complexity_score', 0), 1),
1720
+ 'edge_10x10': round(edge_10, 3),
1721
+ 'colors_10x10': colors_10,
 
 
 
 
 
1722
  },
1723
+ 'classifier': {'category': cc.get('category'), 'signals': cc.get('signals')},
1724
+ 'image': {'size': list(image.size), 'mode': image.mode},
1725
  }
1726
  except HTTPException:
1727
  raise
 
1755
  resolved = MODEL_ALIASES.get(model, model)
1756
  if resolved in VALID_REMBG_MODELS or resolved in CLASSICAL_PROCESSORS:
1757
  user_model = resolved
1758
+ # Dormant / unknown → router picks per-image
1759
 
1760
  zb = io.BytesIO()
1761
  used_names = set()
 
1772
  max_size=max_size, quality=95,
1773
  preserve_hair=preserve_hair,
1774
  use_alpha_matting=use_alpha_matting,
1775
+ border_pixels=border_pixels