✨ add image recognition classification
This commit is contained in:
@@ -4,6 +4,8 @@ import {langStore} from "@/store/modules/langStore.ts";
|
||||
import {useCommentStore} from "@/store/modules/commentStore.ts";
|
||||
import {useWebSocketStore} from "@/store/modules/websocketStore.ts";
|
||||
import {useUpscaleStore} from "@/store/modules/upscaleStore.ts";
|
||||
import {useMenuStore} from "@/store/modules/menuStore.ts";
|
||||
import {useUploadStore} from "@/store/modules/uploadStore.ts";
|
||||
|
||||
export default function useStore() {
|
||||
return {
|
||||
@@ -13,5 +15,7 @@ export default function useStore() {
|
||||
comment: useCommentStore(),
|
||||
websocket: useWebSocketStore(),
|
||||
upscale: useUpscaleStore(),
|
||||
menu: useMenuStore(),
|
||||
upload: useUploadStore(),
|
||||
};
|
||||
}
|
||||
|
||||
18
src/store/modules/menuStore.ts
Normal file
18
src/store/modules/menuStore.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
export const useMenuStore = defineStore(
|
||||
'menu',
|
||||
() => {
|
||||
const currentMenu = ref<string>('photo/all');
|
||||
return {
|
||||
currentMenu,
|
||||
};
|
||||
},
|
||||
{
|
||||
// 开启数据持久化
|
||||
persistedState: {
|
||||
persist: true,
|
||||
storage: localStorage,
|
||||
key: 'menu',
|
||||
includePaths: ['currentMenu']
|
||||
}
|
||||
}
|
||||
);
|
||||
80
src/store/modules/uploadStore.ts
Normal file
80
src/store/modules/uploadStore.ts
Normal file
@@ -0,0 +1,80 @@
|
||||
import {initNSFWJs, predictNSFW} from "@/utils/nsfw/nsfw.ts";
|
||||
import i18n from "@/locales";
|
||||
|
||||
import {NSFWJS} from "nsfwjs";
|
||||
import {message} from "ant-design-vue";
|
||||
|
||||
import {loadCocoSsd, loadMobileNet} from "@/utils/tfjs";
|
||||
|
||||
export const useUploadStore = defineStore(
|
||||
'upload',
|
||||
() => {
|
||||
const openUploadDrawer = ref<boolean>(false);
|
||||
const image: HTMLImageElement = document.createElement('img');
|
||||
|
||||
/**
|
||||
* 图片上传前的校验
|
||||
* @param file
|
||||
*/
|
||||
async function beforeUpload(file: File) {
|
||||
image.src = URL.createObjectURL(file);
|
||||
// 图片 NSFW 检测
|
||||
const nsfw: NSFWJS = await initNSFWJs();
|
||||
const isNSFW: boolean = await predictNSFW(nsfw, image);
|
||||
if (isNSFW) {
|
||||
message.error(i18n.global.t('comment.illegalImage'));
|
||||
return false;
|
||||
}
|
||||
const predictions = await loadMobileNet(image);
|
||||
console.log(predictions);
|
||||
|
||||
const prediction = await loadCocoSsd(image);
|
||||
console.log(prediction);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 自定义上传请求
|
||||
* @param file
|
||||
*/
|
||||
async function customUploadRequest(file: any) {
|
||||
|
||||
const progress = {percent: 1};
|
||||
|
||||
|
||||
const intervalId = setInterval(() => {
|
||||
if (progress.percent < 100) {
|
||||
progress.percent++;
|
||||
file.onProgress(progress);
|
||||
} else {
|
||||
clearInterval(intervalId);
|
||||
}
|
||||
}, 100);
|
||||
file.onSuccess(true);
|
||||
|
||||
// file.onSuccess = () => {
|
||||
// message.success(i18n.global.t('comment.uploadSuccess'));
|
||||
// };
|
||||
// file.onError = () => {
|
||||
// message.error(i18n.global.t('comment.uploadError'));
|
||||
// };
|
||||
// return Promise.resolve(file);
|
||||
|
||||
}
|
||||
|
||||
return {
|
||||
openUploadDrawer,
|
||||
beforeUpload,
|
||||
customUploadRequest
|
||||
};
|
||||
},
|
||||
{
|
||||
// 开启数据持久化
|
||||
persistedState: {
|
||||
persist: false,
|
||||
storage: localStorage,
|
||||
key: 'upload',
|
||||
includePaths: []
|
||||
}
|
||||
}
|
||||
);
|
||||
@@ -101,6 +101,13 @@ export const useUpscaleStore = defineStore(
|
||||
const msg = ref<string>("");
|
||||
const progressBar = ref<number>(0);
|
||||
const status = ref<string>('loading');
|
||||
|
||||
|
||||
const dragging = ref<boolean>(false);
|
||||
const linePosition = ref(0);
|
||||
const draggingLine = ref(false);
|
||||
|
||||
|
||||
/**
|
||||
* 图片上传前的校验
|
||||
* @param file
|
||||
@@ -118,10 +125,11 @@ export const useUpscaleStore = defineStore(
|
||||
uploading.value = false;
|
||||
return false;
|
||||
}
|
||||
await clear();
|
||||
fileData.value = urlData;
|
||||
await loadImg(image);
|
||||
uploading.value = false;
|
||||
await clear();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -142,6 +150,12 @@ export const useUpscaleStore = defineStore(
|
||||
msg.value = "";
|
||||
progressBar.value = 0;
|
||||
isProcessing.value = false;
|
||||
dragging.value = false;
|
||||
linePosition.value = 0;
|
||||
draggingLine.value = false;
|
||||
input.value = null;
|
||||
inputAlpha.value = null;
|
||||
wasmModule.value = null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -203,6 +217,9 @@ export const useUpscaleStore = defineStore(
|
||||
msg,
|
||||
progressBar,
|
||||
status,
|
||||
dragging,
|
||||
linePosition,
|
||||
draggingLine,
|
||||
loadImg,
|
||||
beforeUpload,
|
||||
customUploadRequest,
|
||||
|
||||
Reference in New Issue
Block a user