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.1.tar.gz (4.7 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.1-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: sh_ocr_client-0.1.1.tar.gz
  • Upload date:
  • Size: 4.7 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.1.tar.gz
Algorithm Hash digest
SHA256 9e2cca6df60357d07876f249228322988a65bd3be0c28366cd3465bd04b83fce
MD5 00399f5656a0aafa9ec6973dd6f80df1
BLAKE2b-256 f5bc6874fc846c2a8884a4a0503b2b8e4aa8eb95febee817c8a6854e08cf1eb8

See more details on using hashes here.

File details

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

File metadata

  • Download URL: sh_ocr_client-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 4.6 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b3a12753a2b740f9220831c453b72b1cf03d5af8ea79eaf53d9df56ab274a566
MD5 153c92fa3e3ee2b254625697c6b36acb
BLAKE2b-256 dbeb5a3c4ab4411d5859a8e25c3def8594851b26700ff1a9c54de7361ee980d2

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