Spaces:
Sleeping
Sleeping
Claude commited on
Commit ·
4260bdd
1
Parent(s): 5026b24
fix: use token length as primary noise discriminator — filters td/wht/LN illustration noise
Browse files- smoke_signal_tab.py +18 -6
smoke_signal_tab.py
CHANGED
|
@@ -1738,19 +1738,31 @@ def _run_tesseract_batch(
|
|
| 1738 |
"method": "tesseract-noise-filtered",
|
| 1739 |
"tesseract_lang": lang_hint or "eng",
|
| 1740 |
}
|
| 1741 |
-
# 3. Filter individual regions that look like illustration noise
|
| 1742 |
-
#
|
|
|
|
| 1743 |
clean_regions = []
|
| 1744 |
for r in regions:
|
| 1745 |
txt = r["text"]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1746 |
letters = sum(1 for c in txt if c.isalpha())
|
| 1747 |
printable = sum(1 for c in txt if c.isprintable() and not c.isspace())
|
| 1748 |
r_alpha = (letters / printable) if printable else 0.0
|
| 1749 |
-
tokens
|
| 1750 |
-
r_avg_tok =
|
| 1751 |
-
if r_alpha >= 0.55 or r["confidence"] >= 0.75:
|
| 1752 |
clean_regions.append(r)
|
| 1753 |
-
if
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1754 |
# Recalculate confidence without noise regions
|
| 1755 |
cw = sum(r["confidence"] * r["word_count"] for r in clean_regions)
|
| 1756 |
wc = sum(r["word_count"] for r in clean_regions)
|
|
|
|
| 1738 |
"method": "tesseract-noise-filtered",
|
| 1739 |
"tesseract_lang": lang_hint or "eng",
|
| 1740 |
}
|
| 1741 |
+
# 3. Filter individual regions that look like illustration noise.
|
| 1742 |
+
# Key discriminator: real picture-book words avg 3.5+ chars.
|
| 1743 |
+
# Illustration noise (td, wht, LN, WA) avg 2.5 chars.
|
| 1744 |
clean_regions = []
|
| 1745 |
for r in regions:
|
| 1746 |
txt = r["text"]
|
| 1747 |
+
tokens = [t for t in txt.split() if t and any(c.isalpha() for c in t)]
|
| 1748 |
+
if not tokens:
|
| 1749 |
+
continue
|
| 1750 |
+
r_avg_tok = sum(len(t) for t in tokens) / len(tokens)
|
| 1751 |
letters = sum(1 for c in txt if c.isalpha())
|
| 1752 |
printable = sum(1 for c in txt if c.isprintable() and not c.isspace())
|
| 1753 |
r_alpha = (letters / printable) if printable else 0.0
|
| 1754 |
+
# Keep if: long enough tokens OR high confidence OR single short word (punctuation line)
|
| 1755 |
+
if r_avg_tok >= 3.0 or r["confidence"] >= 0.80 or (len(tokens) == 1 and r["confidence"] >= 0.60):
|
|
|
|
| 1756 |
clean_regions.append(r)
|
| 1757 |
+
if not clean_regions:
|
| 1758 |
+
# Everything filtered — page is pure illustration noise
|
| 1759 |
+
return {
|
| 1760 |
+
"regions": [],
|
| 1761 |
+
"confidence": 0.0,
|
| 1762 |
+
"method": "tesseract-noise-filtered",
|
| 1763 |
+
"tesseract_lang": lang_hint or "eng",
|
| 1764 |
+
}
|
| 1765 |
+
if len(clean_regions) < len(regions):
|
| 1766 |
# Recalculate confidence without noise regions
|
| 1767 |
cw = sum(r["confidence"] * r["word_count"] for r in clean_regions)
|
| 1768 |
wc = sum(r["word_count"] for r in clean_regions)
|