Skip to content
This repository has been archived by the owner on Oct 25, 2023. It is now read-only.

Commit

Permalink
Merge c29cd59 into 050233a
Browse files Browse the repository at this point in the history
  • Loading branch information
imurchie committed Mar 1, 2019
2 parents 050233a + c29cd59 commit cd1da58
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 8 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
language: node_js
os:
- linux
- windows
node_js:
- "8"
- "10"
Expand Down
51 changes: 43 additions & 8 deletions lib/image-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import { Buffer } from 'buffer';
import { PNG } from 'pngjs';
import B from 'bluebird';
import { hasValue } from './util';
import { exec } from 'teen_process';
import path from 'path';
import fs from './fs';
import log from './logger';


const { MIME_JPEG, MIME_PNG, MIME_BMP } = Jimp;
let cv = null;
Expand Down Expand Up @@ -87,18 +92,48 @@ async function getJimpImage (data) {
});
}

/**
* Utility function to extend node functionality, allowing us to require
* modules that are installed globally
*
* @param {string} packageName - the name of the package to be required
* @returns {object} - the package object
*/
async function requirePackage (packageName) {
// see if we can get it locally
try {
return require(packageName);
} catch (ign) {}

// find the npm global root
const {stdout} = await exec('npm', ['root', '-g']);
const globalNPMRoot = stdout.trim();

// get the global package root
const packageDir = path.join(globalNPMRoot, packageName);
log.debug(`Loading package '${packageName}' from '${packageDir}'`);
if (!await fs.exists(packageDir)) {
// not installed
throw new Error(`Unable to find global '${packageName}' module`);
}

return require(packageDir);
}

/**
* @throws {Error} If opencv4nodejs module is not installed or cannot be loaded
*/
function initOpenCV () {
async function initOpenCV () {
if (!cv) {
try {
cv = require('opencv4nodejs');
} catch (ign) {}
cv = await requirePackage('opencv4nodejs');
} catch (err) {
log.debug(`Unable to load 'opencv4nodejs': ${err.message}`);
}
}
if (!cv) {
throw new Error('opencv4nodejs module is required to use OpenCV features. ' +
'Please install it first (npm i -g opencv4nodejs) and restart Appium. ' +
throw new Error(`'opencv4nodejs' module is required to use OpenCV features. ` +
`Please install it first ('npm i -g opencv4nodejs') and restart Appium. ` +
'Read https://github.com/justadudewhohacks/opencv4nodejs#how-to-install for more details on this topic.');
}
}
Expand Down Expand Up @@ -235,7 +270,7 @@ function highlightRegion (mat, region) {
* @throws {Error} If `detectorName` value is unknown.
*/
async function getImagesMatches (img1Data, img2Data, options = {}) {
initOpenCV();
await initOpenCV();

const {detectorName = 'ORB', visualize = false,
goodMatchesFactor, matchFunc = 'BruteForce'} = options;
Expand Down Expand Up @@ -335,7 +370,7 @@ async function getImagesMatches (img1Data, img2Data, options = {}) {
* @throws {Error} If the given images have different resolution.
*/
async function getImagesSimilarity (img1Data, img2Data, options = {}) {
initOpenCV();
await initOpenCV();

const {visualize = false} = options;
let [template, reference] = await B.all([
Expand Down Expand Up @@ -417,7 +452,7 @@ async function getImagesSimilarity (img1Data, img2Data, options = {}) {
* @throws {Error} If no occurences of the partial image can be found in the full image
*/
async function getImageOccurrence (fullImgData, partialImgData, options = {}) {
initOpenCV();
await initOpenCV();

const {visualize = false, threshold = DEFAULT_MATCH_THRESHOLD} = options;
const [fullImg, partialImg] = await B.all([
Expand Down
1 change: 1 addition & 0 deletions lib/logging.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ function getLogger (prefix = null) {
enumerable: true,
configurable: true
});

// This lambda function is necessary to workaround unexpected memory leaks
// caused by NodeJS behavior described in https://bugs.chromium.org/p/v8/issues/detail?id=2869
const unleakIfString = (x) => _.isString(x) ? ` ${x}`.substr(1) : x;
Expand Down

0 comments on commit cd1da58

Please sign in to comment.