UNPKG

imgrecog

Version:

Node.js tool to parse and act on images, using the Google Vision and Sightengine APIs.

155 lines (154 loc) 5.87 kB
"use strict"; // ACTIONS Object.defineProperty(exports, "__esModule", { value: true }); exports.moveImages = exports.deleteUnsafe = exports.deleteBloat = exports.deleteImages = void 0; const utils_1 = require("./utils"); const fs = require("fs"); const path = require("path"); /** * Delete the passed images. * @param options Program options. * @param images Images to be deleted. * @param description Additional description to be appended to the logs. */ function deleteImages(options, images, description) { for (let image of images) { try { if (fs.existsSync) { fs.unlinkSync(image.file); utils_1.logInfo(options, `${image.file} - deleted (${description})`); } } catch (ex) { utils_1.logError(options, `${image.file} - error deleting`, ex); } } } exports.deleteImages = deleteImages; /** * Delete bloat images (memes, screenshots, websites etc). * @param options Program options. * @param images Scanned images. */ async function deleteBloat(options, images) { const bloatTags = ["meme", "photo-caption", "screenshot", "website", "explicit-spoof"]; const extraTags = ["advertising", "document", "text", "logo-facebook", "logo-twitter", "logo-instagram", "logo-whatsapp"]; // Minimum safe single / total scores to consider. // Anything above these values is considered bloat. const singleScore = 0.89; const totalScore = 1.79; const toDelete = []; // Iterate passed images to detect the bloat stuff. for (let image of images) { try { let imgTotalScore = 0; let hasBloat = false; // Images smaller than 50KB are automatically considered bloat. if (image.details.size && image.details.size < 50000) { toDelete.push(image); utils_1.logDebug(options, `${image.file} marked as bloat due to small size`); continue; } // Check for main bloat tags. for (let bt of bloatTags) { if (!image.tags[bt]) { image.tags[bt] = "0"; } imgTotalScore += parseFloat(image.tags[bt]); if (parseFloat(image.tags[bt]) > singleScore) { hasBloat = true; } } // Check for extra tags and logos. for (let et of extraTags) { if (!image.tags[et]) { image.tags[et] = "0"; } imgTotalScore += parseFloat(image.tags[et]); } if (hasBloat && imgTotalScore > totalScore) { toDelete.push(image); utils_1.logDebug(options, `${image.file} marked as bloat due to score`); } } catch (ex) { utils_1.logError(options, `${image.file} - error processing bloat tags`, ex); } } deleteImages(options, toDelete, "bloat"); } exports.deleteBloat = deleteBloat; /** * Delete unsafe images (violent, adult, medical and/or racy). Here we do not count spoof messages. * @param options Program options. * @param images Scanned images. */ async function deleteUnsafe(options, images) { const unsafeTags = ["explicit-adult", "explicit-violence", "explicit-medical", "explicit-racy"]; const toDelete = []; // Minimum safe single / total scores to consider. // Anything above these values is considered unsafe. const singleScore = 0.89; const totalScore = 1.52; // Iterate passed images to detect the unsafe ones. for (let image of images) { try { let imgTotalScore = 0; // Detect violence and adult content. const isAdult = parseFloat(image.tags["explicit-adult"]) > singleScore; const isViolence = parseFloat(image.tags["explicit-violence"]) > singleScore; for (let ut of unsafeTags) { if (!image.tags[ut]) { image.tags[ut] = "0"; } imgTotalScore += parseFloat(image.tags[ut]); } if (isAdult || isViolence || imgTotalScore > totalScore) { toDelete.push(image); utils_1.logDebug(options, `${image.file} marked as unsafe, total score ${imgTotalScore}`); } } catch (ex) { utils_1.logError(options, `${image.file} - error processing unsafe tags`, ex); } } deleteImages(options, toDelete, "unsafe"); } exports.deleteUnsafe = deleteUnsafe; /** * Move scanned images to the specified folder. * @param options Program options. * @param images Scanned images. */ async function moveImages(options, images) { const targetFolder = options.move; utils_1.logDebug(options, `Will move ${images.length} scanned images to: ${targetFolder}`); try { if (!fs.existsSync(targetFolder)) { fs.mkdirSync(targetFolder, { recursive: true }); } } catch (ex) { utils_1.logError(options, `Error creating target folder: ${targetFolder}`, ex); return; } // Iterate passed images to move them. for (let image of images) { try { if (!fs.existsSync(image.file)) { utils_1.logDebug(options, `${image.file} does not exist, will not move`); continue; } const targetFile = path.join(targetFolder, image.file); const folder = path.dirname(targetFile); if (!fs.existsSync(folder)) { fs.mkdirSync(folder, { recursive: true }); } fs.renameSync(image.file, targetFolder); } catch (ex) { utils_1.logError(options, `${image.file} - error moving image file`, ex); } } } exports.moveImages = moveImages;