main.js
5.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
const { app, BrowserWindow, ipcMain, dialog } = require('electron')
const path = require('path')
const url = require('url')
const vision = require('@google-cloud/vision');
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let win
function createWindow(){
//브라우저 창 생성
win = new BrowserWindow({
width: 1080,
minWidth: 680,
height: 840,
webPreferences :{
nodeIntegration : true
}
})
win.loadURL(url.format({
pathname: path.join(__dirname, 'src/index.html'),
protocol: 'file:',
slashes : true
})
)
//win.webContents.openDevTools()
win.on('closed', ()=>{
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
win = null;
})
}
app.on('ready', createWindow)
app.on('window-all-closed', ()=>{
if(process.platform !== 'darwin'){
app.quit();
}
});
app.on('activate', ()=>{
if(win == null){
createWindow();
}
})
ipcMain.on("device-name", (e, arg) => {
win.webContents.send("device", arg);
});
//photos taken in selected dates
ipcMain.on("photo", (e, arg) => {
win.webContents.send("select_result", arg);
});
//open-file-dialog
ipcMain.on('open-file-dialog', async (event) => {
const result = await dialog.showOpenDialog({
properties: ['openFile', 'openDirectory']
})
win.webContents.send('selected-directory',result);
})
//console 출력
ipcMain.on('print', (e, arg)=>{
console.log(arg);
})
//internet connect 확인
/**
* const check = require('check-internet-connected');
const config = {
timeout : 5000,
retries : 3,
domain : 'apple.com'
}
check(config)
.then(()=>{
console.log("Connection available");
}).catch((err)=>{
console.log("No connection", err);
});
*/
//이미지 라벨
var label_result = new Array();
ipcMain.on('image_labels', async (event, list) =>{
label_result = [];
await Promise.all(list.map(async (element)=>{
var path = new String();
path = element.path;
var num = path.indexOf("DCIM");
var folder_path = new String();
folder_path = "./photos/" + path.substring(num);
await quickstart(folder_path, element.display_name);
}))
//라벨 검출 결과 전송
win.webContents.send('labels_result',label_result);
console.log(label_result.length);
})
async function quickstart(path, name) {
const client = new vision.ImageAnnotatorClient();
// Performs label detection on the image file
const [result] = await client.labelDetection(path);
const labels = result.labelAnnotations;
path = '../.'+path;
var content = {path : path, name : name, labels : labels}
label_result.push(content);
}
//detext unsafe image
let detect_result = new Array();
ipcMain.on("detect", async (e, result)=>{
detect_result = [];
await Promise.all(result.map(async (element)=>{
var path = new String();
path = element.path;
var num = path.indexOf("DCIM");
var folder_path = new String();
folder_path = "./photos/" + path.substring(num);
await detect(folder_path, element.display_name);
}))
win.webContents.send('detect_result', detect_result);
console.log("detect_success");
});
async function detect(path, name){
//const vision = require('@google-cloud/vision');
// Creates a client
const client = new vision.ImageAnnotatorClient();
// Performs safe search detection on the local file
const [result] = await client.safeSearchDetection(path);
const detections = result.safeSearchAnnotation;
var adult_sum = 0;
var racy_sum = 0;
if(detections.adult == "VERY_LIKELY"){
adult_sum = 100;
}
else if(detections.adult == "LIKELY"){
adult_sum = 80;
}
else if(detections.adult == "POSSIBLE"){
adult_sum = 60;
}
else if(detections.adult == "UNLIKELY"){
adult_sum = 40;
}
else if(detections.adult == "VERY_UNLIKELY"){
adult_sum = 20;
}
else{
adult_sum = 0;
}
if(detections.racy == "VERY_LIKELY"){
racy_sum = 100;
}
else if(detections.racy == "LIKELY"){
racy_sum = 80;
}
else if(detections.racy == "POSSIBLE"){
racy_sum = 60;
}
else if(detections.racy == "UNLIKELY"){
racy_sum = 40;
}
else if(detections.racy == "VERY_UNLIKELY"){
racy_sum = 20;
}
else{
racy_sum = 0;
}
var score = (adult_sum + racy_sum) / 2;
path = '../.'+path;
if(score>=60){
var content = {path : path, name : name, score : score};
detect_result.push(content);
}
}
ipcMain.on('open-error-dialog', (event) => {
dialog.showErrorBox('detected unsafe contents', '당신의 휴대폰에서 유해한 콘텐츠를 발견했습니다!')
})
//이전에 사용한 photos 폴더 삭제
var rimraf = require("rimraf");
rimraf("./photos", function(){console.log("")});