-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
89 lines (69 loc) · 3.76 KB
/
index.js
File metadata and controls
89 lines (69 loc) · 3.76 KB
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
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-----\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCWtNPF150rcZTx\nSidIsH7/rfjXEXHQIejgmInpFl9qJBgiec7/kg5iw0lPg37g3GKNhgHy7CsVJzfG\nD5Cxk6HH+A01YRVc16inMSRXhie/dgsQbQuFhtOjyorAkjzOlEEWE/tPJ5KNJON9\nmIWu/fzgrh13181l3TyiXU0hHyMu94vqO/ZWaAfrzwiIRrwtl5V2sI34aAfkko0s\nDrbKMYE4L7We5A8sO5CsZdhBgskhLbEFyOpPn8x7TqXO6bREN4DKsHd4WRN8uibw\n82yqzC2pPW89gb4c77ia5W27yX3+5MnMrutwjoa5rpkjk0vbwBXKrvMTscUD/S71\npRmPRAgzAgMBAAECggEAB+Ykb2s0rrE+WENFudaeYXJR55vfdkRAu3zjRZdJmvuX\nVD9ELTsPi/emYQeIX+rNM5PBiGT+gRZUIwpv0A7n3N5QeurCvuIC0fiOahP7A1sB\ni3PbtbzyXNRh32QK0d0NaEXV1zlAwisbzVpwL1LarUATBVQHTP6CjhQL1d+W7YAx\nevSq5apWaUf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hZ\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);
// 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"));