Spaces:
Running
Running
Update README.md
Browse files
README.md
CHANGED
|
@@ -1,93 +1,15 @@
|
|
| 1 |
-
# SVG-SAMurai π‘οΈ
|
| 2 |
-
|
| 3 |
-
**SVG-SAMurai** is an interactive, Streamlit-based web application that leverages the power of Meta's **Segment Anything Model (SAM)** to transform raster (PNG, JPG) and vector (SVG) images into precisely segmented, editable SVG paths.
|
| 4 |
-
|
| 5 |
-
Whether you're starting from a flat image or an existing SVG file, SVG-SAMurai allows you to click on regions of interest, predict their boundaries, and inject those precise vector paths back into a master SVG document.
|
| 6 |
-
|
| 7 |
-
## π Features
|
| 8 |
-
|
| 9 |
-
- **Interactive Segmentation:** Click on any part of an uploaded image to instantly generate an accurate mask using the SAM Vision Transformer (`facebook/sam-vit-base`).
|
| 10 |
-
- **Support for Multiple Formats:** Upload PNG, JPG, or SVG files. Vector images are rasterized cleanly in the backend for processing, allowing you to segment them seamlessly.
|
| 11 |
-
- **Smart Vectorization:** Extracted masks are converted into optimized SVG `<path>` elements using the Ramer-Douglas-Peucker algorithm (via OpenCV) for smooth, simplified contours.
|
| 12 |
-
- **Adjustable Simplification:** Fine-tune the vectorization epsilon factor directly from the UI to control the complexity of the generated paths.
|
| 13 |
-
- **Segment Management:** Name your segments and save them into a live-updating SVG document.
|
| 14 |
-
- **In-Memory Caching:** Heavy image embeddings are cached securely using Streamlit's `@st.cache_data` and `@st.cache_resource`, ensuring snappy performance and instant mask prediction on subsequent clicks.
|
| 15 |
-
- **Easy Export:** Download the final composed SVG with all your tagged, labeled segments neatly organized in `<g>` groups.
|
| 16 |
-
|
| 17 |
-
## π οΈ Tech Stack
|
| 18 |
-
|
| 19 |
-
* **Frontend:** [Streamlit](https://streamlit.io/), [streamlit-image-coordinates](https://pypi.org/project/streamlit-image-coordinates/)
|
| 20 |
-
* **Machine Learning:** [PyTorch](https://pytorch.org/), [Hugging Face Transformers](https://huggingface.co/docs/transformers/index) (Segment Anything Model)
|
| 21 |
-
* **Image Processing:** [OpenCV](https://opencv.org/) (Contour extraction & smoothing), [Pillow (PIL)](https://python-pillow.org/)
|
| 22 |
-
* **SVG / DOM Manipulation:** [lxml](https://lxml.de/) (XML parsing and injection), [CairoSVG](https://cairosvg.org/) (SVG rasterization)
|
| 23 |
-
* **Dependency Management:** [Poetry](https://python-poetry.org/)
|
| 24 |
-
|
| 25 |
-
## π Quick Start
|
| 26 |
-
|
| 27 |
-
### Prerequisites
|
| 28 |
-
|
| 29 |
-
- Python `>=3.10, <3.13` (Required for PyTorch and Triton compatibility)
|
| 30 |
-
- [Poetry](https://python-poetry.org/docs/#installation) installed on your system.
|
| 31 |
-
- System dependencies for CairoSVG and OpenCV (e.g., `libcairo2-dev`, `libgl1-mesa-glx` on Ubuntu/Debian).
|
| 32 |
-
|
| 33 |
-
### Installation
|
| 34 |
-
|
| 35 |
-
1. **Clone the repository:**
|
| 36 |
-
```bash
|
| 37 |
-
git clone <repository-url>
|
| 38 |
-
cd svg-samurai
|
| 39 |
-
```
|
| 40 |
-
|
| 41 |
-
2. **Install dependencies using Poetry:**
|
| 42 |
-
```bash
|
| 43 |
-
poetry install
|
| 44 |
-
```
|
| 45 |
-
|
| 46 |
-
### Running the App
|
| 47 |
-
|
| 48 |
-
Start the Streamlit development server:
|
| 49 |
-
|
| 50 |
-
```bash
|
| 51 |
-
poetry run streamlit run app.py
|
| 52 |
-
```
|
| 53 |
-
|
| 54 |
-
The application will launch in your default web browser at `http://localhost:8501`.
|
| 55 |
-
|
| 56 |
-
## π How to Use
|
| 57 |
-
|
| 58 |
-
1. **Upload an Image:** Use the file uploader to select a PNG, JPG, or SVG file. The app will calculate the complex image embeddings once (this may take a few moments depending on your hardware).
|
| 59 |
-
2. **Select Segments:** Click anywhere on the image in the left panel to prompt the model.
|
| 60 |
-
- *Tip:* You can toggle the "Next Click is Negative Prompt" checkbox in the sidebar to exclude specific regions from your mask.
|
| 61 |
-
3. **Refine & Save:**
|
| 62 |
-
- Use the "Undo Last Click" or "Clear Current Selection" buttons to fix mistakes.
|
| 63 |
-
- Give your highlighted segment a descriptive name (e.g., `car_body`).
|
| 64 |
-
- Adjust the **Simplification (epsilon)** slider if you want fewer, smoother nodes in your final vector path.
|
| 65 |
-
- Click **Save Segment to SVG**.
|
| 66 |
-
4. **Download:** Once you have saved all desired segments, click the **Download Final SVG** button to retrieve your newly layered vector graphic.
|
| 67 |
-
|
| 68 |
-
## π Project Structure
|
| 69 |
-
|
| 70 |
-
```text
|
| 71 |
-
svg-samurai/
|
| 72 |
-
βββ app.py # Main Streamlit user interface and application state
|
| 73 |
-
βββ pyproject.toml # Poetry dependencies and project configuration
|
| 74 |
-
βββ src/ # Backend logic
|
| 75 |
-
β βββ model.py # PyTorch SAM loading, embedding generation, and mask prediction
|
| 76 |
-
β βββ vectorizer.py # OpenCV contour extraction and SVG path conversion
|
| 77 |
-
β βββ xml_manager.py # lxml DOM manipulation and CairoSVG rasterization utilities
|
| 78 |
-
βββ tests/ # Unit tests for core logic
|
| 79 |
-
βββ test_model.py
|
| 80 |
-
βββ test_vectorizer.py
|
| 81 |
-
βββ test_xml_manager.py
|
| 82 |
-
```
|
| 83 |
-
|
| 84 |
-
## π§ͺ Testing
|
| 85 |
-
|
| 86 |
-
The project uses `pytest` for unit testing. To run the test suite, simply execute:
|
| 87 |
-
|
| 88 |
-
```bash
|
| 89 |
-
poetry run pytest
|
| 90 |
-
```
|
| 91 |
-
|
| 92 |
---
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: SVG SAMurai
|
| 3 |
+
emoji: π
|
| 4 |
+
colorFrom: red
|
| 5 |
+
colorTo: red
|
| 6 |
+
sdk: docker
|
| 7 |
+
app_port: 8501
|
| 8 |
+
tags:
|
| 9 |
+
- streamlit
|
| 10 |
+
pinned: false
|
| 11 |
+
short_description: Tool to turn an image to an SVG with named image sections
|
| 12 |
+
license: mit
|
| 13 |
+
---
|
| 14 |
+
|
| 15 |
+
# Welcome to Streamlit!
|