File size: 5,674 Bytes
a97aeb0
f83b3ae
 
 
a97aeb0
 
f83b3ae
a97aeb0
 
 
f83b3ae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
---
title: 3D Trajectory Tracker
emoji: 🎯
colorFrom: blue
colorTo: purple
sdk: docker
app_file: app.py
pinned: false
---

# 3D Trajectory Tracker

A web application that tracks objects in videos and visualizes their movement in 3D space using computer vision and Three.js.

## Features

- πŸŽ₯ Video upload and processing
- 🎯 Automatic object detection and tracking
- πŸ“Š Real-time trajectory visualization in 3D
- 🎬 Playback controls for trajectory animation
- πŸ“ˆ Statistics and analytics dashboard

## Technology Stack

- **Backend**: FastAPI, OpenCV, NumPy
- **Frontend**: Vanilla JavaScript, Three.js
- **Computer Vision**: Background subtraction for object detection

## Project Structure

```
trajectory-tracker/
β”œβ”€β”€ backend/
β”‚   β”œβ”€β”€ app.py              # FastAPI main application
β”‚   β”œβ”€β”€ requirements.txt    # Python dependencies
β”‚   β”œβ”€β”€ tracker.py         # Object tracking logic
β”‚   └── utils.py           # Helper functions
β”œβ”€β”€ frontend/
β”‚   β”œβ”€β”€ index.html         # Main HTML file
β”‚   β”œβ”€β”€ app.js             # Frontend logic
β”‚   β”œβ”€β”€ styles.css         # Styling
β”‚   └── visualizer.js      # 3D visualization with Three.js
β”œβ”€β”€ uploads/                # Temporary video uploads (auto-created)
β”œβ”€β”€ outputs/                # Processed trajectory data (auto-created)
└── README.md              # Documentation
```

## Installation & Setup

### Local Development (VS Code)

1. **Clone or create the project structure**
   ```bash
   mkdir trajectory-tracker
   cd trajectory-tracker
   ```

2. **Set up backend**
   ```bash
   # Create directories
   mkdir backend frontend uploads outputs
   
   # Install Python dependencies
   pip install -r backend/requirements.txt
   ```

3. **Run the application**
   ```bash
   # From the project root directory
   python backend/app.py
   ```

4. **Access the application**
   - Open browser to: `http://localhost:7860`

### Hugging Face Spaces Deployment

1. **Create a new Space**
   - Go to https://huggingface.co/spaces
   - Click "Create new Space"
   - Choose "Docker" as SDK

2. **Create Dockerfile in project root**
   ```dockerfile
   FROM python:3.9

   WORKDIR /app

   # Install system dependencies
   RUN apt-get update && apt-get install -y \
       libgl1-mesa-glx \
       libglib2.0-0 \
       && rm -rf /var/lib/apt/lists/*

   # Copy requirements and install
   COPY backend/requirements.txt .
   RUN pip install --no-cache-dir -r requirements.txt

   # Copy application files
   COPY backend/ ./backend/
   COPY frontend/ ./frontend/

   # Create directories
   RUN mkdir -p uploads outputs

   # Expose port
   EXPOSE 7860

   # Run the application
   CMD ["python", "backend/app.py"]
   ```

3. **Push to Hugging Face**
   ```bash
   git init
   git add .
   git commit -m "Initial commit"
   git remote add origin https://huggingface.co/spaces/YOUR_USERNAME/YOUR_SPACE_NAME
   git push -u origin main
   ```

## Usage

1. **Upload Video**
   - Click the upload box or drag and drop a video file
   - Supported formats: MP4, AVI, MOV, MKV

2. **Process Video**
   - Click "Process Video" button
   - Wait for processing to complete (may take a few moments)

3. **View Results**
   - See tracking statistics (objects tracked, frames, FPS, duration)
   - View 3D visualization of trajectories
   - Use playback controls:
     - β–Ά Play/Pause: Animate the trajectories
     - ↻ Reset: Return to start
     - Show Trails: Toggle trajectory lines

4. **Interact with 3D View**
   - Click and drag to rotate camera
   - Scroll to zoom in/out

## Configuration

### Detection Methods

In `tracker.py`, you can choose detection methods:

```python
# Background subtraction (default)
tracker = VideoTracker('video.mp4', detection_method='background')

# Color-based detection
tracker = VideoTracker('video.mp4', detection_method='color')
```

### Tracking Parameters

Adjust in `tracker.py`:
- `area > 500`: Minimum object size (pixels)
- `max_distance=50`: Maximum movement between frames (pixels)
- `len(points) > 5`: Minimum trajectory length (frames)

## API Endpoints

- `GET /` - Main application interface
- `POST /api/upload` - Upload and process video
- `GET /api/trajectories/{filename}` - Get trajectory data
- `GET /api/list` - List all processed files
- `DELETE /api/clear` - Clear all data

## Troubleshooting

### Video Processing Fails
- Ensure video format is supported
- Check video file is not corrupted
- Verify OpenCV can read the codec

### No Objects Detected
- Adjust detection parameters in `tracker.py`
- Try different detection methods
- Ensure objects have sufficient movement/contrast

### 3D Visualization Issues
- Check browser console for JavaScript errors
- Ensure Three.js CDN is accessible
- Try refreshing the page

## Development

### Add New Features

1. **Custom Detection Methods**: Add to `tracker.py`
2. **UI Enhancements**: Modify `frontend/` files
3. **Export Options**: Add endpoints in `app.py`

### Testing

```bash
# Test with sample video
python backend/app.py

# Access http://localhost:7860 and upload test video
```

## Performance Tips

- Use smaller videos for faster processing
- Reduce video resolution before upload
- Adjust detection parameters for accuracy vs. speed

## License

MIT License

## Contributing

Pull requests welcome! Please ensure:
- Code follows existing style
- Add tests for new features
- Update documentation

## Support

For issues and questions:
- Open an issue on the repository
- Check existing issues for solutions

## Credits

- FastAPI for backend framework
- Three.js for 3D visualization
- OpenCV for computer vision