Skip to main content

Thư viện xử lý ảnh thông minh với hỗ trợ GPU tự động

Project description

MagicImg - Thư viện xử lý ảnh thông minh

MagicImg là một thư viện Python mạnh mẽ cho việc xử lý và tối ưu hóa ảnh, đặc biệt là cho các tác vụ OCR. Thư viện hỗ trợ xử lý trên cả CPU và GPU, với khả năng tự động phát hiện và tối ưu hóa tài nguyên.

Tính năng chính

  • Tự động phát hiện và sử dụng GPU nếu có
  • Quản lý bộ nhớ GPU thông minh
  • Xử lý batch ảnh hiệu quả
  • Tăng cường chất lượng ảnh tự động
  • Phát hiện và sửa góc nghiêng
  • Tối ưu hóa ảnh cho OCR
  • Debug và logging chi tiết

Yêu cầu hệ thống

  • Python 3.7+
  • CUDA (tùy chọn, cho xử lý GPU)
  • OpenCV
  • PyTorch
  • Tesseract (cho OCR)

Cài đặt

pip install -r requirements.txt

Sử dụng cơ bản

from magicimg.core import ImageProcessor

# Khởi tạo với cấu hình mặc định
processor = ImageProcessor()

# Hoặc với cấu hình tùy chỉnh
processor = ImageProcessor(
    config={
        "use_gpu": True,              # Bật/tắt GPU
        "gpu_id": 0,                  # ID của GPU muốn sử dụng
        "gpu_memory_limit": 0.7,      # Giới hạn bộ nhớ GPU (70%)
        "batch_size": 4,              # Số ảnh xử lý cùng lúc
        "parallel_jobs": 2,           # Số luồng xử lý song song
        "optimize_for_gpu": True      # Tối ưu các phép tính cho GPU
    }
)

# Xử lý một ảnh
result = processor.process_image("input.jpg", "output.jpg")

# Kiểm tra kết quả
if result.success:
    print(f"Xử lý thành công!")
    print(f"Chất lượng ảnh: {result.quality_metrics.quality_score}")
    print(f"Các bước đã thực hiện: {result.processing_steps}")
else:
    print(f"Lỗi: {result.error_message}")

Xử lý batch

import glob
from magicimg.core import ImageProcessor

# Khởi tạo processor
processor = ImageProcessor(
    config={
        "use_gpu": True,
        "batch_size": 4
    }
)

# Lấy danh sách ảnh
images = glob.glob("input/*.jpg")

# Xử lý theo batch
for i in range(0, len(images), processor.config["batch_size"]):
    batch = images[i:i + processor.config["batch_size"]]
    
    # Xử lý từng ảnh trong batch
    for img_path in batch:
        output_path = img_path.replace("input", "output")
        result = processor.process_image(img_path, output_path)

Tối ưu hóa GPU

Thư viện tự động quản lý bộ nhớ GPU để tránh rò rỉ và tối ưu hiệu suất:

from magicimg.core import ImageProcessor, gpu_memory_manager

processor = ImageProcessor(config={"use_gpu": True})

# Sử dụng context manager để tự động giải phóng bộ nhớ
with gpu_memory_manager():
    result = processor.process_image("input.jpg", "output.jpg")

Debug và logging

Bật chế độ debug để lưu các ảnh trung gian và thông tin chi tiết:

processor = ImageProcessor(
    debug_dir="debug",
    config={
        "debug": True
    }
)

Cấu hình chi tiết

config = {
    # Cấu hình GPU
    "use_gpu": True,              # Bật/tắt GPU
    "gpu_id": 0,                  # ID của GPU muốn sử dụng
    "gpu_memory_limit": 0.7,      # Giới hạn bộ nhớ GPU (70%)
    "batch_size": 4,              # Số ảnh xử lý cùng lúc
    "parallel_jobs": 2,           # Số luồng xử lý song song
    "optimize_for_gpu": True,     # Tối ưu các phép tính cho GPU
    
    # Ngưỡng chất lượng
    "min_blur_index": 80.0,       # Chỉ số mờ tối thiểu
    "max_dark_ratio": 0.2,        # Tỷ lệ pixel tối tối đa
    "min_brightness": 180.0,      # Độ sáng tối thiểu
    "min_contrast": 50.0,         # Độ tương phản tối thiểu
    "min_resolution": (1000, 1400), # Độ phân giải tối thiểu
    "min_quality_score": 0.7,     # Điểm chất lượng tối thiểu
    
    # Ngưỡng xử lý
    "min_skew_angle": 0.3,        # Góc nghiêng tối thiểu cần sửa
    "max_skew_angle": 30.0,       # Góc nghiêng tối đa có thể sửa
    "min_rotation_confidence": 0.8 # Độ tin cậy tối thiểu khi xoay
}

Đóng góp

Mọi đóng góp đều được hoan nghênh! Vui lòng tạo issue hoặc pull request trên GitHub.

Giấy phép

MIT License

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-1.0.1.tar.gz (30.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-1.0.1-py3-none-any.whl (30.7 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for magicimg-1.0.1.tar.gz
Algorithm Hash digest
SHA256 8a9a4149fd00f0a998964b65a83a996de97a71ccce5efb704cc2dd5034881bfa
MD5 300b47d51af20f1c31bb59745b466090
BLAKE2b-256 f5c74e41ca5dc18e08a30ad80a789bb043d167272367f2828c3d351837c58a74

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for magicimg-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 fe46e11dc885825cd3966a852613a50666adcc12f8225b84a8b2ee7f7a26f5d8
MD5 68b6be230a6ab70565d6c576a5e907fd
BLAKE2b-256 e01ab062376b563f6fe3ebb3c2b75700d72a5a71e485b797a73dec27de3fadf5

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