CloudAnts commited on
Commit
d602994
·
1 Parent(s): 6128255
Files changed (4) hide show
  1. Dockerfile +3 -2
  2. app.py +13 -16
  3. requirements.txt +1 -3
  4. templates/index3.html +42 -9
Dockerfile CHANGED
@@ -1,4 +1,4 @@
1
- # Use Python 3.9 as the base image
2
  FROM python:3.9
3
 
4
  # Create a user with ID 1000
@@ -13,11 +13,12 @@ WORKDIR /app
13
  COPY --chown=user ./requirements.txt requirements.txt
14
  RUN pip install --no-cache-dir --upgrade -r requirements.txt
15
 
16
- # Install system dependencies needed for OpenCV and other libraries
17
  USER root
18
  RUN apt-get update && apt-get install -y \
19
  libgl1-mesa-glx \
20
  libglib2.0-0 \
 
21
  && rm -rf /var/lib/apt/lists/*
22
 
23
  # Return to the user 'user' for safety
 
1
+ # Use Python 3.9 as the base image
2
  FROM python:3.9
3
 
4
  # Create a user with ID 1000
 
13
  COPY --chown=user ./requirements.txt requirements.txt
14
  RUN pip install --no-cache-dir --upgrade -r requirements.txt
15
 
16
+ # Install system dependencies needed for OpenCV, Tesseract, and other libraries
17
  USER root
18
  RUN apt-get update && apt-get install -y \
19
  libgl1-mesa-glx \
20
  libglib2.0-0 \
21
+ tesseract-ocr \
22
  && rm -rf /var/lib/apt/lists/*
23
 
24
  # Return to the user 'user' for safety
app.py CHANGED
@@ -16,10 +16,13 @@ from ultralytics import YOLOv10
16
  import supervision as sv
17
  import supervision as sv
18
  from flask import Flask, request, jsonify, send_from_directory, render_template
19
- print(torch.__version__)
20
- print(torchvision.__version__)
21
- import torch
22
- print(torch.cuda.is_available())
 
 
 
23
  import textwrap
24
  app = Flask(__name__)
25
 
@@ -165,15 +168,9 @@ def process_image():
165
  horizontal_list1 = [box for sublist in horizontal_list1 for box in sublist]
166
  free_list1 = []
167
  horizontal_list_for_recognize = detected_boxes if not horizontal_list1 else horizontal_list1
168
- if horizontal_list1:
169
- result = reader.recognize(cropped_image, detail=0, horizontal_list=horizontal_list1,
170
- free_list=free_list1)
171
- else:
172
- result = reader.recognize(random_image_data1, detail=0, horizontal_list=detected_boxes,
173
- free_list=free_list1)
174
- recognized_text = ' '.join(result) if result else ''
175
- else:
176
- recognized_text = 'No image found'
177
  csv_writer.writerow([object_id, bounding_box_info, cropped_image_name, recognized_text])
178
  print(f"CSV file with recognition results saved at {output_csv_path}")
179
 
@@ -191,8 +188,8 @@ def process_image():
191
  iou = inter_area / float(bbox1_area + bbox2_area - inter_area) if (bbox1_area + bbox2_area - inter_area) > 0 else 0
192
  return iou
193
 
194
- image_path = "/data1" + ii
195
- csv_file_path = output_csv_path = '/Folder2/' + ii + 'bounding_boxes_with_recognition.csv'
196
  image = cv2.imread(image_path)
197
  font = cv2.FONT_HERSHEY_SIMPLEX
198
  font_scale = 1.3
@@ -229,7 +226,7 @@ def process_image():
229
  for line in wrapped_text:
230
  cv2.putText(image, line, (x1, text_y), font, font_scale, color, font_thickness)
231
  text_y += int(font_scale * 20)
232
- output_image_path = "/Folder3/" + "annotated" + ii + ".png"
233
  cv2.imwrite(output_image_path, image)
234
  print(f"Annotated image saved at {output_image_path}")
235
  counter += 1
 
16
  import supervision as sv
17
  import supervision as sv
18
  from flask import Flask, request, jsonify, send_from_directory, render_template
19
+ import pytesseract
20
+ # Set the tesseract_cmd based on the operating system
21
+ if os.name == 'nt': # For Windows
22
+ pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
23
+ elif os.name == 'posix': # For Linux/MacOS
24
+ pytesseract.pytesseract.tesseract_cmd = '/usr/bin/tesseract' # Common location for Tesseract on Linux/MacOS
25
+
26
  import textwrap
27
  app = Flask(__name__)
28
 
 
168
  horizontal_list1 = [box for sublist in horizontal_list1 for box in sublist]
169
  free_list1 = []
170
  horizontal_list_for_recognize = detected_boxes if not horizontal_list1 else horizontal_list1
171
+ recognized_text = pytesseract.image_to_string(cropped_image)
172
+ print(f"Recognized Text: {recognized_text}")
173
+
 
 
 
 
 
 
174
  csv_writer.writerow([object_id, bounding_box_info, cropped_image_name, recognized_text])
175
  print(f"CSV file with recognition results saved at {output_csv_path}")
176
 
 
188
  iou = inter_area / float(bbox1_area + bbox2_area - inter_area) if (bbox1_area + bbox2_area - inter_area) > 0 else 0
189
  return iou
190
 
191
+ image_path = "./app/data1" + ii
192
+ csv_file_path = output_csv_path = './app/Folder2/' + ii + 'bounding_boxes_with_recognition.csv'
193
  image = cv2.imread(image_path)
194
  font = cv2.FONT_HERSHEY_SIMPLEX
195
  font_scale = 1.3
 
226
  for line in wrapped_text:
227
  cv2.putText(image, line, (x1, text_y), font, font_scale, color, font_thickness)
228
  text_y += int(font_scale * 20)
229
+ output_image_path = "./app/Folder3/" + "annotated" + ii + ".png"
230
  cv2.imwrite(output_image_path, image)
231
  print(f"Annotated image saved at {output_image_path}")
232
  counter += 1
requirements.txt CHANGED
@@ -1,4 +1,5 @@
1
  flask
 
2
  gunicorn
3
  easyocr
4
  psutil
@@ -9,8 +10,5 @@ pandas
9
  huggingface_hub
10
  supervision
11
  py-cpuinfo
12
- torch
13
- torchvision
14
- -f https://download.pytorch.org/whl/torch_stable.html
15
 
16
 
 
1
  flask
2
+ pytesseract
3
  gunicorn
4
  easyocr
5
  psutil
 
10
  huggingface_hub
11
  supervision
12
  py-cpuinfo
 
 
 
13
 
14
 
templates/index3.html CHANGED
@@ -6,6 +6,7 @@
6
  <title>Invoice Extraction</title>
7
  <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;500;600;700&display=swap" rel="stylesheet">
8
  <style>
 
9
  * { margin: 0; padding: 0; box-sizing: border-box; }
10
  body { font-family: 'Poppins', sans-serif; background: linear-gradient(to right, #6a11cb, #2575fc); color: #333; display: flex; justify-content: center; align-items: center; height: 100vh; }
11
  .container { width: 90%; max-width: 600px; background: #ffffff; box-shadow: 0 10px 20px rgba(0, 0, 0, 0.2); border-radius: 12px; padding: 30px; }
@@ -19,9 +20,27 @@
19
  .actions { text-align: center; margin-top: 10px; }
20
  .actions button { background-color: #2575fc; color: white; padding: 12px 25px; border: none; border-radius: 8px; font-size: 16px; font-weight: 500; cursor: pointer; transition: background-color 0.3s ease; margin: 10px 5px; }
21
  .actions button:hover { background-color: #1a5bb8; }
22
- .output-section { margin-top: 20px; text-align: center; }
23
- .output-section img { max-width: 100%; margin-bottom: 15px; border: 1px solid #ddd; border-radius: 8px; box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1); }
24
- .output-section button { background-color: #34c759; color: white; padding: 10px 20px; border: none; border-radius: 8px; font-size: 14px; font-weight: 500; cursor: pointer; transition: background-color 0.3s ease; }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  .output-section button:hover { background-color: #28a745; }
26
  </style>
27
  </head>
@@ -41,8 +60,14 @@
41
  <button id="upload-btn">Upload & Process</button>
42
  </div>
43
 
44
- <div class="output-section" id="output-section" style="display: none;">
45
- <img id="output-image" src="" alt="Annotated Image">
 
 
 
 
 
 
46
  <a id="download-csv" href="" download><button>Download CSV</button></a>
47
  <a id="download-image" href="" download><button>Download Annotated Image</button></a>
48
  </div>
@@ -52,7 +77,7 @@
52
  const uploadBtn = document.getElementById('upload-btn');
53
  const invoiceInput = document.getElementById('invoice-upload');
54
  const outputSection = document.getElementById('output-section');
55
- const outputImage = document.getElementById('output-image');
56
  const downloadCsvButton = document.getElementById('download-csv');
57
  const downloadImageButton = document.getElementById('download-image');
58
 
@@ -63,18 +88,26 @@
63
  const formData = new FormData();
64
  formData.append('invoice-upload', file);
65
 
 
 
 
 
 
66
  fetch('/upload', {
67
  method: 'POST',
68
  body: formData
69
  })
70
  .then(response => response.json())
71
  .then(data => {
72
- outputImage.src = data.image_path;
 
73
  downloadCsvButton.href = `/download_csv/${data.csv_path.split('/').pop()}`;
74
  downloadImageButton.href = `/download_image/${data.image_path.split('/').pop()}`;
75
- outputSection.style.display = 'block';
76
  })
77
- .catch(error => alert('Error processing the image: ' + error));
 
 
 
78
  });
79
  </script>
80
  </body>
 
6
  <title>Invoice Extraction</title>
7
  <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;500;600;700&display=swap" rel="stylesheet">
8
  <style>
9
+ /* Your existing styles */
10
  * { margin: 0; padding: 0; box-sizing: border-box; }
