Skip to content

Commit

Permalink
feat(opencv): adjust classifier settings via config
Browse files Browse the repository at this point in the history
  • Loading branch information
jakowenko committed Jun 12, 2022
1 parent 44f4866 commit 2e6c512
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
12 changes: 12 additions & 0 deletions README.md
Expand Up @@ -494,6 +494,18 @@ detectors:
# - garage
```

### `opencv`

```yaml
# opencv settings (default: shown below)
# docs: https://docs.opencv.org/4.6.0/d1/de5/classcv_1_1CascadeClassifier.html
opencv:
scale_factor: 1.05
min_neighbors: 4.5
min_size_width: 30
min_size_height: 30
```

### `schedule`

```yaml
Expand Down
3 changes: 3 additions & 0 deletions api/src/constants/config.js
Expand Up @@ -66,15 +66,18 @@ module.exports = () => {
if (CONFIG?.notify?.gotify)
CONFIG.notify.gotify = _.mergeWith(NOTIFY.gotify, CONFIG.notify.gotify, customizer);

let needsOpenCv = false;
if (CONFIG.detectors)
for (const [key] of Object.entries(CONFIG.detectors)) {
CONFIG.detectors[key] = _.mergeWith(DETECTORS[key], CONFIG.detectors[key], customizer);
if (CONFIG.detectors[key].opencv_face_required) needsOpenCv = true;
}

if (typeof CONFIG.ui.path === 'string') {
if (CONFIG.ui.path.slice(-1) === '/') CONFIG.ui.path = CONFIG.ui.path.slice(0, -1);
if (CONFIG.ui.path && CONFIG.ui.path.slice(0, 1) !== '/') CONFIG.ui.path = `/${CONFIG.ui.path}`;
}
if (!needsOpenCv) delete DEFAULTS.opencv;

CONFIG = _.mergeWith(CONFIG, SYSTEM_CORE);
CONFIG.version = version;
Expand Down
6 changes: 6 additions & 0 deletions api/src/constants/defaults.js
Expand Up @@ -36,6 +36,12 @@ module.exports = {
homeassistant: 'homeassistant',
},
},
opencv: {
scale_factor: 1.05,
min_neighbors: 4.5,
min_size_width: 30,
min_size_height: 30,
},
detectors: {
compreface: {
det_prob_threshold: 0.8,
Expand Down
13 changes: 10 additions & 3 deletions api/src/util/opencv/index.js
Expand Up @@ -2,6 +2,7 @@ const { Canvas, Image, ImageData, loadImage } = require('canvas');
const { JSDOM } = require('jsdom');
const config = require('../../constants/config');
const DETECTORS = require('../../constants/config').detectors();
const { OPENCV } = require('../../constants')();

let isLoaded = false;

Expand Down Expand Up @@ -71,16 +72,22 @@ module.exports.faceCount = async (path) => {
const faces = new cv.RectVector();
const faceCascade = new cv.CascadeClassifier();
faceCascade.load('./api/src/util/opencv/haarcascade_frontalface_default.xml');
const mSize = new cv.Size(0, 0);
faceCascade.detectMultiScale(gray, faces, 1.1, 3, 0, mSize, mSize);
faceCascade.detectMultiScale(
gray,
faces,
OPENCV.SCALE_FACTOR,
OPENCV.MIN_NEIGHBORS,
0,
new cv.Size(OPENCV.MIN_SIZE_WIDTH, OPENCV.MIN_SIZE_HEIGHT)
);
const faceCount = faces.size();
src.delete();
gray.delete();
faceCascade.delete();
faces.delete();
return faceCount;
} catch (error) {
console.error(`opencv error`);
console.error(`opencv error: `, error.message || error);
}
};

Expand Down

0 comments on commit 2e6c512

Please sign in to comment.