On master: wip

This commit is contained in:
omkarande14
2026-09-29 10:38:26 +05:30
parent 10bd585f83
commit 673d181e68
8 changed files with 788 additions and 162 deletions
+30 -2
View File
@@ -39,6 +39,9 @@ except ImportError:
PADDLE_AVAILABLE = False
from app.ocr.stage_cache import OCRStageCache
class DualEngineBenchmarker:
"""
Benchmarks and compares PaddleOCR (deep learning, layout/table focus)
@@ -49,11 +52,16 @@ class DualEngineBenchmarker:
def __init__(
self,
use_gpu: bool = True,
tesseract_cmd: Optional[str] = r"C:\Program Files\Tesseract-OCR\tesseract.exe"
tesseract_cmd: Optional[str] = r"C:\Program Files\Tesseract-OCR\tesseract.exe",
stage_cache: Optional[OCRStageCache] = None,
enable_cache: bool = True
):
if tesseract_cmd and os.path.exists(tesseract_cmd):
pytesseract.pytesseract.tesseract_cmd = tesseract_cmd
self.enable_cache = enable_cache
self.stage_cache = stage_cache or (OCRStageCache() if enable_cache else None)
self.use_gpu = use_gpu and PADDLE_AVAILABLE and (
hasattr(paddle, "is_compiled_with_cuda") and paddle.is_compiled_with_cuda()
)
@@ -84,6 +92,20 @@ class DualEngineBenchmarker:
if not self.paddle_ocr:
return {"error": "PaddleOCR is not available"}
img_sha = None
if self.stage_cache and self.stage_cache.enabled:
try:
img_sha = OCRStageCache.compute_image_sha256(image_path)
cached = self.stage_cache.get(img_sha, "PaddleOCR", "v1.0")
if cached:
# Mark cached run
cached_out = dict(cached)
cached_out["device"] = "Cache (Instant)"
cached_out["runtime_seconds"] = 0.001
return cached_out
except Exception:
pass
start_time = time.time()
actual_device = "GPU" if self.use_gpu else "CPU"
try:
@@ -124,7 +146,7 @@ class DualEngineBenchmarker:
full_text = "\n".join(item["text"] for item in extracted_lines)
avg_conf = round(sum(confidences) / len(confidences), 4) if confidences else 0.0
return {
out = {
"engine": "PaddleOCR",
"device": actual_device,
"runtime_seconds": duration,
@@ -133,6 +155,12 @@ class DualEngineBenchmarker:
"extracted_lines": extracted_lines,
"full_text": full_text
}
if self.stage_cache and self.stage_cache.enabled and img_sha and extracted_lines:
try:
self.stage_cache.put(img_sha, "PaddleOCR", "v1.0", out)
except Exception:
pass
return out
def run_tesseract(self, image_path: Path) -> Dict[str, Any]:
"""Runs Tesseract OCR on a single image and collects text and word confidences."""
+5 -4
View File
@@ -190,17 +190,18 @@ class SelfHealingEngine:
assigned_gstins = {buyer.get("gstin"), vendor.get("gstin")}
unassigned = [c for c in gstin_candidates if c["gstin"] not in assigned_gstins]
all_xs = [pt[0] for l in extracted_lines for pt in l.get("bbox", [])] if extracted_lines else []
all_xs = [pt[0] for l in all_ocr_lines for pt in (l.get("bbox") or l.get("box") or [])] if all_ocr_lines else []
page_width = max(all_xs) if all_xs else 1000.0
mid_x = page_width * 0.5
all_ys = [pt[1] for l in extracted_lines for pt in l.get("bbox", [])] if extracted_lines else []
all_ys = [pt[1] for l in all_ocr_lines for pt in (l.get("bbox") or l.get("box") or [])] if all_ocr_lines else []
page_height = max(all_ys) if all_ys else 1000.0
header_y_limit = page_height * 0.35
for cand in unassigned:
g = cand["gstin"]
ymin = cand["bbox"][0][1] if cand["bbox"] and len(cand["bbox"]) > 0 else 0
xmin = cand["bbox"][0][0] if cand["bbox"] and len(cand["bbox"]) > 0 else 0
c_box = cand.get("bbox") or cand.get("box") or []
ymin = min(p[1] for p in c_box) if c_box else 0
xmin = min(p[0] for p in c_box) if c_box else 0
if not buyer.get("gstin") and (xmin > mid_x or ymin > header_y_limit):
buyer["gstin"] = g