diff --git a/packages/server/package.json b/packages/server/package.json index 9a061618..95179db1 100755 --- a/packages/server/package.json +++ b/packages/server/package.json @@ -30,7 +30,8 @@ "passport": "0.5.2", "passport-jwt": "4.0.0", "passport-local": "1.0.0", - "path-to-regexp": "6.2.0" + "path-to-regexp": "6.2.0", + "sharp": "^0.31.1" }, "devDependencies": { "cross-env": "^7.0.3", diff --git a/packages/server/src/utils/indecent-prediction/index.js b/packages/server/src/utils/indecent-prediction/index.js index fafa1607..cf817671 100644 --- a/packages/server/src/utils/indecent-prediction/index.js +++ b/packages/server/src/utils/indecent-prediction/index.js @@ -1,5 +1,9 @@ const tf = require("@tensorflow/tfjs-node") import * as nsfwjs from "nsfwjs/dist" +import sharp from "sharp" + +import fs from "fs" +import path from "path" import readImage from "../read-image" import imageByteArray from "../image-byte-array" @@ -17,6 +21,22 @@ export default async (payload) => { const model = await nsfwjs.load() + // check if image is not a jpg + if (image.indexOf(".jpg") === -1) { + // convert image to jpg + const converted = await sharp(image) + .jpeg() + .toBuffer() + + // write converted image to disk (use cache) + const destination = path.resolve(global.uploadCachePath, `${Date.now()}.jpg`) + + fs.writeFileSync(destination, converted) + + // set image to the converted image + image = destination + } + const logo = readImage(image) const input = imageToInput(logo, channels)