use sharp to transform to jpeg

This commit is contained in:
srgooglo 2022-10-16 18:22:48 +02:00
parent 65cab457ff
commit 86321e56d1
2 changed files with 22 additions and 1 deletions

View File

@ -30,7 +30,8 @@
"passport": "0.5.2", "passport": "0.5.2",
"passport-jwt": "4.0.0", "passport-jwt": "4.0.0",
"passport-local": "1.0.0", "passport-local": "1.0.0",
"path-to-regexp": "6.2.0" "path-to-regexp": "6.2.0",
"sharp": "^0.31.1"
}, },
"devDependencies": { "devDependencies": {
"cross-env": "^7.0.3", "cross-env": "^7.0.3",

View File

@ -1,5 +1,9 @@
const tf = require("@tensorflow/tfjs-node") const tf = require("@tensorflow/tfjs-node")
import * as nsfwjs from "nsfwjs/dist" import * as nsfwjs from "nsfwjs/dist"
import sharp from "sharp"
import fs from "fs"
import path from "path"
import readImage from "../read-image" import readImage from "../read-image"
import imageByteArray from "../image-byte-array" import imageByteArray from "../image-byte-array"
@ -17,6 +21,22 @@ export default async (payload) => {
const model = await nsfwjs.load() 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 logo = readImage(image)
const input = imageToInput(logo, channels) const input = imageToInput(logo, channels)