-
Notifications
You must be signed in to change notification settings - Fork 10
/
app.js
31 lines (27 loc) · 1.01 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// const { predict } = require('@codait/max-image-segmenter')
const { predict } = require('../dist/max.imgseg.cjs.js')
const { read, MIME_PNG } = require('jimp')
const { createCanvas, loadImage } = require('canvas')
const createCanvasElement = function (imageInput) {
return new Promise(async (resolve, reject) => {
const img = await loadImage(imageInput)
let canvas = createCanvas(img.width, img.height)
let ctx = canvas.getContext('2d')
await ctx.drawImage(img, 0, 0)
resolve(canvas)
})
}
if (process.argv.length < 3) {
console.log('please pass an image to process. ex:')
console.log(' node app.js /path/to/image.jpg')
} else {
let imagePath = process.argv[2]
read(imagePath)
.then(imageData => imageData.scaleToFit(512, 512).getBufferAsync(MIME_PNG))
.then(imageBuffer => createCanvasElement(imageBuffer))
.then(imageElement => predict(imageElement))
.then(prediction => {
// console.log(prediction.segmentationMap)
console.log(prediction.objectsDetected)
})
}