import gradio as gr from utils.cad_operations import create_cad_model from utils.toolpath_generation import generate_toolpath, generate_gcode import cv2 import pytesseract import pandas as pd from PIL import Image import re # Path to your Tesseract executable pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe' # Function to preprocess the image (grayscale, thresholding, etc.) def preprocess_image(image_path): # Read the image using OpenCV img = cv2.imread(image_path) # Convert to grayscale gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # Apply thresholding _, thresh = cv2.threshold(gray, 150, 255, cv2.THRESH_BINARY) # Optional: Remove noise (you can experiment with different kernels) kern = cv2.getStructuringElement(cv2.MORPH_RECT, (5,5)) processed_image = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kern) return processed_image # Function to extract text from image using OCR def extract_text_from_image(image): # Use pytesseract to do OCR on the preprocessed image text = pytesseract.image_to_string(image) return text # Function to extract invoice details using regular expressions def extract_invoice_details(text): # Example patterns (you can modify them based on the format of your invoices) invoice_number = re.search(r'Invoice Number[:\s]*([A-Za-z0-9]+)', text) customer_name = re.search(r'Customer Name[:\s]*([\w\s]+)', text) date = re.search(r'Date[:\s]*([\d/]+)', text) total_amount = re.search(r'Total Amount[:\s]*([\d,]+)', text) # Extract matched values or set to 'N/A' if not found invoice_details = { 'Invoice Number': invoice_number.group(1) if invoice_number else 'N/A', 'Customer Name': customer_name.group(1) if customer_name else 'N/A', 'Date': date.group(1) if date else 'N/A', 'Total Amount': total_amount.group(1) if total_amount else 'N/A', } return invoice_details # Function to save extracted data to Excel def save_to_excel(data, output_file): # Convert the data to a pandas DataFrame and save it to an Excel file df = pd.DataFrame(data) df.to_excel(output_file, index=False) def main(): # Path to the invoice image image_path = 'path_to_your_invoice_image.jpg' # Preprocess the image preprocessed_image = preprocess_image(image_path) # Extract text from the image using OCR extracted_text = extract_text_from_image(preprocessed_image) # Extract invoice details using regex invoice_details = extract_invoice_details(extracted_text) # Save extracted data to an Excel file output_file = 'extracted_invoice_data.xlsx' save_to_excel([invoice_details], output_file) print(f"Data extracted and saved to {output_file}") # Run the script if __name__ == "__main__": main() # Step 1: Collect user input parameters def cnc_workflow(length, width, height, material, tool_size, operation_type): """ Full CNC workflow from parameter input to G-code generation """ # Step 2: Create CAD model part = create_cad_model(length, width, height, material) # Step 3: Generate Toolpath toolpath = generate_toolpath(part, tool_size, operation_type) # Step 4: Generate G-code gcode_file = generate_gcode(toolpath) return f"G-code file generated: {gcode_file}" # Define the Gradio interface and layout def build_gui(): with gr.Blocks() as demo: # Title and Intro gr.Markdown("### CNC G-Code Generation Workflow") # Step 1: Input Parameters with gr.Row(): length_input = gr.Number(label="Length (mm)", value=100) width_input = gr.Number(label="Width (mm)", value=50) height_input = gr.Number(label="Height (mm)", value=20) material_input = gr.Dropdown(choices=["Aluminum", "Steel", "Plastic"], label="Material Type", value="Aluminum") # Step 2: Tool and Operation Input with gr.Row(): tool_size_input = gr.Number(label="Tool Size (mm)", value=5) operation_input = gr.Dropdown(choices=["Milling", "Drilling"], label="Operation Type", value="Milling") # Step 3: Button to Start G-Code Generation generate_button = gr.Button("Generate G-Code") # Step 4: Output section output_text = gr.Textbox(label="Generated G-code File", interactive=False) # Bind the button to the function generate_button.click(cnc_workflow, inputs=[length_input, width_input, height_input, material_input, tool_size_input, operation_input], outputs=output_text) return demo # Launch the Gradio app demo = build_gui() demo.launch()