Script to auto build recent OpenCV + contrib via npm 2024 Edition
npm install @u4/opencv-build

A simple script to auto build recent OpenCV + contrib version via npm. This
script is used to auto build
_opencv4nodejs_.
- OpenCV is explicitly build with opencv-build-npm and accepting parametes see
opencv-build-npm --help
- OpenCV build can now be configured with
new OpenCVBuilder({autoBuildOpencvVersion: "3.4.16", autoBuildBuildCuda: true, autoBuildWithoutContrib: false }).install()
- Each OPENCV_VERSION will be build in his own directory.
- Each AUTOBUILD_FLAGS will be build in his own directory. (induce massive time
gain during development)
- All build can now be build outside node_modules using OPENCV_BUILD_ROOT
environement variable.
- New MSBuild.exe localisation, also works with VS 2019
- Script output is now colorized.
- Add some usefull log.
- Big code refactor.
- Enfoce typing.
- Add comments and documentations.
Each OpenCV build will take around 2Gb on your drive, so I recommand you to
define the OPENCV_BUILD_ROOT environement variable to avoid duplicate
buildsand avoid node_modules auto flushs.
``bash`
npm install opencv-build
`bash`
npm-opencv-build usage:
--version
--flags
--root
--buildRoot
--cuda Enable cuda in OpenCV build (OPENCV4NODEJS_BUILD_CUDA env variable)
--cudaArch
ex if you have a RTX 3080 use --cudaArch=8.6, if you have also a RTX 2080 --cudaArch=7.5,8.6
(OPENCV4NODEJS_BUILD_CUDA_ARCH env variable)
--nocontrib Do not compile Contrib modules (OPENCV4NODEJS_AUTOBUILD_WITHOUT_CONTRIB env variable)
--nobuild Do build OpenCV (OPENCV4NODEJS_DISABLE_AUTOBUILD env variable)
--incDir
--libDir
--binDir
--keepsources Keepsources OpenCV source after build
--dry-run Display command line use to build library
--git-cache Reduce Bandwide usage, by keeping a local git souce un the buildRoot (OPENCV_GIT_CACHE env variable)
- cmake
for nodejs <= 12
`bash`
npm install --global windows-build-tools
It's possible to specify build environment variables by passing argument to the
builder script
`bash`
node lib/main.js --version 4.5.5 --buildRoot ~/openCV
with flags, do not forget the quotes "
`bash`
node lib/main.js --version 4.5.5 --buildRoot ~/openCV --flags "-DOPENCV_GENERATE_PKGCONFIG=ON -DOPENCV_PC_FILE_NAME=opencv.pc"
Using the bin alias
`bash`
opencv-build-npm --version 4.5.5
Or by inserting them into the package.json where the dependency is declared an
object like:
`json`
{
"opencv4nodejs": {
"autoBuildFlags": "-DOPENCV_GENERATE_PKGCONFIG=ON -DOPENCV_PC_FILE_NAME=opencv.pc",
"autoBuildOpencvVersion": "4.6.0"
}
}
By using environement varaibles.
`bash
export OPENCV4NODEJS_AUTOBUILD_FLAGS="-DOPENCV_GENERATE_PKGCONFIG=ON -DOPENCV_PC_FILE_NAME=opencv.pc"
export OPENCV4NODEJS_AUTOBUILD_OPENCV_VERSION="4.6.0"
export OPENCV_BUILD_ROOT="~/openCV"
node build/main.js
`
The prebuild is a smart version selector, to avoid futher re-compilation,
accepted values are:
- "latestBuild" use the last built version"latestVersion"
- use the highest version number built"oldestBuild"
- use the olderst built version"oldestVersion"
- use the lowest version number built
the prebuild option intend to be use at runtime, so you do not have to keep
trak of the version you want to use.
- this parameter can only be provide in OpenCVBuildEnv constructor options.prebuild
- is ignored if OPENCV4NODEJS_DISABLE_AUTOBUILD env variable is set,
or disableAutoBuild is set in package.json
Choose the openCV version you want to build, default is 4.5.5,
This option value can be provide using:
- The --version in build scriptautoBuildOpencvVersion
- The options field provided to OpenCVBuildEnvautoBuildOpencvVersion
constructor options.
- The field in the current package.json opencv4nodejsOPENCV4NODEJS_AUTOBUILD_OPENCV_VERSION
object.
- The environement variable.
The buildRoot is the most important parameter, it define the directory used tonode_modules
build openCV, Default value is the npm-opencv-build directory. You generaly want
to use this value to persist your build files out of your
directory, and buy doing som share openCV built between your project.
This option value can be provide using:
- The --buildRoot in build scriptbuildRoot
- The options field provided to OpenCVBuildEnv constructorOPENCV_BUILD_ROOT
options.
- The environement variable.
The git-cache reduce git clone data transfert, git data will be cache in youbuildRoot, so you will onlyt downdload all git file once.
This option value can be provide using:
- The --git-cache in build scriptgit-cache
- The options field provided to OpenCVBuildEnv constructorOPENCV_GIT_CACHE
options.
- The environement variable.
Set any value to enable, the following cMake flag will be added:
- DWITH_CUDA=ON
- DBUILD_opencv_cudacodec=OFF // video codec (NVCUVID) is deprecated in cuda 10,
so don't add it
- DCUDA_FAST_MATH=ON // optional
- DWITH_CUBLAS=ON // optional
This option value can be enable using:
- The --cuda in build scriptautoBuildBuildCuda
- The options field provided to OpenCVBuildEnvautoBuildBuildCuda
constructor options.
- The field in the current package.json opencv4nodejsOPENCV4NODEJS_BUILD_CUDA
object.
- The environement variable.
Append option to CMake flags.
This option value can be enable using:
- The --flags in build scriptautoBuildFlags
- The options field provided to OpenCVBuildEnv constructorautoBuildFlags
options.
- The field in the current package.json opencv4nodejs object.OPENCV4NODEJS_AUTOBUILD_FLAGS
- The environement variable.
Set any value to enable, this option will skip openCV Contribs.
This option value can be enable using:
- The --nocontrib in build scriptautoBuildWithoutContrib
- The options field provided to OpenCVBuildEnvautoBuildWithoutContrib
constructor options.
- The field in the current package.jsonopencv4nodejs
object.OPENCV4NODEJS_AUTOBUILD_WITHOUT_CONTRIB
- The environement variable.
Set any value to disable compilation from sources.
This option value can be enable using:
- The --nobuild in build scriptdisableAutoBuild
- The options field provided to OpenCVBuildEnv constructordisableAutoBuild
options.
- The field in the current package.json opencv4nodejsOPENCV4NODEJS_DISABLE_AUTOBUILD
object.
- The environement variable.
Generaly you should prefer using the environment variables
_OPENCV4NODEJS_DISABLE_AUTOBUILD_
Over write the _OPENCV_INCLUDE_DIR_ environment variables
Over write the _OPENCV_LIB_DIR_ environment variables
Over write the _OPENCV_BIN_DIR_ environment variables
`bash`
opencv-build-npm --flags="-DBUILD_LIST=core,imgproc,imgcodecs,videoio,highgui,video,calib3d,features2d,objdetect,dnn,ml,flann,photo,stitching,gapi" --version=3.4.15 --nocontrib
`bash``
opencv-build-npm --version=4.5.5 --nocontrib