-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
233 lines (197 loc) · 6.27 KB
/
index.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
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
const express = require('express');
const sleep = require('sleep');
const path = require('path');
const vision = require('@google-cloud/vision');
const kuroshiro = require('kuroshiro');
var multer = require('multer')
var expressValidator = require('express-validator');
var fs = require("fs");
// Creates Express App
var app = express();
//initialize kuroshiro
kuroshiro.init(() => {});
// view engine setup
app.use(express.static(path.join(__dirname, 'public')));
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
// Creates a Google Vision API client
const googleAPI = new vision.ImageAnnotatorClient(); // for OCR
const PORT = process.env.PORT || 5000;
//Multer file upload
var upload = multer({
dest: 'public/upload/',
limits: { //limits is for file verification
fieldNameSize: 255,
fileSize: 25242880,
files: 1,
}})
//custom function for use with express validator
//checks the file-type of an upload
function isImage(value, mimetype){
if (mimetype == undefined)
return false;
switch (mimetype) {
case 'image/jpg':
return true;
case 'image/jpeg':
return true;
case 'image/png':
return true;
default:
return false;
}
}
//middleware for express validator
app.use(expressValidator({
customValidators: {
isImage: isImage}}));
//this is an object to hold translation data
//pronunciation is an inaccurate name, should be readings
var UserImage = {
filename:"image.png",
textDetections:"",
textPronunciation:"",
textDetectionsList:[],
textPronunciationList:[]
}
var errors = null;
var boundingBoxes = new Array();
function renderData(req, res) {
console.log('Sending data back');
console.log(UserImage.filename);
res.render('pages/index', {
UserImage: UserImage,
errors: errors,
boundingBoxes: boundingBoxes
})
}
function resolvePath(pathStr) {
if(fs.existsSync(pathStr)) {
return path.resolve(pathStr);
} else {
return undefined;
}
}
//send the image back over to the ejs file for display
function sendToUpload(req, res) {
res.sendFile(resolvePath('./upload/' + UserImage.filename));
}
//checks characters for being kanji or not using unicode
function isKanji(ch) {
return (ch >= "\u4e00" && ch <= "\u9faf") ||
(ch >= "\u3400" && ch <= "\u4dbf");
}
// process results from the Google OCR including text detected
// and each characters associated bounding vertexes
function readGoogle(results) {
const detections = results[0].textAnnotations;
//set bounding boxes for image, and set individual text detections for highlighting
boundingBoxes = new Array();
var detectionsKanji = "";
var translation = "";
UserImage.textDetectionsList = new Array();
UserImage.textPronunciationList = new Array();
//start at 1, since index 0 is the box/detection for the entire image
for (i = 1; i < detections.length; i++) {
var str = detections[i]['description'];
var hasKanji = false;
for (var j = 0; j < str.length; j++) {
var ch = str.charAt(j);
if ( (ch >= "\u4e00" && ch <= "\u9faf") || (ch >= "\u3400" && ch <= "\u4dbf") ){
hasKanji = true;
break;
}
}
if (hasKanji) {
var box = detections[i]['boundingPoly']['vertices'];
boundingBoxes.push(box);
detectionsKanji += detections[i]['description'];
UserImage.textDetectionsList.push(detections[i]['description']);
var kuro = kuroshiro.toHiragana(detections[i]['description']);
UserImage.textPronunciationList.push(kuro);
translation += kuro;
}
}
UserImage.textPronunciation = translation;
//detections without non-kanji characters
UserImage.textDetections = detectionsKanji;
}
//another part of the image validation
function isValidImageBody(req, res) {
req.checkBody('image', 'Please upload an image file').isImage(req.file.mimetype);
var errors = req.validationErrors();
if(errors) {
res.render('pages/index', {
UserImage: UserImage,
boundingBoxes: boundingBoxes,
errors: errors});
return false;
}
return true;
}
// If valid image uploaded rename and pass it along for processing
function translationUpload(req, res, next) {
if(isValidImageBody(req, res)){
// req.files is array of `photos` files
errors = null; //resets errors variable to remove error message
//rename the file
var tempPath = req.file.path;
var targetPath = req.file.path+".png";
UserImage.filename = "/upload/"+path.basename(targetPath);
fs.renameSync(tempPath, targetPath);
// Get Google API results and pass them along to be processed
googleAPI.textDetection(targetPath).then(readGoogle);
// remove redundant files
fs.readdir('upload', (err, files) => {
if (files.length > 5) {
for (i = 0; i < files.length / 2; i ++) {
if (files[i] != UserImage.filename) {
fs.unlink(".public/upload/" + files[i], (err) => {
if (err) throw err;
});
}
}
console.log('successfully deleted half of files');
}
});
//this is crude but sets a 2 second timer for all processing to be done and then displays.
//should have been done using callbacks but our modularity made it difficult
setTimeout(function() {
res.render('pages/index', {
UserImage: UserImage,
errors: errors,
boundingBoxes: boundingBoxes
})
}, 2000);
}
}
app.post('/translate', upload.single('image'), translationUpload);
app.get('/', renderData);
app.get('/'+UserImage.filename, sendToUpload);
//nodes default error handling middleware
app.use(function(err, req, res, next){
if (err.message == 'Cannot read property \'mimetype\' of undefined') {
errors = [{msg: "Please select a file"}];
}
else {
errors = [{msg: err.message}];
}
if(errors) {
res.render('pages/index', {
UserImage: UserImage,
boundingBoxes: boundingBoxes,
errors: errors});
return false;
}
});
//listen on our specified port
app.listen(PORT, () => console.log(`Listening on ${ PORT }`));
//export functions for testing
exports.isImage = isImage
exports.renderData = renderData
exports.resolvePath = resolvePath
exports.sendToUpload = sendToUpload
exports.readGoogle = readGoogle
exports.translationUpload = translationUpload
exports.isValidImageBody = isValidImageBody
exports.UserImage = UserImage