autocrop-kh


Nameautocrop-kh JSON
Version 0.0.3 PyPI version JSON
download
home_pagehttps://github.com/MetythornPenn/autocrop_kh.git
SummaryDocument Extraction Inference API using DeepLabV3 with Pretrain Model
upload_time2024-10-24 09:16:30
maintainerNone
docs_urlNone
authorMetythorn Penn
requires_pythonNone
licenseApache Software License 2.0
keywords autocrop_kh
VCS
bugtrack_url
requirements torch torchvision numpy opencv-python-headless fastapi request certifi
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 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](https://github.com/MetythornPenn/sdab/blob/main/LICENSE)

## Installation

#### Install from source

```sh

# clone repo 
git clone https://github.com/MetythornPenn/autocrop_kh.git

# install lib from source
pip install -e .

```

#### Install from PyPI
```sh
pip install autocrop-kh
```

## Usage

#### Python Script

```python
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_kh/raw/main/models/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`: Specify `cpu` or `cuda` or `mps` (default is `gpu`).
- `output_path`: Path where the extracted document image will be saved.

#### Result:

<p align="center">
  <img src="sample/img-1.jpg" alt="Left Image" width="45%">
  <img src="sample/result-img-1.png" alt="Right Image" width="45%">
</p>

<p align="center">
  <img src="sample/img-5.png" alt="Left Image" width="45%">
  <img src="sample/result-img-5.png" alt="Right Image" width="45%">
</p>


#### Running as API & Web
```sh
# 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](https://paperswithcode.com/method/deeplabv3)
- [Publish python package to PyPI](https://www.youtube.com/watch?v=90PWQEc--6k)


            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/MetythornPenn/autocrop_kh.git",
    "name": "autocrop-kh",
    "maintainer": null,
    "docs_url": null,
    "requires_python": null,
    "maintainer_email": null,
    "keywords": "autocrop_kh",
    "author": "Metythorn Penn",
    "author_email": "metythorn@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/90/26/38631b9f863a44818d0c6d03502967d51991d89ad673febf505b3c6e6f09/autocrop_kh-0.0.3.tar.gz",
    "platform": null,
    "description": "# Autocrop_kh\n\n#### Automatic Document Segmentation and Cropping for Khmer IDs, Passport and Documents\n\nAutocrop_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.\n\nLicense: [Apache-2.0 License](https://github.com/MetythornPenn/sdab/blob/main/LICENSE)\n\n## Installation\n\n#### Install from source\n\n```sh\n\n# clone repo \ngit clone https://github.com/MetythornPenn/autocrop_kh.git\n\n# install lib from source\npip install -e .\n\n```\n\n#### Install from PyPI\n```sh\npip install autocrop-kh\n```\n\n## Usage\n\n#### Python Script\n\n```python\nimport torch\nimport cv2\nimport requests\nimport os\nfrom autocrop_kh import autocrop\n\n# Function to download files from URLs\ndef download_file(url, local_filename):\n    with requests.get(url, stream=True) as r:\n        r.raise_for_status()\n        with open(local_filename, 'wb') as f:\n            for chunk in r.iter_content(chunk_size=8192):\n                f.write(chunk)\n    print(f\"Downloaded: {local_filename}\")\n\n# URLs for the image and model\nimg_url = \"https://github.com/MetythornPenn/autocrop_kh/raw/main/sample/img-1.jpg\"\nmodel_url = \"https://github.com/MetythornPenn/autocrop_kh/raw/main/models/autocrop_model_v2.onnx\"\n\n# Local paths to save the files\nimg_path = \"img-1.jpg\"\nmodel_path = \"autocrop_model_v2.onnx\"\n\n# Download the image and model files\ndownload_file(img_url, img_path)\ndownload_file(model_url, model_path)\n\n# Verify the files are correctly downloaded\nif not os.path.exists(img_path):\n    raise FileNotFoundError(f\"Image file {img_path} was not found.\")\nif not os.path.exists(model_path):\n    raise FileNotFoundError(f\"Model file {model_path} was not found.\")\n\n# Specify device (CPU or CUDA or Apple Silicon GPU)\nif torch.cuda.is_available():\n    device = \"cuda\"  # Use NVIDIA GPU (if available)\nelif torch.backends.mps.is_available():\n    device = \"mps\"  # Use Apple Silicon GPU (if available)\nelse:\n    device = \"cpu\"  # Default to CPU if no GPU is available\n\n# Perform document extraction\nextracted_document = autocrop(img_path=img_path, model_path=model_path, device=device)\n\n# Save the extracted document\noutput_path = \"extracted_document.jpg\"\ncv2.imwrite(output_path, extracted_document[:, :, ::-1])  # Convert back to BGR for saving\n\nprint(f\"Extracted document saved to {output_path}\")\n\n\n```\n\n- `img_path`: Path of the input image file.\n- `model_path`: Path to the pre-trained model (local path and support both .onnx and .pth).\n- `device`: Specify `cpu` or `cuda` or `mps` (default is `gpu`).\n- `output_path`: Path where the extracted document image will be saved.\n\n#### Result:\n\n<p align=\"center\">\n  <img src=\"sample/img-1.jpg\" alt=\"Left Image\" width=\"45%\">\n  <img src=\"sample/result-img-1.png\" alt=\"Right Image\" width=\"45%\">\n</p>\n\n<p align=\"center\">\n  <img src=\"sample/img-5.png\" alt=\"Left Image\" width=\"45%\">\n  <img src=\"sample/result-img-5.png\" alt=\"Right Image\" width=\"45%\">\n</p>\n\n\n#### Running as API & Web\n```sh\n# clone repo\ngit clone https://github.com/MetythornPenn/autocrop_kh.git\n\n# go to directory\ncd autocrop\n\n# install libraries\npip3 install -r requirements.txt\n\n# start server (http://localhost:5555/docs)\nmake server\n\n# start client ((http://localhost:7860))\nmake client \n\n```\n**Noted** : This model was trained with 25000 datasets include opensource data and my custom synthetic data.\n## Reference \n- Inspired by [DeepLabV3](https://paperswithcode.com/method/deeplabv3)\n- [Publish python package to PyPI](https://www.youtube.com/watch?v=90PWQEc--6k)\n\n",
    "bugtrack_url": null,
    "license": "Apache Software License 2.0",
    "summary": "Document Extraction Inference API using DeepLabV3 with Pretrain Model",
    "version": "0.0.3",
    "project_urls": {
        "Homepage": "https://github.com/MetythornPenn/autocrop_kh.git"
    },
    "split_keywords": [
        "autocrop_kh"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "e353fe1f5a524f3f5fb788b010ef442a7b7cd3b516320b7df0eedc8bdded6a52",
                "md5": "1bdbb32e996e19c4495e5c51f897e70e",
                "sha256": "eb4f35b9a6c5272db8d4bb5ee2f80ac342228d0cb9f29a1b4a2aa2bc2a8e865a"
            },
            "downloads": -1,
            "filename": "autocrop_kh-0.0.3-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "1bdbb32e996e19c4495e5c51f897e70e",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 9514,
            "upload_time": "2024-10-24T09:16:29",
            "upload_time_iso_8601": "2024-10-24T09:16:29.027190Z",
            "url": "https://files.pythonhosted.org/packages/e3/53/fe1f5a524f3f5fb788b010ef442a7b7cd3b516320b7df0eedc8bdded6a52/autocrop_kh-0.0.3-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "902638631b9f863a44818d0c6d03502967d51991d89ad673febf505b3c6e6f09",
                "md5": "b24eab759f1a5d7e1c484d15ce4ea777",
                "sha256": "4af7de8c7edd9c2a8420dcc1e9bae1d899c6e8158d06b2bc3edfd13e7dc20607"
            },
            "downloads": -1,
            "filename": "autocrop_kh-0.0.3.tar.gz",
            "has_sig": false,
            "md5_digest": "b24eab759f1a5d7e1c484d15ce4ea777",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 9505,
            "upload_time": "2024-10-24T09:16:30",
            "upload_time_iso_8601": "2024-10-24T09:16:30.386105Z",
            "url": "https://files.pythonhosted.org/packages/90/26/38631b9f863a44818d0c6d03502967d51991d89ad673febf505b3c6e6f09/autocrop_kh-0.0.3.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-10-24 09:16:30",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "MetythornPenn",
    "github_project": "autocrop_kh",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [
        {
            "name": "torch",
            "specs": []
        },
        {
            "name": "torchvision",
            "specs": []
        },
        {
            "name": "numpy",
            "specs": []
        },
        {
            "name": "opencv-python-headless",
            "specs": []
        },
        {
            "name": "fastapi",
            "specs": []
        },
        {
            "name": "request",
            "specs": []
        },
        {
            "name": "certifi",
            "specs": []
        }
    ],
    "lcname": "autocrop-kh"
}
        
Elapsed time: 0.80247s