Spaces:
Runtime error
Runtime error
File size: 13,135 Bytes
910e0d4 | 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 | ---
title: Intelligent_PID
emoji: π
colorFrom: red
colorTo: red
sdk: gradio
sdk_version: 3.50.2
app_file: gradioChatApp.py
pinned: false
---
# P&ID Processing with AI-Powered Graph Construction
## Overview
This project processes P&ID (Piping and Instrumentation Diagram) images using multiple AI models for symbol detection, text recognition, and line detection. It constructs a graph representation of the diagram and provides an interactive interface for querying the diagram's contents.
## Features
- P&ID Document Processing
- Symbol Detection
- Text Recognition
- Line Detection
- Knowledge Graph Generation
- Interactive Chat Interface
## Usage
1. Upload a P&ID document
2. Click "Process Document"
3. View results in different tabs
4. Ask questions about the P&ID in the chat
## Process Flow
```mermaid
graph TD
subgraph "Document Input"
A[Upload Document] --> B[Validate File]
B -->|PDF/Image| C[Document Processor]
B -->|Invalid| ERR[Error Message]
C -->|PDF| D1[Extract Pages]
C -->|Image| D2[Direct Process]
end
subgraph "Image Preprocessing"
D1 --> E[Optimize Image]
D2 --> E
E -->|CLAHE Enhancement| E1[Contrast Enhancement]
E1 -->|Denoising| E2[Clean Image]
E2 -->|Binarization| E3[Binary Image]
E3 -->|Resize| E4[Normalized Image]
end
subgraph "Line Detection Pipeline"
E4 --> L1[Load DeepLSD Model]
L1 --> L2[Scale Image 0.1x]
L2 --> L3[Grayscale Conversion]
L3 --> L4[Model Inference]
L4 --> L5[Scale Coordinates]
L5 --> L6[Draw Lines]
end
subgraph "Detection Pipeline"
E4 --> F[Symbol Detection]
E4 --> G[Text Detection]
F --> S1[Load YOLO Models]
G --> T1[Load OCR Models]
S1 --> S2[Detect Symbols]
T1 --> T2[Detect Text]
S2 --> S3[Process Symbols]
T2 --> T3[Process Text]
L6 --> L7[Process Lines]
end
subgraph "Data Integration"
S3 --> I[Data Aggregation]
T3 --> I
L7 --> I
I --> J[Create Edges]
J --> K[Build Graph Network]
K --> L[Generate Knowledge Graph]
end
subgraph "User Interface"
L --> M[Interactive Visualization]
M --> N[Chat Interface]
N --> O[Query Processing]
O --> P[Response Generation]
P --> N
end
style A fill:#f9f,stroke:#333,stroke-width:2px
style F fill:#fbb,stroke:#333,stroke-width:2px
style G fill:#bfb,stroke:#333,stroke-width:2px
style H fill:#bbf,stroke:#333,stroke-width:2px
style I fill:#fbf,stroke:#333,stroke-width:2px
style N fill:#bbf,stroke:#333,stroke-width:2px
%% Add style for model nodes
style SM1 fill:#ffe6e6,stroke:#333,stroke-width:2px
style SM2 fill:#ffe6e6,stroke:#333,stroke-width:2px
style LM1 fill:#e6e6ff,stroke:#333,stroke-width:2px
style DC1 fill:#e6ffe6,stroke:#333,stroke-width:2px
style DC2 fill:#e6ffe6,stroke:#333,stroke-width:2px
```
## Architecture

