andevs commited on
Commit
95d5e33
·
verified ·
1 Parent(s): 6613e8b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -58
app.py CHANGED
@@ -1,33 +1,20 @@
1
  #!/usr/bin/env python3
2
- # app.py - Background Remover v32.6.0
3
  #
4
  # Changes in this version:
5
- # • Router rewritten with complexity score restored.
 
 
 
6
  # • Router uses _skin_score (2500 pixels at 50×50) instead of the
7
- # ContentClassifier's unreliable 25-pixel sample. This fixes the
8
- # "woman in a scarf routed wrong" bug.
9
- # • Tier tracking in every image response: TIER_0_FLAT,
10
- # TIER_1_HUMAN, TIER_2_SMALL, TIER_3_COMPLEX, TIER_4_FALLBACK.
11
- # Exposed via X-Router-Tier header.
12
- # • Full signal breakdown in router response (skin, complexity,
13
- # color, border, edge, texture, hair route/score).
14
- # • New /api/debug/route endpoint — runs ONLY the router, returns
15
- # every signal in ~50ms, no model inference. Use this to debug
16
- # misrouting without paying the 25-second inference cost.
17
  # • All 4 models preloaded and pre-warmed (from v32.5.0).
18
  # • RSS-based memory rejection (from v32.5.0).
19
- # • In-memory bulk zip, no tempdir (from v32.5.0).
20
  # • Alpha cache TTL 60s + background purge (from v32.5.0).
21
  # • SpooledTemporaryFile patched to keep uploads in RAM (from v32.5.0).
22
- #
23
- # Privacy contract:
24
- # Image uploads: bytes → PIL → result → zip buffer → HTTP response.
25
- # Never touches disk.
26
- # Video frames: same. base64-encoded results streamed back.
27
- # Bulk images: same. In-memory zip, no tempdir.
28
- # Alpha cache: numpy arrays of alpha values, not the image itself.
29
- # TTL 60s, purged every 15s by background thread.
30
- # Model weights: /tmp/u2net_models/*.onnx — AI models, not user data.
31
 
32
  # ============================================================
33
  # FORCE IN-MEMORY UPLOAD PARSING (before fastapi import)
@@ -849,6 +836,9 @@ def process_with_neural(image, session, model_name, force_no_matting=False):
849
 
850
 
851
  def process_graphic_classical(image):
 
 
 
852
  try:
853
  if image.mode != 'RGBA':
854
  image = image.convert('RGBA')
@@ -1023,8 +1013,8 @@ def _prewarm_models():
1023
 
1024
 
1025
  logger.info("=" * 60)
1026
- logger.info("Background Remover v32.6.0")
1027
- logger.info("4 models preloaded, pre-warmed, complexity score restored")
1028
  logger.info("=" * 60)
1029
 
1030
  preload_all_models()
