Repository: opencv
Issue: Opencv official nodejs binding
Labels: feature

### Descripe the feature and motivation

This issue is following a 2 years long conversation we are having with @UrielCh on the opencv slack (you can find us in the #opencv4nodejs channel)

## A Nodejs binding for Opencv

The goal of this feature request is to get a Nodejs binding for opencv, auto-generated from C-Headers the same way [Python, Java, Obj-C binding are autogenerated from C Headers](https://docs.opencv.org/3.4/da/d49/tutorial_py_bindings_basics.html)

This will include : 
* Conventions to transform C functions/names/parameters/Class in nodejs
* A python generation code from C headers similar to [gen2.py](https://github.com/opencv/opencv/blob/de2f7666fbe6db38088d3cd70209c55252e756b7/modules/python/src2/gen2.py)
* Some C++ macros similar to [opencv4nodejs's ones](https://github.com/UrielCh/opencv4nodejs/tree/master/cc/native-node-utils)
* Code to integrate it as a C++ module included into the Opencv compilation process
* Unit tests inspired from [opencv4nodejs's unit tests](https://github.com/UrielCh/opencv4nodejs/tree/master/test)

And it should generate : 
* C++ headers
* Typescript typing information
* Nodejs OpenCV Documentation
* npm packagable project

## Milestones


__Vocabulary :__

__C-Python binding__ : the C code that create the Python module
__C-Nodejs binding__ : the C code that create the Nodejs module
__Generator-Python__ : Existing Python code that generate C-Python binding code from opencv C headers
__Generator-Nodejs__ : Python code to be written that generate C-Nodejs binding code from opencv C headers

Status is the following : 

## Step 0 : set up environment

- [x] Create an outside project for experimenting -> https://github.com/UrielCh/opencv-sandbox
- [x] Run the __Generator-Python__ outside of the opencv project

## Step 1: Make a __Generator-Nodejs__ v1

- [x] Translate the python-C helpers function into C nodejs helpers functions done in [cc-common](https://github.com/UrielCh/opencv-sandbox/tree/class/cc-common)
- [x] Translate the python C templates helpers function into python helpers functions done in [cc-common](https://github.com/UrielCh/opencv-sandbox/tree/class/cc-common)
- [x] Generate Nodejs Code and make it compile

## Step 2: Discuss __Generator-Nodejs__ design

- [x] Generate the typescript signatures (generated in `cc-generated/cv.d.ts`)
- [x] Review the __C-Nodejs binding__ design 
- [ ] 
## Step 3 : nodejs clean binding

- [x] On a simple example class (`AKAZE`) write manually the __C-Nodejs binding__ files `.h` and `.cpp` (done in https://github.com/UrielCh/opencv-sandbox/tree/class/cc-obj)
- [ ] Review the class `.h` and `.cpp` implementation against nodejs binding best practices
- [ ] Write a python unit test for the generator and compare the generated string with the expected one [IN PROGRESS @piercus]
- [ ] Create more generator unit test, some having function variants signatures, other with create/without create, with constructor/without constructor
- [ ] Take some nodejs unit test from https://github.com/justadudewhohacks/opencv4nodejs/tree/master/test/tests and put it in the project to test the final module
- [ ] Implement the generator and the helpers to make everything pass

## Step 4 : integrate it back to opencv

- [ ] Make a point with the opencv team to present the status of the binding
- [ ] Integrate it into a larger build process, create the CMakeFile ...

### Additional context

## Opencv4nodejs

[Opencv4nodejs](https://github.com/justadudewhohacks/opencv4nodejs) is a native nodejs binding for nodejs made by @justadudewhohacks few years ago.

The original repo is not maintained since 2019, since 2021 we are trying to redirect the community to [UrielCh's fork](https://github.com/urielch/opencv4nodejs)

Opencv4nodejs is widely used, but it has following problems : 
* Main author has been lost
* It is not covering the whole lib
* Compilation is non-optimal

### Main author has been lost

The author @justadudewhohacks has been lost by the community, leaving me and @UrielCh as mainmteners working on @UrielCh's fork, this create a lot of issues for the community [state of npm package](https://github.com/justadudewhohacks/opencv4nodejs/issues/860), [Repo Searchability](https://github.com/UrielCh/opencv4nodejs/issues/68), 

### It is not covering the whole lib

The coverage of opencv4nodejs is quite good, (it can be seen in [the doc](https://justadudewhohacks.github.io/opencv4nodejs/docs/Mat/) )

But there is still no plan to provide a 100% full coverage because the C++ bindings is not generated from Python (as it is the case for official Python, Java and Obj-C bindings)

This non-generated situation creates other problems like : 
* Arguments missing in some function
* Maintainability must be hand-made
* Wording convention from C to nodejs is not 100% coherent through the project

### Compilation is non-optimal

Most of the people raising issues on opencv4nodejs are complaining about the unability to compile the project.

This compilation problems are related to both organisational and technical situation.

**Technical situation** : 
* The current binding use [nan](https://github.com/nodejs/nan), we think [node-addon-api](https://github.com/nodejs/node-addon-api) would be a better choice to allow pre-built retro-compatible binary  
* The compilation of the binding is not made in 2 steps, first you need to compile opencv and then you need to compile the binding, it creates lot issues where you do not know exactly where the problem is coming from. The binding behing very dependant to the opencv version used, a lot of code has been written in order to check which version is installed, how to pass flags from the nodejs project to opencv4nodejs compilation process
* A [specific project](https://github.com/justadudewhohacks/npm-opencv-build) has been made to give the ability to compile opencv from the npm installer 

**Organisational situation**, most of the people involved in the opencv4nodejs community are nodejs's people, typescript's devs, to put it simply, we don't like compiling things, CMake ...

## Opencv.js

I'm less expert on Opencv.js, so i will let others give their feedbacks.

Opencv.js is made for the browser, this is not a binding to OpenCv but more a [browser compatible generated code](https://docs.opencv.org/4.x/df/d0a/tutorial_js_intro.html)

I feel this is totally different for the following reasons : 
* Opencv.js will only access browser-accessible ressources of your computer
* Opencv.js will never get full coverage of the OpenCv library