how to extract data from an image
it is detecting image but it is not extracting the data from the image
To extract data from an image in Docling, make sure your pipeline is set up to run OCR after image detection. If image detection works but data extraction doesn't, check these common issues:
- The required OCR engine (like Tesseract, RapidOCR, or EasyOCR) must be installed and properly configured. For Tesseract, you need the
tesserocrPython package and the correct language models; for RapidOCR, installrapidocr_onnxruntimeexample. - Make sure the OCR engine is enabled in your pipeline options (e.g.,
do_ocr=TrueinPdfPipelineOptions) and thatocr_optionsare set for your preferred engine example. - If using Tesseract, confirm that the
TESSDATA_PREFIXenvironment variable points to the directory with your language models. - Check for error messages or warnings in your logs—missing dependencies or misconfigured options will cause the pipeline to skip extraction and log a warning or error.
- OCR options like
force_full_page_ocrandbitmap_area_thresholdaffect how and when OCR is applied. If your images are not being processed, try adjusting these settings reference.
If OCR is running but you still don't see extracted text, it could be due to layout segmentation or content analysis classifying the image as a picture rather than text, or the OCR result being incomplete or low quality see discussion.
Check your pipeline configuration, dependencies, and logs—these are the most common sources of this issue.
To reply, just mention @dosu.
Docs are dead. Just use Dosu.
give a code snippet to extract text from an image in a pdf give me clear steps
Here's a minimal example for extracting text from images in a PDF using Docling. This uses the EasyOCR engine, but you can swap in Tesseract or RapidOCR by changing the ocr_options import and instantiation.
from docling.datamodel.pipeline_options import PdfPipelineOptions, EasyOcrOptions
from docling.document_converter import DocumentConverter, PdfFormatOption, InputFormat
# Make sure you have the required OCR engine installed, e.g.:
# pip install easyocr
# (or tesserocr, rapidocr_onnxruntime, etc.)
# Configure pipeline options for OCR
pipeline_options = PdfPipelineOptions(
do_ocr=True,
ocr_options=EasyOcrOptions(lang=["en"]) # Set your language(s) here
)
# Set up the converter for PDF input
converter = DocumentConverter(
format_options={
InputFormat.PDF: PdfFormatOption(pipeline_options=pipeline_options)
}
)
# Run the conversion
result = converter.convert("your_file.pdf") # Replace with your PDF path
# Extract and print OCR text from all pages and items
for page in result.document.pages.values():
for item in page.iterate_items():
if hasattr(item, "text") and item.text:
print(item.text)
You can use TesseractOcrOptions() or RapidOcrOptions() instead of EasyOcrOptions() if you prefer another engine—just make sure the corresponding Python package is installed. For more on OCR engine options and parameters, see the pipeline options source.
If you want to extract only text from images (not all text), you may need to filter for items of type PictureItem or similar, depending on your output structure.
To reply, just mention @dosu.
Share context across your team and agents. Try Dosu.
AttributeError Traceback (most recent call last) /tmp/ipython-input-2054324832.py in <cell line: 0>() 24 # Extract and print OCR text from all pages and items 25 for page in result.document.pages.values(): ---> 26 for item in page.iterate_items(): 27 if hasattr(item, "text") and item.text: 28 print(item.text)
/usr/local/lib/python3.11/dist-packages/pydantic/main.py in getattr(self, item) 989 else: 990 # this is the current error --> 991 raise AttributeError(f'{type(self).name!r} object has no attribute {item!r}') 992 993 def setattr(self, name: str, value: Any) -> None:
AttributeError: 'PageItem' object has no attribute 'iterate_items' i have used the same code that you have provided and im getting this error please makesure i dont get any errors
The error happens because PageItem doesn't have an iterate_items() method. Instead, you should use result.document.iterate_items() to walk through all items in the document. To extract OCR text from images in a PDF, filter for PictureItem (for images) or TextItem (for OCR text blocks). Here’s a corrected example:
from docling.datamodel.pipeline_options import PdfPipelineOptions, EasyOcrOptions
from docling.document_converter import DocumentConverter, PdfFormatOption, InputFormat
from docling_core.types.doc.document import PictureItem, TextItem # adjust import if needed
pipeline_options = PdfPipelineOptions(
do_ocr=True,
ocr_options=EasyOcrOptions(lang=["en"])
)
converter = DocumentConverter(
format_options={
InputFormat.PDF: PdfFormatOption(pipeline_options=pipeline_options)
}
)
result = converter.convert("your_file.pdf")
# To get all OCR text from images:
for element, _level in result.document.iterate_items():
if isinstance(element, TextItem) and element.text:
print(element.text)
If you want only OCR text from images, you can first filter for PictureItem and then access their annotations or children, depending on your pipeline setup. The key is to always iterate at the document level, not per page—this matches Docling’s data model design see example. If you want to restrict to a specific page, use result.document.iterate_items(page_no=desired_page_number).
To reply, just mention @dosu.
Share context across your team and agents. Try Dosu.