File size: 6,527 Bytes
bb7dc03
60fb96a
 
 
 
 
 
bb7dc03
60fb96a
c9d0316
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
---
title: ProjectX
emoji: πŸ—οΈ
colorFrom: red
colorTo: gray
sdk: docker
pinned: false
---

# πŸ—οΈ Illegal Construction Detector

> **AI-powered satellite/aerial image analysis for detecting buildings in restricted zones using semantic segmentation.**

[![Python](https://img.shields.io/badge/Python-3.8%2B-blue?logo=python)](https://www.python.org/)
[![PyTorch](https://img.shields.io/badge/PyTorch-2.x-EE4C2C?logo=pytorch)](https://pytorch.org/)
[![Flask](https://img.shields.io/badge/Flask-API-black?logo=flask)](https://flask.palletsprojects.com/)
[![License](https://img.shields.io/badge/License-See%20LICENSE-green)](#license)

---

## πŸ“Œ Overview

This project uses a deep learning segmentation model to automatically identify buildings in aerial or satellite imagery and flag those that fall within user-defined restricted/zoning areas. It is designed for urban planning, land administration, and regulatory compliance workflows.

**Key capabilities:**
- Semantic segmentation of buildings from high-resolution aerial images
- Patch-based inference for large images of arbitrary size
- Zoning mask overlay to classify buildings as **legal** or **illegal**
- REST API backend (Flask) with base64 image response for easy frontend integration
- Visual overlay output highlighting illegal structures in red and legal structures in green

---

## 🧠 Model Architecture

| Component | Detail |
|---|---|
| Architecture | U-Net |
| Encoder | EfficientNet-B3 (ImageNet pretrained) |
| Loss Function | Dice Loss (binary) |
| Optimizer | Adam (lr = 1e-4) |
| LR Scheduler | ReduceLROnPlateau (patience=3, factor=0.5) |
| Input | RGB patches of 256 Γ— 256 px |
| Output | Binary segmentation mask (building / non-building) |

The model was trained on the [Massachusetts Buildings Dataset](https://www.kaggle.com/datasets/balraj98/massachusetts-buildings-dataset) for 30 epochs with augmentations including horizontal/vertical flips, random 90Β° rotation, and brightness/contrast jitter.

---

## πŸ—‚οΈ Repository Structure

```
β”œβ”€β”€ app.py                  # Flask API server
β”œβ”€β”€ best_model.pth          # Trained model weights
β”œβ”€β”€ requirements.txt        # Python dependencies
β”œβ”€β”€ sourcecode.ipynb        # Model training notebook (Kaggle)
β”œβ”€β”€ inference_script.ipynb  # Standalone inference & visualization notebook
β”œβ”€β”€ static/
β”‚   └── index.html          # Frontend UI (served by Flask)
└── README.md
```

---

## βš™οΈ Installation

### Prerequisites
- Python 3.8+
- CUDA-compatible GPU *(optional but recommended)*

### Steps

```bash
# 1. Clone the repository
git clone https://github.com/your-username/illegal-construction-detector.git
cd illegal-construction-detector

# 2. Create and activate a virtual environment
python -m venv venv
source venv/bin/activate      # Windows: venv\Scripts\activate

# 3. Install dependencies
pip install -r requirements.txt

# 4. Place the model weights in the project root
#    Ensure best_model.pth is present (download from Releases or train your own)
```

---

## πŸš€ Usage

### Option 1 β€” Flask Web API

```bash
python app.py
```

The server starts at `http://localhost:5000`. Navigate there in your browser to use the UI, or send requests directly:

```bash
curl -X POST http://localhost:5000/predict \
  -F "image=@/path/to/aerial_image.png"
```

**Response JSON:**

```json
{
  "verdict": "ILLEGAL CONSTRUCTION DETECTED",
  "illegal_count": 3,
  "legal_count": 12,
  "total_count": 15,
  "illegal_percent": 8.42,
  "device": "cuda",
  "original": "<base64_png>",
  "mask": "<base64_png>",
  "overlay": "<base64_png>"
}
```

### Option 2 β€” Jupyter Notebook Inference

Open `inference_script.ipynb` and update the following variables:

```python
MODEL_PATH = "best_model.pth"    # path to weights
IMAGE_PATH = "test_image.png"    # path to your aerial image
```

Run all cells to generate the building mask, zoning overlay, and illegal building highlights.

---

## πŸ—ΊοΈ Zoning Configuration

By default, the right half of any image is treated as a restricted zone. To customise this, modify the `create_zoning_mask` function in `app.py` or the inference notebook:

```python
def create_zoning_mask(shape):
    h, w = shape
    zoning = np.zeros((h, w), dtype=np.uint8)
    # Example: mark a rectangular region as restricted
    zoning[100:400, 200:600] = 1
    return zoning
```

Any building whose pixels overlap with the restricted zone (`zoning == 1`) is classified as **illegal**.

---

## πŸ‹οΈ Training

The full training pipeline is available in `sourcecode.ipynb`, designed to run on Kaggle with GPU acceleration.

**Dataset:** [Massachusetts Buildings Dataset](https://www.kaggle.com/datasets/balraj98/massachusetts-buildings-dataset)

**Training configuration:**

```python
PATCH_SIZE  = 256
BATCH_SIZE  = 8
EPOCHS      = 30
LEARNING_RATE = 1e-4
```

**Augmentations:** Horizontal flip, vertical flip, random 90Β° rotation, brightness/contrast jitter

**Metrics tracked:** Dice Loss Β· IoU Score

To retrain, update `BASE_PATH` in the notebook to your dataset location and run all cells. The best-performing checkpoint (highest IoU) is saved as `best_model.pth`.

---

## πŸ“Š Results

| Metric | Value |
|---|---|
| Validation IoU | Tracked per epoch |
| Inference mode | Patch-based (256Γ—256), supports arbitrary image sizes |
| Supported formats | PNG, JPG, TIFF |

---

## πŸ”§ Dependencies

```
flask
torch
torchvision
segmentation-models-pytorch
albumentations
opencv-python
pillow
numpy
```

Install all with:
```bash
pip install -r requirements.txt
```

> **Note for Windows users:** If you encounter a `fbgemm.dll` error when importing PyTorch, ensure you have the [Microsoft Visual C++ Redistributable](https://aka.ms/vs/17/release/vc_redist.x64.exe) installed.

---

## 🀝 Contributing

Contributions are welcome. Please open an issue first to discuss proposed changes, then submit a pull request.

1. Fork the repository
2. Create a feature branch (`git checkout -b feature/your-feature`)
3. Commit your changes (`git commit -m 'Add your feature'`)
4. Push and open a Pull Request

---

## πŸ“„ License

See the [LICENSE](LICENSE) file for details.

---

## πŸ™ Acknowledgements

- [Massachusetts Buildings Dataset](https://www.kaggle.com/datasets/balraj98/massachusetts-buildings-dataset) by Balraj Ashwath
- [segmentation-models-pytorch](https://github.com/qubvel/segmentation_models.pytorch) by Pavel Iakubovskii
- [Albumentations](https://albumentations.ai/) augmentation library