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)
Acknowledgments
This project is based on/used the following repositories:
Project details
Release history Release notifications | RSS feed
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.1.tar.gz
(8.6 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pageextractor-0.1.1.tar.gz.
File metadata
- Download URL: pageextractor-0.1.1.tar.gz
- Upload date:
- Size: 8.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8c4476160ded6b0c80e69f1a83ddbee538a193fc7727f58ada28b71366d3ae1b
|
|
| MD5 |
783c42ee52839b0e956099a650cf50e6
|
|
| BLAKE2b-256 |
04963a1618da49e2eb7b4e560c990cf9c9018a09533426c512bceac6c463f3f3
|
File details
Details for the file pageextractor-0.1.1-py3-none-any.whl.
File metadata
- Download URL: pageextractor-0.1.1-py3-none-any.whl
- Upload date:
- Size: 9.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
833e510f23b6a8ec42e66961d268069fee23c4fb11fd4d983823a0e7d15b3987
|
|
| MD5 |
dda4348b65d9cdd2fc3bae980f1fc04b
|
|
| BLAKE2b-256 |
510fe32f68a12d2e8cb67d86430519b4f297633aeca6bc0b44234a965276853d
|