Skip to main content

Use SAM2 to extract and warp a page from a photo.

Project description

Page Extractor

Extracts a page from a photo, and warps it to a rectangular image using skimage.

Features

  • Remove margin clutter from photos of pages, usually improved document processing.
  • GroundingDINO detection model integration.
  • SAM 2.1
  • Customizable text prompt, e.g. to "receipt." or "invoice."

Getting Started

Prerequisites

  • Python 3.10 or higher

Installation

Installing PyTorch Dependencies

Before installing pageextractor, install PyTorch:

pip install torch==2.4.1 torchvision==0.19.1 --extra-index-url https://download.pytorch.org/whl/cu124

Installation options

pip install -U git+https://github.com/UG-Team-Data-Science/pageextractor.git

Or:

git clone https://github.com/UG-Team-Data-Science/pageextractor && cd pageextractor
pip install -e .

Usage

from PIL import Image
from matplotlib import pyplot as plt

from pageextractor import PageExtractor

img = Image.open('example.png')
model = PageExtractor(sam_type='sam2.1_hiera_tiny', device='cuda')
mask, polygon, cropped = model.extract_page(img)

_, (ax0, ax1, ax2) = plt.subplots(1, 3, figsize=(30, 15))
ax0.imshow(img)

ax1.imshow(img)
ax1.plot(*polygon[[0,1,2,3,0]].T, 'r:')
ax1.imshow(1-mask, cmap='Blues', alpha=0.8 - 0.8*mask)

ax2.imshow(cropped)

Examples

Photo of a book page, unfortunately centerfold and page block are included

Photo of a book page, unfortunately centerfold and page block are included

Photo of a letter

Photo of a letter

Acknowledgments

This project is based on/used the following repositories:

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pageextractor-0.1.8.tar.gz (10.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pageextractor-0.1.8-py3-none-any.whl (10.7 kB view details)

Uploaded Python 3

File details

Details for the file pageextractor-0.1.8.tar.gz.

File metadata

  • Download URL: pageextractor-0.1.8.tar.gz
  • Upload date:
  • Size: 10.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.13

File hashes

Hashes for pageextractor-0.1.8.tar.gz
Algorithm Hash digest
SHA256 5e1cd8944907a5aad413aa7c84440fb15f0b0a01c0dba1a8594726a4bc549086
MD5 b47d8c029766e1792e1e2d2c872bf7ca
BLAKE2b-256 93c64b1c0d3738df2484bf888647a67209b5a589d21f657a9c88d53b2334d274

See more details on using hashes here.

File details

Details for the file pageextractor-0.1.8-py3-none-any.whl.

File metadata

File hashes

Hashes for pageextractor-0.1.8-py3-none-any.whl
Algorithm Hash digest
SHA256 655e4f057e37512c83e73d3415c78eed1c2af9c26e39793ebf5a53bbb9aebc7c
MD5 839cd5de91e946339f8f81d248c0a94e
BLAKE2b-256 d1669237564f9d2760fcc4d9530a30388fecaf8cddac0250d24a6d374d1f4bad

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page