File size: 17,482 Bytes
0d6ddfa
 
 
 
 
 
 
 
 
 
 
ee7e9d0
 
 
0d6ddfa
 
 
 
 
 
247a2c8
0d6ddfa
 
 
 
 
 
 
144dfb5
 
0d6ddfa
ee7e9d0
bd42982
 
 
 
 
 
 
 
 
144dfb5
bd42982
 
6df3c38
bd42982
6df3c38
144dfb5
 
 
 
 
 
 
 
 
 
 
 
6df3c38
144dfb5
bd42982
 
 
 
 
 
 
 
 
 
 
 
 
144dfb5
 
bd42982
 
144dfb5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6df3c38
144dfb5
 
 
 
 
bd42982
 
 
 
144dfb5
 
bd42982
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ee7e9d0
 
bd42982
144dfb5
bd42982
 
 
 
 
 
 
 
 
 
ee7e9d0
 
144dfb5
bd42982
 
 
 
 
 
144dfb5
bd42982
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
144dfb5
 
bd42982
 
 
 
144dfb5
bd42982
 
 
144dfb5
 
 
 
 
 
 
 
 
bd42982
144dfb5
 
 
 
 
 
bd42982
 
 
 
144dfb5
bd42982
3b9f355
 
 
 
bd42982
 
 
144dfb5
bd42982
 
 
3b9f355
bd42982
3b9f355
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bd42982
3b9f355
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bd42982
 
 
 
ee7e9d0
3b9f355
ee7e9d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bd42982
ee7e9d0
 
 
bd42982
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <!--Favicon-->
    <link rel="shortcut icon" href="/static/images/favicon.ico" title="Favicon" />

    <!-- Main CSS Files -->
    <!-- <link rel="stylesheet" href="/static/css/style.css"> -->
    <link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}">
    <link rel="stylesheet" href="{{ url_for('static', filename='css/extractor.css') }}">

    <!-- Color CSS -->
    <link rel="stylesheet" href="/static/css/color.css">

    <!--Icon Fonts - Font Awesome Icons-->
    <link rel="stylesheet" href="/static/css/font-awesome.min.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css">
    <!-- Animate CSS-->
    <link href="css/animate.css" rel="stylesheet" type="text/css">

    <!--Google Webfonts-->
    <link href='https://fonts.googleapis.com/css?family=Open+Sans:400,300,600,700,800' rel='stylesheet' type='text/css'>
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@100;200;300;400;500;600&display=swap"
        rel="stylesheet">
    <title>ExCeipt | Extractor</title>

    <script src="https://cdn.lordicon.com/lordicon.js"></script>
</head>

