File size: 19,861 Bytes
ba8a76b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3e1a14e
ba8a76b
 
83f8543
3e1a14e
 
 
 
 
 
 
 
83f8543
 
 
 
 
 
 
ba8a76b
83f8543
 
ba8a76b
 
83f8543
ba8a76b
 
 
3e1a14e
 
 
ba8a76b
 
83f8543
ba8a76b
 
3e1a14e
 
ba8a76b
 
3e1a14e
 
 
 
ba8a76b
 
3e1a14e
 
ba8a76b
 
 
3e1a14e
ba8a76b
 
3e1a14e
 
ba8a76b
 
3e1a14e
ba8a76b
 
3e1a14e
ba8a76b
 
3e1a14e
ba8a76b
 
3e1a14e
 
ba8a76b
 
 
3e1a14e
 
 
 
 
 
ba8a76b
 
3e1a14e
ba8a76b
3e1a14e
 
 
ba8a76b
 
3e1a14e
83f8543
 
3e1a14e
 
 
 
83f8543
 
3e1a14e
83f8543
 
3e1a14e
 
 
 
83f8543
 
3e1a14e
83f8543
 
3e1a14e
83f8543
 
3e1a14e
83f8543
 
3e1a14e
83f8543
 
3e1a14e
 
 
 
 
83f8543
 
3e1a14e
 
 
83f8543
 
3e1a14e
 
 
83f8543
 
3e1a14e
 
 
83f8543
 
3e1a14e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ba8a76b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3e1a14e
 
 
 
 
 
ba8a76b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3e1a14e
ba8a76b
3e1a14e
ba8a76b
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
import streamlit as st
from streamlit_option_menu import option_menu
import cv2
import numpy as np
import tempfile
from PIL import Image
import time
import av
from streamlit_webrtc import webrtc_streamer, VideoProcessorBase, RTCConfiguration
import os
import requests

# Set page config
st.set_page_config(
    page_title="Real-Time Object Detection & Tracking",
    page_icon="πŸš—",
    layout="wide",
    initial_sidebar_state="expanded"
)