11
  body { font-family: 'Poppins', sans-serif; background: linear-gradient(to right, #6a11cb, #2575fc); color: #333; display: flex; justify-content: center; align-items: center; height: 100vh; }
12
  .container { width: 90%; max-width: 600px; background: #ffffff; box-shadow: 0 10px 20px rgba(0, 0, 0, 0.2); border-radius: 12px; padding: 30px; }
 
20
  .actions { text-align: center; margin-top: 10px; }
21
  .actions button { background-color: #2575fc; color: white; padding: 12px 25px; border: none; border-radius: 8px; font-size: 16px; font-weight: 500; cursor: pointer; transition: background-color 0.3s ease; margin: 10px 5px; }
22
  .actions button:hover { background-color: #1a5bb8; }
23
+ .output-section { margin-top: 20px; text-align: center; display: none; }
24
+ .please-wait { font-size: 18px; font-weight: 600; color: #ffcc00; display: none; text-align: center; }
25
+
26
+ /* Spinner styling */
27
+ .spinner {
28
+ border: 4px solid #f3f3f3; /* Light background */
29
+ border-top: 4px solid #2575fc; /* Blue color for the spinning part */
30
+ border-radius: 50%;
31
+ width: 50px;
32
+ height: 50px;
33
+ animation: spin 2s linear infinite;
34
+ margin: 10px auto;
35
+ }
36
+
37
+ /* Spinner animation */
38
+ @keyframes spin {
39
+ 0% { transform: rotate(0deg); }
40
+ 100% { transform: rotate(360deg); }
41
+ }
42
+
43
+ .output-section button { background-color: #34c759; color: white; padding: 10px 20px; border: none; border-radius: 8px; font-size: 14px; font-weight: 500; cursor: pointer; transition: background-color 0.3s ease; margin: 10px 5px; }
44
  .output-section button:hover { background-color: #28a745; }
45
  </style>
46
  </head>
 
60
  <button id="upload-btn">Upload & Process</button>
61
  </div>
62
 
63
+ <!-- Please Wait Section with Spinner -->
64
+ <div class="please-wait" id="please-wait">
65
+ <div class="spinner"></div>
66
+ Please wait, processing the file...
67
+ </div>
68
+
69
+ <!-- Output Section (Buttons only) -->
70
+ <div class="output-section" id="output-section">
71
  <a id="download-csv" href="" download><button>Download CSV</button></a>
72
  <a id="download-image" href="" download><button>Download Annotated Image</button></a>
73
  </div>
 
77
  const uploadBtn = document.getElementById('upload-btn');
78
  const invoiceInput = document.getElementById('invoice-upload');
79
  const outputSection = document.getElementById('output-section');
80
+ const pleaseWaitSection = document.getElementById('please-wait');
81
  const downloadCsvButton = document.getElementById('download-csv');
82
  const downloadImageButton = document.getElementById('download-image');
83
 
 
88
  const formData = new FormData();
89
  formData.append('invoice-upload', file);
90
 
91
+ // Show the "Please Wait" message and spinner
92
+ pleaseWaitSection.style.display = 'block';
93
+ outputSection.style.display = 'none';
94
+
95
+ // Send the file to the server for processing
96
  fetch('/upload', {
97
  method: 'POST',
98
  body: formData
99
  })
100
  .then(response => response.json())
101
  .then(data => {
102
+ pleaseWaitSection.style.display = 'none'; // Hide "Please Wait"
103
+ outputSection.style.display = 'block'; // Show download buttons
104
  downloadCsvButton.href = `/download_csv/${data.csv_path.split('/').pop()}`;
105
  downloadImageButton.href = `/download_image/${data.image_path.split('/').pop()}`;
 
106
  })
107
+ .catch(error => {
108
+ pleaseWaitSection.style.display = 'none'; // Hide "Please Wait"
109
+ alert('Error processing the image: ' + error);
110
+ });
111
  });
112
  </script>
113
  </body>