run-llama / llama_index

LlamaIndex is a data framework for your LLM applications
https://docs.llamaindex.ai
MIT License
36.62k stars 5.24k forks source link

[Question]: How to parse images #15163

Open cxycxm opened 3 months ago

cxycxm commented 3 months ago

Question Validation

Question

How to parse images,examle .jpg

dosubot[bot] commented 3 months ago

To parse .jpg files, you can use the ImageReader from the llama_index.legacy.readers.file module. Here is a code snippet that demonstrates how to set up the file extractor to handle .jpg files:

from PIL import Image
from llama_index.legacy.readers.file import (
    DEFAULT_FILE_EXTRACTOR,
    ImageReader,
)

@st.cache_resource
def get_file_extractor():
    image_reader = ImageReader(keep_image=True, parse_text=True)
    file_extractor = DEFAULT_FILE_EXTRACTOR
    file_extractor.update(
        {
            ".jpg": image_reader,
            ".png": image_reader,
            ".jpeg": image_reader,
        }
    )

    return file_extractor

file_extractor = get_file_extractor()

This code sets up an ImageReader that can handle .jpg, .png, and .jpeg files, and integrates it into the default file extractor. The ImageReader can extract text from images using DONUT or pytesseract, and it can also keep the image in the document if needed [1][2][3].

To continue talking to Dosu, mention @dosu.

cxycxm commented 3 months ago

ImportError: cannot import name 'DEFAULT_FILE_EXTRACTOR' from 'llama_index.legacy.readers.file'

dosubot[bot] commented 3 months ago

To resolve the ImportError: cannot import name 'DEFAULT_FILE_EXTRACTOR' from 'llama_index.legacy.readers.file', you should ensure that you are importing the ImageReader correctly. The correct import statement should be:

from llama_index.readers.file.image import ImageReader

This should resolve the ImportError as the ImageReader is correctly defined in the llama_index/readers/file/image/__init__.py file [1].

To continue talking to Dosu, mention @dosu.