Files
api-service/app/service/indecentContentCheck.js
2020-11-23 15:30:21 +08:00

40 lines
1.2 KiB
JavaScript

'use strict';
const Service = require('egg').Service;
const tf = require('@tensorflow/tfjs-node');
// const jpeg = require('jpeg-js');
const { Image } = require('image-js');
const contentBoundary = require('../util/contentBoundary');
const convert = async img => {
// const image = await jpeg.decode(img, true);
const image = await Image.load(img.filepath);
// console.log('jpeg.decode:', image);
const numChannels = 3;
const numPixels = image.width * image.height;
const values = new Int32Array(numPixels * numChannels);
for (let i = 0; i < numPixels; i++) {
for (let c = 0; c < numChannels; ++c) { values[i * numChannels + c] = image.data[i * 4 + c]; }
}
return tf.tensor3d(values, [ image.height, image.width, numChannels ], 'int32');
};
class ContentCheckService extends Service {
async contentCheck(img) {
try {
const image = await convert(img);
const predictions = await this.app.nsfwModel.classify(image);
console.log('Predictions: ', predictions);
const isSafeContent = contentBoundary(predictions);
return isSafeContent;
} catch (err) {
throw new Error(err);
}
}
}
module.exports = ContentCheckService;