Skip to main content

Package xử lý và tiền xử lý ảnh để chuẩn bị cho OCR và các hệ thống thị giác máy tính

Project description

Image Preprocess

PyPI version License: MIT Python 3.7+

Package Python cho việc xử lý và tiền xử lý ảnh để chuẩn bị cho OCR và các hệ thống thị giác máy tính.

Tính năng chính

  • Kiểm tra chất lượng ảnh: Đánh giá độ mờ, độ sáng, độ tương phản, độ phân giải
  • Phát hiện và sửa góc nghiêng: Tự động phát hiện và điều chỉnh góc nghiêng của ảnh
  • Phát hiện hướng ảnh: Sử dụng OCR để xác định hướng đúng của ảnh
  • Tăng cường chất lượng: Tự động cải thiện độ sáng, độ tương phản và độ sắc nét
  • Tiền xử lý cho OCR: Tối ưu hóa ảnh cho việc nhận dạng ký tự quang học
  • Phát hiện đường kẻ ngang: Tìm và phân tích các đường kẻ trong bảng biểu
  • Trích xuất hàng từ bảng: Cắt và tách các hàng từ ảnh bảng
  • Command Line Interface: Sử dụng trực tiếp từ terminal
  • Hỗ trợ nhiều nhà cung cấp API: Tối ưu cho Google Vision, Anthropic Claude, và OCR local

Cài đặt

Cài đặt từ PyPI

pip install image-preprocess

Cài đặt từ source

git clone https://github.com/tacgia/image-preprocess.git
cd image-preprocess
pip install -e .

Cài đặt Tesseract OCR

Package này sử dụng Tesseract OCR để phát hiện hướng ảnh. Cài đặt Tesseract:

Ubuntu/Debian:

sudo apt-get install tesseract-ocr

macOS:

brew install tesseract

Windows: Tải và cài đặt từ: https://github.com/UB-Mannheim/tesseract/wiki

Sử dụng cơ bản

Sử dụng với Python

import image_preprocess

# Sử dụng hàm tiện ích
result = image_preprocess.preprocess_for_ocr("input.jpg", "output.jpg")
print(f"Xử lý thành công: {result.success}")
print(f"Điểm chất lượng: {result.quality_metrics.quality_score}")

# Kiểm tra chất lượng ảnh
is_good, quality_info, enhanced_image = image_preprocess.check_image_quality("input.jpg")
print(f"Chất lượng ảnh tốt: {is_good}")

# Phát hiện hướng ảnh
angle = image_preprocess.detect_orientation("input.jpg")
print(f"Góc cần xoay: {angle}°")

Sử dụng với ImageProcessor class

from magicimg import ImageProcessor

# Tạo processor với thư mục debug
processor = ImageProcessor(debug_dir="debug/")

# Xử lý ảnh hoàn chỉnh
result = processor.process_image(
    input_path="input.jpg",
    output_path="output.jpg", 
    auto_rotate=True
)

if result.success:
    print("Xử lý thành công!")
    print(f"Các bước đã thực hiện: {result.processing_steps}")
    print(f"Góc đã xoay: {result.rotation_angle}°")
else:
    print(f"Lỗi: {result.error_message}")

Sử dụng với cấu hình tùy chỉnh

from magicimg import ImageProcessor

# Cấu hình tùy chỉnh
config = {
    "min_blur_index": 100.0,
    "min_brightness": 150.0,
    "min_contrast": 40.0,
    "min_quality_score": 0.8,
    "skip_rotation": False,
    "line_detection": {
        "min_line_length_ratio": 0.7,
        "histogram_threshold_ratio": 0.6
    }
}

processor = ImageProcessor(config=config)
result = processor.process_image("input.jpg", "output.jpg")

Sử dụng Command Line Interface

Xử lý ảnh

# Xử lý ảnh cơ bản
magicimg process input.jpg --output output.jpg

# Xử lý với thư mục debug
magicimg process input.jpg --debug-dir debug/ --min-quality 0.8

# Xử lý không xoay ảnh
magicimg process input.jpg --no-rotation

Kiểm tra chất lượng ảnh

magicimg quality input.jpg

Phát hiện hướng ảnh

magicimg orientation input.jpg --debug-dir debug/

Xem thông tin ảnh

magicimg info input.jpg

Kiểm tra hệ thống

magicimg system-info

API cho các nhà cung cấp

Package hỗ trợ tối ưu hóa ảnh cho các nhà cung cấp API khác nhau:

from magicimg.core import ImageProcessor

# Tối ưu cho Google Vision API
success, info, output_path = ImageProcessor.preprocess_image_for_api(
    image_path="input.jpg",
    provider="google",
    output_dir="processed/",
    min_quality_score=0.7
)

# Tối ưu cho Anthropic Claude
success, info, output_path = ImageProcessor.preprocess_image_for_api(
    image_path="input.jpg", 
    provider="anthropic",
    auto_rotate=True
)

