feat(ocr): auto-install Tesseract/EasyOCR languages from settings
- Add app/utils/ocr_language_manager.py: detects tessdata dir, downloads missing .traineddata files via wget/curl from tessdata_fast GitHub repo, pre-downloads EasyOCR models, exposes async background-thread helper - TesseractOCRProvider.process() calls ensure_tesseract_languages() before running pytesseract; raises clear error if languages remain unavailable - EasyOCRProvider.process() logs informational message when models download - app/main.py: calls ensure_ocr_languages_async() at startup - app/utils/settings_sync.py: triggers language re-check after every settings reload so UI changes take effect without container restart - app/api/settings.py: adds POST /api/settings/install-ocr-languages endpoint for on-demand language installation from the admin UI - Dockerfile: adds wget for runtime tessdata downloads - docs/ConfigurationGuide.md: documents automatic language download - tests/test_ocr_language_manager.py: 29 unit tests Co-authored-by: christianlouis <361235+christianlouis@users.noreply.github.com>
This commit is contained in:
@@ -405,6 +405,47 @@ async def bulk_update_settings(updates: list[SettingUpdate], request: Request, d
|
||||
}
|
||||
|
||||
|
||||
@router.post("/install-ocr-languages")
|
||||
async def install_ocr_languages(request: Request, admin: AdminUser):
|
||||
"""
|
||||
Trigger on-demand installation of Tesseract language data files and
|
||||
EasyOCR model downloads for the languages currently configured in the
|
||||
application settings.
|
||||
|
||||
This endpoint is useful after changing ``tesseract_language`` or
|
||||
``easyocr_languages`` so that the required data is available without
|
||||
restarting the container. The download runs synchronously and may take
|
||||
a few seconds (or minutes for large EasyOCR models).
|
||||
|
||||
Returns a summary of which languages are now available and which could
|
||||
not be installed.
|
||||
Admin only.
|
||||
"""
|
||||
from app.utils.ocr_language_manager import ensure_ocr_languages_from_settings # noqa: PLC0415
|
||||
|
||||
try:
|
||||
result = ensure_ocr_languages_from_settings()
|
||||
tesseract_missing = result.get("tesseract_missing", [])
|
||||
easyocr_failed = result.get("easyocr_failed", [])
|
||||
all_ok = not tesseract_missing and not easyocr_failed
|
||||
return {
|
||||
"success": all_ok,
|
||||
"tesseract_missing": tesseract_missing,
|
||||
"easyocr_failed": easyocr_failed,
|
||||
"message": (
|
||||
"All configured OCR languages are available."
|
||||
if all_ok
|
||||
else f"Some languages could not be installed: tesseract={tesseract_missing}, easyocr={easyocr_failed}"
|
||||
),
|
||||
}
|
||||
except Exception as e:
|
||||
logger.error(f"Error during OCR language installation: {e}")
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail="Failed to install OCR language data",
|
||||
)
|
||||
|
||||
|
||||
@router.get("/{key}/history")
|
||||
async def get_key_history(key: str, request: Request, db: DbSession, admin: AdminUser):
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user