import sys
import cv2
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QVBoxLayout, QPushButton, QTableWidget, QTableWidgetItem, QWidget
from PyQt5.QtGui import QImage, QPixmap
from PyQt5.QtCore import QTimer
import pytesseract
import openpyxl
import re

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("Envelope Text Extraction")

        # Create a label to display the webcam feed
        self.video_label = QLabel(self)
        self.video_label.setFixedSize(640, 480)

        # Create a button for capturing and processing the text
        self.process_button = QPushButton("Capture and Process", self)
        self.process_button.clicked.connect(self.capture_and_process_text)

        # Create a table widget to display the extracted text
        self.table_widget = QTableWidget(self)
        self.table_widget.setColumnCount(6)
        self.table_widget.setHorizontalHeaderLabels(["FIRST", "LAST", "ADDRESS", "CITY", "STATE", "ZIP"])

        # Create a layout for the GUI
        layout = QVBoxLayout()
        layout.addWidget(self.video_label)
        layout.addWidget(self.process_button)
        layout.addWidget(self.table_widget)

        # Create a central widget and set the layout
        central_widget = QWidget()
        central_widget.setLayout(layout)
        self.setCentralWidget(central_widget)

        # Initialize the webcam capture
        self.video_capture = cv2.VideoCapture(0)
        self.update_frame()

    def update_fra