Spaces:
Runtime error
Runtime error
File size: 28,185 Bytes
3f42a6f |
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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 |
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "header"
},
"source": [
"# π§ eDOCr2 - Engineering Drawing OCR Testing Notebook\n",
"\n",
"This notebook allows you to test the **eDOCr2** tool on your own engineering drawings.\n",
"\n",
"**What it does:**\n",
"- Segments engineering drawings into layers (tables, dimensions, GD&T)\n",
"- Performs OCR on dimensions, tolerances, and symbols\n",
"- Extracts structured data (JSON/CSV)\n",
"- Generates visual masks showing detected elements\n",
"\n",
"---"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "setup_header"
},
"source": [
"## π¦ Step 1: Setup Environment\n",
"\n",
"**IMPORTANT**: After running this cell, you MUST restart the runtime:\n",
"- Go to `Runtime` β `Restart runtime`\n",
"- Then continue with Step 2"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "install_system_deps"
},
"outputs": [],
"source": [
"# Install system dependencies\n",
"!apt-get update -qq\n",
"!apt-get install -y -qq tesseract-ocr poppler-utils\n",
"\n",
"# Clone the eDOCr2 repository\n",
"!git clone https://github.com/javvi51/edocr2.git\n",
"%cd edocr2\n",
"\n",
"# Install all dependencies EXCEPT NumPy-dependent packages\n",
"print(\"π¦ Installing base dependencies...\")\n",
"!pip install -q pdf2image pandas validators imgaug scikit-learn tqdm\n",
"!pip install -q essential_generators editdistance pyclipper python-dotenv\n",
"!pip install -q accelerate sentence-transformers shapely pytesseract\n",
"\n",
"# Install specific versions that work with NumPy 1.x\n",
"print(\"π¦ Installing NumPy 1.26.4...\")\n",
"!pip uninstall -y numpy -q\n",
"!pip install numpy==1.26.4 -q\n",
"\n",
"print(\"π¦ Installing compatible packages...\")\n",
"!pip install -q scikit-image==0.21.0 # Compatible with NumPy 1.x\n",
"!pip install -q opencv-python==4.8.1.78 opencv-contrib-python==4.8.1.78\n",
"!pip install -q efficientnet==1.0.0\n",
"!pip install -q tf-keras\n",
"\n",
"print(\"\\n\" + \"=\"*60)\n",
"print(\"β οΈ IMPORTANT: RESTART RUNTIME NOW!\")\n",
"print(\"=\"*60)\n",
"print(\"1. Go to: Runtime β Restart runtime\")\n",
"print(\"2. Then run Step 2 to verify installation\")\n",
"print(\"=\"*60)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "verify_header"
},
"source": [
"## β
Step 2: Verify Installation\n",
"\n",
"Run this AFTER restarting the runtime to verify NumPy version."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "verify_install"
},
"outputs": [],
"source": [
"import numpy as np\n",
"import cv2\n",
"import sys\n",
"\n",
"print(\"π Checking installation...\\n\")\n",
"print(f\"Python version: {sys.version.split()[0]}\")\n",
"print(f\"NumPy version: {np.__version__}\")\n",
"print(f\"OpenCV version: {cv2.__version__}\")\n",
"\n",
"if np.__version__.startswith('1.'):\n",
" print(\"\\nβ
SUCCESS! NumPy 1.x is installed.\")\n",
" print(\" You can proceed to Step 3.\")\n",
"else:\n",
" print(\"\\nβ οΈ WARNING: NumPy 2.x detected!\")\n",
" print(\" Please go back to Step 1 and restart runtime.\")\n",
"\n",
"# Change to edocr2 directory\n",
"%cd /content/edocr2"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "download_models_header"
},
"source": [
"## π§ Step 3: Download Pre-trained Models\n",
"\n",
"The models are hosted in the GitHub releases (v1.0.0)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "download_models"
},
"outputs": [],
"source": [
"import os\n",
"import urllib.request\n",
"\n",
"# Create models directory\n",
"os.makedirs('edocr2/models', exist_ok=True)\n",
"\n",
"# Model URLs from GitHub releases (v1.0.0)\n",
"base_url = \"https://github.com/javvi51/edocr2/releases/download/v1.0.0/\"\n",
"\n",
"models = {\n",
" 'recognizer_gdts.keras': base_url + 'recognizer_gdts.keras',\n",
" 'recognizer_gdts.txt': base_url + 'recognizer_gdts.txt',\n",
" 'recognizer_dimensions_2.keras': base_url + 'recognizer_dimensions_2.keras',\n",
" 'recognizer_dimensions_2.txt': base_url + 'recognizer_dimensions_2.txt'\n",
"}\n",
"\n",
"print(\"π₯ Downloading models (this may take a few minutes)...\\n\")\n",
"for filename, url in models.items():\n",
" filepath = f'edocr2/models/{filename}'\n",
" if not os.path.exists(filepath):\n",
" try:\n",
" print(f\" β³ Downloading {filename}...\", end=' ')\n",
" urllib.request.urlretrieve(url, filepath)\n",
" file_size = os.path.getsize(filepath) / (1024 * 1024) # Convert to MB\n",
" print(f\"β
({file_size:.1f} MB)\")\n",
" except Exception as e:\n",
" print(f\"β\")\n",
" print(f\" β οΈ Failed: {e}\")\n",
" print(f\" Please download manually from: {url}\")\n",
" else:\n",
" file_size = os.path.getsize(filepath) / (1024 * 1024)\n",
" print(f\" β
{filename} already exists ({file_size:.1f} MB)\")\n",
"\n",
"print(\"\\nβ
Model download complete!\")"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "upload_header"
},
"source": [
"## π€ Step 4: Upload Your Engineering Drawings\n",
"\n",
"Upload your `.jpg`, `.png`, or `.pdf` engineering drawing files."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "upload_files"
},
"outputs": [],
"source": [
"from google.colab import files\n",
"import os\n",
"\n",
"# Create upload directory\n",
"os.makedirs('my_drawings', exist_ok=True)\n",
"\n",
"print(\"π€ Please upload your engineering drawing files...\")\n",
"print(\" Supported formats: .jpg, .png, .pdf\\n\")\n",
"uploaded = files.upload()\n",
"\n",
"# Move uploaded files to my_drawings folder\n",
"for filename in uploaded.keys():\n",
" os.rename(filename, f'my_drawings/{filename}')\n",
" file_size = len(uploaded[filename]) / (1024 * 1024)\n",
" print(f\"β
Uploaded: {filename} ({file_size:.2f} MB)\")\n",
"\n",
"print(f\"\\nβ
Total files uploaded: {len(uploaded)}\")"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "process_header"
},
"source": [
"## π Step 5: Process Your Drawings\n",
"\n",
"This will run the OCR pipeline on all uploaded images."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "process_drawings"
},
"outputs": [],
"source": [
"import cv2\n",
"import numpy as np\n",
"from edocr2 import tools\n",
"from pdf2image import convert_from_path\n",
"from edocr2.keras_ocr.recognition import Recognizer\n",
"from edocr2.keras_ocr.detection import Detector\n",
"import tensorflow as tf\n",
"import time\n",
"import warnings\n",
"import os\n",
"\n",
"# Suppress warnings\n",
"warnings.filterwarnings('ignore')\n",
"os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'\n",
"\n",
"# Verify NumPy version before proceeding\n",
"print(f\"π NumPy version: {np.__version__}\")\n",
"if not np.__version__.startswith('1.'):\n",
" print(\"\\nβ οΈ ERROR: NumPy 2.x detected!\")\n",
" print(\" This will cause errors. Please:\")\n",
" print(\" 1. Go back to Step 1\")\n",
" print(\" 2. Restart runtime after Step 1\")\n",
" print(\" 3. Run Step 2 to verify\")\n",
" raise RuntimeError(\"NumPy 1.x is required!\")\n",
"\n",
"# Configure TensorFlow\n",
"gpus = tf.config.list_physical_devices('GPU')\n",
"for gpu in gpus:\n",
" tf.config.experimental.set_memory_growth(gpu, True)\n",
"\n",
"print(\"π§ Loading models...\")\n",
"start_time = time.time()\n",
"\n",
"# Load GD&T recognizer\n",
"gdt_model = 'edocr2/models/recognizer_gdts.keras'\n",
"recognizer_gdt = Recognizer(alphabet=tools.ocr_pipelines.read_alphabet(gdt_model))\n",
"recognizer_gdt.model.load_weights(gdt_model)\n",
"\n",
"# Load dimension recognizer\n",
"dim_model = 'edocr2/models/recognizer_dimensions_2.keras'\n",
"alphabet_dim = tools.ocr_pipelines.read_alphabet(dim_model)\n",
"recognizer_dim = Recognizer(alphabet=alphabet_dim)\n",
"recognizer_dim.model.load_weights(dim_model)\n",
"\n",
"# Load detector\n",
"detector = Detector()\n",
"\n",
"# Warm up models\n",
"dummy_image = np.zeros((1, 1, 3), dtype=np.float32)\n",
"_ = recognizer_gdt.recognize(dummy_image)\n",
"_ = recognizer_dim.recognize(dummy_image)\n",
"dummy_image = np.zeros((32, 32, 3), dtype=np.float32)\n",
"_ = detector.detect([dummy_image])\n",
"\n",
"end_time = time.time()\n",
"print(f\"β
Models loaded in {end_time - start_time:.2f} seconds\\n\")\n",
"\n",
"# Process each uploaded file\n",
"import glob\n",
"import json\n",
"\n",
"drawing_files = glob.glob('my_drawings/*')\n",
"results_all = {}\n",
"\n",
"if not drawing_files:\n",
" print(\"β οΈ No files found in my_drawings/\")\n",
" print(\" Please upload files in Step 4 first!\")\n",
"else:\n",
" for file_path in drawing_files:\n",
" print(f\"\\n{'='*60}\")\n",
" print(f\"π Processing: {os.path.basename(file_path)}\")\n",
" print(f\"{'='*60}\")\n",
" \n",
" try:\n",
" # Read file\n",
" if file_path.lower().endswith('.pdf'):\n",
" img = convert_from_path(file_path)\n",
" img = np.array(img[0])\n",
" gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)\n",
" _, img = cv2.threshold(gray, 200, 255, cv2.THRESH_BINARY)\n",
" img = cv2.merge([img, img, img])\n",
" else:\n",
" img = cv2.imread(file_path)\n",
" \n",
" if img is None:\n",
" print(f\"β οΈ Could not read file: {file_path}\")\n",
" continue\n",
" \n",
" filename = os.path.splitext(os.path.basename(file_path))[0]\n",
" output_path = f'results/{filename}'\n",
" os.makedirs(output_path, exist_ok=True)\n",
" \n",
" # Segmentation\n",
" print(\" π Segmenting layers...\")\n",
" img_boxes, frame, gdt_boxes, tables, dim_boxes = tools.layer_segm.segment_img(\n",
" img, autoframe=True, frame_thres=0.7, GDT_thres=0.02, binary_thres=127\n",
" )\n",
" \n",
" # OCR Tables\n",
" print(\" π Processing tables...\")\n",
" process_img = img.copy()\n",
" table_results, updated_tables, process_img = tools.ocr_pipelines.ocr_tables(\n",
" tables, process_img, language='eng'\n",
" )\n",
" \n",
" # OCR GD&T\n",
" print(\" π― Processing GD&T symbols...\")\n",
" gdt_results, updated_gdt_boxes, process_img = tools.ocr_pipelines.ocr_gdt(\n",
" process_img, gdt_boxes, recognizer_gdt\n",
" )\n",
" \n",
" # OCR Dimensions\n",
" print(\" π Processing dimensions...\")\n",
" if frame:\n",
" process_img = process_img[frame.y : frame.y + frame.h, frame.x : frame.x + frame.w]\n",
" \n",
" dimensions, other_info, process_img, dim_tess = tools.ocr_pipelines.ocr_dimensions(\n",
" process_img, detector, recognizer_dim, alphabet_dim, frame, dim_boxes,\n",
" cluster_thres=20, max_img_size=1048, language='eng', backg_save=False\n",
" )\n",
" \n",
" # Generate mask image\n",
" print(\" π¨ Generating visualization...\")\n",
" mask_img = tools.output_tools.mask_img(\n",
" img, updated_gdt_boxes, updated_tables, dimensions, frame, other_info\n",
" )\n",
" cv2.imwrite(f'{output_path}/{filename}_mask.png', mask_img)\n",
" \n",
" # Process and save results\n",
" print(\" πΎ Saving results...\")\n",
" table_results, gdt_results, dimensions, other_info = tools.output_tools.process_raw_output(\n",
" output_path, table_results, gdt_results, dimensions, other_info, save=True\n",
" )\n",
" \n",
" results_all[filename] = {\n",
" 'tables': table_results,\n",
" 'gdts': gdt_results,\n",
" 'dimensions': dimensions,\n",
" 'other_info': other_info\n",
" }\n",
" \n",
" print(f\" β
Processing complete!\")\n",
" print(f\" - Tables found: {len(table_results)}\")\n",
" print(f\" - GD&T symbols: {len(gdt_results)}\")\n",
" print(f\" - Dimensions: {len(dimensions)}\")\n",
" print(f\" - Other info: {len(other_info)}\")\n",
" \n",
" except Exception as e:\n",
" print(f\" β Error processing {file_path}: {str(e)}\")\n",
" import traceback\n",
" traceback.print_exc()\n",
"\n",
" print(f\"\\n\\n{'='*60}\")\n",
" print(f\"β
All drawings processed!\")\n",
" print(f\"{'='*60}\")"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "visualize_header"
},
"source": [
"## ποΈ Step 6: Visualize Results\n",
"\n",
"Display the mask images showing detected elements."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "visualize_results"
},
"outputs": [],
"source": [
"from IPython.display import Image, display\n",
"import glob\n",
"import os\n",
"\n",
"mask_images = glob.glob('results/**/*_mask.png', recursive=True)\n",
"\n",
"if mask_images:\n",
" print(f\"π Displaying {len(mask_images)} result(s):\\n\")\n",
" for mask_path in mask_images:\n",
" print(f\"\\n{'='*60}\")\n",
" print(f\"π {os.path.basename(mask_path)}\")\n",
" print(f\"{'='*60}\")\n",
" display(Image(filename=mask_path, width=800))\n",
"else:\n",
" print(\"β οΈ No mask images found. Please run the processing step first.\")"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "view_data_header"
},
"source": [
"## π Step 7: View Extracted Data\n",
"\n",
"Display the extracted structured data (dimensions, tables, GD&T)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "view_extracted_data"
},
"outputs": [],
"source": [
"import json\n",
"import glob\n",
"import os\n",
"\n",
"json_files = glob.glob('results/**/*.json', recursive=True)\n",
"\n",
"if json_files:\n",
" for json_path in json_files:\n",
" print(f\"\\n{'='*60}\")\n",
" print(f\"π {os.path.basename(json_path)}\")\n",
" print(f\"{'='*60}\\n\")\n",
" \n",
" with open(json_path, 'r') as f:\n",
" data = json.load(f)\n",
" print(json.dumps(data, indent=2))\n",
"else:\n",
" print(\"β οΈ No JSON files found.\")\n",
"\n",
"# Also check for CSV files\n",
"csv_files = glob.glob('results/**/*.csv', recursive=True)\n",
"if csv_files:\n",
" import pandas as pd\n",
" print(\"\\n\\nπ CSV Files:\")\n",
" for csv_path in csv_files:\n",
" print(f\"\\n{'='*60}\")\n",
" print(f\"π {os.path.basename(csv_path)}\")\n",
" print(f\"{'='*60}\\n\")\n",
" df = pd.read_csv(csv_path)\n",
" display(df)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "download_header"
},
"source": [
"## πΎ Step 8: Download Results\n",
"\n",
"Download all results as a ZIP file."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "download_results"
},
"outputs": [],
"source": [
"import shutil\n",
"from google.colab import files\n",
"import os\n",
"\n",
"# Create ZIP archive\n",
"if os.path.exists('results'):\n",
" print(\"π¦ Creating ZIP archive...\")\n",
" shutil.make_archive('edocr2_results', 'zip', 'results')\n",
" print(\"β
Archive created!\\n\")\n",
" \n",
" print(\"β¬οΈ Downloading results...\")\n",
" files.download('edocr2_results.zip')\n",
" print(\"β
Download complete!\")\n",
"else:\n",
" print(\"β οΈ No results folder found. Please run the processing step first.\")"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "test_sample_header"
},
"source": [
"## π§ͺ Optional: Test with Sample Images\n",
"\n",
"If you want to test without uploading your own images, use the sample drawings from the repository."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "test_samples"
},
"outputs": [],
"source": [
"import shutil\n",
"import os\n",
"\n",
"# Process a sample drawing from the repository\n",
"sample_file = 'tests/test_samples/Candle_holder.jpg'\n",
"\n",
"if os.path.exists(sample_file):\n",
" print(f\"π§ͺ Testing with sample: {sample_file}\\n\")\n",
" \n",
" # Copy to my_drawings folder\n",
" os.makedirs('my_drawings', exist_ok=True)\n",
" shutil.copy(sample_file, 'my_drawings/')\n",
" \n",
" print(\"β
Sample copied to my_drawings/\")\n",
" print(\" Now run Step 5 to process it!\")\n",
"else:\n",
" print(\"β οΈ Sample file not found. Please run Step 1 first.\")"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "footer"
},
"source": [
"---\n",
"\n",
"## π Usage Instructions\n",
"\n",
"### **CRITICAL WORKFLOW:**\n",
"1. **Run Step 1** (Install dependencies)\n",
"2. **RESTART RUNTIME** (`Runtime` β `Restart runtime`)\n",
"3. **Run Step 2** (Verify NumPy 1.x is installed)\n",
"4. **Continue with Steps 3-8**\n",
"\n",
"### **Why Restart?**\n",
"Google Colab caches NumPy in memory. Restarting ensures NumPy 1.26.4 is loaded correctly.\n",
"\n",
"---\n",
"\n",
"## π Notes\n",
"\n",
"- **Supported formats**: JPG, PNG, PDF\n",
"- **Best results**: High-resolution scans with clear text\n",
"- **Processing time**: ~10-30 seconds per drawing\n",
"- **GPU acceleration**: Automatically used if available\n",
"- **Model size**: ~140 MB total\n",
"- **Required**: NumPy 1.26.4, OpenCV 4.8.1\n",
"\n",
"## π Resources\n",
"\n",
"- [eDOCr2 GitHub Repository](https://github.com/javvi51/edocr2)\n",
"- [Research Paper](http://dx.doi.org/10.2139/ssrn.5045921)\n",
"- [Model Downloads (v1.0.0)](https://github.com/javvi51/edocr2/releases/tag/v1.0.0)\n",
"\n",
"## π Troubleshooting\n",
"\n",
"**NumPy Error (`np.sctypes` or `AttributeError`)**: \n",
"- Did you restart runtime after Step 1?\n",
"- Run Step 2 to verify NumPy version\n",
"- If still NumPy 2.x, re-run Step 1 and restart again\n",
"\n",
"**Model Download Failed**: \n",
"- Check internet connection\n",
"- Download manually from releases page\n",
"\n",
"**Processing Errors**: \n",
"- Ensure images are clear and high-resolution\n",
"- Try the sample image first (Step 9)\n",
"\n",
"---\n",
"\n",
"**Created by**: Jeyanthan GJ \n",
"**Based on**: eDOCr2 by Javier Villena Toro\n"
]
}
],
"metadata": {
"accelerator": "GPU",
"colab": {
"gpuType": "T4",
"provenance": []
},
"kernelspec": {
"display_name": "Python 3",
"name": "python3"
},
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 0
} |