This commit is contained in:
saqib mir
2026-08-01 16:31:55 +05:30
parent 709e61b6a5
commit f89ccfbe5d
2 changed files with 38 additions and 3 deletions
+8 -3
View File
@@ -25,8 +25,13 @@ def recognize_image_bytes(image_bytes: bytes) -> Dict[str, Any]:
img = Image.open(io.BytesIO(image_bytes))
width, height = img.size
# Run RapidOCR
result, _ = _ocr_engine(image_bytes)
# Run RapidOCR with parameters tuned for numbers, single digits, isolated symbols (- / .), and table cells
result, _ = _ocr_engine(
image_bytes,
box_thresh=0.08,
text_score=0.08,
unclip_ratio=2.5,
)
lines = []
if result:
@@ -81,7 +86,7 @@ def recognize_image_bytes(image_bytes: bytes) -> Dict[str, Any]:
}
def process_pdf_page_ocr(doc: Any, page_index: int, dpi: int = 150) -> Dict[str, Any]:
def process_pdf_page_ocr(doc: Any, page_index: int, dpi: int = 200) -> Dict[str, Any]:
if not is_ocr_available():
raise RuntimeError("RapidOCR engine is not available")
+30
View File
@@ -0,0 +1,30 @@
import io
from PIL import Image, ImageDraw, ImageFont
from rapidocr_onnxruntime import RapidOCR
# Create synthetic table image with single numbers 1, 2, 3, 4, 5, 6, 7, 8
img = Image.new("RGB", (300, 400), color=(255, 255, 255))
draw = ImageDraw.Draw(img)
draw.text((20, 20), "#", fill=(0, 0, 0))
draw.text((80, 20), "Item Description", fill=(0, 0, 0))
numbers = ["1", "2", "3", "4", "5", "6", "7", "8"]
y = 50
for num in numbers:
draw.text((20, y), num, fill=(0, 0, 0))
draw.text((80, y), f"Item {num}", fill=(0, 0, 0))
y += 35
buf = io.BytesIO()
img.save(buf, format="PNG")
png_bytes = buf.getvalue()
from app.services.ocr import recognize_image_bytes
print("--- Testing Gateway recognize_image_bytes with tuned number parameters ---")
res = recognize_image_bytes(png_bytes)
for line in res["lines"]:
safe_txt = repr(line["text"]).encode('ascii', 'backslashreplace').decode('ascii')
print(" Detected:", safe_txt, "| box:", line["box"], "| score:", line["confidence"])