-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
145 lines (124 loc) · 4.25 KB
/
app.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
var PORT = process.env.PORT || 3000;
var express = require("express");
var bodyParser = require("body-parser");
var multer = require("multer");
var path = require("path");
var urlencodedParser = bodyParser.urlencoded({extended: false});
var gtts = require("gtts");
const { render } = require("ejs");
var spawn = require("child_process").spawn;
// init app
var app = express();
// create a variables
var imgName = "";
var imgDiretory = "./public/upload/";
var audioDiretory = "./public/audio/";
// set storage engine
var storage = multer.diskStorage({
destination: "./public/upload/",
filename: function(req, file, cb){
imgName = file.fieldname + "-" + Date.now() + path.extname(file.originalname);
console.log("img name: " + imgName);
cb(null, imgName);
}
});
//init upload
var upload = multer({
storage: storage
}).single("img");
app.set("view engine", "ejs");
app.use(express.static("./public"));
//app.use(bodyParser.json());
//app.use(bodyParser.urlencoded({extended: true}));
app.get("/", (req, res) => {
res.render("index", {qs: req.query});
});
function langCode(lan){
lan = lan.toLowerCase();
if(lan == 'chinese'){
return 'zh-cn';
}
else if(lan == 'english'){
return 'en';
}
else if(lan == 'hindi'){
return 'hi';
}
else if(lan == 'tamil'){
return 'ta';
}
else if(lan == 'japanese'){
return 'ja';
}
else if(lan == 'korean'){
return 'ko'
}
else{
return 'en';
}
}
app.post("/upload", urlencodedParser, (req, res) => {
console.log("in post upload: " + req.body);
upload(req, res, (err) => {
if(err){
res.render("index", {
qs: req.query,
msg: err
});
}
else{
console.log("in else");
console.log(req.body);
console.log(req.file);
if(req.file == undefined){
res.render("index", {
qs: req.query,
msg: "Error: No File Found"
});
}
else{
// child process here
var img_dir_and_name = imgDiretory + imgName;
console.log(img_dir_and_name);
// spawn child process
var process = spawn("python3", ["./main.py", img_dir_and_name, req.body.lang]);
var dataToSend;
process.stdout.on("data", function(data){
console.log("child process");
//get the data(extracted text of the image) from running the python script
dataToSend = data.toString();
console.log("====================\nextracted text:\n" + dataToSend + "====================");
// create speech audio using gtts and save under ./public/audio
var destLang = langCode(req.body.lang);
var speech = new gtts(dataToSend, destLang);
var audio_dir_and_name = `./public/audio/${imgName}.mp3`;
console.log("before save");
speech.save(audio_dir_and_name, function(err, result){
console.log('saving');
if(err){
console.log('gtts error: ' + err);
}
else{
// render
console.log('render');
res.render("index", {
qs: req.query,
msg: "File uploaded!",
file: `upload/${req.file.filename}`,
txt: dataToSend,
audio_dir_and_name: `audio/${imgName}.mp3`
});
console.log('gtts success!!!!!!');
}
});
console.log('after save');
});
// print error from child process if any
process.stderr.on("data", function(data){
console.error(`child stderr: ${data}`);
});
}
}
});
})
app.listen(PORT, () => console.log(`listening to port ${PORT}`));