-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
342 lines (296 loc) · 11.6 KB
/
Copy pathapp.js
File metadata and controls
342 lines (296 loc) · 11.6 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
const fs = require('fs');
const ora = require('ora'); // Se requiere la version 5.4.1
const chalk = require('chalk'); // se requiere la version 4.1.2
const ExcelJS = require('exceljs');
const moment = require('moment');
const qrcode = require('qrcode-terminal');
const Jimp = require('jimp');
const jpeg = require('jpeg-js');
const cv = require('./node_modules/opencv.js');
const { Client, LocalAuth , MessageMedia } = require('whatsapp-web.js');
const SESSION_FILE_PATH = './.wwebjs_auth';
let sessionData;
let client;
cv.FS_createLazyFile('/', 'haarcascade_frontalface_default.xml','haarcascade_frontalface_default.xml', true, false);
const withSession = () => {
const spinner = ora(`Cargando${chalk.yellow(' Validando session con WhatsApp ...\n')}`);
spinner.start();
client = new Client({
authStrategy: new LocalAuth({ clientId: "hola" }),
puppeteer: {
headless: true,
args: ['--no-sandbox']
}
});
client.on('authenticated',session => {
sessionData = session;
});
client.on('ready', () => {
console.log("El Cliente esta listo...\n");
spinner.stop();
listenMessage();
});
client.on('auth_failure', () => {
spinner.stop()
console.log(chalk.red('Error de autenticacion vuelve a generar el QRCODE'))
})
client.initialize();
}
/* Se llamara cuando no se tenga la carpeta de autenticacion */
const withOutSession = () => {
console.log("No tenemos session guardada ...");
client = new Client({
authStrategy: new LocalAuth({ clientId: "hola" }),
puppeteer: {
headless: true,
args: ['--no-sandbox']
}
});
client.on('qr', (qr) => {
qrcode.generate(qr,{ small : true });
});
client.on('authenticated',session => {
sessionData = session;
});
client.initialize();
}
/* Esta funcion es para escuchar los mensajes */
const listenMessage = () => {
client.on('message' , (msg) => {
const { from , to , body } = msg;
sendDownloadMedia(msg,from)
/*Preguntas frecuentes*/
switch (body) {
case 'Quiero comunicarme con el bot':
sendMessage(from,'Hola 👋! Soy el bot de computer vision 🤖 las opciones que tenemos son :\n*- Dilatacion*\n*- Borde*\n*- Gray*\n*- Face Detection*');
break;
case 'Dilatacion':
checkHistory(from , 'Quiero comunicarme con el bot' , "Envia la imagen para procesarla 🦾");
break;
case 'Borde':
checkHistory(from , 'Quiero comunicarme con el bot' , "Envia la imagen para procesarla 🦾");
break;
case 'Gray':
checkHistory(from , 'Quiero comunicarme con el bot' , "Envia la imagen para procesarla 🦾");
break;
case 'Face Detection':
checkHistory(from , 'Quiero comunicarme con el bot' , "Envia la imagen para procesarla 🦾");
break;
case '':
pause().then();
processSentImage(from);
break;
}
saveHistory(from,body)
console.log(from , to , body);
})
}
/*Envio de mensajes multimedia*/
const sendMedia = (to,file) => {
const mediaFile = MessageMedia.fromFilePath(`./${file}`);
client.sendMessage(to,mediaFile);
}
/*Envio de mensajes*/
const sendMessage = (to, message) => {
console.log("#### Mensaje enviado ####")
client.sendMessage(to,message)
}
/*Descarga mensajes multimedia*/
const sendDownloadMedia = (msg , from) => {
client.on('message', async msg => {
if(msg.hasMedia) {
const media = await msg.downloadMedia();
const buffer = Buffer.from(media.data, "base64");
fs.writeFileSync(`${from}`+".jpg", buffer);
}
});
}
/*Guardar el historial de los chats*/
const saveHistory = (number,message) => {
const pathChat = `./chats/${number}.xlsx`;
const workbook = new ExcelJS.Workbook();
const today = moment().format('DD-MM-YYYY hh:mm');
console.log("Directorio : ",pathChat,fs.existsSync(pathChat),'\n')
if (fs.existsSync(pathChat)) {
workbook.xlsx.readFile(pathChat)
.then(() => {
const worksheet = workbook.getWorksheet(1);
const lastRow = worksheet.lastRow;
let getRowInsert = worksheet.getRow(++(lastRow.number))
getRowInsert.getCell('A').value = today;
getRowInsert.getCell('B').value = message;
getRowInsert.commit();
workbook.xlsx.writeFile(pathChat)
.then(() => {
console.log("Se agrego el Chat al historial");
})
.catch(() => {
console.log("Algo ocurrio...");
})
})
} else {
const worksheet = workbook.addWorksheet('chats');
worksheet.columns = [
{ header: 'Fecha', key: 'date'},
{ header: 'Mensaje', key: 'message' },
]
worksheet.addRow([today,message])
console.log("hasta aca bien...")
workbook.xlsx.writeFile(pathChat)
.then(() => {
console.log('Historial creado...');
})
.catch(() => {
console.log('Algo fallo!');
})
}
}
/*Revisar el historial del ultimo registro de un chat*/
const checkHistory = (number , msg_check , msg_send) => {
const pathChat = `./chats/${number}.xlsx`;
const workbook = new ExcelJS.Workbook();
if (fs.existsSync(pathChat)) {
workbook.xlsx.readFile(pathChat)
.then(() => {
const worksheet = workbook.getWorksheet(1);
const lastRow = worksheet.lastRow;
let getRowInsert = worksheet.getRow(lastRow.number)
if ( getRowInsert.getCell('B').value == msg_check){
sendMessage(number,msg_send);
}
})
}
}
/*Logica encargada de procesamiento de imagenes*/
async function imageProcessingLogic(number,operacion){
console.log("Se cargo correctamente la imagen ............................");
if (operacion == 'Dilatacion'){
var jpeg_data = fs.readFileSync(`${number}`+".jpg");
var raw_data = jpeg.decode(jpeg_data);
var src = cv.matFromImageData(raw_data);
let dst = new cv.Mat();
let M = cv.Mat.ones(5, 5, cv.CV_8U);
let anchor = new cv.Point(-1, -1);
cv.dilate(src, dst, M, anchor, 1, cv.BORDER_CONSTANT, cv.morphologyDefaultBorderValue());
//cv.putText(frame, name, {x: rect.x, y: rect.y}, cv.FONT_HERSHEY_SIMPLEX, 1.0, [0, 255, 0, 255]);
image = new Jimp({
data: dst.data,
width: dst.size().width,
height: dst.size().height
});
const font = await Jimp.loadFont(Jimp.FONT_SANS_16_WHITE);
image.print(font,0 , 0 ,"-By NickML");
image.write("Output.jpg");
}
if (operacion == 'Borde'){
var jpeg_data = fs.readFileSync(`${number}`+".jpg");
var raw_data = jpeg.decode(jpeg_data);
var src = cv.matFromImageData(raw_data);
cv.cvtColor(src, src, cv.COLOR_RGBA2GRAY); // Convert to grayscale
dst = new cv.Mat();
cv.Canny(src, dst, 50, 150);
cv.cvtColor(dst, dst, cv.COLOR_GRAY2RGBA); // Convert back to RGBA to display
image = new Jimp({
data: dst.data,
width: dst.size().width,
height: dst.size().height
});
const font = await Jimp.loadFont(Jimp.FONT_SANS_16_WHITE);
image.print(font,0 , 0 ,"-By NickML");
image.write("Output.jpg");
}
if (operacion == 'Gray'){
var image = await Jimp.read(`${number}`+".jpg");
image.grayscale()
const font = await Jimp.loadFont(Jimp.FONT_SANS_16_WHITE);
image.print(font,0 , 0 ,"-By NickML");
image.write("Output.jpg");
}
if (operacion == 'Face Detection'){
var jpeg_data = fs.readFileSync(`${number}`+".jpg");
var raw_data = jpeg.decode(jpeg_data);
var src = cv.matFromImageData(raw_data);
let gray = new cv.Mat();
cv.cvtColor(src, gray, cv.COLOR_RGBA2GRAY);
let faces = new cv.RectVector();
let eyes = new cv.RectVector();
let faceCascade = new cv.CascadeClassifier();
faceCascade.load('haarcascade_frontalface_default.xml');
faceCascade.detectMultiScale(gray, faces, 1.3, 5)
for (let i = 0; i < faces.size(); ++i) {
let roiGray = gray.roi(faces.get(i));
let roiSrc = src.roi(faces.get(i));
let point1 = new cv.Point(faces.get(i).x, faces.get(i).y);
let point2 = new cv.Point(faces.get(i).x + faces.get(i).width,
faces.get(i).y + faces.get(i).height);
cv.rectangle(src, point1, point2, [255, 0, 0, 255],2);
//roiGray.delete(); roiSrc.delete();
}
image = new Jimp({
data: src.data,
width: src.size().width,
height: src.size().height
});
const font = await Jimp.loadFont(Jimp.FONT_SANS_16_WHITE);
image.print(font,0 , 0 ,"-By NickML");
image.write("Output.jpg");
}
}
/*Revisar el historial de los 3 ultimos registros de un chats*/
const checkHistoryOfThrreMsg = (number) => {
const pathChat = `./chats/${number}.xlsx`;
const workbook = new ExcelJS.Workbook();
if (fs.existsSync(pathChat)) {
workbook.xlsx.readFile(pathChat)
.then(() => {
const worksheet = workbook.getWorksheet(1);
const lastRow = worksheet.lastRow;
let getRowInsert1 = worksheet.getRow(lastRow.number)
let getRowInsert2 = worksheet.getRow(lastRow.number-1)
let getRowInsert3 = worksheet.getRow(lastRow.number-2)
if (fs.existsSync(`${number}`+".jpg") && getRowInsert1.getCell('B').value == '' && getRowInsert3.getCell('B').value == 'Quiero comunicarme con el bot'){
sendMessage(number,'Imagen procesada 🥳, espera 10 segundos para recibirla 👀')
if (getRowInsert2.getCell('B').value == 'Dilatacion' ){
imageProcessingLogic(number,'Dilatacion')
}
if (getRowInsert2.getCell('B').value == 'Borde' ){
imageProcessingLogic(number,'Borde')
}
if (getRowInsert2.getCell('B').value == 'Gray' ){
imageProcessingLogic(number,'Gray')
}
if (getRowInsert2.getCell('B').value == 'Face Detection' ){
imageProcessingLogic(number,'Face Detection')
}
sendMediaOutput(number)
} else {
console.log("El archivo aun no es creado .......")
console.log(fs.existsSync(`${number}`+".jpg"))
}
})
}
}
/*Pausa*/
const pause = () => {
return new Promise( (resolve,reject) => {
setTimeout( () => {
resolve();
}, 7000);
});
}
/*Asincrona - Procesando la imagen enviada*/
async function processSentImage (from) {
//console.log('Taking a break ###1...');
const datosFetched = await pause();
checkHistoryOfThrreMsg(from);
//console.log('10 second later ###1')
}
/*Asincrona - Envio de el mensaje multimedia procesado*/
async function sendMediaOutput (from) {
//console.log('Taking a break 2... ###2');
const datosFetched = await pause();
sendMedia(from,"Output.jpg");
//console.log('10 second later ###2');
}
/////////// GATILLA todo el funcionamiento de la APP ////////
(fs.existsSync(SESSION_FILE_PATH)) ? withSession() : withOutSession();