Skip to main content

akaOCR Package Tools

Project description

akaOCR

License Python 3.7 ONNX Compatible

Description

This package is compatible with akaOCR for ocr pipeline program (Text Detection & Text Recognition), using ONNX format model (CPU speed can be x2 times faster). This code is referenced from this awesome repo.

Installation

You can install the package using pip:

pip install akaocr

Usage

Step 1: Text Detection.

from akaocr import BoxEngine
import cv2

img_path = "path/to/image.jpg"
image = cv2.imread(img_path)

# side_len: minimum inference image size
box_engine = BoxEngine(model_path: Any | None = None,
                        side_len: int = 736,
                        conf_thres: float = 0.5)

# inference for one image
bboxs = box_engine(image) # [np.array([4 points]),...]

Step 2: Text Recognition.

from akaocr import TextEngine
import cv2

img_path = "path/to/cropped_image.jpg"
cropped_image = cv2.imread(img_path)

text_engine = TextEngine(model_path: Any | None = None,
                        vocab_path: Any | None = None,
                        use_space_char: bool = True,
                        model_shape: list = [3, 48, 320],
                        max_wh_ratio: int = None,
                        device: str='cpu | gpu')

# inference for one or more images
texts = text_engine(cropped_image) # [(text, conf),...]

Step 3: Perspective Transform Image.

import numpy as np

def transform_image(image, box):
    # Get perspective transform image

    assert len(box) == 4, "Shape of points must be 4x2"
    img_crop_width = int(
        max(
            np.linalg.norm(box[0] - box[1]),
            np.linalg.norm(box[2] - box[3])))
    img_crop_height = int(
        max(
            np.linalg.norm(box[0] - box[3]),
            np.linalg.norm(box[1] - box[2])))
    pts_std = np.float32([[0, 0], 
                        [img_crop_width, 0],
                        [img_crop_width, img_crop_height],
                        [0, img_crop_height]])
    M = cv2.getPerspectiveTransform(box, pts_std)
    dst_img = cv2.warpPerspective(
        image,
        M, (img_crop_width, img_crop_height),
        borderMode=cv2.BORDER_REPLICATE,
        flags=cv2.INTER_CUBIC)
    
    img_height, img_width = dst_img.shape[0:2]
    if img_height/img_width >= 1.25:
            dst_img = np.rot90(dst_img, k=3)

    return dst_img

Step 4: Use in your code: OCR Pipeline.

from akaocr import TextEngine
from akaocr import BoxEngine

import cv2

box_engine = BoxEngine()
text_engine = TextEngine()

def main():
    img_path = "sample.png"
    org_image = cv2.imread(img_path)
    images = []

    boxes = box_engine(org_image)
    for box in boxes:
        # crop & transform image
        image = transform_image(org_image, box)
        images.append(image)

    texts = text_engine(images)

if __name__ == '__main__':
    main()

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

akaocr-2.0.6.tar.gz (10.4 MB view hashes)

Uploaded Source

Built Distribution

akaocr-2.0.6-py3-none-any.whl (10.4 MB view hashes)

Uploaded Python 3

Supported by

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