theothergrantdavidson / opencv-ts

A place for the typescript bindings for OpencvJS
Apache License 2.0
51 stars 18 forks source link

Possible to treat opencv.js as an external dependency to avoid huge load time? #36

Open xeger opened 1 year ago

xeger commented 1 year ago

Hi,

I'm a complete neophyte with OpenCV;I have a use case for it in a browser, and your project seems the simplest and least cluttered of the various browser-compatible bindings.

Problem: I'm using Webpack (and the NextJS framework in this case, though that's relatively unimportant). Somehow, including the opencv distributable in my bundle is growing my app's transpiled and minified assets by a factor of 10x! Here is my client-side code without any references to cv:

 du -h .next/cache/webpack/client-production/*
6.8M    .next/cache/webpack/client-production/0.pack

And, once I import cv from your package:

5.7M    .next/cache/webpack/client-production/0.pack
 84M    .next/cache/webpack/client-production/1.pack
164K    .next/cache/webpack/client-production/2.pack

That's an alarming expansion in size, considering that your shipped opencv.js is a mere 8.x MB! I'm not interested in troubleshooting packing issues and neither are you. I think my ideal would be to simply add a script async tag to an HTML page, as shown in OpenCV's own tutorial for their JavaScript bindings, and make use of window.cv directly, relying only on your typings.

Have you given any thought to this use case? As far as I can tell, I can't get the typings for cv from your package without importing the actual object -- what I'd like to do is simply import type {typeOfCV, Mat, etc} and then, if need be, manually declare window.cv to be the correct type.