lukeafullard commited on
Commit
cb5c2ae
Β·
verified Β·
1 Parent(s): 51a42b8

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +14 -92
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
- *Developed with Streamlit and Meta's Segment Anything Model.*
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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!