-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
104 lines (73 loc) · 4.02 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
const express = require('express');
const mongoose = require('mongoose');
const { spawn } = require('child_process');
const dotenv =require('dotenv').config();
const cors = require('cors');
const bodyParser = require('body-parser');
const port = process.env.PORT || 8080;
const app = express();
const vision = require('@google-cloud/vision');
app.use(cors())
let imgresult;
app.use(bodyParser.json());
const CONFIG = {
credentials: {
private_key:"-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCeOi8FgtnGXPAK\n5mVGMR27iZRxleC1F0TmKCfSIPiZUAhyj5FWyBxhjLEoxHZVtOF4MdFnmc5rP87b\nmDVMRgRSVTi0L3y6nZHM5qqRuHZVfyzyu4pdpC7E8g8Ad+4GSvzHd787jKleKW5s\nNM4uEwUOcuzOSMCaetUFuYeZjA28+8ua7959e4r5OOsQUUkaUXj2L+nIl56mCPqE\n8pJY/4dpeJgfCx3yYm19/E/2n4A2jbNMRjk21oYCLRIayFtOF8Vx5uZRh6KYys4Q\nE5ly1ClSc+6kB9jtWdnl8RpWZxpz8Yu9Gz3MvjzeuVtOnQDbxuLcFQBUUjgOd6PC\n8BTXk+7LAgMBAAECggEALlI2RdTC0HGQzIAakev7GlXyoTYRbLJ9Nyv2TGvem3N0\npaUmFVPMCzrW+mpZgknL4BYlicDbT0aE1oC+ya+NwFS2nvlv+Gn8rxGwSazI58qt\na/o/TZ6tGncKsUZq5mi5i+OgllxdkPVsID1tRsvGpS476UAfMppXFq/z5J29v984\n7Kloz9WzLm/E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=\n-----END PRIVATE KEY-----\n",
client_email:process.env.EMAIL
}
};
async function main() {
await mongoose.connect(process.env.MONGO_URL);
console.log('db connected')
}
const clientVision = new vision.ImageAnnotatorClient(CONFIG);
main().catch(err => console.log(err));
const userSchema = new mongoose.Schema({
username: String,
email: String,
predictedText: String,
image:String,
});
const User = mongoose.model('User', userSchema);
// Get route for home
app.get('/', (req, res) => res.send({
"Status": "Server Running..."
}))
//POST route for text-prediction
app.post('/predict', async (req, res) => {
const imageUrl = req.body.imgUrl;
// Call detectText function with the URL and get the result
const imgResult = await detectText(imageUrl);
// Sending the received result from Google Vision for Post-Processing
const pythonProcess = spawn('python', ['processText.py', imgResult]);
// Collect the output from the Python script
pythonProcess.stdout.on('data', (data) => {
imgresult += data.toString();
});
// Return the post processed predicted text as a response
res.json({
predictedText: imgResult
});
});
app.post('/data', async (req, res) => {
let user = new User();
user.username = req.body.username;
user.email = req.body.email;
user.predictedText = req.body.predictedText;
user.image=req.body.image;
const doc = await user.save();
console.log(doc);
res.json(doc);
})
app.get('/data',async (req,res)=>{
const docs = await User.find({});
res.json(docs)
})
// //text detection
const detectText = async (file_path) => {
let [result] = await clientVision.textDetection(file_path);
return result.fullTextAnnotation.text
};
// For predicting the image text
// detectText('https://images.squarespace-cdn.com/content/v1/5005cd4be4b046f04f5a1111/1402697534125-1RO053UCAJYF73GH16M2/image-asset.png');
app.listen(port, () => console.log("Started"))