## Features
- **Multi-modal AI Processing**:
- Combined OCR approach using Tesseract, EasyOCR, and DocTR
- Symbol detection with optimized thresholds
- Intelligent line and connection detection
- **Document Processing**:
- Support for PDF, PNG, JPG, JPEG formats
- Automatic page extraction from PDFs
- Image optimization pipeline
- **Text Detection Types**:
- Equipment Tags
- Line Numbers
- Instrument Tags
- Valve Numbers
- Pipe Sizes
- Flow Directions
- Service Descriptions
- Process Instruments
- Nozzles
- Pipe Connectors
- **Data Integration**:
- Automatic edge detection
- Relationship mapping
- Confidence scoring
- Detailed detection statistics
- **User Interface**:
- Interactive visualization tabs
- Real-time processing feedback
- AI-powered chat interface
- Knowledge graph exploration
The entire process is visualized through an interactive Gradio-based UI, allowing users to upload a P&ID image, follow the detection steps, and view both the results and insights in real time.
## Key Files
- **gradioChatApp.py**: The main Gradio app script that handles the frontend and orchestrates the overall flow.
- **symbol_detection.py**: Module for detecting symbols using YOLO models.
- **text_detection_combined.py**: Unified module for text detection using multiple OCR engines (Tesseract, EasyOCR, DocTR).
- **line_detection_ai.py**: Module for detecting lines and connections using AI.
- **data_aggregation.py**: Aggregates detected elements into a structured format.
- **graph_construction.py**: Constructs the graph network from aggregated data.
- **graph_processor.py**: Handles graph visualization and processing.
- **pdf_processor.py**: Handles PDF document processing and page extraction.
## Setup and Installation
1. Clone the repository:
```bash
git clone https://github.com/IntuigenceAI/intui-PnID-POC.git
cd intui-PnID-POC
```
2. Install dependencies using uv:
```bash
# Install uv if you haven't already
curl -LsSf https://astral.sh/uv/install.sh | sh
# Create and activate virtual environment
uv venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
# Install dependencies
uv pip install -r requirements.txt
```
3. Download required models:
```bash
python download_model.py # Downloads DeepLSD model for line detection
```
4. Run the application:
```bash
python gradioChatApp.py
```
## Models
### Line Detection Model
- **DeepLSD Model**:
- File: deeplsd_md.tar
- Purpose: Line segment detection in P&ID diagrams
- Input Resolution: Variable (scaled to 0.1x for performance)
- Processing: Grayscale conversion and binary thresholding
### Text Detection Models
- **Combined OCR Approach**:
- Tesseract OCR
- EasyOCR
- DocTR
- Purpose: Text recognition and classification
### Graph Processing
- **NetworkX-based**:
- Purpose: Graph construction and analysis
- Features: Node linking, edge creation, path analysis
## Updating the Environment
To update the environment, use the following:
```bash
conda env update --file environment.yml --prune
```
This command will update the environment according to changes made in the `environment.yml`.
### Step 6: Deactivate the environment
When you're done, deactivate the environment by:
```bash
conda deactivate
```
2. Upload a P&ID image through the interface.
3. Follow the sequential steps of symbol, text, and line detection.
4. View the generated graph and AI agent's reasoning in the real-time chat box.
5. Save and export the results if satisfactory.
## Folder Structure
```
βββ assets/
β βββ AiAgent.png
β βββ llm.png
βββ gradioApp.py
βββ symbol_detection.py
βββ text_detection_combined.py
βββ line_detection_ai.py
βββ data_aggregation.py
βββ graph_construction.py
βββ graph_processor.py
βββ pdf_processor.py
βββ pnid_agent.py
βββ requirements.txt
βββ results/
βββ models/
β βββ symbol_detection_model.pth
```
## /models Folder
- **models/symbol_detection_model.pth**: This folder contains the pre-trained model for symbol detection in P&ID diagrams. This model is crucial for detecting key symbols such as valves, instruments, and pipes in the diagram. Make sure to download the model and place it in the `/models` directory before running the app.
## Future Work
- **Advanced Symbol Recognition**: Improve symbol detection by integrating more sophisticated recognition models.
- **Graph Enhancement**: Introduce more complex graph structures and logic for representing the relationships between the diagram's elements.
- **Data Export**: Allow export in additional formats such as DEXPI-compliant XML or JSON.
# Docker Information
We'll cover the basic docker operations here.
## Building
There is a dockerfile for each different project (they have slightly different requiremnts).
### `gradioChatApp.py`
Run this one as follows:
```
> docker build -t exp-pnid-to-graph_chat-w-graph:0.0.4 -f Dockerfile-chatApp .
> docker tag exp-pnid-to-graph_chat-w-graph:0.0.4 intaicr.azurecr.io/intai/exp-pnid-to-graph_chat-w-graph:0.0.4
```
## Deploying to ACR
### `gradioChatApp.py`
```
> az login
> az acr login --name intaicr
> docker push intaicr.azurecr.io/intai/exp-pnid-to-graph_chat-w-graph:0.0.4
```
## Models
### Symbol Detection Models
- **Intui_SDM_41.pt**: Primary model for equipment and large symbol detection
- Classes: Equipment, Vessels, Heat Exchangers
- Input Resolution: 1280x1280
- Confidence Threshold: 0.3-0.7 (adaptive)
- **Intui_SDM_20.pt**: Secondary model for instrument and small symbol detection
- Classes: Instruments, Valves, Indicators
- Input Resolution: 1280x1280
- Confidence Threshold: 0.3-0.7 (adaptive)
### Line Detection Model
- **intui_LDM_01.pt**: Specialized model for line and connection detection
- Classes: Solid Lines, Dashed Lines
- Input Resolution: 1280x1280
- Confidence Threshold: 0.5
### Text Detection Models
- **Tesseract**: v5.3.0
- Configuration:
- OEM Mode: 3 (Default)
- PSM Mode: 11 (Sparse text)
- Custom Whitelist: A-Z, 0-9, special characters
- **EasyOCR**: v1.7.1
- Configuration:
- Language: English
- Paragraph Mode: False
- Height Threshold: 2.0
- Width Threshold: 2.0
- Contrast Threshold: 0.2
- **DocTR**: v0.6.0
- Models:
- fast_base-688a8b34.pt
- crnn_vgg16_bn-9762b0b0.pt
# P&ID Line Detection
A deep learning-based pipeline for detecting lines in P&ID diagrams using DeepLSD.
## Architecture
```mermaid
graph TD
A[Input Image] --> B[Line Detection]
B --> C[DeepLSD Model]
C --> D[Post-processing]
D --> E[Output JSON/Image]
subgraph Line Detection Pipeline
B --> F[Image Preprocessing]
F --> G[Scale Image 0.1x]
G --> H[Grayscale Conversion]
H --> C
C --> I[Scale Coordinates]
I --> J[Draw Lines]
J --> E
end
```
## Setup
### Prerequisites
- Python 3.12+
- uv (for dependency management)
- Git
- CUDA-capable GPU (optional)
### Installation
1. Clone the repository:
```bash
git clone https://github.com/IntuigenceAI/intui-PnID-POC.git
cd intui-PnID-POC
```
2. Install dependencies using uv:
```bash
# Install uv if you haven't already
curl -LsSf https://astral.sh/uv/install.sh | sh
# Create and activate virtual environment
uv venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
# Install dependencies
uv pip install -r requirements.txt
```
3. Download DeepLSD model:
```bash
python download_model.py
```
## Usage
1. Run the line detection:
```bash
python line_detection_ai.py
```
The script will:
- Load the DeepLSD model
- Process input images at 0.1x scale for performance
- Generate line detections
- Save results as JSON and annotated images
## Configuration
Key parameters in `line_detection_ai.py`:
- `scale_factor`: Image scaling (default: 0.1)
- `device`: CPU/GPU selection
- `mask_json_paths`: Paths to text/symbol detection results
## Input/Output
### Input
- Original P&ID images
- Optional text/symbol detection JSON files for masking
### Output
- Annotated images with detected lines
- JSON files containing line coordinates and metadata
## Project Structure
```
βββ line_detection_ai.py # Main line detection script
βββ detectors.py # Line detector implementation
βββ download_model.py # Model download utility
βββ models/ # Directory for model files
β βββ deeplsd_md.tar # DeepLSD model weights
βββ results/ # Output directory
βββ requirements.txt # Project dependencies
```
## Dependencies
Key dependencies:
- torch
- opencv-python
- numpy
- DeepLSD
See `requirements.txt` for the complete list.
## Contributing
1. Fork the repository
2. Create your feature branch (`git checkout -b feature/amazing-feature`)
3. Commit your changes (`git commit -m 'Add some amazing feature'`)
4. Push to the branch (`git push origin feature/amazing-feature`)
5. Open a Pull Request
## License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
## Acknowledgments
- [DeepLSD](https://github.com/cvg/DeepLSD) for the line detection model
- Original P&ID processing pipeline by IntuigenceAI
---
title: PnID Diagram Analyzer
emoji: π
colorFrom: blue
colorTo: red
sdk: gradio
sdk_version: 4.19.2
app_file: gradioChatApp.py
pinned: false
---
# PnID Diagram Analyzer
This app analyzes PnID diagrams using AI to detect and interpret various elements.
## Features
- Line detection
- Symbol recognition
- Text detection
- Graph construction
# Intuigence P&ID Analyzer
Interactive P&ID analysis tool powered by AI.
## Features
- P&ID Document Processing
- Symbol Detection
- Text Recognition
- Line Detection
- Knowledge Graph Generation
- Interactive Chat Interface
## Usage
1. Upload a P&ID document
2. Click "Process Document"
3. View results in different tabs
4. Ask questions about the P&ID in the chat
|