Skip to content

An AI-powered tool that analyzes electrical circuit diagrams from images and generates SPICE-compatible netlists. It utilizes a custom pipeline with a fine-tuned YOLOv11 for high-precision component detection, an adapted SAM 2.1 for accurate segmentation, and Google's Gemini for intelligent value extraction.

License

Notifications You must be signed in to change notification settings

JKc66/CircuitVision

Repository files navigation

CircuitVision 🌿

CircuitVision Logo

From picture to simulation. An AI-powered app that automatically analyzes circuit diagrams and generates SPICE netlists.

Live Demo View on GitHub

License: Apache 2.0 Issues Ask DeepWiki

πŸ“š Table of Contents

πŸš€ Overview

CircuitVision is an innovative application designed to bridge the gap between visual electrical circuit diagrams and functional simulations. It intelligently analyzes images of electrical circuitsβ€”whether hand-drawn, photographed, or from schematicsβ€”and transforms them into SPICE-compatible netlists, providing an easy access to the operating point parameters of the circuit. This project leverages a sophisticated pipeline of fine-tuned and custom-adapted AI models, including YOLOv11 for component detection, an adapted SAM 2 (Segment Anything Model 2) for precise segmentation and intelligent cropping, and Google's Gemini for its robust OCR ability.

Our goal is to automate the tedious process of manual circuit transcription, enabling engineers, students, and hobbyists to quickly digitize, understand, and simulate electrical circuits with unprecedented ease and accuracy.

✨ Key Features & The Engineering Behind Them

Phase 1: Component Detection
Phase 1: Component Detection Phase 2: Topology Analysis
Phase 2: Topology Analysis Phase 3: Netlist Generation
Phase 3: Netlist Generation Phase 4: Simulation
Phase 4: Simulation

The four phases of the CircuitVision pipeline: Detection, Topology Analysis, Netlist Generation, and Simulation.

πŸ‘οΈ Advanced Component Detection (Fine-Tuned YOLOv11)

  • Utilizes a YOLOv11 model fine-tuned specifically on electrical circuit component datasets. This training enables robust identification of diverse electrical components even in complex or noisy images. We obtained a mean average percision (mAp-50) of 0.9313, which is on bar with SOTA detection models.
  • Employs Non-Maximum Suppression (NMS) based on confidence scores to eliminate redundant detections, ensuring a clean and precise component map.
  • Bounding boxes are re-calculated and adjusted after the initial crop to maintain coordinate accuracy relative to the new, focused region of interest.

πŸ“ Intelligent Cropping & Precise Segmentation (YOLO + SAM 2)

  • Intelligent YOLO-Based Cropping: Before segmentation, the application performs an intelligent crop based on the collective bounding box of all components detected by YOLO. This crucial first step isolates the relevant circuit area, significantly boosting the performance and accuracy of subsequent steps by removing background noise.
  • Precise Segmentation with SAM 2: After cropping, a fine-tuned Segment Anything Model 2 (SAM 2) is applied to the focused circuit area. This generates a highly detailed binary mask of the conductive traces, which is essential for accurate node and connection analysis. Our fine-tuned SAM 2 model achieves 98.7% accuracy on circuit segmentation from a small dataset of only 267 images. (This work will be submitted to a journal soon).

πŸ”— Custom-Developed Node & Connection Analysis

  • Our custom-developed node connection algorithm leverages the SAM 2-generated binary mask (after excluding component areas) and the adjusted YOLO bounding boxes. This allows for superior precision in identifying conductive traces and connection nodes within the electrical circuit.
  • Features a newly implemented pixel-based corner finding algorithm, replacing previous methods for enhanced robustness and accuracy across diverse circuit image styles and qualities. Includes comprehensive fallback mechanisms and empty coordinate checks.

πŸ“ Automated & Enriched Netlist Generation

  • Two-Stage Intelligent Process:
    1. Structural Netlist: An initial "valueless" netlist is generated based on the detected components (from fine-tuned YOLO) and their geometric interconnections (from custom node analysis).
    2. Gemini-Powered Value & Type Enrichment: An enumerated image, clearly marking each detected component with a unique ID, is passed to the Gemini Pro Vision model. Gemini then performs advanced OCR and contextual understanding to accurately identify component types (Resistors, Capacitors, Voltage/Current Sources, etc.) and extract their corresponding values (e.g., 10kΞ©, 100Β΅F, 12V). This step also serves as a cross-validation for component types initially detected by YOLO.
  • SPICE-Ready Output: The final netlist is meticulously filtered to remove invalid or incomplete entries, ensuring a clean, reliable, and simulation-ready output for SPICE-based tools.

