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.0.tar.gz (37.8 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.0-py3-none-any.whl (31.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: magicimg-1.0.0.tar.gz
  • Upload date:
  • Size: 37.8 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.0.tar.gz
Algorithm Hash digest
SHA256 7cdebb507bd206e9c23fecd6db9190d65fc05609d45de799008104eb3f10d341
MD5 7110c804ff1edb02567408c5d57cd686
BLAKE2b-256 102952cf548fb004e6c1114393ea23e633af3163381348a6d82a4a06908bf53a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: magicimg-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 31.1 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 770abc8141c4288b6c685f179a430031d8fd34f9576ff6093bc61462e674ad9b
MD5 1e02b29a3dee949f2f758235298a37d3
BLAKE2b-256 224e5c41ece63465d826e85d4aa3b19348e0d01647d9f804a696165e12fd36fc

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