Example using faceapi with smartcrop-sharp on nodejs?
sugoidesune opened this issue · comments
I've managed to make both files work individually but have utterly failed to make them work together. With some import errors related to canvas.
return process.dlopen(module, path.toNamespacedPath(filename));
^
at Object.<anonymous> (C:\Users\timar\code\pureimage\node_modules\canvas\lib\bindings.js:3:18)
at Module._compile (internal/modules/cjs/loader.js:1085:14)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
at Module.load (internal/modules/cjs/loader.js:950:32)
at Function.Module._load (internal/modules/cjs/loader.js:790:12) {
code: 'ERR_DLOPEN_FAILED'
Here are the two files that work on their own:
Face detection and create a boost region for smartcrop
boost.ts
//@ts-nocheck
import * as faceapi from 'face-api.js';
import { canvas, faceDetectionNet, faceDetectionOptions } from './commons/index.ts';
async function run(image) {
await faceDetectionNet.loadFromDisk('./weights')
await faceapi.nets.faceLandmark68Net.loadFromDisk('./weights')
const img = await canvas.loadImage(image)
const results = await faceapi.detectAllFaces(img, faceDetectionOptions)
.withFaceLandmarks()
var box = results[0].detection._box
var boost = [{
x: box._x,
y: box._y,
width: box._width,
height: box._height,
weight: 1 // in the range [0, 1]
}]
console.log(boost)
return boost
}
module.exports = run
Crop an image using smart crop
//@ts-nocheck
const sharp = require('sharp');
const smartcrop = require('smartcrop-sharp');
const boost = require('./boost.ts')
boost.run('guy4.jpg').then(boost=>{
applySmartCrop('guy4.jpg', 'guycrop.jpg', 512, 512, boost);
})
// finds the best crop of src and writes the cropped and resized image to dest.
function applySmartCrop(src, dest, width, height, boost) {
console.log('running apply snart crop')
return smartcrop.crop(src, { minScale: 1, width: width, height: height, ruleOfThirds: false, boost })
.then(function(result) {
console.log('got the result', result)
const crop = result.topCrop;
return sharp(src)
.extract({ width: crop.width, height: crop.height, left: crop.x, top: crop.y })
.resize(width, height)
.toFile(dest);
})
}
As soon as all the imports are joined in one file the error appears.
Hey @sugoidesune ,
I'm not really familiar with how dynamic loading of node modules works on windows but it looks a bit like there is some sort of a symbol conflict. Did you try whether running in WSL helps?
Are you sure that this has anything to do with smartcrop(-sharp) and isn't just an interaction between sharp and face-api? Can use use sharp and face-api together without using smartcrop?
I ended up finding a fantastic fork https://github.com/vladmandic/face-api
- CommonJS and ESM exports
- Javascript and Typescript
- Tensorflow binaries as WASM
- Ditched reliance on Canvas
Worked instantly, I would discourage use of the original.