# \[Opencv.js\] Loading tensorflow model into opencv.js

**URL:** https://forum.opencv.org/t/opencv-js-loading-tensorflow-model-into-opencv-js/3422
**Category:** Uncategorized
**Tags:** dnn, javascript, opencvjs
**Created:** [May 19, 2021, 4:18pm UTC](https://forum.opencv.org/t/opencv-js-loading-tensorflow-model-into-opencv-js/3422 "2021-05-19T16:18:59Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![eastemployeer](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/eastemployeer/32/2147_2.png) [@eastemployeer](https://forum.opencv.org/u/eastemployeer)
#### Post date: [May 19, 2021, 4:18pm UTC](https://forum.opencv.org/t/opencv-js-loading-tensorflow-model-into-opencv-js/3422/1 "2021-05-19T16:18:59Z")

</div>

Hi there,  
I would like to ask whether there is any possibility to load tensorflow model (frozen\_inference\_graph.pb and corresponding .pbtxt file) into opencv.js? I’m struggling with it from several days. It appears that there is a problem with `cv.blobFromImage` function from opencv.js. It expects arguments like std or mean but when using tensorflow model it shouldn’t be necessary to specify them. I don’t know appropriate mean and std values for this type of model. In opencv-python there is a possibility to use `cv.blobFromImage` without std and mean but I don’t see this option in opencv.js.

---

<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: [May 19, 2021, 4:24pm UTC](https://forum.opencv.org/t/opencv-js-loading-tensorflow-model-into-opencv-js/3422/2 "2021-05-19T16:24:21Z")

</div>

please show code, actual errors, tell us what kind of model you’re trying to use, and how it got made, the whole chi-chi, thank you 😉

---

<div class="post-metadata">

### Author: ![eastemployeer](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/eastemployeer/32/2147_2.png) [@eastemployeer](https://forum.opencv.org/u/eastemployeer)
#### Post date: [May 19, 2021, 6:06pm UTC](https://forum.opencv.org/t/opencv-js-loading-tensorflow-model-into-opencv-js/3422/3 "2021-05-19T18:06:57Z")

</div>

This is the way I’m trying to start the video and process first frame. I am getting error when calling `net.forward()`. I am using model that I’ve found on this github: [GitHub - marcomarasca/SDCND-Traffic-Light-Detection: Traffic Light Detection using the tensorflow object detection API](https://github.com/marcomarasca/SDCND-Traffic-Light-Detection). It is working just fine in opencv-python.

```
  startVideo: async function() {
  this.streaming = true;
  let video = this.$refs.camInput;
  navigator.mediaDevices
    .getUserMedia({ video: true, audio: false })
    .then(function(stream) {
      video.srcObject = stream;
      video.play();
    })
    .catch(err => console.log('Error'));

  let src = new cv.Mat(video.height, video.width, cv.CV_8UC4);
  let cap = new cv.VideoCapture(video);
  const labels = await this.loadLables(this.labelsUrl);

  let net = cv.readNetFromTensorflow(this.modelPath, this.configPath);
  net.setInput(
    cv.blobFromImage(
      src,
      this.std,
      new cv.Size(this.inputSize[0], this.inputSize[1]),
      new cv.Scalar(this.mean[0], this.mean[1], this.mean[2]),
      this.swapRB,
      false
    )
  );
// cv.blobFromImage(
// src,
   // new cv.Size(this.inputSize[0], this.inputSize[1]),
  // this.swapRB,
  // false
 // )
// );
  try {
    const result = net.forward(); //getting error here
    const output = this.postProcess(result, labels, src);
    this.updateResult(output);
    setTimeout(() => this.processVideo(cap, src, labels, net, video), 0);
  } catch (err) {
    console.log(err);
  }

```

The error I get is just a strange number printing out in the console.

---

<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: [May 19, 2021, 6:12pm UTC](https://forum.opencv.org/t/opencv-js-loading-tensorflow-model-into-opencv-js/3422/4 "2021-05-19T18:12:04Z")

</div>

> [@eastemployeer](#):
>
> The error I get is just a strange number printing out in the console.

can you be more precise, please ?

---

<div class="post-metadata">

### Author: ![eastemployeer](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/eastemployeer/32/2147_2.png) [@eastemployeer](https://forum.opencv.org/u/eastemployeer)
#### Post date: [May 19, 2021, 6:20pm UTC](https://forum.opencv.org/t/opencv-js-loading-tensorflow-model-into-opencv-js/3422/5 "2021-05-19T18:20:30Z")

</div>

![Zrzut ekranu z 2021-05-19 20-19-58](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/3/3210d635f73bf98a36a02526cb9f5f30164fcde1.png)

I removed try/catch block to see the exact error. Otherwise it is just “8661016”. This value is not constant (I mean I’m getting many “random” error values).

---

<div class="post-metadata">

### Author: ![eastemployeer](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/eastemployeer/32/2147_2.png) [@eastemployeer](https://forum.opencv.org/u/eastemployeer)
#### Post date: [May 20, 2021, 12:11am UTC](https://forum.opencv.org/t/opencv-js-loading-tensorflow-model-into-opencv-js/3422/6 "2021-05-20T00:11:57Z")

</div>

If someone likes JS as much as I do and does even this sort of stuff like OpenCV in this, here is how I finally imported this tensorflow model into OpenCV.js:

Import net like this:

```
let net = cv.readNet(this.configPath, this.modelPath);

```

Proper function for making blob from image: (run it instead of `cv.blobFromImage` in the code above.

```
    getBlobFromImage: function(inputSize, mean, std, swapRB, image) {
  let mat;
  if (typeof image === 'string') {
    mat = cv.imread(image);
  } else {
    mat = image;
  }
  let matC3 = new cv.Mat(mat.matSize[0], mat.matSize[1], cv.CV_8UC3);
  cv.cvtColor(mat, matC3, cv.COLOR_RGBA2BGR);
  let input = cv.blobFromImage(
    matC3,
    std,
    new cv.Size(inputSize[0], inputSize[1]),
    new cv.Scalar(mean[0], mean[1], mean[2]),
    swapRB
  );
  matC3.delete();
  return input;
}

```

Mean values that seem to work: [123, 117, 104]  
STD value: 1  
swapRB: true

---

<div class="post-metadata">

### Author: ![Frankie\_Wild](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/frankie_wild/32/11996_2.png) [@Frankie\_Wild](https://forum.opencv.org/u/Frankie_Wild)
#### Post date: [February 8, 2025, 6:55pm UTC](https://forum.opencv.org/t/opencv-js-loading-tensorflow-model-into-opencv-js/3422/7 "2025-02-08T18:55:26Z")

</div>

That solved it. Will mention it in another open issue.
