· 6 years ago · Apr 15, 2019, 04:14 PM
1// Modules to control application life and create native browser window
2const {app, BrowserWindow} = require('electron')
3
4// Keep a global reference of the window object, if you don't, the window will
5// be closed automatically when the JavaScript object is garbage collected.
6let mainWindow
7
8function createWindow () {
9 // Create the browser window.
10 mainWindow = new BrowserWindow({
11 width: 1280,
12 height: 720,
13 resizable: false,
14 frame: false,
15 webPreferences: {
16 nodeIntegration: true
17 }
18 })
19
20 // and load the index.html of the app.
21 mainWindow.loadFile('gui/gui.html')
22
23 // Open the DevTools.
24 mainWindow.webContents.openDevTools()
25
26 // Emitted when the window is closed.
27 mainWindow.on('closed', function () {
28 // Dereference the window object, usually you would store windows
29 // in an array if your app supports multi windows, this is the time
30 // when you should delete the corresponding element.
31 mainWindow = null
32 })
33}
34
35// This method will be called when Electron has finished
36// initialization and is ready to create browser windows.
37// Some APIs can only be used after this event occurs.
38app.on('ready', createWindow)
39
40// Quit when all windows are closed.
41app.on('window-all-closed', function () {
42 // On macOS it is common for applications and their menu bar
43 // to stay active until the user quits explicitly with Cmd + Q
44 app.quit()
45})
46
47app.on('activate', function () {
48 // On macOS it's common to re-create a window in the app when the
49 // dock icon is clicked and there are no other windows open.
50 if (mainWindow === null) {
51 createWindow()
52 }
53})
54
55// In this file you can include the rest of your app's specific main process
56// code. You can also put them in separate files and require them here.
57
58
59//***************************************
60
61
62//this function in typedInput.html
63function processWordTypedInput()
64{
65 console.log("in processWordTypedInput---");
66 var word=document.getElementById("input1").value;
67 localStorage.setItem("word",word);
68 callOutput(word);
69
70}
71
72
73function processWordOcrInput(word)
74{
75 localStorage.setItem("word",word);
76 callOutput(word);
77}
78
79
80//call processWord.py
81async function callOutput(word)
82{
83 console.log("in callOutput---");
84 const loader = document.getElementById("loader");
85 loader.className += " show";
86 console.log(loader.className);
87 console.log("loader showing...");
88 var row = await checkDatabase();
89 console.log(row);
90 if(typeof row == 'undefined')
91 {
92 console.log("calling python");
93 await callPython();
94 //problem is here... showoutput runs before python program fetches everything from net.... because callpython returns without waiting for the python program to end;
95 ////////////////// showOutput();
96 }
97 else{
98 console.log("no need to call python");
99 showOutput();
100 }
101}
102
103async function checkDatabase(){
104 console.log("in async function check database...")
105 const db=require('better-sqlite3')('G:/PROJECTS/SoftDevProject/tsauruswithsqlite/temp/sqlite2.db');
106 var row = undefined;
107 var word = localStorage.getItem("word");
108 console.log(word);
109 try{
110 row = db.prepare('select * from word_table where word=?;').get(word);
111 //console.log(row);
112 }
113 catch{
114 db.prepare("CREATE TABLE IF NOT EXISTS word_table (word text primary key,dic_text text,speech_type text,description text,img_url1 text,img_url2 text,img_url3 text,img_url4 text,img_url5 text,img_url6 text,freq_of_srch number);");
115 row = db.prepare('select * from word_table where word =?;').get(word);
116 //console.log(row);
117 }
118 return row;
119}
120
121async function callPython(){
122 console.log("in async function callPython...")
123 var word = localStorage.getItem("word");
124 console.log(word)
125 const {PythonShell} = require("python-shell");
126 const path = require("path");
127 const script_path = path.join(__dirname,'../backend/');
128 let options = {
129 mode: 'text',
130 pythonPath: "G:/anaconda3/python.exe",
131 scriptPath: "G:/PROJECTS/SoftDevProject/tsauruswithsqlite/backend",
132 args: [word]
133 }
134 console.log(options);
135 let pyWord = new PythonShell('processWord.py',options) ;
136
137 pyWord.on('message',function(message){
138 console.log(message);
139 });
140 /////////////////////////////////
141
142}
143
144
145var word;
146var row;
147var db;
148
149async function showOutput()
150{
151
152 console.log("in showOutput async function...");
153 db=require('better-sqlite3')('G:/PROJECTS/SoftDevProject/tsauruswithsqlite/temp/sqlite2.db');
154 console.log(db);
155 word = localStorage.getItem("word");
156 console.log(word);
157 console.log(db.prepare('select * from word_table where word=?;').get(word));
158 console.log(row)
159 if( typeof row == 'undefined')
160 {
161 processOutputError();
162 const output = document.getElementById("output");
163 output.className += " show";
164 }
165 else{
166 processOutput(row);
167 const output = document.getElementById("output");
168 output.className += " show";
169 }
170}
171/*
172
173 const db = require('better-sqlite3')('G:/PROJECTS/SoftDevProject/tsauruswithsqlite/temp/sqlite2.db');
174 var row = undefined;
175 try{
176 row = db.prepare('select * from word_table where word=?;').get(word);
177 console.log(row);
178 }
179 catch{
180 db.prepare("CREATE TABLE IF NOT EXISTS word_table (word text primary key,dic_text text,speech_type text,description text,img_url1 text,img_url2 text,img_url3 text,img_url4 text,img_url5 text,img_url6 text,freq_of_srch number);");
181 row = db.prepare('select * from word_table where word =?;').get(word);
182 console.log(row);
183 }
184
185
186 if(typeof row == 'undefined')
187 {
188
189 var word = localStorage.getItem("word");
190 const {PythonShell} = require("python-shell");
191 const path = require("path");
192 const script_path = path.join(__dirname,'../backend/');
193 let options = {
194 mode: 'text',
195 pythonPath: "G:/anaconda3/python.exe",
196 scriptPath: "G:/PROJECTS/SoftDevProject/tsaurusElectron/backend",
197 args: [word]
198 }
199 console.log(options);
200 let pyWord = new PythonShell('processWord.py',options) ;
201
202 var status = false;
203
204 pyWord.on('message',function(message){
205 console.log(message);
206 status = true;
207 one();
208 })
209
210
211
212
213*/
214
215// error output
216function processOutputError(){
217 console.log("in processOutputError---");
218 document.getElementById("suppliedWord").innerHTML = localStorage.getItem("word");
219 localStorage.removeItem("word");
220 document.getElementById("dictionary").innerHTML+= "Check your Internet Connection !!!"
221 document.getElementById("encyclopedia").innerHTML+= "Ooops! Mr.Tsaurus could not reach the net !!! ";
222 for(var i=0;i<6;i++)
223 {
224 var x = document.createElement("IMG");
225 x.setAttribute("src", "../temp/errorImage.jpg");
226 x.setAttribute("width", "200");
227 x.setAttribute("height","140");
228 x.setAttribute("style","margin:4px;");
229 document.getElementById("images_").appendChild(x);
230 }
231}
232
233// optimum output
234function processOutput(row)
235{
236 console.log("in processOutput---");
237 document.getElementById("suppliedWord").innerHTML = row.word;
238 localStorage.removeItem("word");
239 document.getElementById("dictionary").innerHTML+=row.dic_text;
240 document.getElementById("encyclopedia").innerHTML+=row.description;
241 var imageurls = [ row.img_url1, row.img_url2, row.img_url3, row.img_url4, row.img_url5, row.img_url6 ];
242 for(var i=0;i<imageurls.length;i++)
243 {
244 var x = document.createElement("IMG");
245 x.setAttribute("src", imageurls[i]);
246 x.setAttribute("width", "200");
247 x.setAttribute("height","140");
248 x.setAttribute("style","margin:4px;");
249 document.getElementById("images_").appendChild(x);
250 }
251
252}
253
254
255function callDetect(){
256
257 const loader = document.getElementById("loader");
258 loader.className += " show";
259 const {PythonShell} = require("python-shell");
260 const path = require("path");
261 const script_path = path.join(__dirname,'../backend/');
262 let options = {
263 mode: 'text',
264 pythonPath: "G:/anaconda3/python.exe",
265 scriptPath: "G:/PROJECTS/SoftDevProject/tsaurusElectron/backend",
266 }
267 console.log(options);
268 let pyWord = new PythonShell('ocrInput.py',options) ;
269 pyWord.on('message',function(message){
270 location.replace("ocrInput.html");
271 })
272
273}
274
275//called from ocrInput.html
276function getDetectedWords()
277{
278 const fs = require("fs");
279 var filepath = "temp/detected.json";
280 try{
281 var words = JSON.parse(fs.readFileSync(filepath));
282 words = Object.keys(words)
283
284 for(var i=0;i<words.length;i++)
285 {
286 var x = document.createElement("button");
287 x.setAttribute("class", "button");
288 x.setAttribute("style", "width:auto; height:auto;font-size:auto;padding:5px;margin:5px;");
289 x.setAttribute("onclick","processWordOcrInput('"+words[i]+"');");
290 var buttoninside = "<span>"+words[i]+"</span>";
291 x.innerHTML = buttoninside;
292 document.getElementById("menu").appendChild(x);
293 }
294 }
295 catch(err)
296 {
297 //create a text saying ..sorry i could not detect anything..
298 alert(err);
299 }
300}
301
302
303
304/*imageurls.forEach(function(item){
305
306 });*/
307
308/*
309 function loadOutputImages()
310 {
311 const fs = require("fs");
312 var filepath = "temp/imageurls.json";
313 try{
314 var imageurls = JSON.parse(fs.readFileSync(filepath));
315 imageurls = Object.keys(imageurls)
316
317
318 for(var i=0;i<imageurls.length;i++)
319 {
320 var x = document.createElement("IMG");
321 x.setAttribute("src", imageurls[i]);
322 x.setAttribute("width", "200");
323 x.setAttribute("height","140");
324 x.setAttribute("style","margin:4px;");
325 document.getElementById("images_").appendChild(x);
326 }
327 }
328 catch(err)
329 {
330 alert(err);
331 }
332 }
333
334/*
335python path --
336G:\anaconda3\python36.zip
337G:\anaconda3\DLLs
338G:\anaconda3\lib
339G:\anaconda3
340G:\anaconda3\lib\site-packages
341G:\anaconda3\lib\site-packages\win32
342G:\anaconda3\lib\site-packages\win32\lib
343G:\anaconda3\lib\site-packages\Pythonwin
344*/
345
346/*
347<img src="1558.jpg" width="200px" >
348
349*/
350/*
351<div id="loader">
352 <img id="dinosaur" alt="mr. tsaurus image" src= "tsaurus.png" class="coverImage">
353 <img id="loadingGif" alt="loading gif image" src="loadingGif.gif" class="loading">
354 <p1 id="text1" style="right: 140px; top: 320px;text-align: center;">
355 capture some text in the camera window....
356 <br/><br/>
357 <b>Mr. Tsaurus</b> is looking for <b>words</b> in the camera window....
358 </p1>
359</div>
360*/