File size: 7,801 Bytes
77834ec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
Setup script for the Semantic Segmentation Gradio App
This script helps install dependencies and set up the environment
"""

import subprocess
import sys
import os
from pathlib import Path

def run_command(command, description):
    """Run a command and handle errors."""
    print(f"\nπŸ”„ {description}...")
    try:
        result = subprocess.run(command, shell=True, check=True, capture_output=True, text=True)
        print(f"βœ… {description} completed successfully")
        return True
    except subprocess.CalledProcessError as e:
        print(f"❌ Error during {description}:")
        print(f"Command: {command}")
        print(f"Error: {e.stderr}")
        return False

def check_python_version():
    """Check if Python version is compatible."""
    version = sys.version_info
    if version.major < 3 or (version.major == 3 and version.minor < 8):
        print("❌ Python 3.8 or higher is required")
        sys.exit(1)
    print(f"βœ… Python {version.major}.{version.minor}.{version.micro} detected")

def install_dependencies():
    """Install required dependencies."""
    requirements = [
        "gradio>=4.0.0",
        "torch>=1.9.0",
        "torchvision>=0.10.0", 
        "transformers>=4.21.0",
        "pillow>=8.0.0",
        "numpy>=1.21.0",
        "matplotlib>=3.5.0",
        "requests>=2.25.0",
    ]
    
    print("\nπŸ“¦ Installing dependencies...")
    for req in requirements:
        if not run_command(f"pip install {req}", f"Installing {req.split('>=')[0]}"):
            return False
    return True

def create_directory_structure():
    """Create necessary directories."""
    directories = [
        "src",
        "src/models",
        "sample_images",
        "outputs"
    ]
    
    for directory in directories:
        Path(directory).mkdir(parents=True, exist_ok=True)
        print(f"πŸ“ Created directory: {directory}")

def download_sample_images():
    """Download some sample images for testing."""
    import requests
    from PIL import Image
    import io
    
    sample_urls = {
        "street_scene_1.jpg": "https://images.unsplash.com/photo-1449824913935-59a10b8d2000?w=800",
        "street_scene_2.jpg": "https://images.unsplash.com/photo-1502920917128-1aa500764cbd?w=800",
        "urban_road.jpg": "https://images.unsplash.com/photo-1516738901171-8eb4fc13bd20?w=800",
    }
    
    sample_dir = Path("sample_images")
    sample_dir.mkdir(exist_ok=True)
    
    print("\nπŸ–ΌοΈ Downloading sample images...")
    for filename, url in sample_urls.items():
        try:
            response = requests.get(url, timeout=30)
            response.raise_for_status()
            
            image = Image.open(io.BytesIO(response.content))
            image_path = sample_dir / filename
            image.save(image_path)
            print(f"βœ… Downloaded: {filename}")
            
        except Exception as e:
            print(f"⚠️ Failed to download {filename}: {e}")

def create_launch_script():
    """Create a simple launch script."""
    launch_script = '''#!/usr/bin/env python3
"""
Launch script for the Semantic Segmentation App
"""

import sys
import os

# Add the current directory to the path
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))

# Import and run the app
try:
    from complete_gradio_app import create_gradio_interface
    import torch
    
    print("πŸš€ Starting Semantic Segmentation App...")
    print("πŸ’» Device:", "CUDA" if torch.cuda.is_available() else "CPU")
    
    demo = create_gradio_interface()
    demo.launch(
        share=True,
        debug=True,
        server_name="0.0.0.0",
        server_port=7860
    )
    
except ImportError as e:
    print(f"❌ Import error: {e}")
    print("Please make sure all dependencies are installed by running: python setup.py")
    
except Exception as e:
    print(f"❌ Error starting app: {e}")
'''
    
    with open("launch_app.py", "w") as f:
        f.write(launch_script)
    
    # Make it executable on Unix systems
    if os.name != 'nt':
        os.chmod("launch_app.py", 0o755)
    
    print("βœ… Created launch script: launch_app.py")

def create_readme():
    """Create a README file with usage instructions."""
    readme_content = '''# Semantic Segmentation Gradio App

A user-friendly web interface for semantic segmentation using OneFormer and Mask2Former models.

## πŸš€ Quick Start

1. **Install dependencies:**
   ```bash
   python setup.py
   ```

2. **Launch the app:**
   ```bash
   python launch_app.py
   ```
   
   Or run directly:
   ```bash
   python complete_gradio_app.py
   ```

3. **Open your browser** and go to the provided local URL (usually http://localhost:7860)

## πŸ“‹ Requirements

- Python 3.8+
- CUDA-compatible GPU (optional, but recommended)
- At least 8GB RAM
- Internet connection (for model downloads)

## 🎯 Features

- **Two State-of-the-Art Models:**
  - OneFormer: Universal segmentation (semantic, instance, panoptic)
  - Mask2Former: High-accuracy semantic segmentation

- **User-Friendly Interface:**
  - Upload custom images
  - Select from sample images
  - Adjustable overlay transparency
  - Real-time processing

- **Professional Output:**
  - Colored segmentation overlays
  - Detailed class statistics
  - High-quality visualizations

## πŸ”§ Troubleshooting

### Common Issues:

1. **CUDA out of memory:**
   - Reduce image size
   - Use CPU instead of GPU

2. **Model download fails:**
   - Check internet connection
   - Try again (models are large ~1-2GB each)

3. **ImportError:**
   - Run `python setup.py` again
   - Check Python version (3.8+ required)

### Performance Tips:

- First model load takes time (downloading from HuggingFace)
- GPU acceleration significantly speeds up processing
- Images are automatically resized to prevent memory issues

## πŸ“Š Supported Classes

The models are trained on Cityscapes dataset and can recognize:
- Road, sidewalk, building, wall, fence
- Traffic light, traffic sign, pole
- Vegetation, terrain, sky
- Person, rider, car, truck, bus, train, motorcycle, bicycle

## 🎨 Color Coding

Each class is visualized with a specific color following Cityscapes conventions:
- Road: Dark purple
- Sky: Steel blue  
- Person: Crimson
- Car: Dark blue
- Vegetation: Olive green
- And more...

## πŸ“„ License

This project uses pre-trained models from HuggingFace:
- OneFormer: [Model License](https://huggingface.co/shi-labs/oneformer_cityscapes_swin_large)
- Mask2Former: [Model License](https://huggingface.co/facebook/mask2former-swin-large-cityscapes-semantic)

## 🀝 Contributing

Feel free to submit issues and enhancement requests!
'''
    
    with open("README.md", "w") as f:
        f.write(readme_content)
    
    print("βœ… Created README.md")

def main():
    """Main setup function."""
    print("🎯 Semantic Segmentation App Setup")
    print("=" * 50)
    
    # Check Python version
    check_python_version()
    
    # Create directory structure
    create_directory_structure()
    
    # Install dependencies
    if not install_dependencies():
        print("\n❌ Failed to install some dependencies. Please check the errors above.")
        return False
    
    # Download sample images
    try:
        download_sample_images()
    except Exception as e:
        print(f"⚠️ Warning: Could not download sample images: {e}")
    
    # Create launch script
    create_launch_script()
    
    # Create README
    create_readme()
    
    print("\n" + "=" * 50)
    print("βœ… Setup completed successfully!")
    print("\nπŸš€ To launch the app, run:")
    print("   python launch_app.py")
    print("\nπŸ“š For more information, see README.md")
    
    return True

if __name__ == "__main__":
    success = main()
    sys.exit(0 if success else 1)