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

Add support for ImageElements -> Parse images #64

Merged
merged 7 commits into from
Sep 24, 2024

Conversation

ic-xu
Copy link
Contributor

@ic-xu ic-xu commented Sep 12, 2024

The images in the PDF were lost after parsing before. Now RAG needs to use images, so I added the image extraction function.

image

@ic-xu ic-xu changed the title Parse images Add support for ImageElements -> Parse images Sep 12, 2024
@Filimoa
Copy link
Owner

Filimoa commented Sep 12, 2024

Thanks for the PR - this looks awesome! I will look a take a deeper look into this asap

@ic-xu
Copy link
Contributor Author

ic-xu commented Sep 13, 2024

Okay, if you have any needs, please communicate in time. I will be happy to communicate with you.

@Filimoa
Copy link
Owner

Filimoa commented Sep 17, 2024

PdfMiner looks good - I changed the image schema to encode the image data in base64 str to allow for easy serialization. We also added a mime type. Finally added a test - that looks good.

Looks like the pymupdf implementation is failing for me.

import openparse

basic_doc_path = "/Users/sergey/Downloads/pdf-with-image.pdf"
pdf_obj = openparse.Pdf(basic_doc_path)
parsed_basic_doc = ingest(pdf_obj)

Returns

ValidationError: 1 validation error for Bbox
  Value error, y1 must be greater than y0 [type=value_error, input_value={'x0': 72.0, 'y0': 719.54...0, 'page_height': 792.0}, input_type=dict]
    For further information visit https://errors.pydantic.dev/2.9/v/value_error

I noticed you flipped the coordinates? Maybe why?

fy0 = page.rect.height - node["bbox"][1]
fy1 = page.rect.height - node["bbox"][3]

To be honest, I'm not even sure how important it is to have this implemented for pymupdf because those documents are already OCRd and not sure how that affects this?

@Filimoa
Copy link
Owner

Filimoa commented Sep 17, 2024

You can see the changes in the "parse-images-pdf-miner" branch - I can't seem to figure out how to merge it into this

@ic-xu
Copy link
Contributor Author

ic-xu commented Sep 18, 2024

PdfMiner looks good - I changed the image schema to encode the image data in base64 str to allow for easy serialization. We also added a mime type. Finally added a test - that looks good.

Looks like the pymupdf implementation is failing for me.

import openparse

basic_doc_path = "/Users/sergey/Downloads/pdf-with-image.pdf"
pdf_obj = openparse.Pdf(basic_doc_path)
parsed_basic_doc = ingest(pdf_obj)

Returns

ValidationError: 1 validation error for Bbox
  Value error, y1 must be greater than y0 [type=value_error, input_value={'x0': 72.0, 'y0': 719.54...0, 'page_height': 792.0}, input_type=dict]
    For further information visit https://errors.pydantic.dev/2.9/v/value_error

I noticed you flipped the coordinates? Maybe why?

fy0 = page.rect.height - node["bbox"][1]
fy1 = page.rect.height - node["bbox"][3]

To be honest, I'm not even sure how important it is to have this implemented for pymupdf because those documents are already OCRd and not sure how that affects this?

Indeed, for pymupdf, if the image has already been OCRed, then there is no need to parse the image anymore. However, I thought you would process the OCR in the process later, and only parse text in the text.

@ic-xu
Copy link
Contributor Author

ic-xu commented Sep 18, 2024

How about this, I'll submit the image parsing part for pdfminer first. As for pymupdf, I'll keep it as it is, without adding an image parsing module. Is that okay?

@Filimoa
Copy link
Owner

Filimoa commented Sep 18, 2024

Sounds good!

@Filimoa Filimoa merged commit 00f02a2 into Filimoa:main Sep 24, 2024
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants