From bd1536ea9b008fba43fd9f382e0537f6fee647af Mon Sep 17 00:00:00 2001 From: cce <51567+cce@users.noreply.github.com> Date: Wed, 25 Feb 2026 21:09:28 -0500 Subject: [PATCH] fix: use bundled venv python at runtime and suppress easyocr progress output The preinstall script (setup-python-env.js) creates a venv with easyocr and torch installed, but the EasyOCR class hardcodes pythonPath to 'python3', so the venv is never used at runtime. Users must install easyocr globally for it to work, which defeats the purpose of the venv and fails on systems like macOS where homebrew python blocks global pip installs by default. Additionally, easyocr.Reader() prints a progress bar to stdout during model downloads (and on some configurations, during model loading). Since sendCommand() tries to JSON.parse every stdout chunk, the "Progress: ..." text causes a parse error that rejects the init promise. Fix 1: In the constructor, resolve the venv python path relative to __dirname using the same platform-aware convention as setup-python-env.js (venv/bin/python on POSIX, venv/Scripts/python.exe on Windows). Falls back to system python3 if the venv doesn't exist. Fix 2: Pass verbose=False to easyocr.Reader() in the Python bridge script so model downloads and loading never emit non-JSON text to stdout. --- src/easyocr.ts | 8 +++++++- src/easyocr_script.py | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/easyocr.ts b/src/easyocr.ts index ed6ea42..56a3abc 100644 --- a/src/easyocr.ts +++ b/src/easyocr.ts @@ -1,4 +1,5 @@ import { spawn, ChildProcess } from 'child_process'; +import { existsSync } from 'fs'; import path from 'path'; interface OCRResult { @@ -19,7 +20,12 @@ export class EasyOCR { private pythonProcess: ChildProcess | null = null; constructor() { - this.pythonPath = 'python3'; + // Use the venv python created by setup-python-env.js during preinstall, + // falling back to system python3 if the venv doesn't exist. + const venvBin = process.platform === 'win32' ? 'Scripts' : 'bin'; + const venvExe = process.platform === 'win32' ? 'python.exe' : 'python'; + const venvPython = path.join(__dirname, '..', 'venv', venvBin, venvExe); + this.pythonPath = existsSync(venvPython) ? venvPython : 'python3'; this.scriptPath = path.join(__dirname, 'easyocr_script.py'); } diff --git a/src/easyocr_script.py b/src/easyocr_script.py index a9859e7..8aaed39 100644 --- a/src/easyocr_script.py +++ b/src/easyocr_script.py @@ -6,7 +6,7 @@ def init_reader(languages): global reader - reader = easyocr.Reader(languages) + reader = easyocr.Reader(languages, verbose=False) return json.dumps({"status": "success", "message": "Reader initialized"}) def read_text(image_path):