OCR Reader
Prompt
You are an expert automation/full-stack engineer. Build a fully-functional OCR reader for semiconductor equipment diagrams (electrical schematics, equipment tool manuals, PDFs, scanned PNGs). The entire workflow must be launchable from Windows PowerShell. Do not ask clarifying questions. Make reasonable assumptions and deliver a complete, runnable project. IMPORTANT CONSTRAINT: NO FILE UPLOADS - The AI model tester cannot receive or process attached PDF/PNG files. - Do not request or expect file uploads. - Use the textual descriptions in this prompt to generate synthetic mock diagrams for testing. - If the user later places real files in `.\samples\`, the system must accept them via `-InputPath`. - All acceptance tests must pass using generated mock data. Real files are optional. DELIVERABLE Create a project folder named `DiagramReader` with: - `Start-DiagramReader.ps1` β PowerShell entry point. - `backend\` β Python FastAPI/Flask backend, OCR, CV, graph, DB. - `frontend\` β HTML/JS/CSS with OpenSeadragon. - `samples\` β generated mock PDFs/PNGs. - `output\` β runtime output (DZI tiles, SQLite DB, JSON graph, logs). - `requirements.txt`, `README.md`. POWERSHELL LAUNCHER: `Start-DiagramReader.ps1` Parameters: - `-InputPath <string>`: PDF, PNG, or folder. If omitted, use `.\samples\`. - `-Port <int>`: default 8765. - `-OutputDir <string>`: default `.\output\<timestamp>\`. - `-GenerateMockSamples`: if set, generate synthetic test PDFs/PNGs into `.\samples\`. - `-NoBrowser`: do not open browser. - `-ForceReinstall`: recreate venv and reinstall dependencies. Behavior: - Check for Python 3.11+, Tesseract, libvips. If missing, attempt install via `winget`, `choco`, or portable downloads. If impossible, print exact install commands and exit. - Create/activate venv, install `requirements.txt`. - Start backend with Uvicorn on `127.0.0.1:$Port`. - Log all steps to console and `$OutputDir\launcher.log`. - Open default browser to `http://127.0.0.1:$Port` unless `-NoBrowser`. - Handle Ctrl+C to stop server cleanly. INPUT HANDLING - PDF: use PyMuPDF (`fitz`) to render each page at 300β600 DPI. Preserve page order, sheet numbers, and page labels. - PNG: use OpenCV/Pillow to preprocess: - Deskew (Hough transform or minAreaRect). - Denoise (fastNlMeansDenoising). - Adaptive threshold. - Upscale small text (2xβ4x with cubic interpolation). - Perspective correction if camera scan. - Combine all pages into one large canvas: - Arrange pages in a grid based on page number / sheet index. - Add page boundary labels (e.g., "Page 1", "Sheet 8"). - Generate Deep Zoom tiles (DZI) using pyvips/libvips. If unavailable, use OpenCV/PIL to tile. - Store original page images for OCR coordinate mapping. OCR REQUIREMENTS - Use Tesseract OCR (or PaddleOCR if available) with: - `--psm 6` or `--psm 11`. - Preserve bounding boxes, confidence, block/line/word hierarchy. - OSD for rotated text. - Store in SQLite: - Table `words(page, x, y, w, h, text, conf, block, line, word_index)`. - FTS5 virtual table `words_fts` for instant search. - Preprocess for OCR: grayscale, adaptive threshold, deskew, upscale small text. - For camera PNGs: correct perspective if possible; if not, flag low-confidence regions. LINE/WIRE AND SYMBOL DETECTION - Detect electrical lines/wires with OpenCV: - LSD, HoughLinesP, morphology. - Detect junctions, terminals, crossovers. - Detect common symbols via template matching or contour heuristics: - Fuse, breaker, relay, motor, transformer, ground, resistor, capacitor, MCC bucket, bus, terminal block. - Associate OCR text to nearest component/line/junction. - If symbol recognition is uncertain, create a generic node with nearby text. - Store detected geometry in SQLite/JSON. GRAPH AND SIGNAL CHAIN - Build a graph with NetworkX: - Nodes: components, labels, junctions, page anchors, text references. - Edges: detected wires, explicit text references, matching device IDs, matching wire numbers. - Cross-page linking patterns: - `TO SHEET (\d+)` - `SEE DWG ([\w-]+)` - `TO ([\w\s]+ BUS)` - `UNIT (\d+)` - `BUS (\w+)` - `MCC (\d+)` - Device IDs like `52-EC01`, `52-EC02`, `87T2-1`, `27-1`. - Wire numbers. - On click of any node: - Run BFS/DFS to find connected chain. - Prioritize electrical continuity (line tracing) and named references. - Highlight all connected nodes/edges across all pages. - Show chain in side panel: node type, label, part number, page, wire numbers, confidence. - Persist graph as `$OutputDir\graph.json`. - Allow manual correction: merge/split nodes, edit labels, save to DB. WEB UI - Use OpenSeadragon for deep zoom/pan over combined canvas. - Features: - Search bar: searches FTS5 index. Results show page, snippet, confidence. Click zooms/pans to bounding box and highlights. - Click any component/text/line: highlight entire inferred signal chain. Side panel shows chain details. - Layer toggles: OCR text boxes, detected wires, highlighted chain, page boundaries, symbols. - Cross-page jump: clicking a reference like "TO SHEET 8" jumps to that page/component if found. - Export: searchable PDF with OCR text layer, JSON graph, CSV of components, highlighted chain screenshot. - Manual correction mode: edit labels, merge nodes, save. - Serve static files from FastAPI. Bind to `127.0.0.1` only. - Use CDN or bundled OpenSeadragon. MOCK DATA GENERATION (NO UPLOADS) Because no files can be uploaded, the agent must generate synthetic test data: - `mock_pdf_1.pdf`: 3 pages. - Page 1: title block, fuse F1, breaker 52-EC01, label "480V MCC BUS 132", line to terminal, text "TO SHEET 2". - Page 2: breaker 52-EC01, motor M1, label "UNIT 2", text "SEE DWG C-17797". - Page 3: legend, symbols, wire numbers. - `mock_png_1.png`: camera-style scan of a 480V MCC one-line. Add rotation, noise, uneven lighting. - Generate using Python: `reportlab` for PDF, `PIL` for PNG. - Place in `.\samples\`. - Acceptance tests must use these mocks. If user places real files in `.\samples\`, system must process them instead. ACCEPTANCE TESTS Run `.\Start-DiagramReader.ps1 -GenerateMockSamples`. Verify: 1. Canvas shows all 3 PDF pages + PNG combined with zoom/pan. 2. Search finds "52-EC01", "480V MCC BUS 132", "UNIT 2". 3. Clicking fuse F1 highlights chain: F1 -> 52-EC01 -> M1 across pages 1 and 2. 4. Clicking "TO SHEET 2" jumps to page 2. 5. Export searchable PDF and JSON graph. 6. No cloud API. Offline after dependencies. 7. Logs show OCR confidence, line detection counts, graph edges. TECH STACK - Python 3.11+, FastAPI, Uvicorn, PyMuPDF, OpenCV, Pillow, NumPy, SQLite FTS5, NetworkX, pyvips/libvips, Tesseract, reportlab, PaddleOCR optional. - PowerShell orchestrator. - OpenSeadragon frontend. - No cloud services. DOCUMENTATION - `README.md` with install commands, usage, parameters, troubleshooting, dependency list. - Inline comments and type hints. - Logging to file and console. - Error handling for missing dependencies, corrupt files, low OCR confidence. QUALITY - Clean, modular code. - Cache OCR results to avoid reprocessing. - Use multiprocessing for OCR on large PDFs. - Handle 100+ page PDFs with tiling and lazy loading. - Bind to localhost only. - Do not ask for files. Do not expect attachments. The prompt itself contains no attachments. The AI agent must build and test with generated data. If real files are needed, instruct user to place them in `.\samples\` and rerun with `-InputPath .\samples\`. Output the complete project code and instructions.
A system prompt was added to support web rendering
Response not available