πŸ–₯️ Intuitive User Interface & Rich Visualization

  • Streamlined Workflow: Analysis is automatically triggered upon image upload, with a custom loading animation providing real-time feedback.
  • Comprehensive Results Dashboard:
    • Visualize each critical step: annotated component detections (YOLO), SAM 2 segmentation masks, identified node connections.
    • Detailed performance timings for each major analysis stage (YOLO, SAM 2, Cropping, Node Analysis, Netlist Generation).
    • Display of original image properties and extracted EXIF data for traceability.
  • Intelligent Preprocessing: Images are automatically rotated based on EXIF orientation data before any analysis begins.
  • Debugging & Transparency: Dedicated expander sections reveal intermediate debug images, including the enumerated image sent to Gemini, providing insight into the AI's "reasoning."

πŸ“ˆ Our Development Journey & Enhancements

CircuitVision is the culmination of significant research and development in applying and adapting cutting-edge AI to the specialized domain of electrical circuit analysis. We've moved beyond off-the-shelf model usage to:

  • Model Fine-Tuning: Extensive work was done to fine-tune YOLOv11 on specific electrical circuit datasets to maximize component recognition accuracy.
  • SAM 2 Adaptation: We've custom-adapted the application of SAM 2 for optimal circuit segmentation, leading to the intelligent cropping feature which is key to downstream accuracy.
  • Algorithm Development: The node analysis and corner detection algorithms are custom-built for the unique challenges of electrical circuit diagrams.
  • Multi-Model Orchestration: Integrating YOLO, SAM 2, and Gemini into a cohesive and efficient pipeline represents a complex engineering achievement.

πŸ› οΈ Technology Stack

Python PyTorch Streamlit Docker Gemini

πŸ› οΈ Setup

  1. Models & API Access:

    • YOLO & SAM 2 Models: Our system relies on specific pre-trained and fine-tuned model weights. Run the provided script to download the necessary model files:
      python download_models.py
      This will create models/YOLO and models/SAM2 directories and populate them. Ensure you have requests and gdown installed (pip install requests gdown).
    • Gemini API Key: You will need a Google Gemini API key. Set it as an environment variable GEMINI_API_KEY or configure it within the application as required.
  2. Dependencies: Install all required Python packages:

    pip install -r requirements.txt

🐳 Using Docker (Recommended)

For a consistent and isolated environment, we recommend using Docker.

  1. Build the Docker Image: From the project root directory (containing the Dockerfile):

    docker build -t circuitvision .
  2. Run with Docker Compose (docker-compose.yml): This is often the simplest way to manage multi-container applications or complex setups.

    docker compose up --build

    To run in detached mode:

    docker compose up -d --build

    (Ensure your GEMINI_API_KEY is available to the Docker environment, e.g., via an .env file used by Docker Compose or by passing it as an environment variable in the docker-compose.yml)

πŸš€ Usage

  1. Launch Application: Start the CircuitVision Streamlit app (e.g., streamlit run app.py or via Docker).
  2. Upload Image: Use the file uploader to select an image of the electrical circuit you wish to analyze.
  3. Automatic Analysis: The system will automatically:
    • Pre-process the image (EXIF rotation).
    • Perform component detection (YOLO).
    • Segment and crop the circuit (SAM 2).
    • Execute custom node analysis.
    • Generate the initial structural netlist.
    • Query Gemini for component values and types.
    • Produce the final, enriched netlist.

🧠 Training Code

Explore the core training pipelines used to build the intelligence behind CircuitVision:

  • πŸŸͺ SAM 2 Training Custom training notebook for Segment Anything Model 2, tailored specifically for electrical circuit segmentation.

    Open in Kaggle

  • 🟧 YOLOv11 Training End-to-end training and inference pipeline for detecting circuit elements with YOLOv11.

    Open in Kaggle


πŸ™ Acknowledgements

We extend our deepest gratitude to our supervisor, Prof. Adel Abdennour, for his exceptional guidance, support, and invaluable insights throughout this Senior Design Project.


βœ’οΈ Citation

If you find CircuitVision useful in your research or work, please cite it as follows:

@misc{CircuitVision,
  author = {Sameh, Mahmoud and Khan, Jawad},
  title = {CircuitVision: AI-Powered Electrical Circuit Analysis & Netlist Generation},
  year = {2025},
  publisher = {GitHub},
  journal = {GitHub repository},
  howpublished = {\url{https://github.com/JKc66/CircuitVision}}
}

About

An AI-powered tool that analyzes electrical circuit diagrams from images and generates SPICE-compatible netlists. It utilizes a custom pipeline with a fine-tuned YOLOv11 for high-precision component detection, an adapted SAM 2.1 for accurate segmentation, and Google's Gemini for intelligent value extraction.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •