Skip to content

Commit

Permalink
Merge pull request #133 from UrielCh/esm
Browse files Browse the repository at this point in the history
## Version 7.0.0
- Updated code to ESM.
- Introduced new exports model.
- Fixed typing errors.
- Added missing types.
- Removed typing duplication in codebase.
- Native code is now generated directly by the default node-gyp rebuild.
- GitHub actions now test all available Windows, macOS, and Linux environments.
  • Loading branch information
UrielCh authored Apr 30, 2024
2 parents f8b7067 + 6ac85ed commit 5e13426
Show file tree
Hide file tree
Showing 155 changed files with 1,536 additions and 4,557 deletions.
42 changes: 0 additions & 42 deletions .eslintrc

This file was deleted.

2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,5 @@ cjs/install/
cjs/lib/
esm/install/
esm/lib/
u4-opencv4nodejs-*.tgz
examples/yolo_out_py.avi
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# changelog

## Version 7.0.0
- Updated code to ESM.
- Introduced new exports model.
- Fixed typing errors.
- Added missing types.
- Removed typing duplication in codebase.
- Native code is now generated directly by the default node-gyp rebuild.
- GitHub actions now test all available Windows, macOS, and Linux environments.

## Version 6.6.0
- update gcc flag to enable C++ 17
- fix code to support latest onpenCV version up to 4.9
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
Opencv4nodejs can be linked to a prebuild openCV 3 or 4. or can build its own openCV using [@u4/opencv-build](https://www.npmjs.com/package/@u4/opencv-build),
In this case, you have to choose which version you want to link.

> Since version 7, you should be able to use this module directly without any configuration if you have OpenCV installed from your package manager (Chocolatey / Brew / apt).
### To use your OWN openCV build

**3 way to use your own openCV (Without automatic building)**
Expand Down Expand Up @@ -54,7 +56,7 @@ OPENCV_BUILD_ROOT=~/opencv

#### 1) Environment variable
Define environment variable:
- `OPENCV4NODEJS_AUTOBUILD_OPENCV_VERSION`="4.6.0"
- `OPENCV4NODEJS_AUTOBUILD_OPENCV_VERSION`="4.9.0"

#### 2) package.json
Define an opencv4nodejs section in your package.json like:
Expand Down Expand Up @@ -660,7 +662,7 @@ try {
## With TypeScript

``` javascript
import * as cv from '@u4/opencv4nodejs'
import cv from '@u4/opencv4nodejs'
```

Check out the TypeScript [examples](https://github.com/urielch/opencv4nodejs/tree/master/examples).
Expand Down
13 changes: 7 additions & 6 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ shallow_clone: true
# what combinations to test
environment:
OPENCV3_N0: 3.4.16 # 2021-10-11
#OPENCV4_N_0: 4.6.0 # 2022-06-12
OPENCV4_N0: 4.5.5 # 2022-06-12
OPENCV4_N1: 4.5.0 # 2021-12-30
OPENCV4_N2: 4.4.0 # 2020-07-18
OPENCV4_N0: 4.9.0 # 2022-06-12
# OPENCV4_N0: 4.5.5 # 2022-06-12
# OPENCV4_N1: 4.5.0 # 2021-12-30
# OPENCV4_N2: 4.4.0 # 2020-07-18

# OPENCV4_N0: 4.5.3 #
# OPENCV4_N1: 4.5.2 #
Expand Down Expand Up @@ -69,8 +69,8 @@ environment:
install:
- cmd: choco install OpenCV -y --version %OPENCVV%
- if not "%BUILD_TASK%" == "ENVS" SET OPENCV_INCLUDE_DIR=c:\tools\opencv\build\include
- if not "%BUILD_TASK%" == "ENVS" SET OPENCV_LIB_DIR=c:\tools\opencv\build\x64\vc14\lib
- if not "%BUILD_TASK%" == "ENVS" SET OPENCV_BIN_DIR=c:\tools\opencv\build\x64\vc14\bin
- if not "%BUILD_TASK%" == "ENVS" SET OPENCV_LIB_DIR=c:\tools\opencv\build\x64\vc16\lib
- if not "%BUILD_TASK%" == "ENVS" SET OPENCV_BIN_DIR=c:\tools\opencv\build\x64\vc16\bin
- if not "%BUILD_TASK%" == "ENVS" SET PATH=%PATH%;%OPENCV_BIN_DIR%;
- ps: Install-Product node $env:NODEV x64
# - node --version
Expand All @@ -86,6 +86,7 @@ test_script:
- cd c:\projects\opencv4nodejs
- pnpm install --frozen-lockfile
- pnpm run prepack
- pnpm run rebuild
# - npm link
# - build-opencv rebuild
# - cd c:\projects\opencv4nodejs\ci\envs && build-opencv rebuild && pnpm install && pnpm test
Expand Down
2 changes: 1 addition & 1 deletion bin/install.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env node
const compileLib = require("../install/compileLib.js");
const compileLib = require("../cjs/install/compileLib.js");
const {argv} = process;
compileLib.compileLib(argv);
4 changes: 2 additions & 2 deletions bin/install.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env node
import * as compileLib from "../install/compileLib";
import { compileLib } from "../src/install/compileLib";
const {argv} = process;
compileLib.compileLib(argv);
compileLib(argv);
4 changes: 4 additions & 0 deletions cc/dnn/dnn.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ NAN_MODULE_INIT(Dnn::Init) {
FF_CONST_TYPE(DNN_TARGET_HDDL, cv::dnn::DNN_TARGET_HDDL)
#endif

#if CV_VERSION_GREATER_EQUAL(4, 8, 0)
FF_CONST_TYPE(DNN_BACKEND_WEBNN, cv::dnn::DNN_BACKEND_WEBNN)
#endif


Net::Init(target);

Expand Down
3 changes: 3 additions & 0 deletions cjs/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "commonjs"
}
36 changes: 36 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// @ts-check
import eslint from '@eslint/js';
import tseslint from 'typescript-eslint';

export default tseslint.config(
eslint.configs.recommended,
...tseslint.configs.recommended,
{
ignores: [
"test/**/*.ts",
"**/*.js",
"types/**/*.d.ts",
"src/lib/meta.ts",
],
}
,
{"rules": {
"linebreak-style": 0,
"comma-dangle": ["error", "always-multiline"],
"no-plusplus": "off",
"import/no-unresolved": 0,
"import/extensions": 0,
"import/no-extraneous-dependencies": 0,
"no-async-promise-executor": 0,
"radix": 1,
"no-undef": 0,
"no-case-declarations": 0,
// unit tests only
// "max-len" : 0,
// "no-new" : 0,
// "no-unused-expressions": 0,
// "prefer-spread": 0,
// "@typescript-eslint/ban-ts-comment": 0,
// "@typescript-eslint/no-explicit-any": 0
}},
);
3 changes: 3 additions & 0 deletions esm/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "module"
}
9 changes: 5 additions & 4 deletions examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@
"name": "opencv4nodejs-examples",
"version": "0.0.0",
"description": "example for opencv4nodejs",
"main": "EASTTextDetection.js",
"main": "src/EASTTextDetection.js",
"type": "module",
"scripts": {
"clean": "rimraf src/**/*.js src/**/*.map *.js.map *.js *.log"
"clean": "rimraf src/*.js src/**/*.js src/**/*.map *.js.map *.js *.log"
},
"author": "",
"license": "MIT",
"dependencies": {
"@types/lodash.samplesize": "^4.2.9",
"@u4/opencv4nodejs": "link:..",
"axios": "^1.2.2",
"axios": "^1.6.8",
"lodash.samplesize": "^4.2.0",
"mri": "^1.2.0",
"p-limit": "3.1.0",
"p-limit": "5.0.0",
"picocolors": "^1.0.0",
"progress": "^2.0.3"
},
Expand Down
32 changes: 16 additions & 16 deletions examples/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 16 additions & 11 deletions examples/src/AgeGender/AgeGender.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import fs from 'fs';
import fs from 'node:fs';
import mri from 'mri';
import * as openCV from '@u4/opencv4nodejs';
import { Mat, Net, Point2, Rect, Size, Vec3, VideoCapture } from '@u4/opencv4nodejs';
import { cv, getCachedFile, wait4key } from '../utils';
import path from 'path';
import NetIdentifier from './NetIdentifier';
import { cv, getCachedFile, getExampleDirname, wait4key } from '../utils.js';
import NetIdentifier from './NetIdentifier.js';

// ported from https://github.com/spmallick/learnopencv/blob/master/AgeGender/AgeGender.py

Expand Down Expand Up @@ -48,23 +48,28 @@ if (args.help) {
}

const main = async () => {
const faceProto = path.resolve(__dirname, "opencv_face_detector.pbtxt")
const faceModel = await getCachedFile(path.resolve(__dirname, "opencv_face_detector_uint8.pb"), 'https://github.com/spmallick/learnopencv/raw/master/AgeGender/opencv_face_detector_uint8.pb')
const faceProto = getExampleDirname("AgeGender", "opencv_face_detector.pbtxt")
const faceModel = await getCachedFile(getExampleDirname("AgeGender", "opencv_face_detector_uint8.pb"), 'https://github.com/spmallick/learnopencv/raw/master/AgeGender/opencv_face_detector_uint8.pb')

const ageProto = path.resolve(__dirname, "age_deploy.prototxt")
const ageProto = getExampleDirname("AgeGender", "age_deploy.prototxt")
// 44 MB file
const ageModel = path.resolve(__dirname, "age_net.caffemodel") // https://www.dropbox.com/s/xfb20y596869vbb/age_net.caffemodel?dl=0
const ageModel = getExampleDirname("AgeGender", "age_net.caffemodel") // https://www.dropbox.com/s/xfb20y596869vbb/age_net.caffemodel?dl=0

const genderProto = path.resolve(__dirname, "gender_deploy.prototxt")
const genderProto = getExampleDirname("AgeGender", "gender_deploy.prototxt")
// 44 MB file
const genderModel = path.resolve(__dirname, "gender_net.caffemodel") // https://www.dropbox.com/s/iyv483wz7ztr9gh/gender_net.caffemodel?dl=0
const genderModel = getExampleDirname("AgeGender", "gender_net.caffemodel") // https://www.dropbox.com/s/iyv483wz7ztr9gh/gender_net.caffemodel?dl=0

const MODEL_MEAN_VALUES = new Vec3(78.4263377603, 87.7689143744, 114.895847746)

// Load network
if (!fs.existsSync(ageModel)) {
throw Error(`fail to read ${ageModel}`);
throw Error(`fail to read ${ageModel} please download if from https://www.dropbox.com/s/xfb20y596869vbb/age_net.caffemodel?dl=0`);
}

if (!fs.existsSync(genderModel)) {
throw Error(`fail to read ${genderModel} please download if from https://www.dropbox.com/s/iyv483wz7ztr9gh/gender_net.caffemodel?dl=0`);
}

if (!fs.existsSync(ageProto)) {
throw Error(`fail to read ${ageProto}`);
}
Expand Down
1 change: 0 additions & 1 deletion examples/src/AgeGender/NetIdentifier.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import cv, { Net, Mat } from "@u4/opencv4nodejs";


const getMaxIndex = (scores: number[]): number => {
let max = Number.MIN_VALUE;
let classId = -1;
Expand Down
11 changes: 6 additions & 5 deletions examples/src/EASTTextDetection.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'path';
import { cv, drawBlueRect, getCachedFile, getResourcePath, wait4key } from './utils';
import path from 'node:path';
import type { Mat, Rect } from '@u4/opencv4nodejs';
import { cv, drawBlueRect, getCachedFile, getResourcePath, wait4key } from './utils.js';

/**
* Text detection simple code example.
Expand Down Expand Up @@ -45,7 +45,7 @@ function decode(scores: Mat, geometry: Mat, confThreshold = MIN_CONFIDENCE) {
startX,
startY,
endX - startX,
endY - startY
endY - startY,
));
confidences.push(score);
}
Expand Down Expand Up @@ -81,7 +81,8 @@ async function detection(modelPath: string, imgAbsPath: string): Promise<void> {
const indices = cv.NMSBoxes(
boxes,
confidences,
MIN_CONFIDENCE, NMS_THRESHOLD
MIN_CONFIDENCE,
NMS_THRESHOLD,
);

indices.forEach((i) => {
Expand All @@ -90,7 +91,7 @@ async function detection(modelPath: string, imgAbsPath: string): Promise<void> {
rect.x * widthRatio,
rect.y * heightRatio,
rect.width * widthRatio,
rect.height * heightRatio
rect.height * heightRatio,
)
drawBlueRect(img, imgRect);
});
Expand Down
Loading

0 comments on commit 5e13426

Please sign in to comment.