@@ -1041,7 +1031,7 @@ logger.info(
1041
  logger.info(f"Video: FORCE={VIDEO_FORCE_MODEL}, MAX_BATCH={VIDEO_MAX_BATCH}")
1042
  logger.info(f"Video RAM reject: {VIDEO_RAM_REJECT_PERCENT}% (process RSS)")
1043
  logger.info(f"Alpha cache: TTL {ALPHA_CACHE_TTL_SECONDS}s, purge every {ALPHA_PURGE_INTERVAL}s")
1044
- logger.info(f"Router: complexity score + _skin_score 2500px + tier tracking")
1045
  logger.info("=" * 60)
1046
 
1047
 
@@ -1067,7 +1057,7 @@ def get_session_for_model(model_name):
1067
 
1068
 
1069
  # ============================================================
1070
- # ROUTER — complexity score + real skin detector + tiers
1071
  # ============================================================
1072
  def detect_hair_complexity(image):
1073
  try:
@@ -1089,9 +1079,6 @@ def detect_hair_complexity(image):
1089
 
1090
 
1091
  def _skin_score(rgb):
1092
- """Fraction of pixels that look like skin. Vectorized, 2500-pixel sample
1093
- at typical usage. Looser thresholds than ContentClassifier._skin so
1094
- partially-occluded faces (scarf, fan, hat, glasses) still register."""
1095
  if rgb is None or rgb.size == 0:
1096
  return 0.0
1097
  r = rgb[:, :, 0].astype(np.int32)
@@ -1110,11 +1097,11 @@ def analyze_image_complexity(image):
1110
  """Priority router with complexity score and tier tracking.
1111
 
1112
  Tier order:
1113
- TIER_0_FLAT → u2netp (1-2s)
1114
- TIER_1_HUMAN → isnet (2-6s)
1115
- TIER_2_SMALL → u2netp (1-2s)
1116
- TIER_3_COMPLEX → birefnet (15-25s)
1117
- TIER_4_FALLBACK → u2netp
1118
  """
1119
  try:
1120
  cc = content_classifier.classify(image)
@@ -1125,9 +1112,8 @@ def analyze_image_complexity(image):
1125
  cc_edge = float(sig.get('edge_density', 0))
1126
  cc_texture = float(sig.get('texture', 0))
1127
 
1128
- # Direct skin score at 50×50 (2500 pixels). This is the fix.
1129
- # ContentClassifier._skin samples 25 pixels and misses partial
1130
- # occlusion. _skin_score here is much more reliable.
1131
  small_50 = image.copy()
1132
  small_50.thumbnail((50, 50), Image.Resampling.LANCZOS)
1133
  rgb_50 = np.array(small_50.convert('RGB'))
@@ -1136,7 +1122,7 @@ def analyze_image_complexity(image):
1136
  # Hair complexity
1137
  hc = detect_hair_complexity(image)
1138
 
1139
- # Complexity score (the thing that got dropped)
1140
  small_10 = image.copy()
1141
  small_10.thumbnail((10, 10), Image.Resampling.LANCZOS)
1142
  gray_10 = np.array(small_10.convert('L'))
@@ -1148,7 +1134,6 @@ def analyze_image_complexity(image):
1148
  edge_10 = float(np.sum(em > 10) / max(1, em.size))
1149
  colors = len(np.unique(rgb_10.reshape(-1, 3), axis=0))
1150
 
1151
- # Human signal — any of four independent sources firing
1152
  is_human_signal = (
1153
  skin_score > 0.05
1154
  or cc_cat in ('human', 'human_hair')
@@ -1191,9 +1176,7 @@ def analyze_image_complexity(image):
1191
  r.update(extra)
1192
  return r
1193
 
1194
- # TIER 0: FLAT → u2netp
1195
- # Low texture is the key. A cat with fur has high texture even
1196
- # against a plain wall. A flag has zero texture.
1197
  is_flat = (
1198
  not is_human_signal
1199
  and cc_texture < 0.35
@@ -1205,7 +1188,7 @@ def analyze_image_complexity(image):
1205
  )
1206
  if is_flat:
1207
  return _route(
1208
- 'u2netp', 'flat', 'neural', 'TIER_0_FLAT',
1209
  f'flat (cx={complexity:.1f}, tex={cc_texture:.2f}, '
1210
  f'col={cc_color:.2f}, bord={cc_border:.2f}, cat={cc_cat})'
1211
  )
@@ -1360,7 +1343,15 @@ def process_image_sync(image_data, model_name=None, transparent=True,
1360
  diagnostics = []
1361
  result = None
1362
  if selected in CLASSICAL_PROCESSORS:
 
1363
  result = process_graphic_classical(image)
 
 
 
 
 
 
 
1364
 
1365
  if result is None:
1366
  tried = set()
@@ -1398,7 +1389,7 @@ def process_image_sync(image_data, model_name=None, transparent=True,
1398
  except Exception:
1399
  pass
1400
 
1401
- if selected in ('u2netp', 'silueta'):
1402
  frag = {'fragment_ratio': 0.0, 'quality_score': 100.0,
1403
  'has_fragments': False, 'issues': []}
1404
  else:
@@ -1562,7 +1553,7 @@ def _process_frames_sync(raw_frames, resolved_model, transparent,
1562
  # ============================================================
1563
  # FASTAPI
1564
  # ============================================================
1565
- app = FastAPI(title="Background Remover", version="32.6.0")
1566
  app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_credentials=True,
1567
  allow_methods=["*"], allow_headers=["*"])
1568
 
@@ -1650,7 +1641,6 @@ async def process_image_api(
1650
  pt = time.time() - t0
1651
  fn = Path(file.filename).stem
1652
 
1653
- # Encode router signals compactly for the header
1654
  sig = result_data.get('router_signals', {}) or {}
1655
  sig_str = ''
1656
  if sig:
@@ -1692,9 +1682,7 @@ async def process_image_api(
1692
 
1693
  @app.post("/api/debug/route")
1694
  async def debug_route(file: UploadFile = File(...)):
1695
- """Run ONLY the router. No model inference. Returns every signal.
1696
- Takes ~50ms. Use this to debug misrouting without paying the
1697
- inference cost."""
1698
  try:
1699
  contents = await file.read()
1700
  if not contents:
@@ -1718,6 +1706,7 @@ async def debug_route(file: UploadFile = File(...)):
1718
  'category': analysis.get('category'),
1719
  'reason': analysis.get('reason'),
1720
  'complexity': analysis.get('score'),
 
1721
  },
1722
  'signals': analysis.get('signals', {}),
1723
  'comparison': {
@@ -2032,7 +2021,7 @@ async def debug_env():
2032
  'preload_models': [n for n, _ in PRELOAD_MODELS],
2033
  'lazy_models': sorted(LAZY_MODELS.keys()),
2034
  'upload_parser': 'in-memory (SpooledTemporaryFile patched to 200MB)',
2035
- 'router': 'v32.6 — complexity score + _skin_score + tier tracking',
2036
  }
2037
 
2038
 
@@ -2043,7 +2032,7 @@ async def home():
2043
  warmed = sum(1 for w in stats['warmed'].values() if w)
2044
  return HTMLResponse(f"""
2045
  <!DOCTYPE html><html><head><meta charset="UTF-8">
2046
- <title>Background Remover v32.6.0</title>
2047
  <style>body{{font-family:system-ui;background:#0A0A0A;
2048
  color:#fff;min-height:100vh;display:flex;align-items:center;justify-content:center;
2049
  margin:0;padding:20px}}.c{{max-width:900px;background:#111;
@@ -2051,11 +2040,10 @@ async def home():
2051
  border:1px solid #222}}.s{{padding:15px;border-radius:12px;
2052
  margin:20px 0;background:#1a1a1a}}a{{color:#7ED957}}</style>
2053
  </head><body><div class="c">
2054
- <h1>Background Remover v32.6.0</h1>
2055
  <div class="s">AI online. {len(stats['loaded_models'])} models ({warmed} warmed).
2056
  RSS {mem['rss_percent']:.0f}% ({mem['process_rss_mb']}/{mem['total_mb']}MB)</div>
2057
- <p>Router: flat→u2netp · human→isnet · small→u2netp · complex→birefnet</p>
2058
- <p>Complexity score restored · _skin_score 2500px · tier tracking</p>
2059
  <p>Video: silueta · max {VIDEO_MAX_BATCH} frames/batch · reject at {VIDEO_RAM_REJECT_PERCENT}%</p>
2060
  <p>Privacy: no user data on disk. Bulk in-memory zip. Uploads never touch /tmp.</p>
2061
  <p><a href="/docs">API Docs</a> ·
@@ -2108,13 +2096,13 @@ async def health_check():
2108
  'video_ram_reject_percent': VIDEO_RAM_REJECT_PERCENT,
2109
  'video_max_batch': VIDEO_MAX_BATCH,
2110
  'video_force_model': VIDEO_FORCE_MODEL,
2111
- 'version': '32.6.0',
2112
  'preload_models': preload_names,
2113
  'lazy_models': sorted(LAZY_MODELS.keys()),
2114
  'alpha_cache_ttl': ALPHA_CACHE_TTL_SECONDS,
2115
  'alpha_purge_interval': ALPHA_PURGE_INTERVAL,
2116
  'alpha_smoothing': _alpha_cache_stats(),
2117
- 'router_version': 'v32.6',
2118
  'debug_endpoints': [
2119
  '/api/debug/memory',
2120
  '/api/debug/tmp',
@@ -2140,8 +2128,8 @@ async def gen_exc(request, exc):
2140
  @app.on_event("startup")
2141
  async def startup():
2142
  logger.info("=" * 60)
2143
- logger.info("v32.6.0 ready")
2144
- logger.info("Router: complexity score + _skin_score 2500px + tier tracking")
2145
  logger.info("Preload: silueta + u2netp + isnet + birefnet (all 4, all warm)")
2146
  logger.info(f"Video: RSS reject at {VIDEO_RAM_REJECT_PERCENT}%")
2147
  logger.info(f"Alpha cache: {ALPHA_CACHE_TTL_SECONDS}s TTL, {ALPHA_PURGE_INTERVAL}s purge")
@@ -2152,6 +2140,7 @@ async def startup():
2152
 
2153
  @app.on_event("shutdown")
2154
  async def shutdown():
 
2155
  cpu_executor.shutdown(wait=True)
2156
 
2157
 
 
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)
 
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')
 
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()
 
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
 
1058
 
1059
  # ============================================================
1060
+ # ROUTER
1061
  # ============================================================
1062
  def detect_hair_complexity(image):
1063
  try:
 
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)
 
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)
 
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'))
 
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'))
 
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')
 
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
 
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
  )
 
1343
  diagnostics = []
1344
  result = None
1345
  if selected in CLASSICAL_PROCESSORS:
1346
+ t0 = time.time()
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
+ })
1355
 
1356
  if result is None:
1357
  tried = set()
 
1389
  except Exception:
1390
  pass
1391
 
1392
+ if selected in ('u2netp', 'silueta', 'flood_fill', 'graphic_classical'):
1393
  frag = {'fragment_ratio': 0.0, 'quality_score': 100.0,
1394
  'has_fragments': False, 'issues': []}
1395
  else:
 
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
 
 
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:
 
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:
 
1706
  'category': analysis.get('category'),
1707
  'reason': analysis.get('reason'),
1708
  'complexity': analysis.get('score'),
1709
+ 'strategy': analysis.get('strategy'),
1710
  },
1711
  'signals': analysis.get('signals', {}),
1712
  'comparison': {
 
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
 
 
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;
 
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> ·
 
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',
 
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")
 
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