<body>
    <div id="flash-container" class="alert alert-danger" role="alert">
        <!-- Flash message will be displayed here -->
    </div>
    <div class="containerz">
        <div class="left-column">
            <button class="backbutton" id="submitButton" onclick="window.location.href='{{ index_url }}'">
                <i class="fa-solid fa-arrow-left"></i>
                <span>Back</span>
            </button>
            <div id="canvas-container">
                {% if image_paths %}
                {% for image_path, prediction_results_copy in predictions.items() %}
                <div
                    class="image-container {% if prediction_results_copy == 'non-receipt' %}red-border{% elif prediction_results_copy == 'receipt' %}green-border{% endif %}">
                    <div class="tooltiptext">Click to view fullscreen</div>
                    <img src="{{ url_for('static', filename='temp/img_display/' + image_path) }}" id="my-image"
                        alt="uploads"
                        data-img-path="{{ url_for('static', filename='temp/img_display/' + image_path) }}" />
                    <div class="image-name">
                        <p class="images-name">{{image_path}}</p>
                    </div>
                </div>
                {% endfor %}
                {% else %}
                <p>No files uploaded.</p>
                {% endif %}
            </div>
        </div>
        <div class="right-column">
            <div id="logox">
                <img src="/static/images/logo.png" id="banner-logo" alt="Landing Page" />
            </div>

            <div style="display: flex; align-items: center;">
            </div>
            <hr style="color: #ccc; width:95%; opacity:0.35;">
            <div class="labels" style="margin: 0 0 0 20px;">
                <p>Receipt Verification</p>
                <p class="desc">Verify receipt details attentively, acknowledging occasional misclassification, which
                    may arise from variations in image quality or format.</p>
            </div>
            <div class="receipt">
                <table>
                    <thead>
                        <tr>
                            <th>File Name</th>
                            <th>Prediction Result</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for image_path, prediction_results_copy in predictions.items() %}
                        <tr>
                            <td>{{ image_path }}</td>
                            <td>
                                <div class="details">
                                    <div style="display: flex; align-items: center; justify-content: center;">
                                        {% if prediction_results_copy.lower() == 'receipt' %}
                                        <lord-icon src="https://cdn.lordicon.com/lomfljuq.json" trigger="in"
                                            delay="1500" colors="primary:#16c72e" state="morph-check-in-1"
                                            style="width:25px;height:25px">
                                        </lord-icon>
                                        <p class="valid">Valid Receipt</p>
                                        {% else %}
                                        <lord-icon src="https://cdn.lordicon.com/zxvuvcnc.json" trigger="in"
                                            delay="1500" state="morph-cross-in" colors="primary:#e83a30"
                                            style="width:25px;height:25px">
                                        </lord-icon>
                                        <p class="valid">Not a Receipt</p>
                                        {% endif %}
                                    </div>
                                </div>
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
            <hr style="color: #ccc; width:95%; opacity:0.35;">
            <div class="labels" style="margin: 0 0 0 20px;">
                <p>Text Extraction</p>
                <p class="desc">Ensure optimal text extraction accuracy from receipts by making sure that the receipt
                    image is clear, well-lit, and after extraction review the extracted text and edit.</p>
            </div>
            <div style="text-align: center; margin-top: 25px;">
                <button id="uploadbutton" class="uploadbutton" type="submit">
                    <i id="loadingIcon" class="fa fa-spinner fa-spin" style="display: none;"></i>
                    <span id="extractingText">Extract</span>
                </button>
            </div>
            <div class="avatargif">
                <img src="/static/images/avatar.gif" id="avatargif" />
            </div>
            <div class="receipt1" id="receiptdiv">
                <div class="details1">
                    <table id="dataTable" border="1">
                        <thead>
                            <tr>
                                <th>Receipt No.</th>
                                <th>Merchant</th>
                                <th>Address</th>
                                <th>Date</th>
                                <th>Time</th>
                                <th>Items</th>
                                <th>Price</th>
                                <th>Total</th>
                                <th>VAT</th>
                            </tr>
                        </thead>
                        <tbody>
                        </tbody>
                    </table>
                </div>
            </div>

            <div style="text-align: center; display: none; justify-content: center;" id="downloadbutton">
                <a href="{{ url_for('download_csv') }}">
                    <button id="downloadButton" class="exportbutton" type="submit"><i
                            class="fa-solid fa-download"></i>Download CSV</button>
                </a>
                <button class="exportbutton" onclick="window.location.href='{{ index_url }}'">
                    <span id="uploadIcon"><i class="fas fa-upload"></i></span>
                    <span>Upload again</span>
                </button>
            </div>



        </div>
        <a href="https://lordicon.com/" hidden>Icons by Lordicon.com</a>
        <script>
            const index_url = '{{ index_url }}';  // Replace with the actual value if it's not supposed to be a template variable

            document.addEventListener('DOMContentLoaded', function () {
                const dataTable = document.getElementById('receiptdiv');
                const loadingSpinner = document.getElementById('loadingIcon');
                const extractingText = document.getElementById('extractingText');
                const runInferenceButton = document.getElementById('uploadbutton');
                const exportCSVButton = document.getElementById('downloadbutton');

                document.getElementById('uploadbutton').addEventListener('click', function () {
                    loadingSpinner.style.display = 'inline-block'; // Show loading spinner
                    extractingText.textContent = 'Extracting...'; // Change text to "Extracting..."
                    runInferenceButton.disabled = true;
                    // Hide the avatargif image
                    document.getElementById('avatargif').style.display = 'none';
                    runInference();
                });

                async function getData() {
                    const response = await fetch('/get_data');
                    const data = await response.text();

                    updateTable(data);

                    // Show the table when data is updated
                    dataTable.style.display = 'table';
                }
                const imageElement = document.getElementById('my-image'); // replace 'my-image' with the id of your image element


                async function runInference() {
                    await fetch('/run_inference');

                    setTimeout(function () {
                        loadingSpinner.style.display = 'none';
                        runInferenceButton.style.display = 'none';
                        exportCSVButton.style.display = 'flex';
                        var imageElements = document.querySelectorAll('[data-img-path]');

                        // Iterate over each image
                        imageElements.forEach(function (imageElement) {
                            var imagePath = imageElement.dataset.imgPath;
                            // Remove the 'temp/img_display/' part from the imagePath
                            // var newImagePath = imagePath.replace('temp/img_display/', '');
                            // Update the image source
                            imageElement.src = imagePath + "_inference.jpg";

                            // Add an error handler
                            imageElement.onerror = function () {
                                // Hide the image's container if the image fails to load
                                this.parentElement.style.display = 'none';
                            };
                        });
                        getData();
                    }, 500);
                }


                function updateTable(data) {
                    if (!data || data.trim() === '') {
                        return 'Error extracting the information: data is empty';
                    }

                    Papa.parse(data, {
                        header: true,
                        skipEmptyLines: true,
                        complete: function (results) {
                            const tbody = document.querySelector('#dataTable tbody');
                            tbody.innerHTML = ''; // Clear existing rows

                            if (results.data.length === 0) {
                                const tr = document.createElement('tr');
                                const td = document.createElement('td');
                                td.colSpan = 9;
                                td.style.textAlign = 'center'; // Center the content
                                td.style.fontSize = '1.2em'; // Increase the font size
                                td.style.height = '100px'; // Set the height of the row
                                td.style.verticalAlign = 'middle'; // Vertically center the content

                                const icon = document.createElement('i');
                                icon.className = 'fas fa-exclamation-triangle'; // Replace with the class of your error icon
                                td.appendChild(icon);
                                
                                const br = document.createElement('br'); // Create a line break element
                                td.appendChild(br); // Append the line break element

                                const text = document.createTextNode(' An error occurred in extracting the data. Please contact the developers.');
                                td.appendChild(text);

                                tr.appendChild(td);
                                tbody.appendChild(tr);
                            } else {
                                results.data.forEach(row => {
                                    const RECEIPTNUMBER = row['RECEIPTNUMBER'] || '';
                                    const MERCHANTNAME = row['MERCHANTNAME'] || '';
                                    const MERCHANTADDRESS = row['MERCHANTADDRESS'] || '';
                                    const TRANSACTIONDATE = row['TRANSACTIONDATE'] || '';
                                    const TRANSACTIONTIME = row['TRANSACTIONTIME'] || '';
                                    const ITEMS = row['ITEMS'] || '';
                                    const PRICE = row['PRICE'] || '';
                                    const TOTAL = row['TOTAL'] || '';
                                    const VATTAX = row['VATTAX'] || '';

                                    const tr = document.createElement('tr');
                                    tr.innerHTML = `
                                        <td contenteditable="true">${RECEIPTNUMBER}</td>
                                        <td contenteditable="true">${MERCHANTNAME}</td>
                                        <td contenteditable="true">${MERCHANTADDRESS}</td>
                                        <td contenteditable="true">${TRANSACTIONDATE}</td>
                                        <td contenteditable="true">${TRANSACTIONTIME}</td>
                                        <td contenteditable="true">${ITEMS}</td>
                                        <td contenteditable="true">${PRICE}</td>
                                        <td contenteditable="true">${TOTAL}</td>
                                        <td contenteditable="true">${VATTAX}</td>
                                    `;
                                    tbody.appendChild(tr);
                                });
                            }
                        }
                    });
                }
            });


            document.querySelector('#downloadButton').addEventListener('click', function (event) {
                event.preventDefault();  // Prevent the default action

                const tbody = document.querySelector('#dataTable tbody');
                const rows = Array.from(tbody.querySelectorAll('tr'));

                // Define your headers
                const headers = ['RECEIPTNUMBER', 'MERCHANTNAME', 'MERCHANTADDRESS', 'TRANSACTIONDATE', 'TRANSACTIONTIME', 'ITEMS', 'PRICE', 'TOTAL', 'VATTAX'];

                // Convert the content to CSV format
                const csvData = [headers.join(',')].concat(rows.map(row => {
                    const cells = Array.from(row.querySelectorAll('td'));
                    return cells.map(cell => cell.textContent.replace(/,/g, '.')).join(',');
                })).join('\n');

                // Send a POST request to the server with the CSV data
                fetch(index_url + 'download_csv', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'text/csv'
                    },
                    body: csvData
                })
                    .then(response => response.blob())
                    .then(blob => {
                        // Create a Blob URL and initiate the download
                        const url = URL.createObjectURL(blob);
                        const a = document.createElement('a');
                        a.href = url;
                        a.download = 'data.csv';
                        a.click();
                    });
            });
        </script>



        <!-- Include JavaScript resources -->
        <script src="https://cdnjs.cloudflare.com/ajax/libs/PapaParse/5.3.0/papaparse.min.js"></script>
        <script src="/static/js/jquery.1.8.3.min.js"></script>
        <script src="/static/js/wow.min.js"></script>
        <script src="/static/js/featherlight.min.js"></script>
        <script src="/static/js/featherlight.gallery.min.js"></script>
        <script src="/static/js/jquery.enllax.min.js"></script>
        <script src="/static/js/jquery.scrollUp.min.js"></script>
        <script src="/static/js/jquery.easing.min.js"></script>
        <script src="/static/js/jquery.stickyNavbar.min.js"></script>
        <script src="/static/js/jquery.waypoints.min.js"></script>
        <script src="/static/js/images-loaded.min.js"></script>
        <script src="/static/js/lightbox.min.js"></script>
        <script src="/static/js/site.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/4.5.0/fabric.min.js"></script>

</body>

</html>