Sari la conținut

pyt_services/aip (Augmented Image Preprocessing) Routines Documentation

Overview

The aip (Augmented Image Preprocessing) module provides interactive UI components for manual correction of automatically preprocessed images. It integrates with the pyt_utils/image_preprocessing pipeline to allow human intervention when automated algorithms need assistance.

Core Classes

ManualImageCorrectionUI(result_callback)

Class Signature:

class ManualImageCorrectionUI(Callable[[str, Optional[np.ndarray]], None]):

Constructor Parameters: - result_callback: Function accepting (status: str, result: Optional[np.ndarray]) where: - Status: "success" or "error" - Result: Corrected image on success, error message on failure

Instance Methods:

start_correction(image: np.ndarray)

Purpose: Launches the interactive correction interface for the provided image.

Parameters: - image: OpenCV image array (BGR format) to be corrected

UI Features: - Auto-rotation correction - Manual cropping adjustments - Rotation, crop, and reset controls - Accept/Cancel options

apply_rotation()

Purpose: Applies automated rotation detection and correction.

Algorithm: - Uses pyt_utils functions for skew detection - Applies rotation if angle > 0.1 degrees

apply_crop()

Purpose: Applies automated cropping to remove borders.

Algorithm: - Uses contour detection to identify document boundaries - Crops to the largest detected region

reset_image()

Purpose: Restores the original image, discarding all changes.

accept_correction()

Purpose: Accepts current modifications and returns to caller.

Callback Invocation: Calls result_callback('success', current_image)

cancel_correction()

Purpose: Cancels the correction process.

Callback Invocation: Calls result_callback('error', 'User cancelled correction')

Internal Methods

_detect_rotation_angle() -> float

Returns: Detected rotation angle (currently placeholder, integrates with pyt_utils)

_rotate_image(angle: float)

Purpose: Applies rotation transformation to current image.

Parameters: - angle: Rotation angle in degrees

_auto_crop()

Purpose: Automatically crops image to remove empty borders.

Algorithm: - Converts to grayscale - Applies binary thresholding - Finds contours of document regions - Crops to bounding rectangle of largest contour

_update_display()

Purpose: Updates the DearPyGui interface with current image state.

Note: Currently placeholder for DearPyGui integration

Dependencies

Required Packages: - DearPyGui (dearpygui) - OpenCV (cv2) - NumPy - typing (for type hints)

Optional Dependencies: - pyt_utils.opencv_utils (recommended for advanced rotation/skew detection)

Integration Points

With pyt_utils/image_preprocessing:

from pyt_utils.image_preprocessing import fix_image
from pyt_services.aip.manual_image_correction import ManualImageCorrectionUI

def manual_correction_callback(status, result):
    if status == 'success':
        # Use corrected result
        print("Manual correction successful")
    else:
        print(f"Manual correction failed: {result}")

# Enable manual correction in preprocessing
processed_image = fix_image("document.jpg", enable_manual_correction=True)

Standalone Usage:

import cv2
from pyt_services.aip.manual_image_correction import ManualImageCorrectionUI

def handle_correction(status, result):
    if status == 'success':
        cv2.imwrite("corrected.jpg", result)
    else:
        print("Correction cancelled or failed")

img = cv2.imread("input.jpg")
ui = ManualImageCorrectionUI(handle_correction)
ui.start_correction(img)
# Note: Requires active DearPyGui event loop in calling context

Error Handling

Callback-Based Error Propagation: - All errors communicated through the result_callback - Supports both programmatic errors and user cancellations - Graceful degradation when DearPyGui is unavailable

Import Time Validation: - Optional DearPyGui dependency - Graceful fallback when UI components unavailable

UI Design Principles

Minimal Interface: - Only essential controls (Rotate, Crop, Reset, Accept, Cancel) - Focused on quick corrections for OCR optimization - Non-blocking design for integration with larger applications

Workflow Efficiency: - Auto-apply best practices first - Manual controls for edge cases - Immediate preview of changes

Integration-Friendly: - Callback-based architecture - No assumptions about calling context - Compatible with various event loop paradigms