Spaces:
Sleeping
Sleeping
Delete image_processing_cpu.py
Browse files- image_processing_cpu.py +0 -117
image_processing_cpu.py
DELETED
|
@@ -1,117 +0,0 @@
|
|
| 1 |
-
# ==========================================
|
| 2 |
-
# image_processing_cpu.py - Version CPU avec EasyOCR (SANS BASE64)
|
| 3 |
-
# ==========================================
|
| 4 |
-
|
| 5 |
-
"""
|
| 6 |
-
Module de traitement d'images CPU-optimisé pour calculs mathématiques
|
| 7 |
-
Utilise EasyOCR pour des performances rapides sur CPU
|
| 8 |
-
VERSION MODIFIÉE: Format Image natif au lieu de Base64
|
| 9 |
-
"""
|
| 10 |
-
|
| 11 |
-
import time
|
| 12 |
-
from utils import (
|
| 13 |
-
optimize_image_for_ocr,
|
| 14 |
-
prepare_image_for_dataset,
|
| 15 |
-
create_thumbnail_fast,
|
| 16 |
-
create_white_canvas,
|
| 17 |
-
log_memory_usage,
|
| 18 |
-
cleanup_memory,
|
| 19 |
-
validate_ocr_result
|
| 20 |
-
)
|
| 21 |
-
|
| 22 |
-
# Variables globales pour OCR EasyOCR
|
| 23 |
-
easyocr_reader = None
|
| 24 |
-
OCR_MODEL_NAME = "EasyOCR"
|
| 25 |
-
|
| 26 |
-
def init_ocr_model() -> bool:
|
| 27 |
-
"""Initialise EasyOCR (optimisé CPU)"""
|
| 28 |
-
global easyocr_reader
|
| 29 |
-
|
| 30 |
-
try:
|
| 31 |
-
print("🔄 Chargement EasyOCR (CPU optimisé)...")
|
| 32 |
-
import easyocr
|
| 33 |
-
easyocr_reader = easyocr.Reader(['en'], gpu=False, verbose=False)
|
| 34 |
-
print("✅ EasyOCR prêt (CPU) !")
|
| 35 |
-
return True
|
| 36 |
-
|
| 37 |
-
except Exception as e:
|
| 38 |
-
print(f"❌ Erreur lors du chargement EasyOCR: {e}")
|
| 39 |
-
return False
|
| 40 |
-
|
| 41 |
-
def get_ocr_model_info() -> dict:
|
| 42 |
-
"""Retourne les informations du modèle OCR utilisé"""
|
| 43 |
-
return {
|
| 44 |
-
"model_name": OCR_MODEL_NAME,
|
| 45 |
-
"device": "CPU",
|
| 46 |
-
"framework": "EasyOCR",
|
| 47 |
-
"optimized_for": "speed",
|
| 48 |
-
"version": "1.7.x"
|
| 49 |
-
}
|
| 50 |
-
|
| 51 |
-
def recognize_number_fast_with_image(image_dict, debug: bool = False) -> tuple[str, any, dict | None]:
|
| 52 |
-
"""
|
| 53 |
-
OCR avec EasyOCR (CPU optimisé) - VERSION SANS BASE64
|
| 54 |
-
|
| 55 |
-
Args:
|
| 56 |
-
image_dict: Image d'entrée (format Gradio)
|
| 57 |
-
debug: Afficher les logs de debug
|
| 58 |
-
|
| 59 |
-
Returns:
|
| 60 |
-
(résultat_ocr, image_optimisée, données_dataset_avec_image_native)
|
| 61 |
-
"""
|
| 62 |
-
if image_dict is None or easyocr_reader is None:
|
| 63 |
-
if debug:
|
| 64 |
-
print(" ❌ Image manquante ou EasyOCR non initialisé")
|
| 65 |
-
return "0", None, None
|
| 66 |
-
|
| 67 |
-
try:
|
| 68 |
-
start_time = time.time()
|
| 69 |
-
if debug:
|
| 70 |
-
print(" 🔄 Début OCR EasyOCR...")
|
| 71 |
-
|
| 72 |
-
# Optimiser image (fonction commune)
|
| 73 |
-
optimized_image = optimize_image_for_ocr(image_dict, max_size=300)
|
| 74 |
-
if optimized_image is None:
|
| 75 |
-
if debug:
|
| 76 |
-
print(" ❌ Échec optimisation image")
|
| 77 |
-
return "0", None, None
|
| 78 |
-
|
| 79 |
-
# EasyOCR - traitement spécialisé CPU
|
| 80 |
-
if debug:
|
| 81 |
-
print(" ⚡ Lancement EasyOCR...")
|
| 82 |
-
|
| 83 |
-
import numpy as np
|
| 84 |
-
img_array = np.array(optimized_image)
|
| 85 |
-
results = easyocr_reader.readtext(img_array, detail=0, paragraph=False)
|
| 86 |
-
|
| 87 |
-
# Traitement des résultats EasyOCR
|
| 88 |
-
if results:
|
| 89 |
-
all_text = ' '.join(str(r) for r in results)
|
| 90 |
-
final_result = validate_ocr_result(all_text, max_length=4)
|
| 91 |
-
else:
|
| 92 |
-
final_result = "0"
|
| 93 |
-
|
| 94 |
-
# Préparer pour dataset (NOUVELLE VERSION SANS BASE64)
|
| 95 |
-
dataset_image_data = prepare_image_for_dataset(optimized_image)
|
| 96 |
-
|
| 97 |
-
if debug:
|
| 98 |
-
total_time = time.time() - start_time
|
| 99 |
-
print(f" ✅ EasyOCR terminé en {total_time:.1f}s → '{final_result}'")
|
| 100 |
-
if dataset_image_data:
|
| 101 |
-
print(f" 🖼️ Image dataset: {type(dataset_image_data.get('handwriting_image', 'None'))}")
|
| 102 |
-
|
| 103 |
-
return final_result, optimized_image, dataset_image_data
|
| 104 |
-
|
| 105 |
-
except Exception as e:
|
| 106 |
-
print(f"❌ Erreur OCR EasyOCR: {e}")
|
| 107 |
-
return "0", None, None
|
| 108 |
-
|
| 109 |
-
def recognize_number_fast(image_dict) -> tuple[str, any]:
|
| 110 |
-
"""Version rapide standard"""
|
| 111 |
-
result, optimized_image, _ = recognize_number_fast_with_image(image_dict)
|
| 112 |
-
return result, optimized_image
|
| 113 |
-
|
| 114 |
-
def recognize_number(image_dict) -> str:
|
| 115 |
-
"""Interface standard"""
|
| 116 |
-
result, _ = recognize_number_fast(image_dict)
|
| 117 |
-
return result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|