# Beautiful custom CSS for styling
st.markdown("""
    <style>
        :root {
            --primary-color: #6366f1;
            --secondary-color: #4f46e5;
            --background-color: #111827;
            --text-color: #f3f4f6;
            --card-color: #1f2937;
            --accent-color: #10b981;
            --warning-color: #f59e0b;
            --error-color: #ef4444;
        }
        
        body {
            color: var(--text-color);
            background-color: var(--background-color);
        }
        
        .stApp {
            background-color: var(--background-color);
            color: var(--text-color);
        }
        .header {
            color: var(--primary-color);
            font-size: 2.5rem;
            font-weight: bold;
            margin-bottom: 1rem;
            background: linear-gradient(90deg, var(--primary-color), var(--accent-color));
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
        }
        .subheader {
            color: var(--primary-color);
            font-size: 1.5rem;
            margin-bottom: 1rem;
            border-bottom: 2px solid var(--secondary-color);
            padding-bottom: 0.5rem;
        }
        .stButton>button {
            background: linear-gradient(135deg, var(--primary-color), var(--secondary-color));
            color: white;
            border-radius: 8px;
            padding: 0.5rem 1.5rem;
            border: none;
            transition: all 0.3s;
            font-weight: 500;
            box-shadow: 0 2px 5px rgba(0,0,0,0.2);
        }
        .stButton>button:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 10px rgba(0,0,0,0.3);
        }
        .css-1aumxhk {
            background-color: var(--card-color);
            border-radius: 12px;
            padding: 2rem;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
            border-left: 4px solid var(--primary-color);
        }
        .model-card {
            border-radius: 12px;
            padding: 1.5rem;
            margin-bottom: 1rem;
            background-color: var(--card-color);
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
            transition: all 0.3s;
            border-left: 4px solid var(--accent-color);
            height: 100%;
        }
        .model-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 8px 15px rgba(0, 0, 0, 0.2);
            border-left: 4px solid var(--primary-color);
        }
        .model-card h3 {
            color: var(--primary-color);
            margin-top: 0;
        }
        .stSelectbox>div>div>select {
            border-radius: 8px;
            padding: 0.5rem;
            background-color: var(--card-color);
            color: var(--text-color);
            border: 1px solid var(--secondary-color);
        }
        .stSlider>div>div>div>div {
            background: linear-gradient(90deg, var(--primary-color), var(--accent-color));
        }
        .stTextInput>div>div>input {
            color: var(--text-color);
            background-color: var(--card-color);
            border-radius: 8px;
            border: 1px solid var(--secondary-color);
        }
        .stMarkdown {
            color: var(--text-color);
        }
        .stAlert {
            background-color: var(--card-color);
            color: var(--text-color);
            border-radius: 8px;
            border-left: 4px solid var(--accent-color);
        }
        .stProgress>div>div>div>div {
            background: linear-gradient(90deg, var(--primary-color), var(--accent-color));
        }
        .stCheckbox>label {
            color: var(--text-color);
        }
        .stRadio>label {
            color: var(--text-color);
        }
        .stFileUploader>label {
            color: var(--text-color);
        }
        .stMetric {
            color: var(--text-color);
            background-color: var(--card-color);
            border-radius: 8px;
            padding: 1rem;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }
        .stInfo {
            background-color: rgba(59, 130, 246, 0.1);
            color: var(--text-color);
            border-left: 4px solid #3b82f6;
        }
        .stSuccess {
            background-color: rgba(16, 185, 129, 0.1);
            color: var(--text-color);
            border-left: 4px solid var(--accent-color);
        }
        .stWarning {
            background-color: rgba(245, 158, 11, 0.1);
            color: var(--text-color);
            border-left: 4px solid var(--warning-color);
        }
        .stError {
            background-color: rgba(239, 68, 68, 0.1);
            color: var(--text-color);
            border-left: 4px solid var(--error-color);
        }
        .stTabs [data-baseweb="tab-list"] {
            gap: 8px;
        }
        .stTabs [data-baseweb="tab"] {
            background-color: var(--card-color);
            border-radius: 8px 8px 0 0;
            padding: 0.5rem 1.5rem;
            transition: all 0.3s;
        }
        .stTabs [aria-selected="true"] {
            background-color: var(--primary-color);
            color: white;
        }
        footer {
            color: #9ca3af;
            font-size: 0.9rem;
            text-align: center;
            padding: 1rem;
            margin-top: 2rem;
            border-top: 1px solid #374151;
        }
    </style>
""", unsafe_allow_html=True)

# App header
col1, col2 = st.columns([1, 3])
with col1:
    st.image("https://huggingface.co/front/assets/huggingface_logo-noborder.svg", width=100)
with col2:
    st.markdown('<div class="header">Real-Time Object Detection & Tracking</div>', unsafe_allow_html=True)
    st.markdown("Advanced computer vision for autonomous vehicles and surveillance systems")

# Navigation menu
with st.sidebar:
    selected = option_menu(
        menu_title="Main Menu",
        options=["Home", "Live Detection", "Video Processing", "Model Zoo", "Settings", "About"],
        icons=["house", "camera-video", "film", "boxes", "gear", "info-circle"],
        menu_icon="cast",
        default_index=0,
        styles={
            "container": {"padding": "0!important", "background-color": "#1f2937"},
            "icon": {"color": "#6366f1", "font-size": "18px"}, 
            "nav-link": {"font-size": "16px", "text-align": "left", "margin":"0px", "--hover-color": "#374151"},
            "nav-link-selected": {"background-color": "#6366f1"},
        }
    )

