Document Extraction Inference API using DeepLabV3 with Pretrain Model
Project description
Autocrop_kh
Automatic Document Segmentation and Cropping for Khmer IDs, Passport and Documents
Autocrop_kh is a Python package for automatic document segmentation and cropping, with a focus on Khmer IDs, Passport and other documents. It uses a DeepLabV3 model training on Khmer ID, Passport document datasets to accurately segment and extract documents from images.
License: Apache-2.0 License
Installation
Install from source
# clone repo
git clone https://github.com/MetythornPenn/autocrop_kh.git
# install lib from source
pip install -e .
Install from PyPI
pip install autocrop_kh
Usage
Python Script
import torch
import cv2
import requests
import os
from autocrop_kh import autocrop
# Function to download files from URLs
def download_file(url, local_filename):
with requests.get(url, stream=True) as r:
r.raise_for_status()
with open(local_filename, 'wb') as f:
for chunk in r.iter_content(chunk_size=8192):
f.write(chunk)
print(f"Downloaded: {local_filename}")
# URLs for the image and model
img_url = "https://github.com/MetythornPenn/autocrop_kh/raw/main/sample/img-1.jpg"
model_url = "https://github.com/MetythornPenn/autocrop/raw/main/models_kh/autocrop_model_v2.onnx"
# Local paths to save the files
img_path = "img-1.jpg"
model_path = "autocrop_model_v2.onnx"
# Download the image and model files
download_file(img_url, img_path)
download_file(model_url, model_path)
# Verify the files are correctly downloaded
if not os.path.exists(img_path):
raise FileNotFoundError(f"Image file {img_path} was not found.")
if not os.path.exists(model_path):
raise FileNotFoundError(f"Model file {model_path} was not found.")
# Specify device (CPU or CUDA or Apple Silicon GPU)
if torch.cuda.is_available():
device = "cuda" # Use NVIDIA GPU (if available)
elif torch.backends.mps.is_available():
device = "mps" # Use Apple Silicon GPU (if available)
else:
device = "cpu" # Default to CPU if no GPU is available
# Perform document extraction
extracted_document = autocrop(img_path=img_path, model_path=model_path, device=device)
# Save the extracted document
output_path = "extracted_document.jpg"
cv2.imwrite(output_path, extracted_document[:, :, ::-1]) # Convert back to BGR for saving
print(f"Extracted document saved to {output_path}")
img_path
: Path of the input image file.model_path
: Path to the pre-trained model (local path and support both .onnx and .pth).device
: Specifycpu
orcuda
ormps
(default isgpu
).output_path
: Path where the extracted document image will be saved.
Result:
Running as API & Web
# clone repo
git clone https://github.com/MetythornPenn/autocrop_kh.git
# go to directory
cd autocrop
# install libraries
pip3 install -r requirements.txt
# start server (http://localhost:5555/docs)
make server
# start client ((http://localhost:7860))
make client
Noted : This model was trained with 25000 datasets include opensource data and my custom synthetic data.
Reference
- Inspired by DeepLabV3
- Publish python package to PyPI
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
autocrop_kh-0.0.2.tar.gz
(9.5 kB
view details)
Built Distribution
File details
Details for the file autocrop_kh-0.0.2.tar.gz
.
File metadata
- Download URL: autocrop_kh-0.0.2.tar.gz
- Upload date:
- Size: 9.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d8a2ba34beec1380894f44d01af87b600065b8680aa94793611ba37d029fedb2 |
|
MD5 | b37f6b8b08202e124542febe60330ebb |
|
BLAKE2b-256 | bff20454bac4bc20b0394523f6b7e396250cda78673aab9333c8cbc9a6929b64 |
File details
Details for the file autocrop_kh-0.0.2-py3-none-any.whl
.
File metadata
- Download URL: autocrop_kh-0.0.2-py3-none-any.whl
- Upload date:
- Size: 9.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2f830dd919bfef473f32d9d26e1b2e3161dc1b18ecc47d18e0d22164fb322e8e |
|
MD5 | 41f1c616dffa91db3b60c141a575e6b4 |
|
BLAKE2b-256 | a5bd39c4eed4ee6a16a78be12997592225c529bf561ab47f10ddefce13e25fd8 |