File size: 11,403 Bytes
a4c7d4c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fa0041e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a4c7d4c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fa0041e
a4c7d4c
 
 
 
 
 
 
 
 
 
fa0041e
 
 
 
a4c7d4c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
"""
Vehicle Speed Estimation and Counting - Gradio Interface
=========================================================

A real-time vehicle detection, tracking, counting, and speed estimation system
using YOLO object detection and perspective transformation techniques.

Authors:
    - Abhay Gupta (0205CC221005)
    - Aditi Lakhera (0205CC221011)
    - Balraj Patel (0205CC221049)
    - Bhumika Patel (0205CC221050)

This application provides an interactive web interface for analyzing traffic videos
and estimating vehicle speeds using computer vision techniques.
"""

import os
import sys
import tempfile
import logging
from pathlib import Path
from typing import Optional, Tuple

import gradio as gr
import cv2
import numpy as np

# Configure logging
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)

# Try to import spaces for ZeroGPU support (with automatic CPU fallback)
try:
    import spaces
    ZEROGPU_AVAILABLE = True
    logger.info("βœ… ZeroGPU support enabled - GPU acceleration available")
except ImportError:
    ZEROGPU_AVAILABLE = False
    logger.info("ℹ️ ZeroGPU not available - will use CPU (slower but functional)")
    # Create a dummy decorator for compatibility
    class spaces:
        @staticmethod
        def GPU(func):
            """Dummy decorator when ZeroGPU is not available"""
            return func

# Import application modules
try:
    from main import process_video
    from config import VehicleDetectionConfig
except ImportError as e:
    logger.error(f"Failed to import required modules: {e}")
    raise


def validate_video_file(video_path: str) -> Tuple[bool, str]:
    """
    Validate uploaded video file.
    
    Args:
        video_path: Path to video file
        
    Returns:
        Tuple of (is_valid, error_message)
    """
    if not video_path:
        return False, "No video file provided"
    
    if not os.path.exists(video_path):
        return False, f"Video file not found: {video_path}"
    
    # Check file size (limit to 100MB for HF Spaces)
    file_size_mb = os.path.getsize(video_path) / (1024 * 1024)
    if file_size_mb > 100:
        return False, f"Video file too large ({file_size_mb:.1f}MB). Maximum size is 100MB"
    
    # Validate video can be opened
    try:
        cap = cv2.VideoCapture(video_path)
        if not cap.isOpened():
            return False, "Unable to open video file. Please ensure it's a valid video format"
        
        # Check if video has frames
        ret, _ = cap.read()
        cap.release()
        
        if not ret:
            return False, "Video file appears to be empty or corrupted"
            
        return True, ""
    except Exception as e:
        return False, f"Error validating video: {str(e)}"


@spaces.GPU  # Enable ZeroGPU acceleration (automatic CPU fallback if unavailable)
def estimate_vehicle_speed(
    video_file: str,
    model_choice: str,
    line_position: int,
    confidence_threshold: float,
    progress=gr.Progress()
) -> Tuple[Optional[str], str]:
    """
    Process video and estimate vehicle speeds.
    
    This function is decorated with @spaces.GPU to enable automatic GPU
    acceleration when running on ZeroGPU Spaces. If GPU is not available,
    it automatically falls back to CPU processing.
    
    Args:
        video_file: Path to uploaded video
        model_choice: YOLO model selection
        line_position: Y-coordinate for counting line
        confidence_threshold: Detection confidence threshold
        progress: Gradio progress tracker
        
    Returns:
        Tuple of (output_video_path, statistics_text)
    """
    try:
        # Validate input
        progress(0, desc="Validating video file...")
        is_valid, error_msg = validate_video_file(video_file)
        if not is_valid:
            logger.error(f"Video validation failed: {error_msg}")
            return None, f"❌ Error: {error_msg}"
        
        # Create temporary output file
        output_path = tempfile.mktemp(suffix='.mp4')
        
        # Configure processing
        progress(0.1, desc="Configuring detection parameters...")
        config = VehicleDetectionConfig(
            input_video=video_file,
            output_video=output_path,
            model_name=model_choice,
            line_y=line_position,
            confidence_threshold=confidence_threshold
        )
        
        # Process video
        progress(0.2, desc="Processing video (this may take a few minutes)...")
        logger.info(f"Starting video processing: {video_file}")
        
        try:
            stats = process_video(
                config=config,
                progress_callback=lambda p: progress(0.2 + p * 0.7, desc=f"Processing... {int(p*100)}%")
            )
            
            progress(0.95, desc="Finalizing output...")
            
            # Format statistics
            stats_text = f"""
## πŸ“Š Processing Results

### Vehicle Count Statistics
- **Total Vehicles Detected:** {stats['total_count']}
- **Vehicles Entering (In):** {stats['in_count']}
- **Vehicles Exiting (Out):** {stats['out_count']}

### Speed Analysis
- **Average Speed:** {stats['avg_speed']:.1f} km/h
- **Maximum Speed:** {stats['max_speed']:.1f} km/h
- **Minimum Speed:** {stats['min_speed']:.1f} km/h

### Processing Information
- **Frames Processed:** {stats['frames_processed']}
- **Processing Time:** {stats['processing_time']:.2f} seconds
- **Model Used:** {model_choice}
- **Detection Confidence:** {confidence_threshold:.2f}

βœ… **Processing completed successfully!**
"""
            
            progress(1.0, desc="Complete!")
            logger.info("Video processing completed successfully")
            return output_path, stats_text
            
        except Exception as e:
            logger.error(f"Error during video processing: {e}", exc_info=True)
            return None, f"❌ **Processing Error:** {str(e)}\n\nPlease try with different settings or a different video."
    
    except Exception as e:
        logger.error(f"Unexpected error in estimate_vehicle_speed: {e}", exc_info=True)
        return None, f"❌ **Unexpected Error:** {str(e)}"


