ddddocr

repository·master·Indexed 12 days ago

https://github.com/sml2h3/ddddocr

A lightweight, offline Python SDK for general-purpose captcha recognition, version 1.6.1. It supports text recognition (OCR), object detection, and slider captcha processing using deep learning models. Features include GPU acceleration via onnxruntime-gpu, custom ONNX model support, and specialized methods for edge matching and image difference comparison to solve slider captchas.

Tokens
10.1K
Snippets
34
Records
40
Agent score
93%

What's inside ddddocr

  1. Input validation and error handling

    master

    The DdddOcr API enforces several constraints on input data:

    • Image Constraints: Images are validated for size, format, and dimensions. The default limit is 8192 KB and a maximum side length of 4096px. These can be customized in the local library via DdddOcr(max_image_bytes=..., max_image_side=...).
    • Supported Formats: PNG, JPEG, JPG, WEBP, BMP, GIF, TIFF.
    • Error Handling: Invalid inputs (e.g., exceeding size limits) return a 400 Bad Request. The core library uses DdddOcrInputError and InvalidImageError, which the API maps to HTTP 400 errors.
    • Detection Mode: If the service is configured in detection mode (det=True), calling classification methods will result in an error indicating the current mode is set to object detection.
  2. Understand DdddOcr working modes

    master

    Depending on how you initialize the DdddOcr class, the library operates in one of several distinct modes:

    1. Standard OCR Mode:

      • Configuration: ocr=True, det=False (Default)
      • Purpose: Recognizes text within images.
    2. Object Detection Mode:

      • Configuration: ocr=False, det=True (or setting det=True while ocr=True will default to this mode)
      • Purpose: Detects the location of specific targets in an image.
    3. Slider Captcha Mode:

      • Configuration: ocr=False, det=False
      • Purpose: Uses slider matching algorithms (requires calling slide_match or slide_comparison methods).
    4. Custom Model Mode:

      • Configuration: import_onnx_path="path/to/model.onnx", charsets_path="path/to/charset.json"
      • Purpose: Uses a user-trained model. In this mode, ocr and det parameters are ignored. The custom charset JSON must include charset/word/image/channel fields.
  3. Solve slider captchas

    master

    DdddOcr provides two algorithms for slider captcha solving:

    1. Edge Matching (slide_match)

    Best for slider images with transparent backgrounds. It uses edge detection to find the slider's position.

    • Method: slide_match(target_bytes, background_bytes, simple_target=False)
    • simple_target=True: Use this if the slider image does not have a transparent background.
    • Returns: A dictionary containing the target coordinates, e.g., {"target": [x, y, ... ]}.

    2. Image Difference Comparison (slide_comparison)

    Best for comparing a full background image against an image containing the gap/shadow to find the missing piece.

    • Method: slide_comparison(target_bytes, background_bytes)
    • Returns: A dictionary {"target": [x, y]} representing the gap location.
    import ddddocr
    
    # Algorithm 1: Edge Matching
    slide = ddddocr.DdddOcr(det=False, ocr=False)
    res = slide.slide_match(target_bytes, background_bytes, simple_target=True)
    
    # Algorithm 2: Image Difference
    res = slide.slide_comparison(target_bytes, background_bytes)
  4. Install ddddocr via pip or from source

    master

    You can install ddddocr using the recommended PyPI method or by cloning the repository for source installation. If you need API dependencies, you can install them as an extra.

    # Recommended: Install from PyPI
    pip install ddddocr
    
    # Install from source
    git clone https://github.com/sml2h3/ddddocr.git
    cd ddddocr
    pip install .
    
    # Install with API dependencies (optional)
    pip install ".[api]"
  5. Start the DdddOcr API service via CLI

    master

    You can launch a RESTful API service to access all DdddOcr features using the command line.

    Basic usage:

    python -m ddddocr api

    Custom configuration: You can specify the host, port, workers, and enable specific features like OCR or object detection via CLI flags.

    Note: If you run python -m ddddocr.api directly, it binds to 127.0.0.1 by default. You can override this using the DDDDOCR_HOST environment variable.

    # Use default configuration
    python -m ddddocr api
    
    # Specify host, port, and workers
    python -m ddddocr api --host 0.0.0.0 --port 8000 --workers 4
    
    # Configure OCR functionality
    python -m ddddocr api --ocr true --beta true
    
    # Configure object detection functionality
    python -m ddddocr api --ocr false --det true
  6. Optimize performance with batch processing

    master

    When processing a large number of captchas, reuse a single DdddOcr instance to significantly improve efficiency. Avoid re-initializing the object inside loops, as model loading is slow.

    import ddddocr
    import os
    
    # Initialize OCR object once
    ocr = ddddocr.DdddOcr()
    
    def batch_process(directory):
        results = {}
        for filename in os.listdir(directory):
            if filename.endswith(('.png', '.jpg', '.jpeg', '.bmp')):
                file_path = os.path.join(directory, filename)
                with open(file_path, 'rb') as f:
                    image = f.read()
                # Reuse the same instance
                result = ocr.classification(image)
                results[filename] = result
        return results
  7. Preprocess images for better recognition

    master

    For captchas with high interference (noise), you can use libraries like OpenCV to preprocess the image (e.g., grayscale, thresholding, noise removal) before passing the bytes to ocr.classification().

    import ddddocr
    import cv2
    import numpy as np
    import io
    
    def preprocess_captcha(image_bytes):
        nparr = np.frombuffer(image_bytes, np.uint8)
        img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        _, binary = cv2.threshold(gray, 150, 255, cv2.THRESH_BINARY_INV)
        kernel = np.ones((2, 2), np.uint8)
        opening = cv2.morphologyEx(binary, cv2.MORPH_OPEN, kernel)
        is_success, buffer = cv2.imencode(".jpg", opening)
        return io.BytesIO(buffer).getvalue()
    
    ocr = ddddocr.DdddOcr()
    with open("noisy_captcha.jpg", "rb") as f:
        image_bytes = f.read()
    
    processed_bytes = preprocess_captcha(image_bytes)
    result = ocr.classification(processed_bytes)
    print(f"Result: {result}")
  8. Use multi-threading for parallel processing

    master

    To use ddddocr in a multi-threaded environment, you must create a separate DdddOcr instance for each thread to prevent corrupted recognition results.

    import ddddocr
    import concurrent.futures
    import os
    
    def process_image(file_path):
        # Each thread creates its own independent OCR instance
        ocr = ddddocr.DdddOcr()
        with open(file_path, 'rb') as f:
            image = f.read()
        result = ocr.classification(image)
        return os.path.basename(file_path), result
    
    def parallel_process(directory, max_workers=4):
        file_paths = [os.path.join(directory, f) for f in os.listdir(directory) 
                     if f.endswith(('.png', '.jpg', '.jpeg', '.bmp'))]
        
        results = {}
        with concurrent.futures.ThreadPoolExecutor(max_workers=max_workers) as executor:
            future_to_file = {executor.submit(process_image, file_path): file_path for file_path in file_paths}
            for future in concurrent.futures.as_completed(future_to_file):
                filename, result = future.result()
                results[filename] = result
        return results
  9. Optimize performance with GPU acceleration

    master

    To significantly speed up processing for large batches of images, enable GPU acceleration. This requires having CUDA installed and using the onnxruntime-gpu package. You can specify which GPU to use via device_id.

    # Use the first GPU
    ocr = ddddocr.DdddOcr(use_gpu=True, device_id=0)
    
    # Use the second GPU
    ocr = ddddocr.DdddOcr(use_gpu=True, device_id=1)
  10. Run DdddOcr API using Docker

    master

    You can containerize the API service using Docker or Docker Compose.

    Build and Run:

    1. Build the image: docker build -t ddddocr-api .
    2. Run the container: docker run -d --name ddddocr-api -p 8000:8000 ddddocr-api

    Custom Configuration via Docker: Use environment variables to configure the service inside the container (e.g., DDDDOCR_OCR, DDDDOCR_BETA, DDDDOCR_WORKERS).

    Docker Compose: Run with docker-compose up -d. You can pass environment variables before the command to configure the service.

    # Build and run
    docker build -t ddddocr-api .
    docker run -d --name ddddocr-api -p 8000:8000 ddddocr-api
    
    # Run with custom configuration
    docker run -d --name ddddocr-api \
      -p 8000:8000 \
      -e DDDDOCR_OCR=true \
      -e DDDDOCR_BETA=true \
      -e DDDDOCR_WORKERS=4 \
      ddddocr-api
    
    # Docker Compose
    docker-compose up -d
    
    # Docker Compose with custom config
    DDDDOCR_OCR=true DDDDOCR_BETA=true DDDDOCR_WORKERS=4 docker-compose up -d
  11. Configure DdddOcr API via Docker Compose

    master

    You can deploy the DdddOcr API service using Docker Compose. The configuration allows you to customize the API server settings, OCR engine behavior, and hardware acceleration (GPU) using environment variables.

    To run the service, ensure you have a Dockerfile in the same directory as the docker-compose.yml file. You can override the host port mapping using the DDDDOCR_PORT environment variable.

    version: '3.8'
    services:
      ddddocr-api:
        build:
          context: .
          dockerfile: Dockerfile
        container_name: ddddocr-api
        ports:
          - "${DDDDOCR_PORT:-8000}:8000"
        environment:
          - DDDDOCR_HOST=0.0.0.0
          - DDDDOCR_PORT=8000
          - DDDDOCR_WORKERS=${DDDDOCR_WORKERS:-1}
          - DDDDOCR_OCR=${DDDDOCR_OCR:-true}
          - DDDDOCR_DET=${DDDDOCR_DET:-false}
          - DDDDOCR_OLD=${DDDDOCR_OLD:-false}
          - DDDDOCR_BETA=${DDDDOCR_BETA:-false}
          - DDDDOCR_USE_GPU=${DDDDOCR_USE_GPU:-false}
          - DDDDOCR_DEVICE_ID=${DDDDOCR_DEVICE_ID:-0}
          - DDDDOCR_SHOW_AD=${DDDDOCR_SHOW_AD:-true}
          - DDDDOCR_IMPORT_ONNX_PATH=${DDDDOCR_IMPORT_ONNX_PATH:-""}
          - DDDDOCR_CHARSETS_PATH=${DDDDOCR_CHARSETS_PATH:-""}
        restart: unless-stopped
        healthcheck:
          test: ["CMD", "curl", "-f", "http://localhost:8000/health"]
          interval: 30s
          timeout: 10s
          retries: 3
          start_period: 10s
  12. Enable GPU Acceleration in DdddOcr API

    master

    To use GPU acceleration, you must perform two steps in your docker-compose.yml:

    1. Set the environment variable DDDDOCR_USE_GPU to true.
    2. Uncomment the deploy section to reserve the NVIDIA device.

    Note: This requires a host with an NVIDIA GPU and the appropriate drivers/toolkit installed.

    # In environment section:
        environment:
          - DDDDOCR_USE_GPU=true
    
    # In deploy section:
        deploy:
          resources:
            reservations:
              devices:
                - driver: nvidia
                  count: 1
                  capabilities: [gpu]