Skip to content

Commit

Permalink
Softer OCR heuristics, enable float batch multipliers
Browse files Browse the repository at this point in the history
  • Loading branch information
VikParuchuri committed May 27, 2024
1 parent 0281aea commit 3ab7523
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 32 deletions.
2 changes: 1 addition & 1 deletion marker/layout/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def surya_layout(doc, pages: List[Page], layout_model, batch_multiplier=1):
text_detection_results = [p.text_lines for p in pages]

processor = layout_model.processor
layout_results = batch_layout_detection(images, layout_model, processor, detection_results=text_detection_results, batch_size=get_batch_size() * batch_multiplier)
layout_results = batch_layout_detection(images, layout_model, processor, detection_results=text_detection_results, batch_size=int(get_batch_size() * batch_multiplier))
for page, layout_result in zip(pages, layout_results):
page.layout = layout_result

Expand Down
2 changes: 1 addition & 1 deletion marker/layout/order.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def surya_order(doc, pages: List[Page], order_model, batch_multiplier=1):
bboxes.append(bbox)

processor = order_model.processor
order_results = batch_ordering(images, bboxes, order_model, processor, batch_size=get_batch_size() * batch_multiplier)
order_results = batch_ordering(images, bboxes, order_model, processor, batch_size=int(get_batch_size() * batch_multiplier))
for page, order_result in zip(pages, order_results):
page.order = order_result

Expand Down
2 changes: 1 addition & 1 deletion marker/ocr/detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def surya_detection(doc: PdfDocument, pages: List[Page], det_model, batch_multip
max_len = min(len(pages), len(doc))
images = [render_image(doc[pnum], dpi=settings.SURYA_DETECTOR_DPI) for pnum in range(max_len)]

predictions = batch_text_detection(images, det_model, processor, batch_size=get_batch_size() * batch_multiplier)
predictions = batch_text_detection(images, det_model, processor, batch_size=int(get_batch_size() * batch_multiplier))
for (page, pred) in zip(pages, predictions):
page.text_lines = pred

Expand Down
6 changes: 3 additions & 3 deletions marker/ocr/heuristics.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def should_ocr_page(page: Page, no_text: bool):

# OCR page if we got minimal text, or if we got too many spaces
conditions = [
no_text , # Full doc has no text, and needs full OCR
no_text, # Full doc has no text, and needs full OCR
(len(page.prelim_text) > 0 and detect_bad_ocr(page.prelim_text)), # Bad OCR
detected_lines_found is False, # didn't extract text for all detected lines
]
Expand All @@ -39,7 +39,7 @@ def detect_bad_ocr(text, space_threshold=.7, newline_threshold=.6, alphanum_thre
return True

invalid_chars = len([c for c in text if c in settings.INVALID_CHARS])
if invalid_chars > max(4.0, len(text) * .03):
if invalid_chars > max(6.0, len(text) * .03):
return True

return False
Expand All @@ -52,7 +52,7 @@ def no_text_found(pages: List[Page]):
return len(full_text.strip()) == 0


def detected_line_coverage(page: Page, intersect_thresh=.5, detection_thresh=.65):
def detected_line_coverage(page: Page, intersect_thresh=.5, detection_thresh=.4):
found_lines = 0
for detected_line in page.text_lines.bboxes:

Expand Down
2 changes: 1 addition & 1 deletion marker/ocr/recognition.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def surya_recognition(doc, page_idxs, langs: List[str], rec_model, pages: List[P
detection_results = [p.text_lines.bboxes for p in selected_pages]
polygons = [[b.polygon for b in bboxes] for bboxes in detection_results]

results = run_recognition(images, surya_langs, rec_model, processor, polygons=polygons, batch_size=get_batch_size() * batch_multiplier)
results = run_recognition(images, surya_langs, rec_model, processor, polygons=polygons, batch_size=int(get_batch_size() * batch_multiplier))

new_pages = []
for (page_idx, result, old_page) in zip(page_idxs, results, selected_pages):
Expand Down
44 changes: 22 additions & 22 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "marker-pdf"
version = "0.2.9"
version = "0.2.10"
description = "Convert PDF to markdown with high speed and accuracy."
authors = ["Vik Paruchuri <[email protected]>"]
readme = "README.md"
Expand Down Expand Up @@ -33,10 +33,10 @@ tabulate = "^0.9.0"
ftfy = "^6.1.1"
texify = "^0.1.9"
rapidfuzz = "^3.8.1"
surya-ocr = "^0.4.8"
surya-ocr = "^0.4.9"
filetype = "^1.2.0"
regex = "^2024.4.28"
pdftext = "^0.3.8"
pdftext = "^0.3.10"
grpcio = "^1.63.0"

[tool.poetry.group.dev.dependencies]
Expand Down

0 comments on commit 3ab7523

Please sign in to comment.