Spaces:
Running on Zero
Running on Zero
File size: 2,045 Bytes
a39757f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | import os
import sys
# Auto-relaunch inside virtualenv if executed with global python
_root_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
_venv_python = os.path.join(_root_dir, "venv", "Scripts", "python.exe")
if os.path.exists(_venv_python) and os.path.abspath(sys.executable).lower() != os.path.abspath(_venv_python).lower():
import subprocess
sys.exit(subprocess.run([_venv_python] + sys.argv).returncode)
import cv2
# Fix Windows console UTF-8 encoding for Arabic characters
if sys.platform == "win32":
try:
sys.stdout.reconfigure(encoding="utf-8")
sys.stderr.reconfigure(encoding="utf-8")
except Exception:
pass
# Ensure app can be imported
sys.path.append(_root_dir)
from app.infrastructure.ocr.easyocr_engine import EasyOCREngine
print("--- EasyOCR Engine Initialization Debug ---")
try:
engine = EasyOCREngine()
print("Engine initialized successfully.")
print("Reader languages:", getattr(engine.reader, "lang_list", ["ar", "en"]))
print("Device used (CPU/GPU):", getattr(engine.reader, "device", "cpu"))
except Exception as e:
print("Error initializing EasyOCR engine:", e)
sys.exit(1)
print("\n--- Running OCR on test images ---")
test_images = [
os.path.join(_root_dir, "tests", "ARABIC.jpg"),
os.path.join(_root_dir, "tests", "ENGLISH.webp"),
os.path.join(_root_dir, "tests", "test_id.jpg"),
]
for image_path in test_images:
if not os.path.exists(image_path):
print(f"Skipping {image_path}: File not found.")
continue
print(f"\nProcessing: {os.path.basename(image_path)}")
image = cv2.imread(image_path)
if image is None:
print(f"Error: Could not read image {image_path}")
continue
result = engine.process_image(image)
print(f"Detected {len(result.blocks)} blocks in {result.total_time_ms:.2f} ms (Avg Conf: {result.average_confidence:.4f})")
for idx, block in enumerate(result.blocks):
print(f" [{idx}] Text=\"{block.text}\" | Conf={block.confidence:.4f}")
|