mirror of
https://github.com/Ladebeze66/llm_ticket3.git
synced 2025-12-16 00:26:51 +01:00
82 lines
2.8 KiB
Python
82 lines
2.8 KiB
Python
#!/usr/bin/env python
|
|
# -*- coding: utf-8 -*-
|
|
|
|
import os
|
|
import sys
|
|
import argparse
|
|
import json
|
|
from PIL import Image
|
|
from utils.ocr_utils import extraire_texte_fr
|
|
from utils.translate_utils import fr_to_en, en_to_fr
|
|
|
|
def parse_args():
|
|
parser = argparse.ArgumentParser(description="Test direct d'OCR et traduction")
|
|
parser.add_argument("image_path", help="Chemin vers l'image à analyser")
|
|
parser.add_argument("--verbose", "-v", action="store_true", help="Mode verbeux")
|
|
parser.add_argument("--info", "-i", action="store_true", help="Afficher les infos de l'image")
|
|
return parser.parse_args()
|
|
|
|
def test_ocr_traduction(image_path, verbose=False, show_info=False):
|
|
"""
|
|
Teste l'OCR et la traduction sur une image spécifique
|
|
"""
|
|
# Vérification de l'existence du fichier
|
|
if not os.path.exists(image_path):
|
|
print(f"Erreur: Le fichier {image_path} n'existe pas")
|
|
return
|
|
|
|
# Afficher les infos sur l'image si demandé
|
|
if show_info:
|
|
try:
|
|
with Image.open(image_path) as img:
|
|
print(f"Format: {img.format}")
|
|
print(f"Mode: {img.mode}")
|
|
print(f"Taille: {img.size}")
|
|
print(f"Palette: {hasattr(img, 'palette')}")
|
|
except Exception as e:
|
|
print(f"Erreur lors de l'analyse de l'image: {e}")
|
|
return
|
|
|
|
# Exécution de l'OCR
|
|
print(f"Exécution de l'OCR sur {image_path}...")
|
|
ocr_fr = extraire_texte_fr(image_path)
|
|
|
|
# Affichage du résultat OCR
|
|
if ocr_fr:
|
|
print("\n--- TEXTE DÉTECTÉ (FR) ---")
|
|
print(ocr_fr)
|
|
print("-------------------------")
|
|
else:
|
|
print("Aucun texte détecté par l'OCR")
|
|
|
|
# Traduction si du texte a été détecté
|
|
if ocr_fr:
|
|
print("\nTraduction FR -> EN...")
|
|
ocr_en = fr_to_en(ocr_fr)
|
|
print("\n--- TRADUCTION (EN) ---")
|
|
print(ocr_en)
|
|
print("-------------------------")
|
|
|
|
print("\nTraduction EN -> FR (vérification)...")
|
|
ocr_en_back_fr = en_to_fr(ocr_en)
|
|
print("\n--- TRADUCTION RETOUR (FR) ---")
|
|
print(ocr_en_back_fr)
|
|
print("-------------------------")
|
|
|
|
# Enregistrer les résultats dans un JSON
|
|
results = {
|
|
"filename": os.path.basename(image_path),
|
|
"ocr_fr": ocr_fr,
|
|
"ocr_en": ocr_fr and fr_to_en(ocr_fr) or "",
|
|
"ocr_en_back_fr": ocr_fr and en_to_fr(fr_to_en(ocr_fr)) or "",
|
|
}
|
|
|
|
output_file = f"ocr_test_{os.path.basename(image_path)}.json"
|
|
with open(output_file, "w", encoding="utf-8") as f:
|
|
json.dump(results, f, ensure_ascii=False, indent=2)
|
|
|
|
print(f"\nRésultats enregistrés dans {output_file}")
|
|
|
|
if __name__ == "__main__":
|
|
args = parse_args()
|
|
test_ocr_traduction(args.image_path, args.verbose, args.info) |