# Object detection class (simplified for demo)
class ObjectDetector:
    def __init__(self, model_type="yolov5"):
        self.model_type = model_type
        self.classes = ["person", "car", "truck", "bicycle", "motorcycle", "bus"]
        self.colors = np.random.uniform(0, 255, size=(len(self.classes), 3))
        
    def detect(self, image):
        # Convert image to numpy array
        frame = np.array(image)
        
        # Simulate detection by adding random bounding boxes
        height, width = frame.shape[:2]
        detections = []
        
        for _ in range(np.random.randint(2, 6)):
            class_id = np.random.randint(0, len(self.classes))
            confidence = np.random.uniform(0.7, 0.95)
            
            x = int(np.random.uniform(0, width * 0.8))
            y = int(np.random.uniform(0, height * 0.8))
            w = int(np.random.uniform(width * 0.1, width * 0.3))
            h = int(np.random.uniform(height * 0.1, height * 0.3))
            
            detections.append({
                "class_id": class_id,
                "confidence": confidence,
                "box": [x, y, x+w, y+h]
            })
        
        return detections
    
    def draw_detections(self, frame, detections):
        for detection in detections:
            class_id = detection["class_id"]
            confidence = detection["confidence"]
            box = detection["box"]
            
            color = self.colors[class_id]
            label = f"{self.classes[class_id]}: {confidence:.2f}"
            
            cv2.rectangle(frame, (box[0], box[1]), (box[2], box[3]), color, 2)
            cv2.putText(frame, label, (box[0], box[1]-10), 
                        cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
        
        return frame

# Video processor for WebRTC
class VideoProcessor(VideoProcessorBase):
    def __init__(self):
        self.detector = ObjectDetector()
        self.confidence_threshold = 0.5
        self.tracking_enabled = True
    
    def recv(self, frame):
        img = frame.to_ndarray(format="bgr24")
        
        # Perform detection
        detections = self.detector.detect(img)
        
        # Filter by confidence
        detections = [d for d in detections if d["confidence"] >= self.confidence_threshold]
        
        # Draw detections
        img = self.detector.draw_detections(img, detections)
        
        return av.VideoFrame.from_ndarray(img, format="bgr24")

# Home Page
if selected == "Home":
    st.markdown('<div class="subheader">Welcome to Real-Time Object Detection</div>', unsafe_allow_html=True)
    
    col1, col2 = st.columns(2)
    with col1:
        st.markdown("""
            **Advanced computer vision system** for:
            - Autonomous vehicles πŸš—
            - Surveillance systems 🏒
            - Traffic monitoring 🚦
            - Smart cities πŸŒ†
            
            **Features:**
            - Real-time object detection
            - Multi-object tracking
            - Customizable models
            - High-performance inference
        """)
        
        st.button("Get Started β†’", key="home_get_started")
    
    with col2:
        st.image("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers-task-cv-object-detection.png", 
                 caption="Object Detection Example", use_column_width=True)
    
    st.markdown("---")
    st.markdown("### How It Works")
    st.markdown("""
        1. **Select a model** from our Model Zoo or upload your own
        2. **Choose input source** - live camera, video file, or image
        3. **Configure settings** - confidence threshold, tracking options
        4. **Run detection** and view real-time results
    """)

# Live Detection Page
elif selected == "Live Detection":
    st.markdown('<div class="subheader">Live Object Detection</div>', unsafe_allow_html=True)
    
    tab1, tab2 = st.tabs(["Webcam", "RTSP Stream"])
    
    with tab1:
        st.markdown("### Webcam Detection")
        st.info("This will use your device's camera for real-time object detection")
        
        # WebRTC configuration
        RTC_CONFIGURATION = RTCConfiguration(
            {"iceServers": [{"urls": ["stun:stun.l.google.com:19302"]}]}
        )
        
        # Confidence threshold
        confidence_threshold = st.slider(
            "Confidence Threshold", 
            min_value=0.1, 
            max_value=0.9, 
            value=0.5, 
            step=0.05,
            help="Adjust the minimum confidence score for detections"
        )
        
        # Toggle tracking
        tracking_enabled = st.checkbox(
            "Enable Object Tracking", 
            value=True,
            help="Track objects across frames for consistent identification"
        )
        
        # Start WebRTC streamer
        webrtc_ctx = webrtc_streamer(
            key="object-detection",
            video_processor_factory=VideoProcessor,
            rtc_configuration=RTC_CONFIGURATION,
            media_stream_constraints={"video": True, "audio": False},
            async_processing=True,
        )
        
        if webrtc_ctx.video_processor:
            webrtc_ctx.video_processor.confidence_threshold = confidence_threshold
            webrtc_ctx.video_processor.tracking_enabled = tracking_enabled
    
    with tab2:
        st.markdown("### RTSP Stream Detection")
        st.warning("This feature requires an RTSP stream URL (e.g., from an IP camera)")
        
        rtsp_url = st.text_input("Enter RTSP Stream URL", "rtsp://example.com/stream")
        
        if st.button("Connect to Stream"):
            st.warning("RTSP stream processing would be implemented here in a production app")
            st.info(f"Would connect to: {rtsp_url}")

# Video Processing Page
elif selected == "Video Processing":
    st.markdown('<div class="subheader">Video File Processing</div>', unsafe_allow_html=True)
    
    uploaded_file = st.file_uploader(
        "Upload a video file", 
        type=["mp4", "avi", "mov"],
        help="Upload a video file for object detection processing"
    )
    
    if uploaded_file is not None:
        st.success("Video file uploaded successfully!")
        
        # Save uploaded file to temporary location
        tfile = tempfile.NamedTemporaryFile(delete=False) 
        tfile.write(uploaded_file.read())
        
        # Display video info
        video = cv2.VideoCapture(tfile.name)
        width = int(video.get(cv2.CAP_PROP_FRAME_WIDTH))
        height = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT))
        fps = video.get(cv2.CAP_PROP_FPS)
        frame_count = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
        duration = frame_count / fps
        
        col1, col2, col3 = st.columns(3)
        col1.metric("Resolution", f"{width}x{height}")
        col2.metric("FPS", f"{fps:.2f}")
        col3.metric("Duration", f"{duration:.2f} seconds")
        
        # Processing options
        st.markdown("### Processing Options")
        confidence_threshold = st.slider(
            "Confidence Threshold", 
            min_value=0.1, 
            max_value=0.9, 
            value=0.5, 
            step=0.05
        )
        
        tracking_enabled = st.checkbox("Enable Object Tracking", value=True)
        show_fps = st.checkbox("Show FPS Counter", value=True)
        
        # Process video button
        if st.button("Process Video"):
            st.warning("Video processing would be implemented here in a production app")
            
            # Simulate processing with progress bar
            progress_bar = st.progress(0)
            status_text = st.empty()
            
            for i in range(1, 101):
                progress_bar.progress(i)
                status_text.text(f"Processing: {i}% complete")
                time.sleep(0.05)
            
            st.success("Video processing completed!")
            st.balloons()

