Sharad9084 commited on
Commit
026cb4d
Β·
verified Β·
1 Parent(s): 79c7797

Upload 4 files

Browse files
Files changed (4) hide show
  1. app.py +50 -0
  2. extractor.py +41 -0
  3. packages.txt +1 -0
  4. requirements.txt +7 -0
app.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import cv2
3
+ import pytesseract
4
+ import numpy as np
5
+ import pandas as pd
6
+ from extractor import extract_multi_records
7
+ import pytesseract
8
+ # βœ… Tesseract path (IMPORTANT)
9
+ # pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"
10
+
11
+ st.set_page_config(page_title="Document Extractor", layout="wide")
12
+
13
+ st.title("πŸ“„ Smart Multi-Document Data Extractor")
14
+
15
+ uploaded_file = st.file_uploader("Upload Image", type=["jpg", "png", "jpeg"])
16
+
17
+ # OCR Function
18
+ def extract_text(img):
19
+ gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
20
+ return pytesseract.image_to_string(gray)
21
+
22
+ if uploaded_file:
23
+ file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
24
+ img = cv2.imdecode(file_bytes, 1)
25
+
26
+ st.image(img, caption="Uploaded Image", use_container_width=True)
27
+
28
+ if st.button("πŸš€ Extract Data"):
29
+
30
+ with st.spinner("Processing..."):
31
+
32
+ text = extract_text(img)
33
+
34
+ # πŸ”₯ Multi-record extraction
35
+ records = extract_multi_records(text)
36
+
37
+ df = pd.DataFrame(records)
38
+
39
+ st.success("βœ… Extraction Complete!")
40
+
41
+ if not df.empty:
42
+ st.dataframe(df)
43
+
44
+ # Save Excel
45
+ df.to_excel("output.xlsx", index=False)
46
+
47
+ with open("output.xlsx", "rb") as f:
48
+ st.download_button("πŸ“₯ Download Excel", f, file_name="output.xlsx")
49
+ else:
50
+ st.warning("⚠️ No structured data found. Try a clearer image.")
extractor.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import re
2
+
3
+ def extract_multi_records(text):
4
+ lines = text.split("\n")
5
+ data = []
6
+
7
+ for line in lines:
8
+ line = line.strip()
9
+
10
+ # Skip empty lines
11
+ if not line:
12
+ continue
13
+
14
+ words = line.split()
15
+
16
+ # Skip header row
17
+ if any(word.lower() in ["name", "date", "amount"] for word in words):
18
+ continue
19
+
20
+ # Find date
21
+ date = re.findall(r'\d{2}[-/]\d{2}[-/]\d{4}', line)
22
+
23
+ # Find amount (last number in line)
24
+ amount = re.findall(r'\d+', line)
25
+
26
+ # Extract name (first word only if valid)
27
+ name = words[0] if words else "Unknown"
28
+
29
+ # Validate name (should not be numeric or date)
30
+ if name.isdigit():
31
+ continue
32
+
33
+ # Save record if valid
34
+ if date and amount:
35
+ data.append({
36
+ "Name": name,
37
+ "Date": date[0],
38
+ "Amount": amount[-1]
39
+ })
40
+
41
+ return data
packages.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ tesseract-ocr
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ streamlit
2
+ pytesseract
3
+ opencv-python-headless
4
+ numpy
5
+ pandas
6
+ pillow
7
+ openpyxl