File size: 2,208 Bytes
79a28ae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import cv2
import pytesseract
import numpy as np
import pandas as pd
from extractor import extract_multi_records

st.set_page_config(page_title="Smart Multi-Document Data Extractor", layout="wide")

st.title("📄 Smart Multi-Document Data Extractor")

# OCR Function (Improved)
def extract_text(img):
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    gray = cv2.threshold(gray, 150, 255, cv2.THRESH_BINARY)[1]
    return pytesseract.image_to_string(gray)

# File uploader (multiple files supported)
uploaded_files = st.file_uploader(
    "Upload Images",
    type=["jpg", "png", "jpeg"],
    accept_multiple_files=True
)

if uploaded_files:

    all_data = []

    for uploaded_file in uploaded_files:
        try:
            file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
            img = cv2.imdecode(file_bytes, 1)

            # Check image valid
            if img is None:
                st.error(f"❌ Could not read image: {uploaded_file.name}")
                continue

            # Show image
            st.image(img, caption=uploaded_file.name, use_container_width=True)

            # Extract text
            text = extract_text(img)

            if not text.strip():
                st.warning(f"⚠️ No text found in {uploaded_file.name}")
                continue

            # Extract structured data
            records = extract_multi_records(text)

            if not records:
                st.warning(f"⚠️ No structured data found in {uploaded_file.name}")
                continue

            all_data.extend(records)

        except Exception as e:
            st.error(f"❌ Error processing {uploaded_file.name}: {str(e)}")

    # Show final data
    if all_data:
        df = pd.DataFrame(all_data)

        st.success("✅ Extraction Complete!")
        st.dataframe(df)

        # Download Excel
        df.to_excel("output.xlsx", index=False)

        with open("output.xlsx", "rb") as f:
            st.download_button(
                "📥 Download Excel",
                f,
                file_name="output.xlsx"
            )
    else:
        st.warning("⚠️ No data extracted from uploaded files.")