-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
112 lines (98 loc) · 3.02 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
const http = require("http");
const express = require("express");
const bodyParser = require("body-parser");
const AzureStorageBlob = require("@azure/storage-blob");
const { BlobServiceClient } = require("@azure/storage-blob");
var cors = require("cors");
const fileUpload = require("express-fileupload");
const fs = require("fs");
const {
FormRecognizerClient,
AzureKeyCredential,
} = require("@azure/ai-form-recognizer");
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
const hostname = "127.0.0.1";
const port = process.env.PORT || "5000";
app.set("port", port);
const server = http.createServer(app);
server.listen(port);
// enable files upload
app.use(
fileUpload({
createParentPath: true,
})
);
app.use((req, res, next) => {
console.log("Enter CORS");
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept, Authorization"
);
res.setHeader(
"Access-Control-Allow-Methods",
"GET, POST, PATCH, DELETE, OPTIONS, PUT"
);
next();
});
app.options("*", cors()); // include before other routes
app.use("/api/analyze", (req, res, next) => {
if (!req.files) {
return res.status(400).send("No files were uploaded.");
}
let file = req.files.file;
uploadPath = __dirname + "/uploads/" + new Date().getTime() + ".jpg";
file.mv(uploadPath, async () => {
recognizeForm(uploadPath).then((result) => {
return res.status(200).json({
output: result,
});
});
});
});
async function recognizeForm(file) {
const endpoint = "https://bankslip.cognitiveservices.azure.com/";
const apiKey = "cf755ff0b06742c2a3a9bd9b5c4a8a6f";
const modelId = "05108577-1b63-446f-ab82-b533c22ae74f";
console.log("Entering Forms Recognizer");
let fileStream = fs.createReadStream(file);
const client = new FormRecognizerClient(
endpoint,
new AzureKeyCredential(apiKey)
);
const poller = await client.beginRecognizeCustomForms(modelId, fileStream, {
contentType: "image/jpeg",
onProgress: (state) => {
console.log(`status: ${state.status}`);
},
});
const forms = await poller.pollUntilDone();
console.log("Forms:");
for (const form of forms || []) {
console.log(`${form.formType}, page range: ${form.pageRange}`);
console.log("Pages:");
for (const page of form.pages || []) {
console.log(`Page number: ${page.pageNumber}`);
console.log("Tables");
for (const table of page.tables || []) {
for (const cell of table.cells) {
console.log(
`cell (${cell.rowIndex},${cell.columnIndex}) ${cell.text}`
);
}
}
}
console.log("Fields:");
for (const fieldName in form.fields) {
// each field is of type FormField
const field = form.fields[fieldName];
console.log(
`Field ${fieldName} has value '${field.value}' with a confidence score of ${field.confidence}`
);
}
}
fs.unlinkSync(uploadPath);
return forms;
}