File size: 10,118 Bytes
42ac402
 
 
 
 
 
 
9361ede
 
42ac402
 
 
 
d325ede
 
 
 
 
 
 
 
 
92f4a87
d325ede
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92f4a87
 
d325ede
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
284
285
286
287
288
289
---
title: TwoTh
emoji: "πŸ—Ώ"
colorFrom: blue
colorTo: indigo
sdk: gradio
sdk_version: "5.33.0"
python_version: "3.10"
suggested_hardware: "t4-small"
app_file: app.py
pinned: false
---

# TwoTh - Multi-View 3D Object Reconstruction Web App

A production-grade, visually impressive full-stack web application that allows users to upload photos of physical objects and generates interactive 3D models viewable in the browser.

![Tech Stack](https://img.shields.io/badge/React-18.2-blue)
![Tech Stack](https://img.shields.io/badge/TypeScript-5.2-blue)
![Tech Stack](https://img.shields.io/badge/FastAPI-python-green)
![Tech Stack](https://img.shields.io/badge/Three.js-r160-blue)

---

## πŸ”— Architecture

```
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                              Browser                                 β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”‚
β”‚  β”‚  UploadZone │───▢│ ImagePreview │───▢│   ModelViewer3D     β”‚    β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β”‚
β”‚         β”‚                β”‚                        β”‚                  β”‚
β”‚         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                  β”‚
β”‚                          β–Ό                                            β”‚
β”‚                   React + Three.js                                    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                              β”‚ HTTP / SSE
                              β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                         FastAPI Backend                               β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”‚
β”‚  β”‚   Upload   │───▢│  Reconstruct    │───▢│     Download      β”‚    β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β”‚
β”‚         β”‚                   β”‚                      β”‚                 β”‚
β”‚         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                 β”‚
β”‚                             β–Ό                                          β”‚
β”‚                    COLMAP Pipeline                                     β”‚
β”‚                 (SfM + MVS + Meshing)                                  β”‚
β”‚                             β”‚                                          β”‚
β”‚                             β–Ό                                          β”‚
β”‚                    Open3D / trimesh                                    β”‚
β”‚                         β”‚                                              β”‚
β”‚                         β–Ό                                              β”‚
β”‚                    .GLB Output                                         β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
```

---

## 🎯 Features

### Upload Experience
- Drag-and-drop zone accepting 6–30 images simultaneously
- Real-time thumbnail previews with angle labels
- Client-side validation (JPG/PNG only, max 10MB each)
- Animated upload progress bar

### 3D Viewer
- Load and display .GLB models with auto-centering and auto-scaling
- OrbitControls: rotate, zoom, pan
- Toggle wireframe mode
- 3 lighting presets (Studio, Outdoor, Dramatic)
- Download model as .GLB or .OBJ
- Screenshot functionality
- Auto-rotate (turntable) mode

### Visual Design
- Dark, modern UI with neon accent colors
- Smooth Framer Motion transitions
- Animated processing screen with rotating 3D cube
- Glassmorphism panels
- Fully responsive

---

## πŸ› οΈ Prerequisites

- **Docker** & Docker Compose
- **Node.js** 18+ (for local frontend development)
- **Python** 3.11+ (for local backend development)
- **COLMAP** (installed in backend container)

---

## πŸš€ Quick Start

### Development (Docker Compose)

```bash
# Clone and navigate to project
cd TwoTh

# Start all services
docker-compose up --build

# Access at:
# Frontend: http://localhost:5173
# Backend API: http://localhost:8000
# API Docs: http://localhost:8000/docs
```

### Manual Setup

#### Backend

```bash
cd backend

# Create virtual environment
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate

# Install dependencies
pip install -r requirements.txt

# Set environment variables (see .env.example)
export COLMAP_PATH=colmap
export SIMULATION_MODE=false
export UPLOAD_DIR=./uploads
export OUTPUT_DIR=./outputs

# Run server
uvicorn backend.main:app --reload
```

#### Frontend

```bash
cd frontend

# Install dependencies
npm install

# Create .env.local (create from .env.local)
echo "VITE_API_BASE=http://localhost:8000" > .env.local

# Run development server
npm run dev
```

---

## πŸ“š API Documentation

### Endpoints

| Method | Path | Description |
|--------|------|-------------|
| POST | `/api/upload` | Upload 6-30 images |
| POST | `/api/reconstruct/{job_id}` | Start reconstruction |
| GET | `/api/status/{job_id}` | SSE status stream |
| GET | `/api/model/{job_id}` | Download GLB model |
| GET | `/api/model/{job_id}/obj` | Download OBJ model |
| GET | `/api/stats/{job_id}` | Get model statistics |
| GET | `/api/health` | Health check |

### Example Usage

#### Upload Images
```bash
curl -X POST http://localhost:8000/api/upload \
  -F "images=@photo1.jpg" \
  -F "images=@photo2.jpg" \
  # ... add 6+ images
```

**Response:**
```json
{
  "job_id": "550e8400-e29b-41d4-a716-446655440000",
  "image_count": 8,
  "images": ["photo1.jpg", "photo2.jpg", ...]
}
```

#### Start Reconstruction
```bash
curl -X POST http://localhost:8000/api/reconstruct/550e8400-e29b-41d4-a716-446655440000
```

**Response:**
```json
{
  "message": "Reconstruction started",
  "job_id": "550e8400-e29b-41d4-a716-446655440000"
}
```

#### Status Stream (SSE)
```javascript
const eventSource = new EventSource('http://localhost:8000/api/status/{job_id}');
eventSource.onmessage = (event) => {
  const data = JSON.parse(event.data);
  console.log(data); // { step: "extracting", progress: 20, message: "Extracting features..." }
};
```

---

## πŸ”¬ How It Works

### Structure from Motion (SfM)
SfM finds matching keypoints across multiple photos and estimates camera positions:
1. **Feature Extraction**: Identifies corners/edges in each image
2. **Matching**: Finds common features between image pairs
3. **Sparse Reconstruction**: Builds a 3D point cloud with camera poses

### Multi-View Stereo (MVS)
MVS generates dense 3D geometry from SfM results:
1. **Undistortion**: Corrects lens distortion
2. **Depth Estimation**: Computes depth for each pixel
3. **Stereo Fusion**: Merges depth maps into a dense point cloud

### Mesh Generation
1. **Poisson Surface Reconstruction**: Creates a watertight mesh
2. **Format Conversion**: Converts PLY to GLB for browser rendering

---

## πŸ’‘ Tips for Best Results

1. **Image Count**: Use 8-12 images for optimal reconstruction
2. **Coverage**: Capture images in a circle around the object (every 15-20Β°)
3. **Overlap**: Ensure >70% overlap between consecutive shots
4. **Lighting**: Use consistent, diffuse lighting (avoid harsh shadows)
5. **Avoid**: Reflective surfaces, transparent objects, textureless areas
6. **Stability**: Keep the object stationary during capture

---

## ⚠️ Known Limitations

1. **CPU Performance**: Dense reconstruction on CPU is slow (10-30 mins)
2. **GPU Required**: For faster results, enable GPU in docker-compose
3. **Object Size**: Works best for objects that fit on a table
4. **Complex Surfaces**: May struggle with highly reflective materials

## 🩺 Troubleshooting

1. **Output always looks like a random spiky blob/cube**: Ensure `SIMULATION_MODE=false` in backend env (`docker-compose.yml` or `.env`).
2. **Reconstruction fails quickly with COLMAP errors**: Verify COLMAP is installed and `COLMAP_PATH` points to a valid executable.
3. **"No images with matches" / "No good initial image pair"**: Your images likely have too little overlap or too many near-duplicates. Use 8-12 distinct views around the object with 60-80% overlap between adjacent shots.
4. **Upload accepted files but reconstruction still weak**: Avoid repeating the same frame; include all sides and keep focus sharp with consistent lighting.

---

## 🧰 Tech Stack

### Frontend
- React 18 + Vite + TypeScript
- Tailwind CSS (dark theme)
- React Three Fiber + Drei
- Framer Motion

### Backend
- Python 3.11 + FastAPI
- COLMAP (SfM + MVS)
- Open3D + trimesh (mesh processing)

### Infrastructure
- Docker + Docker Compose
- Server-Sent Events (SSE)

---

## πŸ“„ License

MIT License - College Project

---

## πŸ™ Acknowledgments

- [COLMAP](https://colmap.github.io/) - Structure-from-Motion library
- [Three.js](https://threejs.org/) - 3D graphics library
- [React Three Fiber](https://docs.pmnd.rs/react-three-fiber) - React renderer for Three.js
READMEOF