UNPKG

easyocr-js

Version:

A Node.js wrapper for EasyOCR

68 lines (58 loc) • 2.01 kB
import { execSync } from "child_process"; function runCommand(command, ignoreErrors = false) { try { console.log(`\nšŸš€ Running: ${command}`); const startTime = performance.now(); execSync(command, { stdio: "inherit" }); const endTime = performance.now(); console.log(`āœ… Completed in ${((endTime - startTime) / 1000).toFixed(2)}s`); return true; } catch (error) { if (ignoreErrors) { console.warn(`āš ļø Warning: Command failed (ignored): ${command}`); return false; } else { console.error(`āŒ Error executing command: ${command}`); console.error(error.message); process.exit(1); } } } function getPythonCommand() { const pythonVariants = ["python3", "python", "py"]; for (const cmd of pythonVariants) { try { const versionOutput = execSync(`${cmd} --version`, { encoding: "utf-8", stdio: "pipe", }); if (versionOutput.includes("Python 3")) { console.log(`šŸ Using Python command: ${cmd} (${versionOutput.trim()})`); return cmd; } } catch { // ignore and try next } } console.error( "āŒ Python 3.x is not installed or not found in PATH. Please install Python 3.x." ); process.exit(1); } async function main() { const pythonCmd = getPythonCommand(); // Upgrade pip runCommand(`${pythonCmd} -m pip install --upgrade pip`); // Install PyTorch CPU version (required by EasyOCR) runCommand( `${pythonCmd} -m pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu` ); // Install EasyOCR runCommand(`${pythonCmd} -m pip install easyocr`); // Install OpenCV headless (for image processing) runCommand(`${pythonCmd} -m pip install opencv-python-headless`); // Install numpy (dependency) runCommand(`${pythonCmd} -m pip install numpy`); console.log("\nšŸŽ‰ All dependencies installed successfully!"); } main();