TypeError: cv.Mat is not a constructor(React & OpenCV.js)

Viewed 216

I am trying to integrate Opencv.js to React(TypeScript). What I did is:

Put

<script async src="https://docs.opencv.org/master/opencv.js" type="text/javascript"></script>

to index.html. And call it as global. I wrote

declare global {
  interface Window { cv: any; }
}
window.cv = window.cv || {};
const cv = window.cv;

in react component which I want to use opencv.js.

Therefore, I solved the error "Cannot find name 'cv'." However, I am facing "TypeError: cv.Mat is not a constructor" when I use

let dst = new cv.Mat();

How could I correctly use opencv.js with React ?

1 Answers

try this and put it at the end of your code

window.addEventListener('DOMContentLoaded',function (e){
    Main();
});

or put all your running code in this function and call it in your HTML

function Run() {
  cv['onRuntimeInitialized']=()=>{
    Main()
  };
}

Related