Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug/OCR language parameters isn't passed to table model #1564

Closed
yuming-long opened this issue Sep 28, 2023 · 0 comments · Fixed by #1801
Closed

bug/OCR language parameters isn't passed to table model #1564

yuming-long opened this issue Sep 28, 2023 · 0 comments · Fixed by #1801
Assignees
Labels
bug Something isn't working

Comments

@yuming-long
Copy link
Contributor

yuming-long commented Sep 28, 2023

Describe the bug
OCR language parameters isn't passed to table model thus we can't extract texts from tables from different language

To Reproduce
use this Korean files with tables: korean_table.pdf

from unstructured.partition.pdf import partition_pdf


elements = partition_pdf(filename="/Users/yumingl/Downloads/korean_table.pdf", infer_table_structure=True, ocr_languages="kor+eng", strategy="hi_res")
tables = [el for el in elements if el.category == "Table"]
if len(tables) >= 1:
    for table in tables:
        print(table.text)
        print(table.metadata.text_as_html)

see output like:

업종 적립 대상 할인점 이마트, 이마트 트 레 이 더 스 , 롯 데 마트, 홈 플러스 주유 모든 주유소 온 라 인 쇼핑몰 심 성 카 드 쇼핑, 6 마 켓 옥션, 11 번 가, 인터파크, 삼 쿠 팡 , 티 몬 , 위 메 프
<table><thead><th>as</th><th>Ae ches</th></thead><tr><td></td><td>O/OLE, ODE SAo|yA, SHOVE, SSAA</td></tr><tr><td rowspan="2"></td><td>ATES AW GOP, SM, THI}, QIE{T ES,</td></tr><tr><td></td><td>$2, EE, Path jz</td></tr></table>

And the text_as_html doesn't have Korean compared to the text field.

Expected behavior
I notice the tesseract in table model doesn't have lang parameters passed in and i manually modified it with:

  • pip install unstructured-inference into unstructured env with pip install -e . (make install-project-local)
  • add lang="kor+eng", after this line
  • also add lang="kor+eng", after this line

and get expected result like:

업종 적립 대상 할인점 이마트, 이마트 트 레 이 더 스 , 롯 데 마트, 홈 플러스 주유 모든 주유소 온 라 인 쇼핑몰 심 성 카 드 쇼핑, 6 마 켓 옥션, 11 번 가, 인터파크, 삼 쿠 팡 , 티 몬 , 위 메 프
<table><thead><th>업종</th><th>적립 대상</th></thead><tr><td></td><td>이마트, 이마트 트 레 이 더 스 , 롯 데 마트, 홈 플러스</td></tr><tr><td rowspan="2"></td><td>삼 성 카드 쇼핑, 6 마 켓 , 옥션, 11 번 가, 인터파크,</td></tr><tr><td></td><td>쿠 팡 , 티 몬 , 위 메 프</td></tr></table>

so table model should do OCR with the lang parameter passed down from partition ocr_languages

@yuming-long yuming-long added the bug Something isn't working label Sep 28, 2023
@yuming-long yuming-long self-assigned this Oct 18, 2023
github-merge-queue bot pushed a commit that referenced this issue Oct 21, 2023
…#1801)

### Summary

Table OCR refactor, move the OCR part for table model in inference repo
to unst repo.
* Before this PR, table model extracts OCR tokens with texts and
bounding box and fills the tokens to the table structure in inference
repo. This means we need to do an additional OCR for tables.
* After this PR, we use the OCR data from entire page OCR and pass the
OCR tokens to inference repo, which means we only do one OCR for the
entire document.

**Tech details:**
* Combined env `ENTIRE_PAGE_OCR` and `TABLE_OCR` to `OCR_AGENT`, this
means we use the same OCR agent for entire page and tables since we only
do one OCR.
* Bump inference repo to `0.7.9`, which allow table model in inference
to use pre-computed OCR data from unst repo. Please check in
[PR](Unstructured-IO/unstructured-inference#256).
* All notebooks lint are made by `make tidy`
* This PR also fixes
[issue](#1564),
I've added test for the issue in
`test_pdf.py::test_partition_pdf_hi_table_extraction_with_languages`
* Add same scaling logic to image [similar to previous Table
OCR](https://github.com/Unstructured-IO/unstructured-inference/blob/main/unstructured_inference/models/tables.py#L109C1-L113),
but now scaling is applied to entire image

### Test
* Not much to manually testing expect table extraction still works
* But due to change on scaling and use pre-computed OCR data from entire
page, there are some slight (better) changes on table output, here is an
comparison on test outputs i found from the same test
`test_partition_image_with_table_extraction`:

screen shot for table in `layout-parser-paper-with-table.jpg`:
<img width="343" alt="expected"
src="https://github.com/Unstructured-IO/unstructured/assets/63475068/278d7665-d212-433d-9a05-872c4502725c">
before refactor:
<img width="709" alt="before"
src="https://github.com/Unstructured-IO/unstructured/assets/63475068/347fbc3b-f52b-45b5-97e9-6f633eaa0d5e">
after refactor:
<img width="705" alt="after"
src="https://github.com/Unstructured-IO/unstructured/assets/63475068/b3cbd809-cf67-4e75-945a-5cbd06b33b2d">

### TODO
(added as a ticket) Still have some clean up to do in inference repo
since now unst repo have duplicate logic, but can keep them as a fall
back plan. If we want to remove anything OCR related in inference, here
are items that is deprecated and can be removed:
*
[`get_tokens`](https://github.com/Unstructured-IO/unstructured-inference/blob/main/unstructured_inference/models/tables.py#L77)
(already noted in code)
* parameter `extract_tables` in inference
*
[`interpret_table_block`](https://github.com/Unstructured-IO/unstructured-inference/blob/main/unstructured_inference/inference/layoutelement.py#L88)
*
[`load_agent`](https://github.com/Unstructured-IO/unstructured-inference/blob/main/unstructured_inference/models/tables.py#L197)
* env `TABLE_OCR` 

### Note
if we want to fallback for an additional table OCR (may need this for
using paddle for table), we need to:
* pass `infer_table_structure` to inference with `extract_tables`
parameter
* stop passing `infer_table_structure` to `ocr.py`

---------

Co-authored-by: Yao You <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant