I'm new to Javascript and React. I would like to embed OpenCV.js into a basic React.js web app to build an online client-side document scanner (I already have the image processing pipeline developped in Python).
I used yarn create react-app
to get a basic working React sandbox containing : App.js
which is the main React ponent and index.js
which plugs it into the DOM. I added a custom imageProcessing.js
file that will contain my image processing pipeline. A yarn start
mand is available to pile everything and display the result in my browser.
In order to use OpenCV in this pipeline, I downloaded opencv.js
from official page, put it next to imageProcessing.js
and finally called it through const cv = require('../lib/opencv')
.
The problem is the yarn start
mand piles sources for hours and it is clearly not possible to develop anything.
Now my question is : how to efficiently pile my web app with that heavy opencv.js
(13MB) in my sources ?
Is their a better way of integrating the lib ?
Thanks for your help !
I'm new to Javascript and React. I would like to embed OpenCV.js into a basic React.js web app to build an online client-side document scanner (I already have the image processing pipeline developped in Python).
I used yarn create react-app
to get a basic working React sandbox containing : App.js
which is the main React ponent and index.js
which plugs it into the DOM. I added a custom imageProcessing.js
file that will contain my image processing pipeline. A yarn start
mand is available to pile everything and display the result in my browser.
In order to use OpenCV in this pipeline, I downloaded opencv.js
from official page, put it next to imageProcessing.js
and finally called it through const cv = require('../lib/opencv')
.
The problem is the yarn start
mand piles sources for hours and it is clearly not possible to develop anything.
Now my question is : how to efficiently pile my web app with that heavy opencv.js
(13MB) in my sources ?
Is their a better way of integrating the lib ?
Thanks for your help !
opencv.js
as <script>
in your index.html instead.
– user5734311
Commented
Nov 28, 2018 at 10:22
imageProcessing.js
in order to use for instance cv.imread()
in it ?
– fweber
Commented
Nov 28, 2018 at 10:53
imageProcessing.js
gets bundled by webpack, you need to put /* global cv */
at the top. This should allow you to use cv
in your scripts without lint plaining about cv being undefined.
– user5734311
Commented
Nov 28, 2018 at 11:10
You could use a cdn instead of using locally this way react won't bother you. (but make sure that you don't have/have sufficient data limit because opencv.js is a heavy library & it'll request everytime when you reload page...) but you can store that library in catch with PWA so that it'll store opencv.js in browser's cache & whenever you reload page it'll send cached file instead of request 1000s of times... & obviously it'll reduce bandwidth usage & other point is you can use their functions methods in useEffect or outside ponent function
// opencv.js code here
function App(props){...}
OR
function App(props){
useEffect(() => {
// opencv.js code here
}, [])
}
make sure to let the user know that some heavy loading in background running while loading the opencv.js file...