Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,336 +1,298 @@
|
|
| 1 |
-
from flask import Flask, request, jsonify, render_template, send_file
|
| 2 |
-
import os
|
| 3 |
-
from ultralytics import YOLO
|
| 4 |
-
from easyocr import Reader
|
| 5 |
-
import zipfile
|
| 6 |
-
import pandas as pd
|
| 7 |
-
import cv2
|
| 8 |
-
from fuzzywuzzy import fuzz, process
|
| 9 |
-
import re
|
| 10 |
-
import numpy as np
|
| 11 |
-
import json
|
| 12 |
-
import sqlite3
|
| 13 |
-
|
| 14 |
-
app = Flask(__name__)
|
| 15 |
-
app.config['UPLOAD_FOLDER'] = 'uploads/'
|
| 16 |
-
app.config['ALLOWED_EXTENSIONS'] = {'zip', 'xlsx'}
|
| 17 |
-
app.config['RESULTS_FOLDER'] = 'results/'
|
| 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 |
-
input_address['PINCODE Match Score'] =
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
row['Extracted
|
| 125 |
-
row['Extracted
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
row['Name Match
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
row['
|
| 142 |
-
row['
|
| 143 |
-
row['
|
| 144 |
-
row['
|
| 145 |
-
row['
|
| 146 |
-
row['
|
| 147 |
-
row['
|
| 148 |
-
row['Final Address Match'] =
|
| 149 |
-
row['
|
| 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 |
-
row
|
| 176 |
-
row['
|
| 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 |
-
for image_path in image_paths:
|
| 300 |
-
file_name = os.path.basename(image_path)
|
| 301 |
-
key = file_name.split('.')[0][:3]
|
| 302 |
-
if key not in processed_results: # Check if key already exists
|
| 303 |
-
extracted_data = process_image(image_path)
|
| 304 |
-
if extracted_data:
|
| 305 |
-
processed_results[key] = extracted_data
|
| 306 |
-
|
| 307 |
-
# Read Excel and compare data
|
| 308 |
-
df = pd.read_excel(excel_path)
|
| 309 |
-
df = df.astype('str')
|
| 310 |
-
comparison_results = compare_data(df, processed_results)
|
| 311 |
-
comparison_results['Accepted/Rejected'] = np.where(comparison_results['Final Remarks'] == 'All matched', 'Accepted', 'Rejected')
|
| 312 |
-
|
| 313 |
-
# Save results to a new Excel file
|
| 314 |
-
results_df = pd.DataFrame(comparison_results)
|
| 315 |
-
os.makedirs(app.config['RESULTS_FOLDER'], exist_ok=True)
|
| 316 |
-
results_file_path = os.path.join(app.config['RESULTS_FOLDER'], 'results.xlsx')
|
| 317 |
-
results_df.to_excel(results_file_path, index=False)
|
| 318 |
-
|
| 319 |
-
visualization_data = create_visualizations(comparison_results)
|
| 320 |
-
|
| 321 |
-
create_database_and_table() # Ensure database and table exist
|
| 322 |
-
save_results_to_database(comparison_results[
|
| 323 |
-
['SrNo', 'Document Type', 'Accepted/Rejected', 'Final Remarks']
|
| 324 |
-
].to_dict(orient='records'))
|
| 325 |
-
|
| 326 |
-
return jsonify({"message": "Files processed successfully!",
|
| 327 |
-
"results": comparison_results[
|
| 328 |
-
['SrNo', 'Document Type', 'Accepted/Rejected', 'Final Remarks']
|
| 329 |
-
].to_dict(orient='records'),
|
| 330 |
-
"visualization_data": visualization_data})
|
| 331 |
-
|
| 332 |
-
return jsonify({"error": "Both files are required."}), 400
|
| 333 |
-
|
| 334 |
-
if __name__ == '__main__':
|
| 335 |
-
# Configure HTTPS with self-signed certificate
|
| 336 |
-
app.run(ssl_context=('cert.pem', 'key.pem'), debug=True)
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify, render_template, send_file
|
| 2 |
+
import os
|
| 3 |
+
from ultralytics import YOLO
|
| 4 |
+
from easyocr import Reader
|
| 5 |
+
import zipfile
|
| 6 |
+
import pandas as pd
|
| 7 |
+
import cv2
|
| 8 |
+
from fuzzywuzzy import fuzz, process
|
| 9 |
+
import re
|
| 10 |
+
import numpy as np
|
| 11 |
+
import json
|
| 12 |
+
import sqlite3
|
| 13 |
+
|
| 14 |
+
app = Flask(__name__)
|
| 15 |
+
app.config['UPLOAD_FOLDER'] = 'uploads/'
|
| 16 |
+
app.config['ALLOWED_EXTENSIONS'] = {'zip', 'xlsx'}
|
| 17 |
+
app.config['RESULTS_FOLDER'] = 'results/'
|
| 18 |
+
|
| 19 |
+
classifier = YOLO("./models/classification.pt")
|
| 20 |
+
detector = YOLO("./models/detection.pt")
|
| 21 |
+
reader = Reader(['en'])
|
| 22 |
+
|
| 23 |
+
def process_image(image_path):
|
| 24 |
+
if classifier.predict(image_path)[0].probs.numpy().top1 == 0:
|
| 25 |
+
fields = detector(image_path)
|
| 26 |
+
image = cv2.imread(image_path)
|
| 27 |
+
extracted_data = {}
|
| 28 |
+
for field in fields[0].boxes.data.tolist():
|
| 29 |
+
x1, y1, x2, y2, confidence, class_id = map(int, field[:6])
|
| 30 |
+
field_class = detector.names[class_id]
|
| 31 |
+
cropped_roi = image[y1:y2, x1:x2]
|
| 32 |
+
gray_roi = cv2.cvtColor(cropped_roi, cv2.COLOR_BGR2GRAY)
|
| 33 |
+
text = reader.readtext(gray_roi, detail=0)
|
| 34 |
+
extracted_data[field_class] = ' '.join(text)
|
| 35 |
+
return extracted_data
|
| 36 |
+
return None
|
| 37 |
+
|
| 38 |
+
# Helper Functions
|
| 39 |
+
def normalize_text(text):
|
| 40 |
+
if not text:
|
| 41 |
+
return "text empty"
|
| 42 |
+
text = re.sub(r"[^\w\s]", "", text)
|
| 43 |
+
return " ".join(text.split()).lower()
|
| 44 |
+
|
| 45 |
+
def calculate_match_score(input_value, extracted_value):
|
| 46 |
+
if pd.isna(input_value) or pd.isna(extracted_value):
|
| 47 |
+
return 0
|
| 48 |
+
return fuzz.ratio(str(input_value), str(extracted_value))
|
| 49 |
+
|
| 50 |
+
def name_match(input_name, extracted_name):
|
| 51 |
+
if extracted_name is None:
|
| 52 |
+
return False
|
| 53 |
+
input_name = normalize_text(input_name)
|
| 54 |
+
extracted_name = normalize_text(extracted_name)
|
| 55 |
+
|
| 56 |
+
if input_name == extracted_name:
|
| 57 |
+
return True
|
| 58 |
+
|
| 59 |
+
input_parts = input_name.split()
|
| 60 |
+
extracted_parts = extracted_name.split()
|
| 61 |
+
|
| 62 |
+
if sorted(input_parts) == sorted(extracted_parts):
|
| 63 |
+
return True
|
| 64 |
+
|
| 65 |
+
if len(input_parts) == 2 and len(extracted_parts) == 3:
|
| 66 |
+
if input_parts[0] == extracted_parts[0] and input_parts[1] == extracted_parts[2]:
|
| 67 |
+
return True
|
| 68 |
+
if len(input_parts) == 3 and len(extracted_parts) == 2:
|
| 69 |
+
if extracted_parts[0] == input_parts[0] and extracted_parts[1] == input_parts[2]:
|
| 70 |
+
return True
|
| 71 |
+
|
| 72 |
+
for part in input_parts:
|
| 73 |
+
if part not in extracted_parts:
|
| 74 |
+
return False
|
| 75 |
+
return True
|
| 76 |
+
|
| 77 |
+
def address_match(input_address, extracted_address):
|
| 78 |
+
print(input_address, extracted_address)
|
| 79 |
+
if input_address is None or extracted_address is None:
|
| 80 |
+
return False, 0.0, {}
|
| 81 |
+
|
| 82 |
+
# Handle input_address if it's a Series
|
| 83 |
+
if isinstance(input_address, pd.Series):
|
| 84 |
+
input_address = input_address.to_dict()
|
| 85 |
+
|
| 86 |
+
print(extracted_address)
|
| 87 |
+
extracted_address = normalize_text(extracted_address)
|
| 88 |
+
final_score = 0
|
| 89 |
+
print(extracted_address)
|
| 90 |
+
weights = {
|
| 91 |
+
"State": 0.2,
|
| 92 |
+
"Landmark": 0.2,
|
| 93 |
+
"Premise Building Name": 0.2,
|
| 94 |
+
"City":0.2,
|
| 95 |
+
"Street Road Name":0.1,
|
| 96 |
+
"Floor Number": 0.05,
|
| 97 |
+
"House Flat Number": 0.05
|
| 98 |
+
}
|
| 99 |
+
tokens = extracted_address.split(" ")
|
| 100 |
+
# Component matching logic
|
| 101 |
+
for field, weight in weights.items():
|
| 102 |
+
input_value = input_address.get(field, "")
|
| 103 |
+
match_score = fuzz.token_set_ratio(normalize_text(input_value), extracted_address) if input_value else 0
|
| 104 |
+
input_address[field + " Match Score"] = match_score
|
| 105 |
+
final_score += match_score * weight
|
| 106 |
+
pincode_score = process.extractOne(input_address.get("PINCODE"), tokens)[1]
|
| 107 |
+
input_address['PINCODE Match Score'] = pincode_score
|
| 108 |
+
pincode_matched = True if input_address['PINCODE Match Score'] == 100 else False
|
| 109 |
+
|
| 110 |
+
return final_score >= 70 and pincode_matched, final_score, input_address
|
| 111 |
+
|
| 112 |
+
def compare_data(input_data, json_data):
|
| 113 |
+
excel_data = input_data.copy()
|
| 114 |
+
for idx, row in excel_data.iterrows():
|
| 115 |
+
serial_no = row.get("SrNo")
|
| 116 |
+
uid = row.get("UID")
|
| 117 |
+
extracted = json_data.get(serial_no)
|
| 118 |
+
|
| 119 |
+
if extracted:
|
| 120 |
+
extracted_uid = extracted.get("uid", "").replace(" ", "")
|
| 121 |
+
extracted_name = extracted.get("name", "")
|
| 122 |
+
extracted_address = extracted.get("address", "")
|
| 123 |
+
row['Extracted UID'] = extracted_uid
|
| 124 |
+
row['Extracted Name'] = extracted_name
|
| 125 |
+
row['Extracted Address'] = extracted_address
|
| 126 |
+
# UID Match
|
| 127 |
+
uid_match = uid == extracted_uid
|
| 128 |
+
uid_score = 100 if uid_match else 0
|
| 129 |
+
row['UID Match Score'] = uid_score
|
| 130 |
+
|
| 131 |
+
# Name Match
|
| 132 |
+
name_match_result = name_match(row.get("Name"), extracted_name)
|
| 133 |
+
name_score = calculate_match_score(row.get("Name"), extracted_name)
|
| 134 |
+
row['Name Match Score'] = name_score
|
| 135 |
+
row['Name Match Percentage'] = name_score
|
| 136 |
+
|
| 137 |
+
# Address Match
|
| 138 |
+
address_match_result, address_score, partial_scores = address_match(row, extracted_address)
|
| 139 |
+
if partial_scores:
|
| 140 |
+
row['House Flat Number Match Score'] = partial_scores['House Flat Number Match Score']
|
| 141 |
+
row['Street Road Name Match Score'] = partial_scores['Street Road Name Match Score']
|
| 142 |
+
row['City Match Score'] = partial_scores['City Match Score']
|
| 143 |
+
row['Floor Number Match Score'] = partial_scores['Floor Number Match Score']
|
| 144 |
+
row['Premise Building Name Match Score'] = partial_scores['Premise Building Name Match Score']
|
| 145 |
+
row['Landmark Match Score'] = partial_scores['Landmark Match Score']
|
| 146 |
+
row['State Match Score'] = partial_scores['State Match Score']
|
| 147 |
+
row['Final Address Match'] = address_match_result
|
| 148 |
+
row['Final Address Match Score'] = address_score
|
| 149 |
+
row['PINCODE Match Score'] = partial_scores['PINCODE Match Score']
|
| 150 |
+
|
| 151 |
+
# Final Match
|
| 152 |
+
overall_match = uid_match and name_match_result and address_match_result
|
| 153 |
+
|
| 154 |
+
row['Overall Match'] = overall_match
|
| 155 |
+
|
| 156 |
+
if overall_match:
|
| 157 |
+
row['Final Remarks'] = "All matched"
|
| 158 |
+
elif not uid_match:
|
| 159 |
+
row['Final Remarks'] = "UID mismatch"
|
| 160 |
+
elif not name_match_result:
|
| 161 |
+
row['Final Remarks'] = "Name mismatch"
|
| 162 |
+
elif not address_match_result:
|
| 163 |
+
row['Final Remarks'] = "Address mismatch"
|
| 164 |
+
else:
|
| 165 |
+
if extracted_address is None:
|
| 166 |
+
row['Final Remarks'] = "Address missing in aadhar"
|
| 167 |
+
elif extracted_name is None:
|
| 168 |
+
row['Final Remarks'] = "Name missing in aadhar"
|
| 169 |
+
else:
|
| 170 |
+
row['Final Remarks'] = "Non Aadhar"
|
| 171 |
+
|
| 172 |
+
row["Document Type"] = "Aadhaar" if overall_match else "Non-Aadhaar"
|
| 173 |
+
else:
|
| 174 |
+
row.replace(float('nan'), 0)
|
| 175 |
+
row['Final Remarks'] = "Non Aadhar"
|
| 176 |
+
row['Document Type'] = "Non Aadhar"
|
| 177 |
+
excel_data.loc[idx] = row
|
| 178 |
+
return excel_data
|
| 179 |
+
|
| 180 |
+
def create_visualizations(comparison_results):
|
| 181 |
+
visualization_data = {}
|
| 182 |
+
|
| 183 |
+
# TODO: Implement your visualization logic here
|
| 184 |
+
|
| 185 |
+
# Example: Frequency of 'Final Remarks' (for a bar chart)
|
| 186 |
+
visualization_data['final_remarks_frequency'] = {
|
| 187 |
+
'labels': comparison_results['Final Remarks'].value_counts().index.tolist(),
|
| 188 |
+
'values': comparison_results['Final Remarks'].value_counts().values.tolist()
|
| 189 |
+
}
|
| 190 |
+
|
| 191 |
+
# 2. Document Type Proportion (Pie Chart)
|
| 192 |
+
visualization_data['document_type_proportion'] = {
|
| 193 |
+
'labels': comparison_results['Document Type'].value_counts().index.tolist(),
|
| 194 |
+
'values': comparison_results['Document Type'].value_counts().values.tolist()
|
| 195 |
+
}
|
| 196 |
+
|
| 197 |
+
# 3. Accepted vs. Rejected Proportion (Pie Chart)
|
| 198 |
+
visualization_data['accepted_rejected_proportion'] = {
|
| 199 |
+
'labels': comparison_results['Accepted/Rejected'].value_counts().index.tolist(),
|
| 200 |
+
'values': comparison_results['Accepted/Rejected'].value_counts().values.tolist()
|
| 201 |
+
}
|
| 202 |
+
|
| 203 |
+
# 4. UID Match Score Distribution (Histogram)
|
| 204 |
+
visualization_data['uid_match_score_distribution'] = {
|
| 205 |
+
'values': comparison_results['UID Match Score'].tolist()
|
| 206 |
+
}
|
| 207 |
+
|
| 208 |
+
# 5. Name Match Score Distribution (Histogram)
|
| 209 |
+
visualization_data['name_match_score_distribution'] = {
|
| 210 |
+
'values': comparison_results['Name Match Score'].tolist()
|
| 211 |
+
}
|
| 212 |
+
|
| 213 |
+
# 6. Final Address Match Score Distribution (Histogram)
|
| 214 |
+
visualization_data['address_match_score_distribution'] = {
|
| 215 |
+
'values': comparison_results['Final Address Match Score'].tolist()
|
| 216 |
+
}
|
| 217 |
+
|
| 218 |
+
return visualization_data
|
| 219 |
+
|
| 220 |
+
|
| 221 |
+
@app.route('/download', methods=['GET'])
|
| 222 |
+
def download_results():
|
| 223 |
+
file_path = os.path.join(app.config['RESULTS_FOLDER'], 'results.xlsx')
|
| 224 |
+
return send_file(file_path, as_attachment=True)
|
| 225 |
+
|
| 226 |
+
@app.route('/')
|
| 227 |
+
def home():
|
| 228 |
+
return render_template('index.html')
|
| 229 |
+
|
| 230 |
+
@app.route('/services', methods=['GET'])
|
| 231 |
+
def services():
|
| 232 |
+
return render_template('services.html')
|
| 233 |
+
|
| 234 |
+
@app.route('/about', methods=['GET'])
|
| 235 |
+
def about():
|
| 236 |
+
return render_template('about.html')
|
| 237 |
+
|
| 238 |
+
@app.route('/contact', methods=['GET'])
|
| 239 |
+
def contact():
|
| 240 |
+
return render_template('contact.html')
|
| 241 |
+
|
| 242 |
+
@app.route('/upload', methods=['POST'])
|
| 243 |
+
def upload_files():
|
| 244 |
+
if 'zipfile' in request.files and 'excelfile' in request.files:
|
| 245 |
+
zip_file = request.files['zipfile']
|
| 246 |
+
excel_file = request.files['excelfile']
|
| 247 |
+
|
| 248 |
+
# Save files
|
| 249 |
+
zip_path = os.path.join(app.config['UPLOAD_FOLDER'], zip_file.filename)
|
| 250 |
+
excel_path = os.path.join(app.config['UPLOAD_FOLDER'], excel_file.filename)
|
| 251 |
+
zip_file.save(zip_path)
|
| 252 |
+
excel_file.save(excel_path)
|
| 253 |
+
|
| 254 |
+
# Unzip and process images
|
| 255 |
+
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
|
| 256 |
+
zip_ref.extractall(app.config['UPLOAD_FOLDER'])
|
| 257 |
+
|
| 258 |
+
image_paths = [os.path.join(app.config['UPLOAD_FOLDER'], f) for f in os.listdir(app.config['UPLOAD_FOLDER']) if f.endswith(('.jpg', '.png'))]
|
| 259 |
+
processed_results = {}
|
| 260 |
+
|
| 261 |
+
for image_path in image_paths:
|
| 262 |
+
file_name = os.path.basename(image_path)
|
| 263 |
+
key = file_name.split('.')[0][:3]
|
| 264 |
+
if key not in processed_results: # Check if key already exists
|
| 265 |
+
extracted_data = process_image(image_path)
|
| 266 |
+
if extracted_data:
|
| 267 |
+
processed_results[key] = extracted_data
|
| 268 |
+
|
| 269 |
+
# Read Excel and compare data
|
| 270 |
+
df = pd.read_excel(excel_path)
|
| 271 |
+
df = df.astype('str')
|
| 272 |
+
comparison_results = compare_data(df, processed_results)
|
| 273 |
+
comparison_results['Accepted/Rejected'] = np.where(comparison_results['Final Remarks'] == 'All matched', 'Accepted', 'Rejected')
|
| 274 |
+
|
| 275 |
+
# Save results to a new Excel file
|
| 276 |
+
results_df = pd.DataFrame(comparison_results)
|
| 277 |
+
os.makedirs(app.config['RESULTS_FOLDER'], exist_ok=True)
|
| 278 |
+
results_file_path = os.path.join(app.config['RESULTS_FOLDER'], 'results.xlsx')
|
| 279 |
+
results_df.to_excel(results_file_path, index=False)
|
| 280 |
+
|
| 281 |
+
visualization_data = create_visualizations(comparison_results)
|
| 282 |
+
|
| 283 |
+
create_database_and_table() # Ensure database and table exist
|
| 284 |
+
save_results_to_database(comparison_results[
|
| 285 |
+
['SrNo', 'Document Type', 'Accepted/Rejected', 'Final Remarks']
|
| 286 |
+
].to_dict(orient='records'))
|
| 287 |
+
|
| 288 |
+
return jsonify({"message": "Files processed successfully!",
|
| 289 |
+
"results": comparison_results[
|
| 290 |
+
['SrNo', 'Document Type', 'Accepted/Rejected', 'Final Remarks']
|
| 291 |
+
].to_dict(orient='records'),
|
| 292 |
+
"visualization_data": visualization_data})
|
| 293 |
+
|
| 294 |
+
return jsonify({"error": "Both files are required."}), 400
|
| 295 |
+
|
| 296 |
+
if __name__ == '__main__':
|
| 297 |
+
# Configure HTTPS with self-signed certificate
|
| 298 |
+
app.run(debug=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|