Use Async API with Promises or Callbacks
masterThe library provides an asynchronous API. You can consume it in three ways:
- Callbacks: Pass a callback function as the last argument (e.g.,
cv.imreadAsync(path, (err, mat) => { ... })). - Promises: If no callback is provided, the method returns a
Promise. - Async/Await: Use
awaitwith the Promise-returning methods for cleaner code.
Note: Many methods have an Async suffix (e.g., detectMultiScaleAsync, bgrToGrayAsync).
const cv = require('@u4/opencv4nodejs');
// Using async/await
try {
const img = await cv.imreadAsync('./faceimg.jpg');
const grayImg = await img.bgrToGrayAsync();
const { objects, numDetections } = await classifier.detectMultiScaleAsync(grayImg);
} catch (err) {
console.error(err);
}