Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import pytesseract | |
| from PIL import Image | |
| from docx import Document | |
| from docx.shared import Inches | |
| import io | |
| import os | |
| # Function to process images and generate a report | |
| def generate_report(images): | |
| if not images: | |
| return "No images uploaded.", None | |
| document = Document() | |
| for idx, img_path in enumerate(images): | |
| try: | |
| # Load image | |
| image = Image.open(img_path) | |
| # OCR text extraction | |
| text = pytesseract.image_to_string(image) | |
| # Add page header | |
| document.add_heading(f"Mapping Tag No. {idx + 1}", level=1) | |
| document.add_picture(img_path, width=Inches(5.5)) | |
| document.add_paragraph(f"Extracted text:\n{text.strip()}") | |
| # Add table similar to your format | |
| table = document.add_table(rows=3, cols=3) | |
| table.style = "Table Grid" | |
| hdr_cells = table.rows[0].cells | |
| hdr_cells[0].text = "Location" | |
| hdr_cells[1].text = "-" | |
| hdr_cells[2].text = f"Mapping Tag No. {idx + 1}" | |
| row_cells = table.rows[1].cells | |
| row_cells[0].text = "Description" | |
| row_cells[1].merge(row_cells[2]) | |
| row_cells[1].text = text.strip() if text.strip() else "Text not detected" | |
| rem_cells = table.rows[2].cells | |
| rem_cells[0].text = "Remarks" | |
| rem_cells[1].merge(rem_cells[2]) | |
| rem_cells[1].text = "-" | |
| document.add_page_break() | |
| except Exception as e: | |
| print("Error processing image:", e) | |
| continue | |
| # Save document | |
| output_path = "report.docx" | |
| document.save(output_path) | |
| # Return text preview + file download | |
| return "Report generated successfully!", output_path | |
| # Define Gradio UI | |
| iface = gr.Interface( | |
| fn=generate_report, | |
| inputs=gr.Files(label="Upload crack images", file_count="multiple", type="filepath"), | |
| outputs=[ | |
| gr.Textbox(label="Status / Preview"), | |
| gr.File(label="Download Word Report") | |
| ], | |
| title="Auto Crack Report Generator", | |
| description="Upload crack ruler images to automatically generate a formatted Word report with OCR data." | |
| ) | |
| if __name__ == "__main__": | |
| iface.launch() | |