|
| 1 | +"use strict"; |
| 2 | +var __importDefault = (this && this.__importDefault) || function (mod) { |
| 3 | + return (mod && mod.__esModule) ? mod : { "default": mod }; |
| 4 | +}; |
| 5 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 6 | +exports.getOpenCV = void 0; |
| 7 | +const opencv_build_1 = require("@u4/opencv-build"); |
| 8 | +const fs_1 = __importDefault(require("fs")); |
| 9 | +const path_1 = __importDefault(require("path")); |
| 10 | +const commons_1 = require("./commons"); |
| 11 | +const picocolors_1 = __importDefault(require("picocolors")); |
| 12 | +const npmlog_1 = require("npmlog"); |
| 13 | +const logDebug = process.env.OPENCV4NODES_DEBUG_REQUIRE ? npmlog_1.info : () => { }; |
| 14 | +function tryGetOpencvBinDir(builder) { |
| 15 | + if (process.env.OPENCV_BIN_DIR) { |
| 16 | + logDebug('tryGetOpencvBinDir', `${picocolors_1.default.yellow('OPENCV_BIN_DIR')} environment variable is set`); |
| 17 | + return process.env.OPENCV_BIN_DIR; |
| 18 | + } |
| 19 | + // if the auto build is not disabled via environment do not even attempt |
| 20 | + // to read package.json |
| 21 | + if (!builder.env.isAutoBuildDisabled) { |
| 22 | + logDebug('tryGetOpencvBinDir', 'auto build has not been disabled via environment variable, using opencv bin dir of opencv-build'); |
| 23 | + return builder.env.opencvBinDir; |
| 24 | + } |
| 25 | + logDebug('tryGetOpencvBinDir', 'auto build has not been explicitly disabled via environment variable, attempting to read envs from package.json...'); |
| 26 | + // const envs = builder.env.readEnvsFromPackageJson() |
| 27 | + if (!builder.env.isAutoBuildDisabled && process.env.OPENCV_BIN_DIR) { |
| 28 | + logDebug('tryGetOpencvBinDir', 'auto build has not been disabled via package.json, using opencv bin dir of opencv-build'); |
| 29 | + return process.env.OPENCV_BIN_DIR; //.opencvBinDir |
| 30 | + } |
| 31 | + if (builder.env.opencvBinDir) { |
| 32 | + logDebug('tryGetOpencvBinDir', 'found opencv binary environment variable in package.json'); |
| 33 | + return builder.env.opencvBinDir; |
| 34 | + } |
| 35 | + logDebug('tryGetOpencvBinDir', 'failed to find opencv binary environment variable in package.json'); |
| 36 | + return null; |
| 37 | +} |
| 38 | +function getOpenCV(opt) { |
| 39 | + if (!opt) |
| 40 | + opt = { prebuild: 'latestBuild' }; |
| 41 | + const builder = new opencv_build_1.OpenCVBuilder(opt); |
| 42 | + let opencvBuild = null; |
| 43 | + let requirePath = ''; |
| 44 | + if ((0, commons_1.isElectronWebpack)()) { |
| 45 | + requirePath = '../build/Release/opencv4nodejs.node'; |
| 46 | + } |
| 47 | + else { |
| 48 | + requirePath = path_1.default.join(__dirname, '../build/Debug/opencv4nodejs.node'); |
| 49 | + if (!fs_1.default.existsSync(requirePath)) { |
| 50 | + requirePath = path_1.default.join(__dirname, '../build/Release/opencv4nodejs.node'); |
| 51 | + } |
| 52 | + requirePath = requirePath.replace(/\.node$/, ''); |
| 53 | + // path.join(__dirname, process.env.BINDINGS_DEBUG ? '../build/Debug/opencv4nodejs' : '../build/Release/opencv4nodejs') |
| 54 | + } |
| 55 | + try { |
| 56 | + logDebug('require', `require path is ${picocolors_1.default.yellow(requirePath)}`); |
| 57 | + opencvBuild = require(requirePath); |
| 58 | + } |
| 59 | + catch (err) { |
| 60 | + // err.code === 'ERR_DLOPEN_FAILED' |
| 61 | + logDebug('require', `failed to require cv with exception: ${picocolors_1.default.red(err.toString())}`); |
| 62 | + logDebug('require', 'attempting to add opencv binaries to path'); |
| 63 | + if (!process.env.path) { |
| 64 | + logDebug('require', 'there is no path environment variable, skipping...'); |
| 65 | + throw err; |
| 66 | + } |
| 67 | + const opencvBinDir = tryGetOpencvBinDir(builder); |
| 68 | + logDebug('require', 'adding opencv binary dir to path: ' + opencvBinDir); |
| 69 | + if (!fs_1.default.existsSync(opencvBinDir)) { |
| 70 | + throw new Error('opencv binary dir does not exist: ' + opencvBinDir); |
| 71 | + } |
| 72 | + // ensure binaries are added to path on windows |
| 73 | + if (!process.env.path.includes(opencvBinDir)) { |
| 74 | + process.env.path = `${process.env.path};${opencvBinDir};`; |
| 75 | + } |
| 76 | + logDebug('require', 'process.env.path: ' + process.env.path); |
| 77 | + try { |
| 78 | + opencvBuild = require(requirePath); |
| 79 | + } |
| 80 | + catch (e) { |
| 81 | + if (e instanceof Error) { |
| 82 | + let msg = ''; |
| 83 | + const message = e.message; |
| 84 | + if (message.startsWith('Cannot find module')) { |
| 85 | + msg = `require("${picocolors_1.default.yellow(requirePath)}"); |
| 86 | + Failed with: ${picocolors_1.default.red(message)}, openCV binding not available, reed: |
| 87 | + build-opencv --help |
| 88 | + And build missing file with: |
| 89 | + npx build-opencv --version 4.6.0 rebuild |
| 90 | + |
| 91 | + PS: a 'npm link' may help |
| 92 | + `; |
| 93 | + } |
| 94 | + else if (message.startsWith('The specified module could not be found.')) { |
| 95 | + msg = `require("${picocolors_1.default.yellow(requirePath)}"); |
| 96 | + Failed with: ${picocolors_1.default.red(message)}, openCV module looks broken, clean you builds directory and rebuild everything |
| 97 | + rm -r <path to your build directory> |
| 98 | + npx build-opencv --version 4.6.0 rebuild |
| 99 | + `; |
| 100 | + } |
| 101 | + else { |
| 102 | + msg = `require("${picocolors_1.default.yellow(requirePath)}"); |
| 103 | + Failed with: ${picocolors_1.default.red(message)} |
| 104 | + `; |
| 105 | + } |
| 106 | + throw Error(msg); |
| 107 | + } |
| 108 | + throw e; |
| 109 | + } |
| 110 | + } |
| 111 | + // resolve haarcascade files |
| 112 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 113 | + const { haarCascades, lbpCascades } = opencvBuild; |
| 114 | + Object.keys(haarCascades).forEach(key => opencvBuild[key] = (0, commons_1.resolvePath)(path_1.default.join(__dirname, 'haarcascades'), haarCascades[key])); |
| 115 | + Object.keys(lbpCascades).forEach(key => opencvBuild[key] = (0, commons_1.resolvePath)(path_1.default.join(__dirname, 'lbpcascades'), lbpCascades[key])); |
| 116 | + return opencvBuild; |
| 117 | +} |
| 118 | +exports.getOpenCV = getOpenCV; |
| 119 | +exports.default = getOpenCV; |
0 commit comments