-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
92 lines (72 loc) · 2.45 KB
/
server.js
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
//const openApiDocumentation = require('./openApiDocumentation');
const express = require('express');
const swagger = require('swagger-ui-express');
const yaml = require('js-yaml');
const cors = require('cors');
const path = require('path');
const fs = require('fs')
const { promisify } = require('util')
const unlinkAsync = promisify(fs.unlink)
const app = express();
let fileContents = fs.readFileSync('./openApiDocumentation.yaml', 'utf8');
let openApiDocumentation = yaml.safeLoad(fileContents);
//Configura cors para que api pueda ser usada en forma remota
app.use(cors());
var port = process.env.PORT || 5000;
app.listen(port, function () {
console.log("Listening on " + port);
});
// configura storage para subir imágenes, las imagenes se registran en formato jpg con la fecha y hora actual como nombre
const multer = require('multer');
const storage = multer.diskStorage({
destination: path.join(__dirname,'/uploads'),
filename: (req, file, cb) => {
cb(null, Date.now() + '.jpg')
}
});
const upload = multer({ storage: storage })
//importa y configura parámetros y credenciales de google vision
const vision = require('@google-cloud/vision')({
projectId: 'testimages-269619',
keyFilename: './cloud-credentials.json'
});
//Objetos detectados en la imagen
app.post("/objects", upload.single('uploads'), function (req, res) {
const currentFile = req.file.path;
const request = {
source: {
filename: currentFile
}
};
vision.labelDetection(request)
.then((results) => {
const objects = results[0].labelAnnotations;
unlinkAsync(req.file.path);
res.send(objects);
})
.catch((err) => {
console.error('ERROR:', err);
res.send("ERROR");
});
});
//Contenido explícito
app.post("/explicit", upload.single('uploads'), function (req, res) {
const currentFile = req.file.path;
const request = {
source: {
filename: currentFile
}
};
vision.safeSearchDetection(request)
.then((results) => {
const objects = results[0].safeSearchAnnotation;
//console.log(objects);
unlinkAsync(req.file.path);
res.send(objects);
})
.catch((err) => {
console.error('ERROR:', err);
res.send("ERROR");
});
});
app.use('/api-doc', swagger.serve, swagger.setup(openApiDocumentation));