Extract svgs from multiple paths - javascript

here is my code just need to refactor it a bit
const fs = require('fs');
const path = require('path');
const config = require('../configs/icons.config.js');
const filterDirectory = (file, srcPath) => fs.statSync(path.join(srcPath, file)).isDirectory();
const ensureDirectoryExistence = (filePath) => {
const dirname = path.dirname(filePath);
if (fs.existsSync(dirname)) {
return true;
}
ensureDirectoryExistence(dirname);
return fs.mkdirSync(dirname);
};
const filterExclude = fileName => config.ignore
.filter(ignore => fileName.indexOf(ignore) >= 0).length === 0;
const getDirectories = srcPath => fs.readdirSync(srcPath)
.filter(file => filterDirectory(file, srcPath))
.filter(file => filterExclude(file));
const availableIcons = getDirectories(path.resolve(config.origin));
availableIcons.forEach((file) => {
ensureDirectoryExistence(path.resolve(`${config.temporary}/${file}`));
fs.createReadStream(`${config.origin}/${file}/_jcr_content/renditions/original`)
.pipe(fs.createWriteStream(`${config.temporary}/${file}`));
});
problem is that is want the same funcionality with
const config = require('../configs/globalicons.config.js')
if i change the line 3 it is generating new directory for a different config file how to pass both configs at the same time??? like
const config = require('../configs/icons.config.js,../configs/globalicons.config.js');
not working though
i want to execute the same javascript for 2 different configs 2 generate 2 different directory on a single deployment any tip?

Related

running the formatVideoToWebpSticker function

