更新API

This commit is contained in:
dengtongyu committed 2020-11-23 15:30:21 +08:00
1 parent 16769a8726
commit ece1e8065c
16 files changed
+161 -1

No files matched your search

+39
View File
@@ -0,0 +1,39 @@
'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;