# Face Detection using Haar Cascades in opencv.js - Node.js error

**URL:** <https://forum.opencv.org/t/face-detection-using-haar-cascades-in-opencv-js-node-js-error/12608>\
**Category:** Uncategorized\
**Tags:** programming\
**Created:** [March 30, 2023, 2:29pm UTC](https://forum.opencv.org/t/face-detection-using-haar-cascades-in-opencv-js-node-js-error/12608 "2023-03-30T14:29:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rusaik](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/rusaik/32/7537_2.png) [@rusaik](https://forum.opencv.org/u/rusaik)\
**Post date:** [March 30, 2023, 2:29pm UTC](https://forum.opencv.org/t/face-detection-using-haar-cascades-in-opencv-js-node-js-error/12608/1 "2023-03-30T14:29:20Z")

</div>

I have implemented the opencv.js in Node.js server using the tutorials from [OpenCV: Using OpenCV.js In Node.js](https://docs.opencv.org/3.4/dc/de6/tutorial_js_nodejs.html).

Emulating HTML DOM and canvas in the tutorials works successfully in the project but the code for detecting objects(Face) is throwing error while loading Haar Cascades.

Here is my code in github,  
[rusaik/opencv\_detect (github.com)](https://github.com/rusaik/opencv_detect)

I have attached the project which i have forked in Stackblitz using above repository, Unfortunately it is not running , but you can easily view the code there.  
[Stackblitz repo](https://stackblitz.com/edit/node-fjumdp?file=index.js)

When the compiler is running the code to load the haarcascade\_frontalface\_default.xml

```auto
  faceCascade.load('./haarcascade_frontalface_default.xml');

```

Error

```auto
6620792

```

Please let me know anyone find what i’m missing here.

---

<div class="post-metadata">

**Author:** ![matti.vuori](https://avatars.discourse-cdn.com/v4/letter/m/e79b87/32.png) [@matti.vuori](https://forum.opencv.org/u/matti.vuori)\
**Post date:** [March 31, 2023, 7:10am UTC](https://forum.opencv.org/t/face-detection-using-haar-cascades-in-opencv-js-node-js-error/12608/2 "2023-03-31T07:10:29Z")

</div>

Of course, you should post your code here and not ask people to navigate a repository #makeHelpingAsEasyAsPosdible

You obviously have googled about this, but must have missed this

> **[Problem loading haar cascade file in opencv.js - OpenCV Q&A Forum](https://answers.opencv.org/question/189783/problem-loading-haar-cascade-file-in-opencvjs/)**
>
> i am to implement face detection using opencv.js but haar cascade file is not getting loaded. let faceCascade = new cv.CascadeClassifier(); console.log(faceCascade); if(faceCascade.load('haarcascade\_frontalface\_default.xml')) { }else{...

---

<div class="post-metadata">

**Author:** ![rusaik](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/rusaik/32/7537_2.png) [@rusaik](https://forum.opencv.org/u/rusaik)\
**Post date:** [April 11, 2023, 10:53am UTC](https://forum.opencv.org/t/face-detection-using-haar-cascades-in-opencv-js-node-js-error/12608/5 "2023-04-11T10:53:18Z")

</div>

index.js file

```auto
const { Canvas, createCanvas, Image, ImageData, loadImage } = require('canvas');
const { JSDOM } = require('jsdom');
const { writeFileSync, existsSync, mkdirSync } = require('fs');
const Utils = require("./utils.js");
(async () => {
  await loadOpenCV();
  const image = await loadImage('lena.jpg');
  const src = cv.imread(image);
  //await copyFile(src);
  let gray = new cv.Mat();
  console.log('image loaded');
  cv.cvtColor(src, gray, cv.COLOR_RGBA2GRAY);
  console.log('0');
  let faces = new cv.RectVector();
  let eyes = new cv.RectVector();
  console.log('1');

  let faceCascade = new cv.CascadeClassifier();
  let eyeCascade = new cv.CascadeClassifier();
  console.log('2');
  console.log(gray.shape);
  let utils = new Utils('errorMessage');

  let faceCascadeFile = 'haarcascade_frontalface_default.xml';
  let url = 'https://github.com/opencv/opencv/blob/4.x/data/haarcascades/haarcascade_frontalface_default.xml';
  
  await utils.createFileFromUrl(url, faceCascadeFile);
  //, async () => {});
  console.log(`faceCascade loading started`);
  // Load pre-trained classifier files. Notice how we reference local files using relative paths just
  // like we normally would do
  faceCascade.load('haarcascade_frontalface_default.xml');
  // eyeCascade.load('./haarcascade_eye.xml');
  console.log(`faceCascade`);
 // console.log(faceCascade.empty());

  //let mSize = new cv.Size(0, 0);
  /*faceCascade.detectMultiScale(gray, faces, 1.1, 3, 0, mSize, mSize);
  for (let i = 0; i < faces.size(); ++i) {
    let roiGray = gray.roi(faces.get(i));
    let roiSrc = src.roi(faces.get(i));
    let point1 = new cv.Point(faces.get(i).x, faces.get(i).y);
    let point2 = new cv.Point(faces.get(i).x + faces.get(i).width, faces.get(i).y + faces.get(i).height);
    cv.rectangle(src, point1, point2, [255, 0, 0, 255]);
    roiGray.delete();
    roiSrc.delete();
  }
  const canvas = createCanvas(image.width, image.height);
  cv.imshow(canvas, src);
  writeFileSync('output3.jpg', canvas.toBuffer('image/jpeg'));
  src.delete(); gray.delete(); faceCascade.delete(); eyeCascade.delete(); faces.delete(); eyes.delete()*/
})();
/**
 * Loads opencv.js.
 *
 * Installs HTML Canvas emulation to support `cv.imread()` and `cv.imshow`
 *
 * Mounts given local folder `localRootDir` in emscripten filesystem folder `rootDir`. By default it will mount the local current directory in emscripten `/work` directory. This means that `/work/foo.txt` will be resolved to the local file `./foo.txt`
 * @param {string} rootDir The directory in emscripten filesystem in which the local filesystem will be mount.
 * @param {string} localRootDir The local directory to mount in emscripten filesystem.
 * @returns {Promise} resolved when the library is ready to use.
 */
function loadOpenCV(rootDir = '/work', localRootDir = process.cwd()) {
  if (global.Module && global.Module.onRuntimeInitialized && global.cv && global.cv.imread) {
    return Promise.resolve()
  }
  return new Promise(resolve => {
    installDOM()
    global.Module = {
      onRuntimeInitialized() {
        // We change emscripten current work directory to 'rootDir' so relative paths are resolved
        // relative to the current local folder, as expected
        cv.FS.chdir(rootDir)
        resolve()
      },
      preRun() {
        console.log('pre run')
        // preRun() is another callback like onRuntimeInitialized() but is called just before the
        // library code runs. Here we mount a local folder in emscripten filesystem and we want to
        // do this before the library is executed so the filesystem is accessible from the start
        const FS = global.Module.FS
        // create rootDir if it doesn't exists
        if (!FS.analyzePath(rootDir).exists) {
          FS.mkdir(rootDir);
        }
        // create localRootFolder if it doesn't exists
        if (!existsSync(localRootDir)) {
          mkdirSync(localRootDir, { recursive: true });
        }
        // FS.mount() is similar to Linux/POSIX mount operation. It basically mounts an external
        // filesystem with given format, in given current filesystem directory.
        FS.mount(FS.filesystems.NODEFS, { root: localRootDir }, rootDir);
      }
    };
    global.cv = require('./opencv.js')
  });
}
function installDOM() {
  const dom = new JSDOM();
  global.document = dom.window.document;
  global.Image = Image;
  global.HTMLCanvasElement = Canvas;
  global.ImageData = ImageData;
  global.HTMLImageElement = Image;
  console.log('install dom')
}

```

utils.js file

```auto
const fs = require('fs');
const util = require('util');
const fetch = require('cross-fetch');

// utils.js
class Utils {
    constructor(errorMessage) {
        this.errorMessage = errorMessage;
        console.log(this.errorMessage);
    }

    writeFile = util.promisify(fs.writeFile);

    throwErrorIfNull(value) {
        if (value === null || value === undefined) {
            throw new Error(this.errorMessage);
        }
    }

    async createFileFromUrl(url, filePath) {
        try {
            const response = await fetch(url);
            const buffer = await response.buffer();
            await this.writeFile(filePath, buffer);
            console.log(`File ${filePath} created successfully from ${url}`);
        } catch (error) {
            console.error(`Error creating file from ${url}: ${error}`);
        }
    }
}

module.exports = Utils;

```

Downloaded the [haarcascade\_frontalface\_default.xml](https://github.com/opencv/opencv/blob/4.x/data/haarcascades/haarcascade_frontalface_default.xml) file from [OpenCV sources] ([opencv/data/haarcascades at 4.x · opencv/opencv · GitHub](https://github.com/opencv/opencv/tree/4.x/data/haarcascades))

Every files are in the same directory as below structure.

- haarcascade\_frontalface\_default.xml
- index.js
- lena.jpg
- opencv.js

The error thrown while loading the facecascade is given below.

```auto
abort(6620792). Build with -s ASSERTIONS=1 for more info.
Thrown at:
    at path\opencv\opencv.js:30:1693
    at emit (node:events:513:28)

```

@matti.vuori i have tried the example you mentioned, please check the code given, Also i am running this code in Noed.js server environment (Not browser)

Thanks in advance!

​

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [April 11, 2023, 1:12pm UTC](https://forum.opencv.org/t/face-detection-using-haar-cascades-in-opencv-js-node-js-error/12608/6 "2023-04-11T13:12:32Z")

</div>

> [@rusaik](#):
>
> ```auto
> let url = 'https://github.com/opencv/opencv/blob/4.x/data/haarcascades/haarcascade_frontalface_default.xml';
>   
> 
> ```

this will dl the _html rendered view_, not the required raw xml, use a different URL:

[https://github.com/opencv/opencv/raw/4.x/data/haarcascades/haarcascade\_frontalface\_default.xml](https://github.com/opencv/opencv/raw/4.x/data/haarcascades/haarcascade_frontalface_default.xml)

(s/blob/raw)

---

<div class="post-metadata">

**Author:** ![rusaik](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/rusaik/32/7537_2.png) [@rusaik](https://forum.opencv.org/u/rusaik)\
**Post date:** [April 11, 2023, 4:04pm UTC](https://forum.opencv.org/t/face-detection-using-haar-cascades-in-opencv-js-node-js-error/12608/7 "2023-04-11T16:04:12Z")

</div>

It works! Thank you very much @berak ❤