this is my code trying to convert an mp4 to webp file.
for some reason its not saving the file to my folder and when I console.logged it, it sent a path to my temp folder but the file wasn't there.
Tried looking it up but found nothing helpful :(
how to fix this please? thank you
const path = require('path');
const Crypto = require('crypto');
const { tmpdir } = require('os');
const ffmpeg = require('fluent-ffmpeg');
const webp = require('node-webpmux');
const fs = require('fs').promises;
const has = (o, k) => Object.prototype.hasOwnProperty.call(o, k);
const { MessageMedia } = require('whatsapp-web.js');
const media = MessageMedia.fromFilePath('./1.mp4');
async function formatVideoToWebpSticker(media) {
if (!media.mimetype.includes('video'))
throw new Error('media is not a video');
const videoType = media.mimetype.split('/')[1];
const tempFile = path.join(
tmpdir(),
`${Crypto.randomBytes(6).readUIntLE(0, 6).toString(36)}.webp`
);
const stream = new (require('stream').Readable)();
const buffer = Buffer.from(
media.data.replace(`data:${media.mimetype};base64,`, ''),
'base64'
);
stream.push(buffer);
stream.push(null);
await new Promise((resolve, reject) => {
ffmpeg(stream)
.inputFormat(videoType)
.on('error', reject)
.on('end', () => resolve(true))
.addOutputOptions([
'-vcodec',
'libwebp',
'-vf',
// eslint-disable-next-line no-useless-escape
'scale=\'iw*min(300/iw\,300/ih)\':\'ih*min(300/iw\,300/ih)\',format=rgba,pad=300:300:\'(300-iw)/2\':\'(300-ih)/2\':\'#00000000\',setsar=1,fps=10',
'-loop',
'0',
'-ss',
'00:00:00.0',
'-t',
'00:00:05.0',
'-preset',
'default',
'-an',
'-vsync',
'0',
'-s',
'512:512',
])
.toFormat('webp')
.save(tempFile)
console.log(tempFile);
});
const data = await fs.readFile(tempFile, 'base64');
console.log(tempFile)
await fs.unlink(tempFile);
return {
mimetype: 'image/webp',
data: data,
filename: media.filename,
};
}
formatVideoToWebpSticker(media)

How can I increase the number of reference images in face-api.js using node.js?

The default face-api.js uses only one image as reference for face recognition, but through my tests I noticed a pretty high error gap. So I was wondering, how can I manage to increase the number of reference images, in order to reduce the error gap?
Assuming my images are in the imgs/ folder, how can I do this?
Here's my project folder :
Project Folder
Here's the faceRecognition.ts file :
import * as faceapi from 'face-api.js';
import { canvas, faceDetectionNet, faceDetectionOptions, saveFile } from './commons';
const REFERENCE_IMAGE = '../../imgs/test1.jpeg'
const QUERY_IMAGE = '../../test/test.jpeg'
// i want to have many images for the REFERENCE_IMAGE
// in folder imgs, i have 5 images in a want to use all five images for increase
// the result. Actually i have some bad prediction when i use only one image
async function run() {
await faceDetectionNet.loadFromDisk('../../weights')
await faceapi.nets.faceLandmark68Net.loadFromDisk('../../weights')
await faceapi.nets.faceRecognitionNet.loadFromDisk('../../weights')
const referenceImage = await canvas.loadImage(REFERENCE_IMAGE)
const queryImage = await canvas.loadImage(QUERY_IMAGE)
const resultsRef = await faceapi.detectAllFaces(referenceImage, faceDetectionOptions)
.withFaceLandmarks()
.withFaceDescriptors()
const resultsQuery = await faceapi.detectAllFaces(queryImage, faceDetectionOptions)
.withFaceLandmarks()
.withFaceDescriptors()
const faceMatcher = new faceapi.FaceMatcher(resultsRef)
const labels = faceMatcher.labeledDescriptors
.map(ld => ld.label)
const refDrawBoxes = resultsRef
.map(res => res.detection.box)
.map((box, i) => new faceapi.draw.DrawBox(box, { label: labels[i] }))
const outRef = faceapi.createCanvasFromMedia(referenceImage)
refDrawBoxes.forEach(drawBox => drawBox.draw(outRef))
saveFile('referenceImage.jpg', (outRef as any).toBuffer('image/jpeg'))
const queryDrawBoxes = resultsQuery.map(res => {
const bestMatch = faceMatcher.findBestMatch(res.descriptor)
return new faceapi.draw.DrawBox(res.detection.box, { label: bestMatch.toString() })
})
const outQuery = faceapi.createCanvasFromMedia(queryImage)
queryDrawBoxes.forEach(drawBox => drawBox.draw(outQuery))
saveFile('queryImage.jpg', (outQuery as any).toBuffer('image/jpeg'))
}
run()
Can anyone help ?
const path = require('path')
import * as faceapi from 'face-api.js';
import { canvas, faceDetectionNet, faceDetectionOptions, saveFile } from './commons';
async function start() {
await faceDetectionNet.loadFromDisk('../../weights')
await faceapi.nets.faceLandmark68Net.loadFromDisk('../../weights')
await faceapi.nets.faceRecognitionNet.loadFromDisk('../../weights')
const labeledFaceDescriptors = await loadLabeledImages()
const faceMatcher = new faceapi.FaceMatcher(labeledFaceDescriptors, 0.6)
const queryImage = await canvas.loadImage(`test/test.jpeg`)
//absolute link to image
const detections = await faceapi.detectAllFaces(queryImage ).withFaceLandmarks().withFaceDescriptors()
const queryDrawBoxes = detections.map(res => {
const bestMatch = faceMatcher.findBestMatch(res.descriptor)
return new faceapi.draw.DrawBox(res.detection.box, { label: bestMatch.toString() })
})
const outQuery = faceapi.createCanvasFromMedia(queryImage)
queryDrawBoxes.forEach(drawBox => drawBox.draw(outQuery))
saveFile('queryImage.jpg', (outQuery as any).toBuffer('image/jpeg'))
console.log('done, saved results to out/queryImage.jpg')
}
function loadLabeledImages() {
const labels = ['imgs']
return Promise.all(
labels.map(async label => {
const descriptions = []
for (let i = 1; i <= 5; i++) {
const img = await canvas.loadImage(`/imgs/test${i}.jpeg` )
// for example if you are test1 , test2, etc. like image's names
const detections = await faceapi.detectSingleFace(img).withFaceLandmarks().withFaceDescriptor()
descriptions.push(detections.descriptor)
}
return new faceapi.LabeledFaceDescriptors(label, descriptions)
})
)
}
start()

How could I automatically read all of the directories and add all files that end in .js to discord collection

This is the code I currently have, how would adapt this to check each sub-directory:
const fs = require('fs')
module.exports = (client, Discord) =>{
const command_files = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'))
for(const file of command_files){
const command = require(`../commands/${file}`);
if(command.name) {
client.commands.set(command.name, command);
} else {
continue
}
}
}
And this is the layout I have for the commands folder the folder layout
You need to wrap the whole code into a function and use some recursion.
Please note that, when using recusion, a depth variable is a wise way to handle it
Something like this should do it:
const fs = require('fs')
module.exports = (client, Discord) =>{
const depth = 3;
const finder = (path, currentDepth = 0) => {
if (currentDepth >= depth) {
return; // Breaks here
}
const dirContent = fs.readdirSync(path);
const command_files = dirContent.filter(file => file.endsWith('.js'));
const folders = dirContent.filter(file => {
const dirPath = path + file;
// Exists + is a directory verification
return fs.existsSync(dirPath) && fs.lstatSync(dirPath).isDirectory();
);
for(const file of command_files){
const filePath = '../' + path + file;
const command = require(filePath);
if(command.name) {
client.commands.set(command.name, command);
} else {
continue
}
}
// Loops through folders
folders.map((folder) => finder(path + folder + '/', currentDepth + 1));
}
finder('./commands/');
}

output list of files in a directory by array of extensions

I'm trying to get only image filenames to output into console from a given directory.
I was originally following this answer but I couldn't figure out how to give it multiple extensions.
How do I get a list of files with specific file extension using node.js?
function fName(fp){
const { readdirSync } = require('fs');
const path = require("path");
const fs = require('fs');
// dont need whole filepaths atm
// const gl = require('glob');
//gl(fp + `/**/*.#(jpg|png)`, {}, (err, files) => {
// console.log(files)
// })
let extensions = ["jpg", "jpeg", "png"];
let nameFilter = [];
const rawFileNames = readdirSync(fp, {withFileTypes: true})
.filter(dirent => dirent.isFile())
.map(dirent => dirent.name)
//filter the extensions
let fileNames = rawFileNames.filter(file => {
path.extname(file).toLowerCase() === extensions;
})
console.log(fileNames);
}
EDIT
I also just attempted to make a function that iterates over the extension list and call it, but it still returns an empty array
let extensions = ["jpg", "jpeg", "png"];
let getexts = function(exte){
for (ex in exte){
return exte[ex];
}
}
let nameFilter = [];
const rawFileNames = readdirSync(fp, {withFileTypes: true})
.filter(dirent => dirent.isFile())
.map(dirent => dirent.name)
//filter the extensions
let fileNames = rawFileNames.filter(file => {
path.extname(file).toLowerCase() === getexts(extensions);
})
In your filter, couldn't you just do:
let fileNames = rawFileNames.filter(file => {
return extensions.includes(path.extname(file).toLowerCase())
})
Modify your filer condition as below.
//filter the extensions
let fileNames = rawFileNames.filter(file => {
return extensions.includes(path.extname(file).toLowerCase())
});
console.log(fileNames);

NodeJS: How to zip (local) files after done with JIMP

I'm trying to use NodeJS to automate some trivial procedures on my computer. Right now I'm able to convert some png files into jpg. I would like to bundle them all up in a zip.
const fs = require('fs')
const path = require('path')
const jimp = require('jimp')
const files = fs.readdirSync('./')
// Convert all png to jpg
const pngs = files.filter(file => path.extname(file).toLowerCase() === '.png')
let jpgs = []
Promise.all(pngs.map(png => jimp.read('./' + png))).then(jimps => {
jimps.map((img, i) => {
img
.rgba(false)
.background(0xffffffff)
.write(`./jpgs/${path.basename(pngs[i], '.png')}.jpg`)
})
console.log('Done converting')
})
// Zip all the .png and .jpg files into PNGs.zip and JPGs.zip
// TODO:
I fiddled a bite around with JSZip but couldn't make it work.
SOLUTION
const fs = require('fs')
const path = require('path')
const jimp = require('jimp')
const CLIProgress = require('cli-progress')
const zipPNG = new require('node-zip')()
const zipJPG = new require('node-zip')()
const files = fs.readdirSync('./')
// Convert all png to jpg
const pngs = files.filter(file => path.extname(file).toLowerCase() === '.png')
let jpgs = []
Promise.all(pngs.map(png => jimp.read('./' + png))).then(jimps => {
const bar = new CLIProgress.Bar({}, CLIProgress.Presets.shades_classic)
bar.start(pngs.length, 0)
jimps.map((img, i) => {
img
.rgba(false)
.background(0xffffffff)
.write(`./jpgs/${path.basename(pngs[i], '.png')}.jpg`)
bar.update(i + 1)
})
bar.stop()
console.log('Done converting')
// Pack the files nicely in ZIP
pngs.forEach(png => {
zipPNG.file(png, fs.readFileSync(path.join('./', png)))
zipJPG.file(
`${path.basename(png, '.png')}.jpg`,
fs.readFileSync(`./jpgs/${path.basename(png, '.png')}.jpg`)
)
})
let data = zipPNG.generate({ base64: false, compression: 'DEFLATE' })
fs.writeFileSync('PNG.zip', data, 'binary')
console.log('PNGs zipped')
data = zipJPG.generate({ base64: false, compression: 'DEFLATE' })
fs.writeFileSync('./jpgs/JPG.zip', data, 'binary')
console.log('JPGs zipped')
})
I would use the npm package node-zip. It is a very straightforward library with an easy to use interface.

Categories