TechStark / opencv-js

OpenCV JavaScript version for node.js or browser
Apache License 2.0
357 stars 32 forks source link

cannot use package in nodejs #9

Closed kgtrey1 closed 2 years ago

kgtrey1 commented 2 years ago

Hello, I'm trying to use the open-cv js but with this code, I have infinite output on stdout:

I dont want to use it with webpack

`` import cv from "@techstark/opencv-js";

console.log(cv.getBuildInformation()) ``

Is there something to do other than just import the module and use it?

image

Here is my package.json `` { "name": "test", "version": "1.0.0", "description": "", "main": "test.js", "scripts": { "start": "node test.js", "test": "echo \"Error: no test specified\" && exit 1" }, "author": "", "license": "ISC", "dependencies": { "@techstark/opencv-js": "^4.5.5-release.1" }, "type": "module" }

``

I also get TypeError: cv.getBuildInformation is not a function which make me think I have to do something to wait for cv to get ready but I dont know how

ttt43ttt commented 2 years ago

@kgtrey1 this works for me https://codesandbox.io/s/angry-dust-0dzjip?file=/index.js

var cv = require('@techstark/opencv-js');
console.log(cv.getBuildInformation());
sclausen commented 1 year ago

@ttt43ttt how would one read an image since imread want's a canvasOrImageHtmlElement?

ttt43ttt commented 1 year ago

@sclausen for example, you can pass the Image HTML element to imread.

image
sclausen commented 1 year ago

Oh sorry, since the original question was targeting nodejs, I thought I didn't have to mention this in my follow up question too. Do you have an example for nodejs too reading an image?

ttt43ttt commented 1 year ago

@sclausen I haven't try this in node.js, but you may want to read https://docs.opencv.org/4.6.0/dc/de6/tutorial_js_nodejs.html

ctooley21 commented 1 year ago

Hi all! I'm working on incorporating this library into a backend node.js application like the poster above.

I ran into the same issue that was mentioned in the OP. From what I can tell there are a few issues with either this library or the raw OpenCV.js implementation that people are likely to encounter in the first little bit of getting this working. 1) Any errors you encounter when running OpenCV functions will result in the dump of the entire OpenCV.js file contents, followed by the usual error. See an example below.

const cv = require('@techstark/opencv-js');
console.log(cv.getBuildInformation())

This can be resolved by wrapping any OpenCV function calls in a try/catch statement. When you do this, you'll end up with the usual error messages rather than a file dump.

const cv = require('@techstark/opencv-js');

try {
    console.log(cv.getBuildInformation())
} catch(e) {
    console.log(e);
}

2) The OpenCV.js documentation listed here has a function called onRuntimeInitialized that is used to signify when OpenCV has loaded and is ready to take in requests. If you try to call an OpenCV method before this has been run, you'll get an error. See an example below.

const cv = require('@techstark/opencv-js');
console.log(cv.getBuildInformation())

This can be resolved by waiting for a few seconds for the module to initialize. See solution below.

const cv = require('@techstark/opencv-js');

function sleep(ms) {
  return new Promise((resolve) => {
    setTimeout(resolve, ms);
  });
}

async function test() {
    await sleep(200);
    try {
        console.log(cv.getBuildInformation())
    } catch(e) {
        console.log(e);
    }   
}

test();

I'm sure there are better solutions to these problems, but hopefully, these temporary solutions are helpful to someone! I'm guessing there's gotta be a way to use the same onRuntimeInitialized with this package as well. I'll keep messing around with it and post here if I find anything new.

@ttt43ttt I"d appreciate any insight you have! I think if this package is advertising itself as a node.js package, it'd be good for us to figure these little issues out, and potentially write up an example or two for others to reference.

@kgtrey1

zhylmzr commented 10 months ago

Hi, i found a way to use opencv in node.js

https://gist.github.com/zhylmzr/1af1ee009daa1506f5156dd7d55114e3