easyocr-js
Version:
A Node.js wrapper for EasyOCR
68 lines (58 loc) ⢠2.01 kB
JavaScript
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();