File size: 13,880 Bytes
6df3c38 0d6ddfa 6df3c38 e43e91a 6df3c38 0d6ddfa 6df3c38 0d6ddfa 6df3c38 0d6ddfa 6df3c38 0d6ddfa 6df3c38 0d6ddfa 6df3c38 0d6ddfa 6df3c38 0d6ddfa 6df3c38 0d6ddfa 6df3c38 0d6ddfa 6df3c38 6e80c6f 0d6ddfa 6df3c38 0d6ddfa 6df3c38 0d6ddfa 6df3c38 0d6ddfa 6df3c38 0d6ddfa 6df3c38 0d6ddfa 6df3c38 0d6ddfa 6df3c38 0d6ddfa 6df3c38 0d6ddfa 6df3c38 0d6ddfa 6df3c38 0d6ddfa 6df3c38 0d6ddfa 6df3c38 0d6ddfa 6df3c38 0d6ddfa 6df3c38 0d6ddfa 6df3c38 0d6ddfa |
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 347 348 349 |
from flask import Flask, request, render_template, jsonify, send_file, redirect, url_for, flash, send_from_directory, session
from PIL import Image, ImageDraw
import torch
from transformers import LayoutLMv2ForTokenClassification, LayoutLMv3Tokenizer
import csv
import json
import subprocess
import os
import torch
import warnings
from PIL import Image
import sys
from fastai import *
from fastai.vision import *
from fastai.metrics import error_rate
from werkzeug.utils import secure_filename
import pandas as pd
from itertools import zip_longest
import inspect
from threading import Lock
import signal
import shutil
from datetime import datetime
import zipfile
# LLM
import argparse
from asyncio.log import logger
from Layoutlmv3_inference.ocr import prepare_batch_for_inference
from Layoutlmv3_inference.inference_handler import handle
import logging
import os
import warnings
# Ignore SourceChangeWarning
warnings.filterwarnings("ignore", category=DeprecationWarning)
# warnings.filterwarnings("ignore", category=SourceChangeWarning)
UPLOAD_FOLDER = 'static/temp/uploads'
if not os.path.exists(UPLOAD_FOLDER):
os.makedirs(UPLOAD_FOLDER)
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg'}
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['SECRET_KEY'] = 'supersecretkey'
# Added "temp" files cleaning for privacy and file managements.
# All temporary files were moved to "output_folders" for review and recovery.
# Moving of temp files were called at home page to ensure that new data were being supplied for extractor.
@app.route('/', methods=['GET', 'POST'])
def index():
try:
# Current date and time
now = datetime.now()
dt_string = now.strftime("%Y%m%d_%H%M%S")
# Source folders
temp_folder = r'static/temp'
inferenced_folder = r'inferenced'
# Destination folder path
destination_folder = os.path.join('output_folders', dt_string) # Create a new folder with timestamp
# Move the temp and inferenced folders to the destination folder
shutil.move(temp_folder, destination_folder)
shutil.move(inferenced_folder, destination_folder)
return render_template('index.html', destination_folder=destination_folder)
except:
return render_template('index.html')
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/upload', methods=['GET', 'POST'])
def upload_files():
UPLOAD_FOLDER = 'static/temp/uploads'
if not os.path.exists(UPLOAD_FOLDER):
os.makedirs(UPLOAD_FOLDER)
if request.method == 'POST':
if 'files[]' not in request.files:
resp = jsonify({'message' : 'No file part in the request'})
resp.status_code = 400
return resp
files = request.files.getlist('files[]')
filenames = []
for file in files:
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
filenames.append(filename)
return redirect(url_for('predict_files', filenames=filenames))
return render_template('index.html')
def make_predictions(image_paths):
temp = None
try:
# For Windows OS
temp = pathlib.PosixPath # Save the original state
pathlib.PosixPath = pathlib.WindowsPath # Change to WindowsPath temporarily
model_path = Path(r'model/export')
learner = load_learner(model_path)
predictions = []
for image_path in image_paths:
# Open the image using fastai's open_image function
image = open_image(image_path)
# Make a prediction
prediction_class, prediction_idx, probabilities = learner.predict(image)
# If you want the predicted class as a string
predicted_class_str = str(prediction_class)
predictions.append(predicted_class_str)
return predictions
except Exception as e:
return {"error in make_predictions": str(e)}
finally:
pathlib.PosixPath = temp
import copy
@app.route('/predict/<filenames>', methods=['GET', 'POST'])
def predict_files(filenames):
prediction_results = []
image_paths = eval(filenames) # Convert the filenames string back to a list
for filename in image_paths:
file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
folder_path = UPLOAD_FOLDER
destination_folder = r'static/temp/img_display'
if not os.path.exists(destination_folder):
os.makedirs(destination_folder)
# Get a list of all files in the source folder
files = os.listdir(folder_path)
# Loop through each file and copy it to the destination folder
for file in files:
# Construct the full path of the source file
source_file_path = os.path.join(folder_path, file)
# Construct the full path of the destination file
destination_file_path = os.path.join(destination_folder, file)
# Copy the file to the destination folder
shutil.copy(source_file_path, destination_file_path)
if os.path.exists(file_path):
# Call make_predictions automatically
prediction_result = make_predictions([file_path]) # Pass file_path as a list
prediction_results.extend(prediction_result)
# Create a copy of prediction_results before deletion
prediction_results_copy = copy.deepcopy(prediction_results)
if prediction_result[0] != 'non-receipt': # Check if prediction is not 'non-receipt'
prediction_results.extend(prediction_result) # Use extend to add elements of list to another list
else:
# Delete the image if it's predicted as non-receipt
os.remove(file_path)
print(image_paths)
return render_template('extractor.html', image_paths=image_paths, prediction_results = prediction_results, predictions=dict(zip(image_paths, prediction_results_copy)))
@app.route('/get_inference_image')
def get_inference_image():
# Assuming the new image is stored in the 'inferenced' folder with the name 'temp_inference.jpg'
inferenced_image = 'inferenced/temp_inference.jpg'
return jsonify(updatedImagePath=inferenced_image), 200 # Return the image path with a 200 status code
def process_images(model_path: str, images_path: str) -> None:
try:
image_files = os.listdir(images_path)
images_path = [os.path.join(images_path, image_file) for image_file in image_files]
inference_batch = prepare_batch_for_inference(images_path)
context = {"model_dir": model_path}
handle(inference_batch, context)
except Exception as err:
os.makedirs('log', exist_ok=True)
logging.basicConfig(filename='log/error_output.log', level=logging.ERROR,
format='%(asctime)s %(levelname)s %(name)s %(message)s')
logger = logging.getLogger(__name__)
logger.error(err)
@app.route('/run_inference', methods=['GET'])
def run_inference():
try:
model_path = r"model"
images_path = r"static/temp/uploads/"
process_images(model_path, images_path)
return redirect(url_for('create_csv'))
except Exception as err:
return f"Error processing images: {str(err)}", 500
@app.route('/stop_inference', methods=['GET'])
def stop_inference():
try:
# Get the process ID of the run_inference process
run_inference_pid = os.getpid() # Assuming it's running in the same process
# Send the SIGTERM signal to gracefully terminate the process
os.kill(run_inference_pid, signal.SIGTERM)
return render_template('index.html')
except ProcessLookupError:
logging.warning("run_inference process not found.")
except Exception as err:
logging.error(f"Error terminating run_inference process: {err}")
# Define a function to replace all symbols with periods
def replace_symbols_with_period(value):
# return re.sub(r'\W+', '.', str(value))
return value.replace(',', '.')
from itertools import zip_longest
@app.route('/create_csv', methods=['GET'])
def create_csv():
try:
# Path to the folder containing JSON files
json_folder_path = r"static/temp/labeled" # Change this to your folder path
# Path to the output CSV folder
output_folder_path = r"inferenced/csv_files"
os.makedirs(output_folder_path, exist_ok=True)
# Initialize an empty list to store all JSON data
all_data = []
# Iterate through JSON files in the folder
for filename in os.listdir(json_folder_path):
if filename.endswith(".json"):
json_file_path = os.path.join(json_folder_path, filename)
with open(json_file_path, 'r') as file:
data = json.load(file)
all_data.extend(data['output'])
# Creating a dictionary to store labels and corresponding texts for this JSON file
label_texts = {}
for item in data['output']:
label = item['label']
text = item['text']
# Ensure label exists before adding to dictionary
if label not in label_texts:
label_texts[label] = []
label_texts[label].append(text)
# Order of columns as requested
column_order = [
'RECEIPTNUMBER', 'MERCHANTNAME', 'MERCHANTADDRESS',
'TRANSACTIONDATE', 'TRANSACTIONTIME', 'ITEMS',
'PRICE', 'TOTAL', 'VATTAX'
]
# Writing data to CSV file with ordered columns
csv_file_path = os.path.join(output_folder_path, os.path.splitext(filename)[0] + '.csv')
with open(csv_file_path, 'w', newline='') as csvfile:
csv_writer = csv.DictWriter(csvfile, fieldnames=column_order, delimiter=",")
csv_writer.writeheader()
# Iterate through items and prices
max_length = max(len(label_texts.get('ITEMS', [])), len(label_texts.get('PRICE', [])))
for i in range(max_length):
# Use get() with default '' to avoid KeyError
items = label_texts.get('ITEMS', [])[i] if i < len(label_texts.get('ITEMS', [])) else ''
prices = label_texts.get('PRICE', [])[i] if i < len(label_texts.get('PRICE', [])) else ''
# Check if items and prices are separated by space
if ' ' in items or ' ' in prices:
item_list = items.split() if items else []
price_list = prices.split() if prices else []
# Create new rows for each combination of items and prices
for item, price in zip(item_list, price_list):
row_data = {label: replace_symbols_with_period(label_texts[label][i]) if label == 'ITEMS' else replace_symbols_with_period(label_texts[label][i]) for label in column_order}
row_data['ITEMS'] = item
row_data['PRICE'] = price
csv_writer.writerow(row_data)
else:
# Use get() with default '' to avoid KeyError
row_data = {label: replace_symbols_with_period(label_texts.get(label, [])[i]) if i < len(label_texts.get(label, [])) else '' for label in column_order}
csv_writer.writerow(row_data)
# Combining contents of CSV files into a single CSV file
output_file_path = r"inferenced/output.csv"
with open(output_file_path, 'w', newline='') as combined_csvfile:
combined_csv_writer = csv.DictWriter(combined_csvfile, fieldnames=column_order, delimiter=",")
combined_csv_writer.writeheader()
# Iterate through CSV files in the folder
for csv_filename in os.listdir(output_folder_path):
if csv_filename.endswith(".csv"):
csv_file_path = os.path.join(output_folder_path, csv_filename)
# Read data from CSV file and write to the combined CSV file
with open(csv_file_path, 'r') as csv_file:
csv_reader = csv.DictReader(csv_file)
for row in csv_reader:
combined_csv_writer.writerow(row)
return '', 204 # Return an empty response with a 204 status code
except Exception as e:
print(f"An error occurred in create_csv: {str(e)}")
return None
@app.route('/get_data')
def get_data():
return send_from_directory('inferenced','output.csv', as_attachment=False)
from flask import jsonify
@app.route('/download_csv', methods=['GET'])
def download_csv():
try:
output_file_path = r"inferenced/output.csv" # path to output CSV file
# Check if the file exists
if os.path.exists(output_file_path):
return send_file(output_file_path, as_attachment=True, download_name='output.csv')
else:
return jsonify({"error": "CSV file not found"})
except Exception as e:
return jsonify({"error": f"Download failed: {str(e)}"})
if __name__ == '__main__':
app.run(debug=True) |