LayoutParser
LayoutParser is a unified toolkit for Deep Learning Based Document Image Analysis, providing a comprehensive set of tools for tasks like document layout detection, OCR, and visualization. It is currently at version 0.3.4 and maintains an active development cycle with regular patch releases and significant minor/major updates that introduce new models and backend support.
Common errors
-
ModuleNotFoundError: No module named 'detectron2'
cause You attempted to use a Detectron2-based model (e.g., `Detectron2LayoutModel` or a model requiring Detectron2 via `AutoLayoutModel`) without installing the `detectron2` dependency.fixInstall LayoutParser with the Detectron2 extras: `pip install 'layoutparser[detectron2]'` or `pip install 'layoutparser[all]'`. -
FileNotFoundError: [Errno 2] No such file or directory: 'tesseract'
cause The Python `pytesseract` library is installed, but the Tesseract OCR executable is not found in your system's PATH.fixInstall the Tesseract OCR engine on your operating system (e.g., `brew install tesseract` on macOS, or see Tesseract's official documentation for other OSes) and ensure it's added to your system's PATH environment variable. -
AttributeError: 'NoneType' object has no attribute 'detect'
cause This usually happens when a layout model fails to load correctly, resulting in the model object being `None`. Common reasons include incorrect `model_path` (or `config_path`), missing dependencies for the chosen backend, or a corrupted model cache.fixDouble-check your `model_path` string for typos. Ensure all necessary dependencies for that specific model backend (e.g., `detectron2` for Detectron2 models) are installed using the correct `layoutparser` extras. Clear the LayoutParser model cache if you suspect corruption (usually in `~/.cache/layoutparser`).
Warnings
- gotcha LayoutParser relies on various deep learning backends and OCR engines. The minimal `pip install layoutparser` only installs core dependencies. For full functionality (e.g., using Detectron2 models or Tesseract OCR), you must install with 'extras' like `layoutparser[all]`, `layoutparser[detectron2]`, or `layoutparser[tesseract]`.
- breaking Starting from v0.3.0, LayoutParser introduced `AutoLayoutModel` for multi-backend support. While direct `Detectron2LayoutModel` usage is still possible, `AutoLayoutModel` is the recommended and more flexible way to load models. Using older explicit model classes might require more configuration or become less idiomatic.
- gotcha When using `TesseractAgent` for OCR, the Tesseract OCR engine must be installed on your system (not just the Python `pytesseract` package) and added to your system's PATH. This is a common oversight.
Install
-
pip install layoutparser -
pip install 'layoutparser[all]' -
pip install 'layoutparser[detectron2]' 'layoutparser[tesseract]'
Imports
- layoutparser
import layoutparser as lp
- Image
from PIL import Image
from layoutparser import Image
- AutoLayoutModel
from layoutparser import AutoLayoutModel
- Detectron2LayoutModel
from layoutparser import Detectron2LayoutModel
- TesseractAgent
from layoutparser import TesseractAgent
- draw_box
from layoutparser import draw_box
Quickstart
import layoutparser as lp
from PIL import Image
import io
import requests
# Download a sample image
image_url = "https://layout-parser.github.io/assets/images/publaynet.png"
response = requests.get(image_url)
image_bytes = io.BytesIO(response.content)
# Load the image using PIL, then convert to layoutparser.Image
pil_image = Image.open(image_bytes)
lp_image = lp.Image(pil_image)
# Load a pre-trained layout model (using AutoLayoutModel since v0.3.0+)
# Requires 'layoutparser[detectron2]' installed.
model = lp.AutoLayoutModel(model_path="lp://PubLayNet/faster_rcnn_R_50_FPN_3x/config")
# Detect the layout
layout = model.detect(lp_image)
# Print detected blocks and their types
print(f"Detected {len(layout)} blocks:")
for block in layout:
print(f" - Type: {block.type}, Box: {block.coordinates}")
# (Optional) Visualize the layout
# You might need matplotlib for this to display the image
# import matplotlib.pyplot as plt
# fig = lp.draw_box(lp_image, layout, box_width=3)
# plt.imshow(fig)
# plt.show()