opencv4nodejs

repository·master·Indexed 43 days ago

https://github.com/urielch/opencv4nodejs

Asynchronous Node.js bindings for OpenCV 3.x and 4.x with JavaScript and TypeScript APIs. Provides comprehensive access to OpenCV and OpenCV-contrib modules, including support for Mat, Vec, and Point data structures, image I/O, video capture, and integration with YOLOv3 for object detection. Version 7.1.2.

Tokens
8K
Snippets
38
Records
39
Agent score
54%

What's inside opencv4nodejs

  1. Use Async API with Promises or Callbacks

    master

    The library provides an asynchronous API. You can consume it in three ways:

    1. Callbacks: Pass a callback function as the last argument (e.g., cv.imreadAsync(path, (err, mat) => { ... })).
    2. Promises: If no callback is provided, the method returns a Promise.
    3. Async/Await: Use await with the Promise-returning methods for cleaner code.

    Note: Many methods have an Async suffix (e.g., detectMultiScaleAsync, bgrToGrayAsync).

    const cv = require('@u4/opencv4nodejs');
    
    // Using async/await
    try {
      const img = await cv.imreadAsync('./faceimg.jpg');
      const grayImg = await img.bgrToGrayAsync();
      const { objects, numDetections } = await classifier.detectMultiScaleAsync(grayImg);
    } catch (err) {
      console.error(err);
    }
  2. Use your own OpenCV build (Manual Installation)

    master

    If you want to use a pre-existing OpenCV installation instead of letting the package build its own, you must disable the automatic build process. You can do this in three ways:

    1. Environment Variables

    Set OPENCV4NODEJS_DISABLE_AUTOBUILD=1.

    If your OpenCV installation is not in a standard location (like those managed by Chocolatey, apt, or brew), you must also define the following:

    • OPENCV_INCLUDE_DIR: Path to the include directory containing the opencv2 subfolder.
    • OPENCV_LIB_DIR: Path to the directory containing OpenCV .lib files.
    • OPENCV_BIN_DIR: Path to the directory containing OpenCV .dll files (on Windows).

    Note for Windows: You must also add %OPENCV_BIN_DIR% to your system PATH environment variable and restart your console session.

    2. package.json Configuration

    Add an opencv4nodejs section to your package.json:

    "opencv4nodejs": {
      "disableAutoBuild": "1",
      "OPENCV_INCLUDE_DIR": "path/to/include",
      "OPENCV_LIB_DIR": "path/to/lib",
      "OPENCV_BIN_DIR": "path/to/bin"
    }

    3. Using build-opencv CLI

    Run the build-opencv command with the --nobuild rebuild flag:

    npm link
    build-opencv --nobuild rebuild

    Or, if paths are non-standard:

    npm link
    build-opencv --incDir /usr/include/opencv4/ --libDir /lib/x86_64-linux-gnu/ --binDir=/usr/bin/ --nobuild rebuild
    "opencv4nodejs": {
      "disableAutoBuild": "1"
    }
  3. Configure Windows Build Tools

    master

    On Windows, you need Windows Build Tools to compile OpenCV and opencv4nodejs. If you do not have Visual Studio or Windows Build Tools installed, you can install the VS2015 build tools globally via npm.

    npm install --global windows-build-tools
  4. Install @u4/opencv4nodejs via npm

    master

    Install the package using npm. Note that native node modules are built via node-gyp, which requires Python to be installed on your system.

    Important: Ensure your project directory path does not contain any whitespaces, as node-gyp may fail to handle them correctly, leading to header file errors (e.g., fatal error C1083: Cannot open include file: 'opencv2/core.hpp').

    npm install --save @u4/opencv4nodejs
  5. Train the YOLOv3 Model

    master

    Once Darknet is compiled, download the pretrained weights and start the training process.

    1. Download pretrained weights:
      wget https://pjreddie.com/media/files/darknet53.conv.74 -O darknet53.conv.74
    
    2. **Run Training**:
       Execute the darknet detector training command, providing paths to your `.data` file, `.cfg` file, and the downloaded `.conv.74` weights. Output is redirected to `train.log`:
       ```bash
       ~/darknet-vNext/darknet detector train darknet.data darknet-yolov3.cfg darknet53.conv.74 > train.log

    Upon completion, the trained weights will be located at ./weights/darknet-yolov3_final.weights.

    wget https://pjreddie.com/media/files/darknet53.conv.74 -O darknet53.conv.74
    ~/darknet-vNext/darknet detector train darknet.data darknet-yolov3.cfg darknet53.conv.74 > train.log
  6. Install OpenCV via Auto Build Script

    master

    By default, installing @u4/opencv4nodejs triggers an auto-build script using the opencv-build npm package. This requires git and a recent version of cmake to be installed on your system.

    Customizing the Build

    You can control the build process using environment variables:

    • Custom Flags: Use OPENCV4NODEJS_AUTOBUILD_FLAGS to pass space-separated CMake flags. This is useful for building only specific modules to reduce compilation time (e.g., export OPENCV4NODEJS_AUTOBUILD_FLAGS=-DBUILD_LIST=dnn).
    • Specific Version: Set OPENCV4NODEJS_AUTOBUILD_OPENCV_VERSION to install a specific OpenCV version (e.g., export OPENCV4NODEJS_AUTOBUILD_OPENCV_VERSION=4.6.0).
    export OPENCV4NODEJS_AUTOBUILD_FLAGS=-DBUILD_LIST=dnn
    export OPENCV4NODEJS_AUTOBUILD_OPENCV_VERSION=4.6.0
  7. Prepare Data for YOLOv3 Training

    master

    Follow these steps to prepare your dataset from OpenImages:

    1. Fetch Data: Run the script to download and locate relevant OpenImages files and OpenImagesV4 data:

      ts-node getDataFromOpenImages_snowman.ts
    2. Split Dataset: Create the train-test split from the JPEGImages directory:

      ts-node splitTrainAndTest.ts JPEGImages
    ts-node getDataFromOpenImages_snowman.ts
    ts-node splitTrainAndTest.ts JPEGImages
  8. Train a YOLOv3 Object Detector for Snowmen

    master

    This guide outlines the process of training a custom YOLOv3 object detector specifically for detecting snowmen using OpenImages data and the Darknet framework. The process involves gathering data, splitting it into training/testing sets, compiling Darknet, training the model, and finally running detection using TypeScript.

    Prerequisites & Dependencies

    First, install the necessary Node.js dependencies and global TypeScript tools:

    pnpm install
    pnpm install -g ts-node typescript @types/node

    (Optional) To suppress warnings when using ts-node, you can set the following environment variable: export NODE_OPTIONS="-r ts-node/register --no-warnings"

  9. Compile Darknet for YOLOv3 Training

    master

    To train the model, you must install and compile the darknet-vNext repository.

    1. Clone the repository:

      cd ~
      git clone https://github.com/sowson/darknet-vNext
      cd darknet-vNext
    2. Configure the Makefile: Edit the first lines of the Makefile to enable or disable specific features (e.g., GPU, CUDNN, OPENCV, OPENMP, DEBUG). Example configuration:

      • GPU=0
      • CUDNN=0
      • OPENCV=0
      • OPENMP=0
      • DEBUG=0
    3. Compile:

      make
    cd ~
    git clone https://github.com/sowson/darknet-vNext
    cd darknet-vNext
    make
  10. Build OpenCV using the built-in builder

    master

    To build OpenCV automatically using the built-in builder, you can specify the version and optimize the build process.

    Speed up builds

    Define the OPENCV_BUILD_ROOT environment variable to process the OpenCV build outside of your node_modules folder.

    OPENCV_BUILD_ROOT=~/opencv

    Specify OpenCV version

    You can control which version is built using one of these methods:

    1. Environment Variable: Set OPENCV4NODEJS_AUTOBUILD_OPENCV_VERSION="4.9.0".

    2. package.json:

    "opencv4nodejs": {
      "autoBuildOpencvVersion": "4.6.0"
    }

    3. build-opencv CLI:

    npm link
    build-opencv --version 4.6.0 rebuild
  11. Use @u4/opencv4nodejs with Electron

    master

    To use the module with Electron, you must rebuild it for the Electron runtime. Add a rebuild script to your package.json using the build-opencv command with the --electron flag.

    "electron-rebuild": "build-opencv --electron --version 4.5.4 build"

    Then run:

    npm run electron-rebuild
  12. Use @u4/opencv4nodejs with Docker

    master

    To avoid building OpenCV inside your container, you can use the pre-built image urielch/opencv-nodejs.

    Important: Since the image already has opencv4nodejs installed globally, do not include @u4/opencv4nodejs in your package.json. Instead, use the global installation by setting the NODE_PATH environment variable to /usr/lib/node_modules in your Dockerfile.

    FROM urielch/opencv-nodejs