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 
 
 
 
1
+ import streamlit as st
2
+ from streamlit_option_menu import option_menu
3
+ import cv2
4
+ import numpy as np
5
+ import tempfile
6
+ from PIL import Image
7
+ import time
8
+ import av
9
+ from streamlit_webrtc import webrtc_streamer, VideoProcessorBase, RTCConfiguration
10
+ import os
11
+ import requests
12
+
13
+ # Set page config
14
+ st.set_page_config(
15
+ page_title="Real-Time Object Detection & Tracking",
16
+ page_icon="🚗",
17
+ layout="wide",
18
+ initial_sidebar_state="expanded"
19
+ )
20
+
21
+ # Custom CSS for styling
22
+ st.markdown("""
23
+ <style>
24
+ .stApp {
25
+ background-color: #f8f9fa;
26
+ }
27
+ .header {
28
+ color: #2c3e50;
29
+ font-size: 2.5rem;
30
+ font-weight: bold;
31
+ margin-bottom: 1rem;
32
+ }
33
+ .subheader {
34
+ color: #3498db;
35
+ font-size: 1.5rem;
36
+ margin-bottom: 1rem;
37
+ }
38
+ .stButton>button {
39
+ background-color: #3498db;
40
+ color: white;
41
+ border-radius: 5px;
42
+ padding: 0.5rem 1rem;
43
+ border: none;
44
+ transition: all 0.3s;
45
+ }
46
+ .stButton>button:hover {
47
+ background-color: #2980b9;
48
+ transform: translateY(-2px);
49
+ box-shadow: 0 4px 8px rgba(0,0,0,0.1);
50
+ }
51
+ .css-1aumxhk {
52
+ background-color: #ffffff;
53
+ border-radius: 10px;
54
+ padding: 2rem;
55
+ box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
56
+ }
57
+ .model-card {
58
+ border-radius: 10px;
59
+ padding: 1.5rem;
60
+ margin-bottom: 1rem;
61
+ background-color: #ffffff;
62
+ box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
63
+ transition: all 0.3s;
64
+ }
65
+ .model-card:hover {
66
+ transform: translateY(-5px);
67
+ box-shadow: 0 8px 15px rgba(0, 0, 0, 0.1);
68
+ }
69
+ .stSelectbox>div>div>select {
70
+ border-radius: 5px;
71
+ padding: 0.5rem;
72
+ }
73
+ .stSlider>div>div>div>div {
74
+ background-color: #3498db;
75
+ }
76
+ </style>
77
+ """, unsafe_allow_html=True)
78
+
79
+ # App header
80
+ col1, col2 = st.columns([1, 3])
81
+ with col1:
82
+ st.image("https://huggingface.co/front/assets/huggingface_logo-noborder.svg", width=100)
83
+ with col2:
84
+ st.markdown('<div class="header">Real-Time Object Detection & Tracking</div>', unsafe_allow_html=True)
85
+ st.markdown("Advanced computer vision for autonomous vehicles and surveillance systems")
86
+
87
+ # Navigation menu
88
+ with st.sidebar:
89
+ selected = option_menu(
90
+ menu_title="Main Menu",
91
+ options=["Home", "Live Detection", "Video Processing", "Model Zoo", "Settings", "About"],
92
+ icons=["house", "camera-video", "film", "boxes", "gear", "info-circle"],
93
+ menu_icon="cast",
94
+ default_index=0,
95
+ )
96
+
97
+ # Object detection class (simplified for demo)
98
+ class ObjectDetector:
99
+ def __init__(self, model_type="yolov5"):
100
+ self.model_type = model_type
101
+ self.classes = ["person", "car", "truck", "bicycle", "motorcycle", "bus"]
102
+ self.colors = np.random.uniform(0, 255, size=(len(self.classes), 3))
103
+
104
+ def detect(self, image):
105
+ # In a real app, you would use an actual model here
106
+ # This is a simplified version for demonstration
107
+
108
+ # Convert image to numpy array
109
+ frame = np.array(image)
110
+
111
+ # Simulate detection by adding random bounding boxes
112
+ height, width = frame.shape[:2]
113
+ detections = []
114
+
115
+ for _ in range(np.random.randint(2, 6)):
116
+ class_id = np.random.randint(0, len(self.classes))
117
+ confidence = np.random.uniform(0.7, 0.95)
118
+
119
+ x = int(np.random.uniform(0, width * 0.8))
120
+ y = int(np.random.uniform(0, height * 0.8))
121
+ w = int(np.random.uniform(width * 0.1, width * 0.3))
122
+ h = int(np.random.uniform(height * 0.1, height * 0.3))
123
+
124
+ detections.append({
125
+ "class_id": class_id,
126
+ "confidence": confidence,
127
+ "box": [x, y, x+w, y+h]
128
+ })
129
+
130
+ return detections
131
+
132
+ def draw_detections(self, frame, detections):
133
+ for detection in detections:
134
+ class_id = detection["class_id"]
135
+ confidence = detection["confidence"]
136
+ box = detection["box"]
137
+
138
+ color = self.colors[class_id]
139
+ label = f"{self.classes[class_id]}: {confidence:.2f}"
140
+
141
+ cv2.rectangle(frame, (box[0], box[1]), (box[2], box[3]), color, 2)
142
+ cv2.putText(frame, label, (box[0], box[1]-10),
143
+ cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
144
+
145
+ return frame
146
+
147
+ # Video processor for WebRTC
148
+ class VideoProcessor(VideoProcessorBase):
149
+ def __init__(self):
150
+ self.detector = ObjectDetector()
151
+ self.confidence_threshold = 0.5
152
+ self.tracking_enabled = True
153
+
154
+ def recv(self, frame):
155
+ img = frame.to_ndarray(format="bgr24")
156
+
157
+ # Perform detection
158
+ detections = self.detector.detect(img)
159
+
160
+ # Filter by confidence
161
+ detections = [d for d in detections if d["confidence"] >= self.confidence_threshold]
162
+
163
+ # Draw detections
164
+ img = self.detector.draw_detections(img, detections)
165
+
166
+ return av.VideoFrame.from_ndarray(img, format="bgr24")
167
+
168
+ # Home Page
169
+ if selected == "Home":
170
+ st.markdown('<div class="subheader">Welcome to Real-Time Object Detection</div>', unsafe_allow_html=True)
171
+
172
+ col1, col2 = st.columns(2)
173
+ with col1:
174
+ st.markdown("""
175
+ **Advanced computer vision system** for:
176
+ - Autonomous vehicles 🚗
177
+ - Surveillance systems 🏢
178
+ - Traffic monitoring 🚦
179
+ - Smart cities 🌆
180
+
181
+ **Features:**
182
+ - Real-time object detection
183
+ - Multi-object tracking
184
+ - Customizable models
185
+ - High-performance inference
186
+ """)
187
+
188
+ st.button("Get Started →", key="home_get_started")
189
+
190
+ with col2:
191
+ st.image("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers-task-cv-object-detection.png",
192
+ caption="Object Detection Example", use_column_width=True)
193
+
194
+ st.markdown("---")
195
+ st.markdown("### How It Works")
196
+ st.markdown("""
197
+ 1. **Select a model** from our Model Zoo or upload your own
198
+ 2. **Choose input source** - live camera, video file, or image
199
+ 3. **Configure settings** - confidence threshold, tracking options
200
+ 4. **Run detection** and view real-time results
201
+ """)
202
+
203
+ # Live Detection Page
204
+ elif selected == "Live Detection":
205
+ st.markdown('<div class="subheader">Live Object Detection</div>', unsafe_allow_html=True)
206
+
207
+ tab1, tab2 = st.tabs(["Webcam", "RTSP Stream"])
208
+
209
+ with tab1:
210
+ st.markdown("### Webcam Detection")
211
+ st.info("This will use your device's camera for real-time object detection")
212
+
213
+ # WebRTC configuration
214
+ RTC_CONFIGURATION = RTCConfiguration(
215
+ {"iceServers": [{"urls": ["stun:stun.l.google.com:19302"]}]}
216
+ )
217
+
218
+ # Confidence threshold
219
+ confidence_threshold = st.slider(
220
+ "Confidence Threshold",
221
+ min_value=0.1,
222
+ max_value=0.9,
223
+ value=0.5,
224
+ step=0.05,
225
+ help="Adjust the minimum confidence score for detections"
226
+ )
227
+
228
+ # Toggle tracking
229
+ tracking_enabled = st.checkbox(
230
+ "Enable Object Tracking",
231
+ value=True,
232
+ help="Track objects across frames for consistent identification"
233
+ )
234
+
235
+ # Start WebRTC streamer
236
+ webrtc_ctx = webrtc_streamer(
237
+ key="object-detection",
238
+ video_processor_factory=VideoProcessor,
239
+ rtc_configuration=RTC_CONFIGURATION,
240
+ media_stream_constraints={"video": True, "audio": False},
241
+ async_processing=True,
242
+ )
243
+
244
+ if webrtc_ctx.video_processor:
245
+ webrtc_ctx.video_processor.confidence_threshold = confidence_threshold
246
+ webrtc_ctx.video_processor.tracking_enabled = tracking_enabled
247
+
248
+ with tab2:
249
+ st.markdown("### RTSP Stream Detection")
250
+ st.warning("This feature requires an RTSP stream URL (e.g., from an IP camera)")
251
+
252
+ rtsp_url = st.text_input("Enter RTSP Stream URL", "rtsp://example.com/stream")
253
+
254
+ if st.button("Connect to Stream"):
255
+ st.warning("RTSP stream processing would be implemented here in a production app")
256
+ st.info(f"Would connect to: {rtsp_url}")
257
+
258
+ # Video Processing Page
259
+ elif selected == "Video Processing":
260
+ st.markdown('<div class="subheader">Video File Processing</div>', unsafe_allow_html=True)
261
+
262
+ uploaded_file = st.file_uploader(
263
+ "Upload a video file",
264
+ type=["mp4", "avi", "mov"],
265
+ help="Upload a video file for object detection processing"
266
+ )
267
+
268
+ if uploaded_file is not None:
269
+ st.success("Video file uploaded successfully!")
270
+
271
+ # Save uploaded file to temporary location
272
+ tfile = tempfile.NamedTemporaryFile(delete=False)
273
+ tfile.write(uploaded_file.read())
274
+
275
+ # Display video info
276
+ video = cv2.VideoCapture(tfile.name)
277
+ width = int(video.get(cv2.CAP_PROP_FRAME_WIDTH))
278
+ height = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT))
279
+ fps = video.get(cv2.CAP_PROP_FPS)
280
+ frame_count = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
281
+ duration = frame_count / fps
282
+
283
+ col1, col2, col3 = st.columns(3)
284
+ col1.metric("Resolution", f"{width}x{height}")
285
+ col2.metric("FPS", f"{fps:.2f}")
286
+ col3.metric("Duration", f"{duration:.2f} seconds")
287
+
288
+ # Processing options
289
+ st.markdown("### Processing Options")
290
+ confidence_threshold = st.slider(
291
+ "Confidence Threshold",
292
+ min_value=0.1,
293
+ max_value=0.9,
294
+ value=0.5,
295
+ step=0.05
296
+ )
297
+
298
+ tracking_enabled = st.checkbox("Enable Object Tracking", value=True)
299
+ show_fps = st.checkbox("Show FPS Counter", value=True)
300
+
301
+ # Process video button
302
+ if st.button("Process Video"):
303
+ st.warning("Video processing would be implemented here in a production app")
304
+
305
+ # Simulate processing with progress bar
306
+ progress_bar = st.progress(0)
307
+ status_text = st.empty()
308
+
309
+ for i in range(1, 101):
310
+ progress_bar.progress(i)
311
+ status_text.text(f"Processing: {i}% complete")
312
+ time.sleep(0.05)
313
+
314
+ st.success("Video processing completed!")
315
+ st.balloons()
316
+
317
+ # Model Zoo Page
318
+ elif selected == "Model Zoo":
319
+ st.markdown('<div class="subheader">Model Selection</div>', unsafe_allow_html=True)
320
+
321
+ st.markdown("""
322
+ Choose from our pre-trained models or upload your own custom model.
323
+ Different models offer different trade-offs between speed and accuracy.
324
+ """)
325
+
326
+ # Model cards
327
+ col1, col2, col3 = st.columns(3)
328
+
329
+ with col1:
330
+ st.markdown("""
331
+ <div class="model-card">
332
+ <h3>YOLOv5s</h3>
333
+ <p><b>Type:</b> Object Detection</p>
334
+ <p><b>Speed:</b> ⚡⚡⚡⚡⚡</p>
335
+ <p><b>Accuracy:</b> ⭐⭐⭐</p>
336
+ <p>Ultra-fast detection for real-time applications</p>
337
+ </div>
338
+ """, unsafe_allow_html=True)
339
+ if st.button("Select YOLOv5s", key="yolov5s"):
340
+ st.session_state.selected_model = "yolov5s"
341
+ st.success("YOLOv5s selected!")
342
+
343
+ with col2:
344
+ st.markdown("""
345
+ <div class="model-card">
346
+ <h3>Faster R-CNN</h3>
347
+ <p><b>Type:</b> Object Detection</p>
348
+ <p><b>Speed:</b> ⚡⚡</p>
349
+ <p><b>Accuracy:</b> ⭐⭐⭐⭐⭐</p>
350
+ <p>High accuracy for critical applications</p>
351
+ </div>
352
+ """, unsafe_allow_html=True)
353
+ if st.button("Select Faster R-CNN", key="frcnn"):
354
+ st.session_state.selected_model = "faster_rcnn"
355
+ st.success("Faster R-CNN selected!")
356
+
357
+ with col3:
358
+ st.markdown("""
359
+ <div class="model-card">
360
+ <h3>DeepSORT</h3>
361
+ <p><b>Type:</b> Object Tracking</p>
362
+ <p><b>Speed:</b> ⚡⚡⚡</p>
363
+ <p><b>Accuracy:</b> ⭐⭐⭐⭐</p>
364
+ <p>Tracking with deep learning features</p>
365
+ </div>
366
+ """, unsafe_allow_html=True)
367
+ if st.button("Select DeepSORT", key="deepsort"):
368
+ st.session_state.selected_model = "deepsort"
369
+ st.success("DeepSORT selected!")
370
+
371
+ st.markdown("---")
372
+ st.markdown("### Custom Model Upload")
373
+
374
+ custom_model = st.file_uploader(
375
+ "Upload your custom model (PyTorch or TensorFlow)",
376
+ type=["pt", "pth", "h5", "onnx"],
377
+ help="Upload your custom trained model file"
378
+ )
379
+
380
+ if custom_model is not None:
381
+ st.success("Custom model uploaded successfully!")
382
+ st.info("Model would be loaded and validated here in a production app")
383
+
384
+ # Settings Page
385
+ elif selected == "Settings":
386
+ st.markdown('<div class="subheader">Application Settings</div>', unsafe_allow_html=True)
387
+
388
+ st.markdown("### Detection Parameters")
389
+ confidence_threshold = st.slider(
390
+ "Default Confidence Threshold",
391
+ min_value=0.1,
392
+ max_value=0.9,
393
+ value=0.5,
394
+ step=0.05
395
+ )
396
+
397
+ iou_threshold = st.slider(
398
+ "IOU Threshold (for NMS)",
399
+ min_value=0.1,
400
+ max_value=0.9,
401
+ value=0.45,
402
+ step=0.05,
403
+ help="Intersection over Union threshold for non-maximum suppression"
404
+ )
405
+
406
+ st.markdown("### Tracking Parameters")
407
+ max_age = st.slider(
408
+ "Max Track Age (frames)",
409
+ min_value=1,
410
+ max_value=100,
411
+ value=30,
412
+ help="Number of frames to keep a track alive without detection"
413
+ )
414
+
415
+ min_hits = st.slider(
416
+ "Min Detection Hits",
417
+ min_value=1,
418
+ max_value=10,
419
+ value=3,
420
+ help="Number of detections needed before a track is confirmed"
421
+ )
422
+
423
+ if st.button("Save Settings"):
424
+ st.success("Settings saved successfully!")
425
+
426
+ # About Page
427
+ elif selected == "About":
428
+ st.markdown('<div class="subheader">About This Project</div>', unsafe_allow_html=True)
429
+
430
+ st.markdown("""
431
+ **Real-Time Object Detection & Tracking System**
432
+
433
+ This application demonstrates advanced computer vision capabilities for:
434
+ - Autonomous vehicle perception systems
435
+ - Surveillance and security applications
436
+ - Traffic monitoring and analysis
437
+ - Smart city infrastructure
438
+
439
+ **Key Technologies:**
440
+ - Deep learning-based object detection
441
+ - Multi-object tracking algorithms
442
+ - Real-time video processing
443
+ - Edge computing optimization
444
+
445
+ **Underlying Models:**
446
+ - YOLOv5 for fast object detection
447
+ - Faster R-CNN for high accuracy
448
+ - DeepSORT for object tracking
449
+
450
+ Developed with ❤️ using Streamlit and OpenCV.
451
+ """)
452
+
453
+ st.markdown("---")
454
+ st.markdown("""
455
+ **Disclaimer:** This is a demonstration application. For production use,
456
+ please ensure proper testing and validation of all components.
457
+ """)
458
+
459
+ # Footer
460
+ st.markdown("---")
461
+ st.markdown("""
462
+ <div style="text-align: center; color: #7f8c8d; font-size: 0.9rem;">
463
+ Real-Time Object Detection & Tracking | Powered by Streamlit and Hugging Face
464
+ </div>
465
+ """, unsafe_allow_html=True)