# Tối ưu cho OCR local
success, info, output_path = ImageProcessor.preprocess_image_for_api(
    image_path="input.jpg",
    provider="local",
    debug_dir="debug/"
)

Cấu hình nâng cao

Tham số chất lượng ảnh

  • min_blur_index: Chỉ số mờ tối thiểu (mặc định: 80.0)
  • max_dark_ratio: Tỷ lệ pixel tối tối đa (mặc định: 0.2)
  • min_brightness: Độ sáng tối thiểu (mặc định: 180.0)
  • min_contrast: Độ tương phản tối thiểu (mặc định: 50.0)
  • min_resolution: Độ phân giải tối thiểu (mặc định: 1000x1400)
  • min_quality_score: Điểm chất lượng tối thiểu (mặc định: 0.7)

Tham số xử lý ảnh

  • min_skew_angle: Góc nghiêng tối thiểu để xử lý (mặc định: 0.3°)
  • max_skew_angle: Góc nghiêng tối đa có thể xử lý (mặc định: 30.0°)
  • skip_rotation: Bỏ qua xoay ảnh (mặc định: False)
  • reuse_rotation: Sử dụng lại góc xoay đã biết (mặc định: None)

Tham số phát hiện đường kẻ

  • min_line_length_ratio: Tỷ lệ chiều dài đường kẻ tối thiểu (mặc định: 0.6)
  • histogram_threshold_ratio: Ngưỡng histogram (mặc định: 0.5)
  • min_line_distance: Khoảng cách tối thiểu giữa các đường kẻ (mặc định: 20px)

Ví dụ thực tế

Xử lý ảnh phiếu bầu cử

from magicimg import ImageProcessor

# Cấu hình đặc biệt cho phiếu bầu cử
ballot_config = {
    "min_brightness": 200.0,  # Yêu cầu độ sáng cao hơn
    "min_contrast": 60.0,     # Tương phản rõ ràng
    "ballot_keywords": [      # Từ khóa đặc trưng
        "phieu bau cu", "doan dai bieu", "dai hoi"
    ]
}

processor = ImageProcessor(
    debug_dir="ballot_debug/",
    config=ballot_config
)

result = processor.process_image("phieu_bau.jpg", "processed_phieu.jpg")

Xử lý ảnh tài liệu quét

# Cấu hình cho tài liệu quét
document_config = {
    "min_blur_index": 60.0,   # Chấp nhận ảnh hơi mờ
    "min_brightness": 150.0,  # Độ sáng thấp hơn
    "skip_rotation": True,    # Không xoay tự động
}

processor = ImageProcessor(config=document_config)

Yêu cầu hệ thống

  • Python 3.7+
  • OpenCV 4.5.0+
  • NumPy 1.19.0+
  • Matplotlib 3.3.0+ (cho debug)
  • pytesseract 0.3.8+ (cho phát hiện hướng)
  • Pillow 8.0.0+

Đóng góp

Chúng tôi rất hoan nghênh mọi đóng góp! Vui lòng:

  1. Fork repository này
  2. Tạo feature branch (git checkout -b feature/AmazingFeature)
  3. Commit changes (git commit -m 'Add some AmazingFeature')
  4. Push to branch (git push origin feature/AmazingFeature)
  5. Mở Pull Request

Giấy phép

Dự án này được phân phối dưới giấy phép MIT. Xem file LICENSE để biết thêm thông tin.

Changelog

v0.1.0 (2024-12-15)

  • Phiên bản đầu tiên
  • Hỗ trợ kiểm tra chất lượng ảnh
  • Phát hiện và sửa góc nghiêng
  • Phát hiện hướng ảnh bằng OCR
  • Tăng cường chất lượng ảnh
  • Command Line Interface
  • Hỗ trợ nhiều nhà cung cấp API

Liên hệ

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

magicimg-0.1.2.tar.gz (34.4 kB view details)

Uploaded Source

Built Distribution

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

magicimg-0.1.2-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file magicimg-0.1.2.tar.gz.

File metadata

  • Download URL: magicimg-0.1.2.tar.gz
  • Upload date:
  • Size: 34.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.3

File hashes

Hashes for magicimg-0.1.2.tar.gz
Algorithm Hash digest
SHA256 1e846c8f4cb7d0d295bf6148f890eb9fd69854e3529baead4ea33c3944258774
MD5 5f8aa4eaf5d8e4f1281701d0ba84a0e3
BLAKE2b-256 08fcb5d5cb9eb1c573a6a41d50e8b262106d66d30416b72bff24bde42bf3e2dd

See more details on using hashes here.

File details

Details for the file magicimg-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: magicimg-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 5.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.3

File hashes

Hashes for magicimg-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 bf668cba61c5bc9210017076afa7a0cb4d7fe6f740b7c0f0d07168997b8a1f8a
MD5 508840353a2833fc107476dc81f8066b
BLAKE2b-256 ecdeb6ae024f400ca86d01523bc24888cfc74503e1520df89458788abeac638b

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