def create_demo_interface() -> gr.Blocks:
    """
    Create Gradio interface for vehicle speed estimation.
    
    Returns:
        Gradio Blocks interface
    """
    with gr.Blocks(
        title="Vehicle Speed Estimation & Counting"
    ) as demo:
        
        gr.Markdown("""
# πŸš— Vehicle Speed Estimation & Counting System

An intelligent traffic analysis system that detects, tracks, counts, and estimates the speed of vehicles in video footage using advanced computer vision techniques.

### 🎯 Features
- **Real-time Vehicle Detection** using YOLO
- **Multi-Object Tracking** with ByteTrack
- **Accurate Speed Estimation** via perspective transformation
- **Vehicle Counting** with configurable detection zones

### πŸ‘₯ Developed By
- **Abhay Gupta** (0205CC221005)
- **Aditi Lakhera** (0205CC221011)
- **Balraj Patel** (0205CC221049)
- **Bhumika Patel** (0205CC221050)

---
""")
        
        with gr.Row():
            with gr.Column(scale=1):
                gr.Markdown("### πŸ“€ Input Configuration")
                
                video_input = gr.Video(
                    label="Upload Traffic Video",
                    format="mp4"
                )
                
                with gr.Accordion("βš™οΈ Advanced Settings", open=False):
                    model_choice = gr.Dropdown(
                        choices=["yolov8n", "yolov8s", "yolov8m", "yolov8l"],
                        value="yolov8n",
                        label="YOLO Model",
                        info="Larger models are more accurate but slower"
                    )
                    
                    line_position = gr.Slider(
                        minimum=100,
                        maximum=1000,
                        value=480,
                        step=10,
                        label="Counting Line Position (Y-coordinate)",
                        info="Vertical position of the vehicle counting line"
                    )
                    
                    confidence_threshold = gr.Slider(
                        minimum=0.1,
                        maximum=0.9,
                        value=0.3,
                        step=0.05,
                        label="Detection Confidence Threshold",
                        info="Higher values reduce false positives"
                    )
                
                process_btn = gr.Button(
                    "πŸš€ Process Video",
                    variant="primary",
                    size="lg"
                )
                
                gr.Markdown("""
### πŸ“‹ Instructions
1. Upload a traffic video (MP4 format, max 100MB)
2. Adjust settings if needed (optional)
3. Click "Process Video" and wait for results
4. Download the annotated video with speed estimates

### πŸ’‘ Tips
- Use videos with clear vehicle visibility
- Ensure consistent camera angle
- Better lighting improves detection accuracy
""")
            
            with gr.Column(scale=1):
                gr.Markdown("### πŸ“₯ Output Results")
                
                video_output = gr.Video(
                    label="Processed Video with Annotations"
                )
                
                stats_output = gr.Markdown(
                    label="Statistics",
                    value="*Processing results will appear here...*"
                )
        
        # Example videos section
        gr.Markdown("""
---
### 🎬 Example Videos
Upload your own traffic video or use sample footage to test the system.
""")
        
        gr.Examples(
            examples=[
                ["./data/vehicles.mp4", "yolov8n", 480, 0.3],
            ],
            inputs=[video_input, model_choice, line_position, confidence_threshold],
            outputs=[video_output, stats_output],
            fn=estimate_vehicle_speed,
            cache_examples=False,
            label="Sample Videos"
        )
        
        # Connect processing function
        process_btn.click(
            fn=estimate_vehicle_speed,
            inputs=[video_input, model_choice, line_position, confidence_threshold],
            outputs=[video_output, stats_output]
        )
        
        gr.Markdown("""
---
### πŸ”¬ Technical Details

This system uses:
- **YOLO (You Only Look Once)** for real-time object detection
- **ByteTrack** for multi-object tracking across frames
- **Perspective Transformation** for accurate speed calculation
- **OpenCV** for video processing and computer vision operations

### πŸ“š References
- [Ultralytics YOLO](https://github.com/ultralytics/ultralytics)
- [Supervision Library](https://github.com/roboflow/supervision)
- [OpenCV](https://opencv.org/)

### πŸ“„ License
MIT License - See LICENSE file for details
""")
    
    return demo


if __name__ == "__main__":
    try:
        logger.info("Starting Vehicle Speed Estimation application...")
        demo = create_demo_interface()
        demo.launch(
            server_name="0.0.0.0",
            server_port=7860,
            share=False
        )
    except Exception as e:
        logger.error(f"Failed to launch application: {e}", exc_info=True)
        sys.exit(1)