Spaces:
Running
Running
Fix non-text background lines with CC filter and 1D NMS
Browse filesFiles changed (2)
- Dockerfile+1-1
- ocr_webapp/app.py+59-8
12345FROM python:3.11-slim# Rebuild timestamp: 2026-07-30T11:08:00Z - Non-text background line pruningENV DEBIAN_FRONTEND=noninteractive12345FROM python:3.11-slim# Rebuild timestamp: 2026-07-30T11:30:20Z - CC ink density filter & 1D Vertical NMSENV DEBIAN_FRONTEND=noninteractive
410 unmodified lines4114124134144154164178 unmodified lines426427428429430431432433434435436437438439440427 unmodified lines86886987087187287387419 unmodified lines894895896897898899410 unmodified linesfor i in range(1, num_labels):x, y, w, h, area = stats[i]if area < 5 or h < 3 or w < 3:continuecy = y + h / 2assigned_lines = []8 unmodified linesfor l_idx in assigned_lines:line_components[l_idx].add(i)true_core_coords = []for idx, (s, e) in enumerate(base_coords):comps = line_components[idx]if comps:core_s = min(stats[i][1] for i in comps)core_e = max(stats[i][1] + stats[i][3] for i in comps)true_core_coords.append((core_s, core_e))else:true_core_coords.append((s, e))page_safe = Truefor idx in range(len(base_coords) - 1):427 unmodified linesselection_sec = time.time() - t_selecttotal_sec = time.time() - t_start# Phase 7: Non-Text Background Line Pruningfiltered_lines_meta = []filtered_final_crops = []19 unmodified linesfiltered_lines_meta = [lines_meta[best_i]]filtered_final_crops = [final_crops[best_i]]lines_meta = filtered_lines_metafinal_crops = filtered_final_crops410 unmodified lines4114124134144154164174188 unmodified lines427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458427 unmodified lines88688788888989089189219 unmodified lines912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950410 unmodified linesfor i in range(1, num_labels):x, y, w, h, area = stats[i]# Ignore individual tiny noise or massive image-wide background blobsif area < 5 or h < 3 or w < 3 or area > 0.25 * img_h * img_w or h > 0.6 * img_h:continuecy = y + h / 2assigned_lines = []8 unmodified linesfor l_idx in assigned_lines:line_components[l_idx].add(i)valid_base_coords = []valid_line_components = []true_core_coords = []for idx, (s, e) in enumerate(base_coords):comps = line_components[idx]total_area = sum(stats[i][4] for i in comps) if comps else 0# If line has no text components or total ink area is below minimum threshold, skip empty background lineif not comps or total_area < 80:continuecore_s = min(stats[i][1] for i in comps)core_e = max(stats[i][1] + stats[i][3] for i in comps)valid_base_coords.append((s, e))valid_line_components.append(comps)true_core_coords.append((core_s, core_e))# Fallback safeguard: If all lines were filtered out, preserve original base_coordsif not true_core_coords:valid_base_coords = list(base_coords)valid_line_components = list(line_components)true_core_coords = [(s, e) for s, e in base_coords]base_coords = valid_base_coordsline_components = valid_line_componentspage_safe = Truefor idx in range(len(base_coords) - 1):427 unmodified linesselection_sec = time.time() - t_selecttotal_sec = time.time() - t_start# Phase 7: Non-Text Background Line Pruning & 1D Vertical NMSfiltered_lines_meta = []filtered_final_crops = []19 unmodified linesfiltered_lines_meta = [lines_meta[best_i]]filtered_final_crops = [final_crops[best_i]]# 1D Vertical Non-Maximum Suppression (NMS) to eliminate overlapping border linesif len(filtered_lines_meta) > 1:indexed_items = []for meta, crop in zip(filtered_lines_meta, filtered_final_crops):sel_type = meta.get("selected_crop_type", "safe")cand_sel = meta["candidates"].get(sel_type, {})conf = cand_sel.get("confidence", 0.0)box = meta["core_box"]indexed_items.append((conf, box, meta, crop))indexed_items.sort(key=lambda x: x[0], reverse=True)nms_kept = []for conf, box, meta, crop in indexed_items:s1, e1 = boxh1 = max(1, e1 - s1)keep = Truefor k_conf, k_box, _, _ in nms_kept:s2, e2 = k_boxh2 = max(1, e2 - s2)inter_s = max(s1, s2)inter_e = min(e1, e2)inter = max(0, inter_e - inter_s)iom = inter / min(h1, h2)if iom > 0.25:keep = Falsebreakif keep:nms_kept.append((conf, box, meta, crop))nms_kept.sort(key=lambda x: x[1][0])filtered_lines_meta = [x[2] for x in nms_kept]filtered_final_crops = [x[3] for x in nms_kept]lines_meta = filtered_lines_metafinal_crops = filtered_final_crops
Dockerfile+1-1
ocr_webapp/app.py+59-8