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

Uploaded Python 3

File details

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

File metadata

  • Download URL: magicimg-1.0.2.tar.gz
  • Upload date:
  • Size: 30.7 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.2.tar.gz
Algorithm Hash digest
SHA256 6502ad650a4b9e1aefcd31802cbee747f059c615651a88e897b77ff0d09cf027
MD5 955d9dec9c37ec670c00844da0c85d53
BLAKE2b-256 a991fc2f9683abcda01ac76b0f9f3e682b262760cc68269678b2e221419673aa

See more details on using hashes here.

File details

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

File metadata

  • Download URL: magicimg-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 30.9 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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 405f08157b55469016371580ba4491cfa234c4809557e25d5bbea32089063a67
MD5 4213d39c3b06c0ebae78997605ca84f1
BLAKE2b-256 2517488018c197ba7c4c689b81f4523b93c6f9ce9220b049c648f01eff12ce09

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