-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
62 lines (53 loc) · 1.65 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
const Koa = require('koa');
const Router = require('koa-router');
const multer = require('koa-multer');
const vision = require('@google-cloud/vision');
const logger = require('koa-logger');
const config = require('./config')
const PORT = process.env.PORT || 4000;
const upload = multer({ dest: 'uploads/' });
const app = new Koa();
const router = new Router();
app
.use(router.routes())
.use(router.allowedMethods());
app.use(logger());
app.listen(PORT);
console.log(`Server listen on port ${PORT}`);
const client = new vision.ImageAnnotatorClient(config);
router.post('/labels', upload.single('image'), async ctx => {
const file = ctx.req.file.path;
try {
const request = await client.labelDetection(file)
ctx.body = request[0].labelAnnotations
} catch (error) {
ctx.throw(500, error)
}
});
router.post('/landmark', upload.single('image'), async ctx => {
const file = ctx.req.file.path;
try {
const request = await client.landmarkDetection(file)
ctx.body = request[0].landmarkAnnotations
} catch (error) {
ctx.throw(500, error)
}
});
router.post('/text', upload.single('image'), async ctx => {
const file = ctx.req.file.path;
try {
const request = await client.documentTextDetection(file)
ctx.body = request[0].fullTextAnnotation
} catch (error) {
ctx.throw(500, error)
}
});
router.post('/face-detection', upload.single('image'), async ctx => {
const file = ctx.req.file.path;
try {
const request = await client.faceDetection(file)
ctx.body = request[0].faceAnnotations
} catch (error) {
ctx.throw(500, error)
}
});