Skip to main content

A Python client library for OCR service

Project description

sh-ocr-client

A Python client library for accessing OCR (Optical Character Recognition) service. This library allows you to extract text from images by providing the image path and language code.

Features

  • Simple API to process images through OCR service
  • Support for multiple languages (e.g., 'japan', 'ch', 'en')
  • Returns structured text blocks with coordinates and metadata
  • API key authentication support

Installation

pip install sh-ocr-client

Requirements

  • Python 3.9 or higher
  • requests library (automatically installed as a dependency)

Usage

from sh_ocr_client import process_ocr

# Process an image
result = process_ocr(
    image_path="/path/to/your/image.png",
    language="ch",  # Language code: 'japan', 'ch', 'en', etc.
    api_key="your-api-key-here"
)

# The result is a dictionary containing:
# - success: Boolean indicating if processing was successful
# - data: Dictionary containing:
#   - text_blocks: List of extracted text blocks
#   - total_blocks: Number of text blocks found
#   - language: Detected/used language
print(result)

Parameters

  • image_path (str): Path to the image file to process
  • language (str): Language code for OCR. Supported languages include:
    • 'ch' - Chinese
    • 'japan' - Japanese
    • 'en' - English
    • And other language codes supported by the OCR service
  • api_key (str): API key for authentication (required)

Returns

Returns a dictionary with the following structure:

{
    "success": True,
    "data": {
        "text_blocks": [...],  # List of text blocks with coordinates
        "total_blocks": 10,    # Number of text blocks
        "language": "ch"       # Language used
    }
}

Error Handling

The function raises:

  • requests.RequestException: If the request fails (network issues, etc.)
  • requests.HTTPError: If the API returns an error status (e.g., 401 for invalid API key)

Example

from sh_ocr_client import process_ocr
import requests

try:
    result = process_ocr(
        image_path="menu.png",
        language="ch",
        api_key="sk-ocr-your-key-here"
    )
    
    if result.get('success'):
        text_blocks = result.get('data', {}).get('text_blocks', [])
        print(f"Found {len(text_blocks)} text blocks")
        for block in text_blocks:
            print(block.get('text', ''))
except requests.HTTPError as e:
    print(f"API Error: {e}")
except requests.RequestException as e:
    print(f"Request Error: {e}")

License

MIT License

Copyright (c) 2025 shuaronhong

Repository

https://github.com/shuaronhong/Pin_Hw09

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

sh_ocr_client-0.1.0.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

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

sh_ocr_client-0.1.0-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

Details for the file sh_ocr_client-0.1.0.tar.gz.

File metadata

  • Download URL: sh_ocr_client-0.1.0.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for sh_ocr_client-0.1.0.tar.gz
Algorithm Hash digest
SHA256 61e302f8e99e15645b13eaa562b5f3014a6e8167f82d178024a41de86a2fbdb7
MD5 45d83d73cce448c2cd68b4c6a77fbde1
BLAKE2b-256 b97cf54731163035b7875c1f887491e1ddff2b079ec32247d3bbf1e3a5744249

See more details on using hashes here.

File details

Details for the file sh_ocr_client-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: sh_ocr_client-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for sh_ocr_client-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3d0efeb2e043f210608866a1b3be2d6bf78372789a352aab092e6c1652d2f1fb
MD5 c27408e720c0abb68334c9c3eec92239
BLAKE2b-256 7394073bc96581e203851e76d2301ad1038e89dcf8ef2c279b686960e0a5cffa

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