Sharad9084 commited on
Commit
79a28ae
Β·
verified Β·
1 Parent(s): bb60cf3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +78 -50
app.py CHANGED
@@ -1,50 +1,78 @@
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.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
8
+ st.set_page_config(page_title="Smart Multi-Document Data Extractor", layout="wide")
9
+
10
+ st.title("πŸ“„ Smart Multi-Document Data Extractor")
11
+
12
+ # OCR Function (Improved)
13
+ def extract_text(img):
14
+ gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
15
+ gray = cv2.threshold(gray, 150, 255, cv2.THRESH_BINARY)[1]
16
+ return pytesseract.image_to_string(gray)
17
+
18
+ # File uploader (multiple files supported)
19
+ uploaded_files = st.file_uploader(
20
+ "Upload Images",
21
+ type=["jpg", "png", "jpeg"],
22
+ accept_multiple_files=True
23
+ )
24
+
25
+ if uploaded_files:
26
+
27
+ all_data = []
28
+
29
+ for uploaded_file in uploaded_files:
30
+ try:
31
+ file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
32
+ img = cv2.imdecode(file_bytes, 1)
33
+
34
+ # Check image valid
35
+ if img is None:
36
+ st.error(f"❌ Could not read image: {uploaded_file.name}")
37
+ continue
38
+
39
+ # Show image
40
+ st.image(img, caption=uploaded_file.name, use_container_width=True)
41
+
42
+ # Extract text
43
+ text = extract_text(img)
44
+
45
+ if not text.strip():
46
+ st.warning(f"⚠️ No text found in {uploaded_file.name}")
47
+ continue
48
+
49
+ # Extract structured data
50
+ records = extract_multi_records(text)
51
+
52
+ if not records:
53
+ st.warning(f"⚠️ No structured data found in {uploaded_file.name}")
54
+ continue
55
+
56
+ all_data.extend(records)
57
+
58
+ except Exception as e:
59
+ st.error(f"❌ Error processing {uploaded_file.name}: {str(e)}")
60
+
61
+ # Show final data
62
+ if all_data:
63
+ df = pd.DataFrame(all_data)
64
+
65
+ st.success("βœ… Extraction Complete!")
66
+ st.dataframe(df)
67
+
68
+ # Download Excel
69
+ df.to_excel("output.xlsx", index=False)
70
+
71
+ with open("output.xlsx", "rb") as f:
72
+ st.download_button(
73
+ "πŸ“₯ Download Excel",
74
+ f,
75
+ file_name="output.xlsx"
76
+ )
77
+ else:
78
+ st.warning("⚠️ No data extracted from uploaded files.")