# Model Zoo Page
elif selected == "Model Zoo":
    st.markdown('<div class="subheader">Model Selection</div>', unsafe_allow_html=True)
    
    st.markdown("""
        Choose from our pre-trained models or upload your own custom model.
        Different models offer different trade-offs between speed and accuracy.
    """)
    
    # Model cards
    col1, col2, col3 = st.columns(3)
    
    with col1:
        st.markdown("""
            <div class="model-card">
                <h3>YOLOv5s</h3>
                <p><b>Type:</b> Object Detection</p>
                <p><b>Speed:</b> ⚑⚑⚑⚑⚑</p>
                <p><b>Accuracy:</b> ⭐⭐⭐</p>
                <p>Ultra-fast detection for real-time applications</p>
            </div>
        """, unsafe_allow_html=True)
        if st.button("Select YOLOv5s", key="yolov5s"):
            st.session_state.selected_model = "yolov5s"
            st.success("YOLOv5s selected!")
    
    with col2:
        st.markdown("""
            <div class="model-card">
                <h3>Faster R-CNN</h3>
                <p><b>Type:</b> Object Detection</p>
                <p><b>Speed:</b> ⚑⚑</p>
                <p><b>Accuracy:</b> ⭐⭐⭐⭐⭐</p>
                <p>High accuracy for critical applications</p>
            </div>
        """, unsafe_allow_html=True)
        if st.button("Select Faster R-CNN", key="frcnn"):
            st.session_state.selected_model = "faster_rcnn"
            st.success("Faster R-CNN selected!")
    
    with col3:
        st.markdown("""
            <div class="model-card">
                <h3>DeepSORT</h3>
                <p><b>Type:</b> Object Tracking</p>
                <p><b>Speed:</b> ⚑⚑⚑</p>
                <p><b>Accuracy:</b> ⭐⭐⭐⭐</p>
                <p>Tracking with deep learning features</p>
            </div>
        """, unsafe_allow_html=True)
        if st.button("Select DeepSORT", key="deepsort"):
            st.session_state.selected_model = "deepsort"
            st.success("DeepSORT selected!")
    
    st.markdown("---")
    st.markdown("### Custom Model Upload")
    
    custom_model = st.file_uploader(
        "Upload your custom model (PyTorch or TensorFlow)", 
        type=["pt", "pth", "h5", "onnx"],
        help="Upload your custom trained model file"
    )
    
    if custom_model is not None:
        st.success("Custom model uploaded successfully!")
        st.info("Model would be loaded and validated here in a production app")

