Spaces:
Runtime error
Runtime error
Delete app.py
Browse files
app.py
DELETED
|
@@ -1,85 +0,0 @@
|
|
| 1 |
-
|
| 2 |
-
import gradio as gr
|
| 3 |
-
import easyocr
|
| 4 |
-
import cv2
|
| 5 |
-
import numpy as np
|
| 6 |
-
import re
|
| 7 |
-
import json
|
| 8 |
-
from PIL import Image
|
| 9 |
-
from io import BytesIO
|
| 10 |
-
|
| 11 |
-
VERSION = "v3.4 - Dynamic OCR Receipt Parser (with Resize Fix)"
|
| 12 |
-
|
| 13 |
-
reader = easyocr.Reader(['en'])
|
| 14 |
-
|
| 15 |
-
def resize_image(image, max_width=1024):
|
| 16 |
-
if image.width > max_width:
|
| 17 |
-
ratio = max_width / image.width
|
| 18 |
-
return image.resize((max_width, int(image.height * ratio)))
|
| 19 |
-
return image
|
| 20 |
-
|
| 21 |
-
def preprocess_image(image):
|
| 22 |
-
gray = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2GRAY)
|
| 23 |
-
blur = cv2.GaussianBlur(gray, (3, 3), 0)
|
| 24 |
-
_, thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
|
| 25 |
-
return Image.fromarray(thresh)
|
| 26 |
-
|
| 27 |
-
def extract_fields(results):
|
| 28 |
-
lines = [r[1] for r in results]
|
| 29 |
-
full_text = " ".join(lines)
|
| 30 |
-
|
| 31 |
-
fields = {}
|
| 32 |
-
patterns = {
|
| 33 |
-
"INVOICE_RECEIPT_ID": r"(Invoice\s*No\.?|INVOICE\s*NO[:\-]?)\s*[:#]?\s*([A-Z0-9\-]+)",
|
| 34 |
-
"INVOICE_RECEIPT_DATE": r"(Date[:\-]?)\s*([0-9]{1,2}/[0-9]{1,2}/[0-9]{2,4})",
|
| 35 |
-
"TOTAL": r"(TOTAL|Grand Total|Total Payable)[^\d]{0,10}([S$]?[\\$]?\d+[.,]?\d*)"
|
| 36 |
-
}
|
| 37 |
-
|
| 38 |
-
for key, pattern in patterns.items():
|
| 39 |
-
match = re.search(pattern, full_text, re.IGNORECASE)
|
| 40 |
-
if match:
|
| 41 |
-
fields[key] = match.groups()[-1]
|
| 42 |
-
|
| 43 |
-
for line in lines[:5]:
|
| 44 |
-
if "pte ltd" in line.lower() or "traders" in line.lower():
|
| 45 |
-
fields["VENDOR_NAME"] = line.strip()
|
| 46 |
-
break
|
| 47 |
-
|
| 48 |
-
return fields, lines
|
| 49 |
-
|
| 50 |
-
def draw_boxes(image, results):
|
| 51 |
-
img = np.array(image).copy()
|
| 52 |
-
for (bbox, text, _) in results:
|
| 53 |
-
top_left = tuple(map(int, bbox[0]))
|
| 54 |
-
bottom_right = tuple(map(int, bbox[2]))
|
| 55 |
-
cv2.rectangle(img, top_left, bottom_right, (0, 255, 0), 2)
|
| 56 |
-
return Image.fromarray(img)
|
| 57 |
-
|
| 58 |
-
def analyze_receipt(uploaded_img):
|
| 59 |
-
image = Image.open(uploaded_img).convert("RGB")
|
| 60 |
-
image = resize_image(image) # ✅ Resize to 1024px if needed
|
| 61 |
-
preprocessed = preprocess_image(image)
|
| 62 |
-
results = reader.readtext(np.array(preprocessed))
|
| 63 |
-
fields, lines = extract_fields(results)
|
| 64 |
-
image_with_boxes = draw_boxes(image, results)
|
| 65 |
-
|
| 66 |
-
output_json = {
|
| 67 |
-
"version": VERSION,
|
| 68 |
-
"RECEIPT/INVOICE_SUMMARY": fields,
|
| 69 |
-
"raw_lines": lines
|
| 70 |
-
}
|
| 71 |
-
return image_with_boxes, json.dumps(output_json, indent=2)
|
| 72 |
-
|
| 73 |
-
demo = gr.Interface(
|
| 74 |
-
fn=analyze_receipt,
|
| 75 |
-
inputs=gr.Image(type="filepath", label="Upload Receipt Image"),
|
| 76 |
-
outputs=[
|
| 77 |
-
gr.Image(label="Highlighted Image"),
|
| 78 |
-
gr.Code(label="Extracted JSON", language="json")
|
| 79 |
-
],
|
| 80 |
-
title="OCR Receipt Parser (v3.4)",
|
| 81 |
-
description="Upload any receipt or invoice with printed or handwritten data. Automatically extracts structured data from the image."
|
| 82 |
-
)
|
| 83 |
-
|
| 84 |
-
if __name__ == "__main__":
|
| 85 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|