UNPKG

easyocr-js

Version:

A Node.js wrapper for PuddleOCR

129 lines (111 loc) โ€ข 4.7 kB
import { execSync } from "child_process"; function runCommand(command, ignoreErrors = false) { try { const startTime = performance.now(); execSync(command, { stdio: "inherit" }); const endTime = performance.now(); console.log(`[TIME] '${command}' completed in ${((endTime - startTime) / 1000).toFixed(2)}s`); return true; } catch (error) { if (ignoreErrors) { console.warn(`Warning: Command failed (ignoring): '${command}'`, error.message); return false; } else { console.error(`Error executing command: '${command}'`, error.message); process.exit(1); } } } function getPythonCommand() { const pythonVariants = ["python", "python3", "py"]; for (const cmd of pythonVariants) { try { const versionOutput = execSync(`${cmd} --version`, { encoding: "utf-8", stdio: "pipe" }); const version = versionOutput.trim(); console.log(`Found ${cmd}: ${version}`); // Check if it's Python 3.x if (version.includes("Python 3.")) { return cmd; } } catch { // Command not found, try next continue; } } console.error("Python 3.x is not installed or not found in PATH. Please install Python 3.x before proceeding."); process.exit(1); } function checkPythonVersion(pythonCmd) { try { const versionOutput = execSync(`${pythonCmd} --version`, { encoding: "utf-8", stdio: "pipe" }); const versionMatch = versionOutput.match(/Python (\d+)\.(\d+)\.(\d+)/); if (versionMatch) { const major = parseInt(versionMatch[1]); const minor = parseInt(versionMatch[2]); if (major === 3 && minor >= 8) { console.log(`โœ… Python version ${versionMatch[0]} is compatible`); return true; } else { console.warn(`โš ๏ธ Python version ${versionMatch[0]} detected. PaddleOCR works best with Python 3.8+`); return true; // Still proceed but with warning } } } catch (error) { console.error("Could not check Python version:", error.message); return false; } } console.log("๐Ÿš€ Starting PaddleOCR installation..."); console.log("Checking Python installation..."); const pythonCmd = getPythonCommand(); console.log(`Using '${pythonCmd}' for installation...`); // Check Python version compatibility checkPythonVersion(pythonCmd); // Upgrade pip first console.log("\n๐Ÿ“ฆ Upgrading pip..."); runCommand(`${pythonCmd} -m pip install --upgrade pip`); // Uninstall existing conflicting packages (ignore errors) console.log("\n๐Ÿงน Cleaning existing packages..."); const packagesToRemove = [ "torch", "torchvision", "torchaudio", "paddlepaddle", "paddleocr", "opencv-python", "opencv-python-headless" ]; for (const pkg of packagesToRemove) { console.log(`Uninstalling ${pkg}...`); runCommand(`${pythonCmd} -m pip uninstall -y ${pkg}`, true); // Ignore errors } // Install PyTorch CPU version console.log("\n๐Ÿ”ฅ Installing PyTorch (CPU version)..."); runCommand( `${pythonCmd} -m pip install torch torchvision torchaudio --force-reinstall --index-url https://download.pytorch.org/whl/cpu` ); // Install PaddlePaddle CPU version console.log("\n๐Ÿ“ Installing PaddlePaddle (CPU version)..."); runCommand(`${pythonCmd} -m pip install paddlepaddle --force-reinstall`); // Install OpenCV (headless version to avoid GUI dependencies) console.log("\n๐Ÿ“ท Installing OpenCV (headless)..."); runCommand(`${pythonCmd} -m pip install opencv-python-headless --force-reinstall`); // Install other required packages console.log("\n๐Ÿ“Š Installing additional dependencies..."); runCommand(`${pythonCmd} -m pip install numpy scipy Pillow --force-reinstall`); // Install PaddleOCR last console.log("\n๐Ÿ” Installing PaddleOCR..."); runCommand(`${pythonCmd} -m pip install paddleocr --force-reinstall`); // Verify installation console.log("\nโœ… Verifying installation..."); try { execSync(`${pythonCmd} -c "import paddleocr; print('PaddleOCR imported successfully')"`, { stdio: "inherit" }); console.log("๐ŸŽ‰ All dependencies installed and verified successfully!"); console.log("\n๐Ÿ“ Installation Summary:"); console.log("- PyTorch (CPU version)"); console.log("- PaddlePaddle (CPU version)"); console.log("- PaddleOCR"); console.log("- OpenCV (headless)"); console.log("- NumPy, SciPy, Pillow"); console.log("\n๐Ÿš€ Ready to use OCR functionality!"); } catch (error) { console.error("โŒ Installation verification failed:", error.message); console.error("Please check the installation logs above for errors."); process.exit(1); }