# Settings Page
elif selected == "Settings":
    st.markdown('<div class="subheader">Application Settings</div>', unsafe_allow_html=True)
    
    st.markdown("### Detection Parameters")
    confidence_threshold = st.slider(
        "Default Confidence Threshold", 
        min_value=0.1, 
        max_value=0.9, 
        value=0.5, 
        step=0.05
    )
    
    iou_threshold = st.slider(
        "IOU Threshold (for NMS)", 
        min_value=0.1, 
        max_value=0.9, 
        value=0.45, 
        step=0.05,
        help="Intersection over Union threshold for non-maximum suppression"
    )
    
    st.markdown("### Tracking Parameters")
    max_age = st.slider(
        "Max Track Age (frames)", 
        min_value=1, 
        max_value=100, 
        value=30,
        help="Number of frames to keep a track alive without detection"
    )
    
    min_hits = st.slider(
        "Min Detection Hits", 
        min_value=1, 
        max_value=10, 
        value=3,
        help="Number of detections needed before a track is confirmed"
    )
    
    if st.button("Save Settings"):
        st.success("Settings saved successfully!")

# About Page
elif selected == "About":
    st.markdown('<div class="subheader">About This Project</div>', unsafe_allow_html=True)
    
    st.markdown("""
        **Real-Time Object Detection & Tracking System**
        
        This application demonstrates advanced computer vision capabilities for:
        - Autonomous vehicle perception systems
        - Surveillance and security applications
        - Traffic monitoring and analysis
        - Smart city infrastructure
        
        **Key Technologies:**
        - Deep learning-based object detection
        - Multi-object tracking algorithms
        - Real-time video processing
        - Edge computing optimization
        
        **Underlying Models:**
        - YOLOv5 for fast object detection
        - Faster R-CNN for high accuracy
        - DeepSORT for object tracking
        
        Developed with ❀️ using Streamlit and OpenCV.
    """)
    
    st.markdown("---")
    st.markdown("""
        **Disclaimer:** This is a demonstration application. For production use, 
        please ensure proper testing and validation of all components.
    """)

# Footer
st.markdown("""
    <footer>
        Real-Time Object Detection & Tracking | Powered by Streamlit and Hugging Face
    </footer>
""", unsafe_allow_html=True)