Spaces:
Sleeping
Sleeping
| # ========================================== | |
| # image_processing_cpu.py - Version CPU avec EasyOCR | |
| # ========================================== | |
| """ | |
| Module de traitement d'images CPU-optimisé pour calculs mathématiques | |
| Utilise EasyOCR pour des performances rapides sur CPU | |
| """ | |
| import time | |
| from utils import ( | |
| optimize_image_for_ocr, | |
| prepare_image_for_dataset, | |
| create_thumbnail_fast, | |
| create_white_canvas, | |
| log_memory_usage, | |
| cleanup_memory, | |
| decode_image_from_dataset, | |
| validate_ocr_result | |
| ) | |
| # Variables globales pour OCR EasyOCR | |
| easyocr_reader = None | |
| OCR_MODEL_NAME = "EasyOCR" | |
| def init_ocr_model() -> bool: | |
| """Initialise EasyOCR (optimisé CPU)""" | |
| global easyocr_reader | |
| try: | |
| print("🔄 Chargement EasyOCR (CPU optimisé)...") | |
| import easyocr | |
| easyocr_reader = easyocr.Reader(['en'], gpu=False, verbose=False) | |
| print("✅ EasyOCR prêt (CPU) !") | |
| return True | |
| except Exception as e: | |
| print(f"❌ Erreur lors du chargement EasyOCR: {e}") | |
| return False | |
| def get_ocr_model_info() -> dict: | |
| """Retourne les informations du modèle OCR utilisé""" | |
| return { | |
| "model_name": OCR_MODEL_NAME, | |
| "device": "CPU", | |
| "framework": "EasyOCR", | |
| "optimized_for": "speed", | |
| "version": "1.7.x" | |
| } | |
| def recognize_number_fast_with_image(image_dict, debug: bool = False) -> tuple[str, any, dict | None]: | |
| """ | |
| OCR avec EasyOCR (CPU optimisé) | |
| Args: | |
| image_dict: Image d'entrée (format Gradio) | |
| debug: Afficher les logs de debug | |
| Returns: | |
| (résultat_ocr, image_optimisée, données_dataset) | |
| """ | |
| if image_dict is None or easyocr_reader is None: | |
| if debug: | |
| print(" ❌ Image manquante ou EasyOCR non initialisé") | |
| return "0", None, None | |
| try: | |
| start_time = time.time() | |
| if debug: | |
| print(" 🔄 Début OCR EasyOCR...") | |
| # Optimiser image (fonction commune) | |
| optimized_image = optimize_image_for_ocr(image_dict, max_size=300) | |
| if optimized_image is None: | |
| if debug: | |
| print(" ❌ Échec optimisation image") | |
| return "0", None, None | |
| # EasyOCR - traitement spécialisé CPU | |
| if debug: | |
| print(" ⚡ Lancement EasyOCR...") | |
| import numpy as np | |
| img_array = np.array(optimized_image) | |
| results = easyocr_reader.readtext(img_array, detail=0, paragraph=False) | |
| # Traitement des résultats EasyOCR | |
| if results: | |
| all_text = ' '.join(str(r) for r in results) | |
| final_result = validate_ocr_result(all_text, max_length=4) | |
| else: | |
| final_result = "0" | |
| # Préparer pour dataset (fonction commune) | |
| dataset_image_data = prepare_image_for_dataset(optimized_image) | |
| if debug: | |
| total_time = time.time() - start_time | |
| print(f" ✅ EasyOCR terminé en {total_time:.1f}s → '{final_result}'") | |
| return final_result, optimized_image, dataset_image_data | |
| except Exception as e: | |
| print(f"❌ Erreur OCR EasyOCR: {e}") | |
| return "0", None, None | |
| def recognize_number_fast(image_dict) -> tuple[str, any]: | |
| """Version rapide standard""" | |
| result, optimized_image, _ = recognize_number_fast_with_image(image_dict) | |
| return result, optimized_image | |
| def recognize_number(image_dict) -> str: | |
| """Interface standard""" | |
| result, _ = recognize_number_fast(image_dict) | |
| return result |