KeyarSrini commited on
Commit
18f69d7
·
verified ·
1 Parent(s): ed67ae0

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -111
app.py DELETED
@@ -1,111 +0,0 @@
1
- # app.py
2
- import gradio as gr
3
- import cv2
4
- import easyocr
5
- import numpy as np
6
- import pytesseract
7
- import json
8
- import re
9
- from PIL import Image, ImageDraw
10
- from typing import List
11
-
12
- reader = easyocr.Reader(['en'], gpu=False)
13
-
14
-
15
- def extract_text_lines(image: Image.Image) -> List[str]:
16
- gray = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2GRAY)
17
- gray = cv2.bilateralFilter(gray, 11, 17, 17)
18
- lines = pytesseract.image_to_string(gray).splitlines()
19
- return [line.strip() for line in lines if line.strip()]
20
-
21
-
22
- def extract_receipt_data(lines: List[str]) -> dict:
23
- data = {
24
- "version": "v3.7 - Universal OCR Receipt Parser",
25
- "RECEIPT/INVOICE_SUMMARY": {},
26
- "LINE_ITEMS": [],
27
- "HIGHLIGHTS": [],
28
- "raw_lines": lines
29
- }
30
-
31
- for line in lines:
32
- lower = line.lower()
33
-
34
- if "name" in lower:
35
- value = line.split("name")[-1].strip()
36
- data["RECEIPT/INVOICE_SUMMARY"]["NAME"] = value
37
- data["HIGHLIGHTS"].append({"type": "text", "value": "NAME", "line": line})
38
- data["HIGHLIGHTS"].append({"type": "value", "value": value, "line": line})
39
-
40
- elif "address" in lower:
41
- value = line.split("address")[-1].strip()
42
- data["RECEIPT/INVOICE_SUMMARY"]["ADDRESS"] = value
43
- data["HIGHLIGHTS"].append({"type": "text", "value": "ADDRESS", "line": line})
44
- data["HIGHLIGHTS"].append({"type": "value", "value": value, "line": line})
45
-
46
- elif "phone" in lower:
47
- match = re.search(r'(\+\d{1,3}\s)?\(?\d{2,4}\)?[\s.-]\d{3,4}[\s.-]\d{4}', line)
48
- if match:
49
- phone = match.group()
50
- data["RECEIPT/INVOICE_SUMMARY"]["PHONE"] = phone
51
- data["HIGHLIGHTS"].append({"type": "text", "value": "PHONE", "line": line})
52
- data["HIGHLIGHTS"].append({"type": "value", "value": phone, "line": line})
53
-
54
- elif "date" in lower or re.search(r'\d{1,2}[/-]\d{1,2}[/-]\d{2,4}', line):
55
- date_match = re.search(r'\d{1,2}[/-]\d{1,2}[/-]\d{2,4}', line)
56
- if date_match:
57
- date = date_match.group()
58
- data["RECEIPT/INVOICE_SUMMARY"]["DATE"] = date
59
- data["HIGHLIGHTS"].append({"type": "text", "value": "DATE", "line": line})
60
- data["HIGHLIGHTS"].append({"type": "value", "value": date, "line": line})
61
-
62
- elif "total" in lower:
63
- match = re.search(r'(\d+[.,]?\d*)$', line)
64
- if match:
65
- amount = match.group().replace(',', '')
66
- data["RECEIPT/INVOICE_SUMMARY"]["TOTAL"] = amount
67
- data["HIGHLIGHTS"].append({"type": "text", "value": "TOTAL", "line": line})
68
- data["HIGHLIGHTS"].append({"type": "value", "value": amount, "line": line})
69
-
70
- elif re.search(r'(\d+[.,]\d{2})$', line):
71
- parts = line.rsplit(" ", 1)
72
- if len(parts) == 2:
73
- item, price = parts
74
- data["LINE_ITEMS"].append({"Item": item.strip(), "Price": price.strip()})
75
- data["HIGHLIGHTS"].append({"type": "text", "value": item.strip(), "line": line})
76
- data["HIGHLIGHTS"].append({"type": "value", "value": price.strip(), "line": line})
77
-
78
- return data
79
-
80
-
81
- def highlight_image(image: Image.Image, data: dict) -> Image.Image:
82
- draw = ImageDraw.Draw(image)
83
- ocr_boxes = pytesseract.image_to_data(image, output_type=pytesseract.Output.DICT)
84
- for i in range(len(ocr_boxes['text'])):
85
- text = ocr_boxes['text'][i].strip()
86
- if not text:
87
- continue
88
- x, y, w, h = ocr_boxes['left'][i], ocr_boxes['top'][i], ocr_boxes['width'][i], ocr_boxes['height'][i]
89
- for item in data["HIGHLIGHTS"]:
90
- if item['value'] in text:
91
- color = 'green' if item['type'] == 'text' else 'blue'
92
- draw.rectangle([x, y, x + w, y + h], outline=color, width=2)
93
- return image
94
-
95
-
96
- def analyze_receipt(image):
97
- lines = extract_text_lines(image)
98
- parsed_data = extract_receipt_data(lines)
99
- highlighted_img = highlight_image(image.copy(), parsed_data)
100
- return highlighted_img, parsed_data
101
-
102
-
103
- demo = gr.Interface(
104
- fn=analyze_receipt,
105
- inputs=gr.Image(type="pil"),
106
- outputs=[gr.Image(label="Highlighted Receipt"), gr.JSON(label="Extracted JSON")],
107
- title="v3.7 Dynamic OCR Receipt Parser",
108
- description="Upload any printed or handwritten receipt and get structured data with highlighted labels and values."
109
- )
110
-
111
- demo.launch()