Skip to content

Commit

Permalink
fix: 🐛 plugin-nsfw已存在的数据无法检测
Browse files Browse the repository at this point in the history
  • Loading branch information
meetqy committed Apr 17, 2023
1 parent 9bed7ae commit fc1dddf
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 21 deletions.
13 changes: 6 additions & 7 deletions packages/plugin-nsfw/lib/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as tf from "@tensorflow/tfjs-node";
import fs from "fs-extra";
import { join, resolve } from "path";
import { NSFWJS } from "nsfwjs";
import { logger } from "@raopics/utils";
// import { logger } from "@raopics/utils";

let isLoad = false;

Expand All @@ -22,19 +22,18 @@ function overideLoad(context: NSFWJS, modelBaseUrl: string) {
*/
const pathOrIOHandler = this.pathOrIOHandler;
const loadOptions = {
onProgress: (fraction: number) => {
{
logger.info(`ModelLoad onProgress:${(fraction * 100).toFixed(1)}%`);
}
},
// onProgress: (fraction: number) => {
// {
// logger.info(`ModelLoad onProgress:${(fraction * 100).toFixed(1)}%`);
// }
// },
fetchFunc(fpath: string) {
let curPath = fpath;
//ForWindows
if (!fs.existsSync(curPath)) {
curPath = resolve(modelBaseUrl, "./" + fpath);
}

// console.log("ModelLoad file: " + fpath, curPath);
return import("node-fetch").then(({ Response: fetchResponse }) => {
return new Promise((resolve, reject) => {
fs.readFile(curPath, (err, data) => {
Expand Down
26 changes: 16 additions & 10 deletions packages/plugin-nsfw/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,12 @@ const _NSFWTags = ["Drawing", "Hentai", "Neutral", "Porn", "Sexy"];
* @param probability predictions[].probability > 阈值, 默认0.35
* @returns
*/
export const PLUGIN_NSFW = async function ({ metadata, database }: TransformBeforeArgs, probability?: 0.35) {
const PLUGIN_NSFW = async function ({ metadata, database }: TransformBeforeArgs, probability?: 0.35) {
if (["jpg", "jpeg", "bmp", "png"].includes(metadata.ext)) {
const { LIBRARY } = process.env;
const { tags } = metadata;

if (database) {
// 【Core】EagleApp 中导入图片,已经存在,并勾选使用已存在的图片,NSFW检测结果会被覆盖。 #90
// https://github.com/rao-pics/core/issues/90
const oldNSFWTags = database.tags.filter((item) => _NSFWTags.includes(item.name));

if (oldNSFWTags) {
metadata.tags = tags.concat(oldNSFWTags.map((item) => item.name));
}
} else {
const createNSFW = async () => {
const predictions = await getPredictions(
`${LIBRARY}/images/${metadata.id}.info/${metadata.name}.${metadata.ext}`
);
Expand All @@ -33,6 +25,20 @@ export const PLUGIN_NSFW = async function ({ metadata, database }: TransformBefo

metadata.nsfw = true;
metadata.tags = tags.concat(className);
};

if (database) {
// 【Core】EagleApp 中导入图片,已经存在,并勾选使用已存在的图片,NSFW检测结果会被覆盖。 #90
// https://github.com/rao-pics/core/issues/90
const oldNSFWTags = database.tags.filter((item) => _NSFWTags.includes(item.name));

if (oldNSFWTags && oldNSFWTags.length > 0) {
metadata.tags = tags.concat(oldNSFWTags.map((item) => item.name));
} else {
await createNSFW();
}
} else {
await createNSFW();
}

return metadata;
Expand Down
1 change: 0 additions & 1 deletion packages/plugin-nsfw/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
"url": "https://github.com/rao-pics/core/issues"
},
"dependencies": {
"@raopics/utils": "workspace:^",
"@tensorflow/tfjs-node": "4.1.0",
"fs-extra": "^11.1.0",
"node-fetch": "^3.3.1",
Expand Down
3 changes: 0 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit fc1dddf

Please sign in to comment.