File size: 28,938 Bytes
fe5ba63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
"""
BLIND ASSISTANCE MODEL - HUGGING FACE SPACES DEPLOYMENT
Enhanced Video Navigation System with Audio Guidance
"""

import gradio as gr
import cv2
import numpy as np
from ultralytics import YOLO
from gtts import gTTS
import pygame
import os
import time
from collections import deque
from PIL import Image, ImageEnhance
import torch
import threading
from moviepy.editor import VideoFileClip, AudioFileClip, CompositeAudioClip
import tempfile
import json

# Optional imports
try:
    import easyocr
    EASYOCR_AVAILABLE = True
except ImportError:
    EASYOCR_AVAILABLE = False
    print("⚠️ EasyOCR not available")

try:
    import segmentation_models_pytorch as smp
    SMP_AVAILABLE = True
except ImportError:
    SMP_AVAILABLE = False
    print("⚠️ segmentation_models_pytorch not available")


class AudioNavigationSystem:
    def __init__(self):
        print("πŸš€ Initializing Blind Assistance Model...")
        
        # Load YOLOv8 model
        print("Loading YOLOv8 model...")
        self.model = YOLO('yolov8n.pt')
        print("βœ… Model loaded successfully!")

        # Initialize Semantic Segmentation Model
        print("Loading Semantic Segmentation Model...")
        self.segmentation_model = self.load_segmentation_model()
        print("βœ… Segmentation model loaded!")

        # Define segmentation classes
        self.segmentation_classes = {
            0: 'road', 1: 'sidewalk', 2: 'building', 3: 'wall', 4: 'fence',
            5: 'pole', 6: 'traffic light', 7: 'traffic sign', 8: 'vegetation',
            9: 'terrain', 10: 'sky', 11: 'person', 12: 'rider', 13: 'car',
            14: 'truck', 15: 'bus', 16: 'train', 17: 'motorcycle', 18: 'bicycle',
            19: 'void'
        }

        # Initialize Text Detection
        print("Loading Text Detection...")
        self.reader = self.load_text_detector()
        print("βœ… Text detection initialized!")

        # Audio system
        self.use_audio = True
        self.audio_files = []
        self.audio_timestamps = []
        self.video_start_time = None
        self.speaking = False
        self.audio_lock = threading.Lock()

        # Navigation classes
        self.navigation_classes = {
            'person': 'person', 'car': 'vehicle', 'truck': 'vehicle', 'bus': 'vehicle',
            'motorcycle': 'vehicle', 'bicycle': 'bicycle', 'traffic light': 'traffic light',
            'stop sign': 'stop sign', 'chair': 'chair', 'bench': 'bench'
        }

        # Priority levels
        self.object_priority = {
            'important_text': 10,
            'vehicle': 5,
            'person': 4,
            'bicycle': 4,
            'traffic light': 3,
            'stop sign': 3,
            'stairs': 4,
            'curb': 4,
            'crosswalk': 3,
            'text': 2,
            'road': 1,
            'sidewalk': 1,
            'building': 1,
            'vegetation': 1
        }

        # Important keywords for text
        self.important_keywords = [
            'exit', 'entrance', 'warning', 'danger', 'caution', 'stop',
            'stairs', 'elevator', 'escalator', 'crosswalk', 'curb',
            'emergency', 'hospital', 'police', 'fire', 'help',
            'men', 'women', 'toilet', 'restroom', 'washroom',
            'up', 'down', 'left', 'right', 'north', 'south', 'east', 'west',
            'hazard', 'attention'
        ]

        # Frame dimensions
        self.frame_width = 0
        self.frame_height = 0

        # Announcement cooldown
        self.last_announcement = time.time()
        self.announcement_cooldown = 3

        # Store detected items
        self.detected_items = set()
        self.text_size_reference = 100
        self.last_segmentation_analysis = ""
        self.segmentation_cooldown = 2

        print("βœ… System initialized successfully!")

    def load_text_detector(self):
        """Load text detection model"""
        if EASYOCR_AVAILABLE:
            try:
                return easyocr.Reader(['en'])
            except Exception as e:
                print(f"⚠️ EasyOCR initialization failed: {e}")
        return None

    def load_segmentation_model(self):
        """Load segmentation model"""
        if not SMP_AVAILABLE:
            return None
        try:
            model = smp.Unet(
                encoder_name="mobilenet_v2",
                encoder_weights="voc",
                classes=20,
                activation=None,
            )
            return model
        except Exception as e:
            print(f"⚠️ Could not load segmentation model: {e}")
            return None

    def perform_semantic_segmentation(self, frame):
        """Perform semantic segmentation"""
        try:
            h, w = frame.shape[:2]
            seg_map = np.zeros((h, w), dtype=np.uint8)
            hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
            
            # Road detection
            dark_mask = cv2.inRange(hsv, (0, 0, 0), (180, 255, 100))
            seg_map[h//2:, :][dark_mask[h//2:, :] > 0] = 0
            
            # Sky detection
            sky_mask = cv2.inRange(hsv, (100, 50, 150), (140, 255, 255))
            seg_map[:h//3, :][sky_mask[:h//3, :] > 0] = 10
            
            return seg_map
        except Exception as e:
            return np.zeros((frame.shape[0], frame.shape[1]), dtype=np.uint8)

    def analyze_segmentation_map(self, seg_map):
        """Analyze segmentation map"""
        h, w = seg_map.shape
        analysis = {
            'immediate_walkable': 0,
            'immediate_obstacles': 0,
            'critical_warnings': [],
            'guidance': [],
            'environment': 'unknown'
        }
        
        immediate_path = seg_map[int(h*0.7):, :]
        road_pixels = np.sum(immediate_path == 0)
        total_pixels = immediate_path.size
        
        if total_pixels > 0:
            road_percentage = (road_pixels / total_pixels) * 100
            if road_percentage > 60:
                analysis['guidance'].append("Clear path ahead")
                analysis['environment'] = 'road'
            elif road_percentage > 30:
                analysis['guidance'].append("Moderate path clarity")
                analysis['environment'] = 'mixed'
            else:
                analysis['guidance'].append("Obstructed path ahead")
                analysis['environment'] = 'obstructed'
        
        return analysis

    def generate_segmentation_guidance(self, seg_analysis):
        """Generate guidance from segmentation"""
        if not seg_analysis['guidance']:
            return None
        
        guidance = ". ".join(seg_analysis['guidance'])
        if seg_analysis['environment'] == 'road':
            guidance += ". You appear to be on a road."
        elif seg_analysis['environment'] == 'obstructed':
            guidance += ". Path may be obstructed."
        
        return guidance

    def preprocess_image_for_text(self, image):
        """Preprocess image for text detection"""
        pil_image = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
        enhancer = ImageEnhance.Contrast(pil_image)
        pil_image = enhancer.enhance(2.0)
        enhancer = ImageEnhance.Sharpness(pil_image)
        pil_image = enhancer.enhance(2.0)
        return cv2.cvtColor(np.array(pil_image), cv2.COLOR_RGB2BGR)

    def detect_text_easyocr(self, frame):
        """Detect text using EasyOCR"""
        if self.reader is None:
            return []
        
        try:
            processed_frame = self.preprocess_image_for_text(frame)
            gray = cv2.cvtColor(processed_frame, cv2.COLOR_BGR2GRAY)
            blurred = cv2.GaussianBlur(gray, (5, 5), 0)
            thresh = cv2.adaptiveThreshold(blurred, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
                                         cv2.THRESH_BINARY, 11, 2)
            kernel = np.ones((2, 2), np.uint8)
            morphed = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)
            processed_for_ocr = cv2.cvtColor(morphed, cv2.COLOR_GRAY2BGR)
            
            results = self.reader.readtext(processed_for_ocr,
                                         decoder='beamsearch',
                                         beamWidth=5,
                                         batch_size=1,
                                         height_ths=0.5,
                                         width_ths=0.5,
                                         min_size=20,
                                         text_threshold=0.3,
                                         link_threshold=0.3)
            
            detected_texts = []
            for (bbox, text, confidence) in results:
                if confidence > 0.4 and len(text.strip()) > 1:
                    clean_text = text.strip().lower()
                    
                    if len(bbox) >= 4:
                        y_coords = [point[1] for point in bbox]
                        text_height = max(y_coords) - min(y_coords)
                        distance = self.calculate_text_distance(text_height)
                        distance_category = self.get_distance_category(distance)
                        is_important = any(keyword in clean_text for keyword in self.important_keywords)
                        
                        detected_texts.append({
                            'type': 'text',
                            'text': clean_text,
                            'confidence': confidence,
                            'bbox': bbox,
                            'position': self.get_text_position(bbox),
                            'distance': distance,
                            'distance_category': distance_category,
                            'is_important': is_important,
                            'priority': 10 if is_important else 2
                        })
            
            return detected_texts
        except Exception as e:
            print(f"Text detection error: {e}")
            return []

    def get_text_position(self, bbox):
        """Determine text position"""
        if isinstance(bbox, list) and len(bbox) == 4:
            x_coords = [point[0] for point in bbox]
            x_center = sum(x_coords) / len(x_coords)
            third = self.frame_width / 3
            
            if x_center < third:
                return "left"
            elif x_center < 2 * third:
                return "center"
            else:
                return "right"
        return "center"

    def calculate_text_distance(self, bbox_height):
        """Estimate text distance"""
        if bbox_height <= 0:
            return 10.0
        distance = (self.text_size_reference * 2.0) / bbox_height
        return max(0.5, min(distance, 15.0))

    def get_distance_category(self, distance):
        """Convert distance to category"""
        if distance < 2:
            return "very close"
        elif distance < 4:
            return "close"
        elif distance < 7:
            return "moderate distance"
        elif distance < 10:
            return "far"
        else:
            return "very far"

    def calculate_object_distance(self, bbox_height, object_type="person"):
        """Estimate object distance"""
        reference_sizes = {
            'person': 1.7, 'vehicle': 1.5, 'bicycle': 1.0,
            'animal': 0.5, 'chair': 1.0, 'bench': 1.0,
            'pole': 2.0, 'default': 1.0
        }
        real_height = reference_sizes.get(object_type, reference_sizes['default'])
        focal_length = 500
        
        if bbox_height > 0:
            distance = (focal_length * real_height) / bbox_height
            return max(0.5, min(distance, 20))
        return 20

    def get_object_position(self, bbox):
        """Determine object position"""
        x_center = (bbox[0] + bbox[2]) / 2
        third = self.frame_width / 3
        
        if x_center < third:
            return "left"
        elif x_center < 2 * third:
            return "center"
        else:
            return "right"

    def get_comprehensive_priority(self, item):
        """Calculate comprehensive priority"""
        base_priority = self.object_priority.get(item.get('label', 'object'), 1)
        distance = item.get('distance', 10)
        distance_factor = max(0, 10 - distance) / 2
        position = item.get('position', 'right')
        position_factor = 2 if position == 'center' else 1
        
        if item.get('type') == 'text':
            if item.get('is_important', False):
                return 10 + distance_factor
            else:
                return 5 + distance_factor
        
        return base_priority * position_factor + distance_factor

    def generate_comprehensive_announcement(self, all_detections):
        """Generate balanced announcements"""
        if not all_detections:
            return "Path clear"
        
        messages = []
        all_detections.sort(key=self.get_comprehensive_priority, reverse=True)
        
        announced_count = 0
        max_announcements = 4
        
        for item in all_detections:
            if announced_count >= max_announcements:
                break
            
            item_type = item.get('type', 'object')
            
            if item_type == 'text':
                text = item['text']
                position = item['position']
                distance_category = item['distance_category']
                
                if item['is_important']:
                    messages.append(f"IMPORTANT: {text} {distance_category} on your {position}")
                else:
                    messages.append(f"Sign: {text} {distance_category} on your {position}")
                
                announced_count += 1
            else:
                if announced_count < max_announcements:
                    label = item['label']
                    position = item['position']
                    distance_category = item['distance_category']
                    
                    if position == "center" and item['distance'] < 3:
                        messages.append(f"Warning! {label} directly ahead, {distance_category}")
                    else:
                        messages.append(f"{label} on your {position}, {distance_category}")
                    
                    announced_count += 1
        
        center_objects = [item for item in all_detections
                         if item.get('position') == 'center' and item.get('distance', 10) < 3]
        
        if center_objects and len(messages) < 5:
            left_count = sum(1 for item in all_detections[:6] if item.get('position') == 'left')
            right_count = sum(1 for item in all_detections[:6] if item.get('position') == 'right')
            
            if left_count < right_count:
                messages.append("Consider moving left")
            elif right_count < left_count:
                messages.append("Consider moving right")
        
        return ". ".join(messages)

    def speak_gtts(self, text, timestamp=None):
        """Text-to-speech using gTTS"""
        if not text or self.speaking:
            return
        
        with self.audio_lock:
            self.speaking = True
            try:
                if timestamp is None:
                    if self.video_start_time:
                        timestamp = time.time() - self.video_start_time
                    else:
                        timestamp = 0
                
                minutes = int(timestamp // 60)
                seconds = int(timestamp % 60)
                timestamp_str = f"{minutes:02d}:{seconds:02d}"
                
                print(f"πŸ”Š [{timestamp_str}] GUIDANCE: {text}")
                
                tts = gTTS(text=text, lang='en', slow=False)
                audio_filename = f"audio_{timestamp_str.replace(':', '-')}_{int(time.time() * 1000)}.mp3"
                tts.save(audio_filename)
                
                self.audio_files.append(audio_filename)
                self.audio_timestamps.append({
                    'filename': audio_filename,
                    'timestamp': timestamp,
                    'timestamp_str': timestamp_str,
                    'text': text
                })
                
            except Exception as e:
                print(f"⚠️ Speech generation error: {e}")
            finally:
                self.speaking = False
                time.sleep(0.5)

    def process_frame(self, frame):
        """Process video frame"""
        self.frame_height, self.frame_width = frame.shape[:2]
        
        seg_map = self.perform_semantic_segmentation(frame)
        seg_analysis = self.analyze_segmentation_map(seg_map)
        
        results = self.model(frame, conf=0.4, verbose=False)
        
        all_detections = []
        objects_info = []
        text_info = []
        
        # Process YOLO detections
        for result in results:
            boxes = result.boxes
            for box in boxes:
                x1, y1, x2, y2 = map(int, box.xyxy[0])
                conf = float(box.conf[0])
                cls = int(box.cls[0])
                label = self.model.names[cls]
                
                if label.lower() in self.navigation_classes:
                    nav_label = self.navigation_classes[label.lower()]
                    bbox_height = y2 - y1
                    distance = self.calculate_object_distance(bbox_height, nav_label)
                    distance_category = self.get_distance_category(distance)
                    position = self.get_object_position([x1, y1, x2, y2])
                    
                    object_info = {
                        'type': 'object',
                        'label': nav_label,
                        'distance': distance,
                        'distance_category': distance_category,
                        'position': position,
                        'bbox': [x1, y1, x2, y2],
                        'confidence': conf,
                        'priority': self.object_priority.get(nav_label, 1)
                    }
                    
                    objects_info.append(object_info)
                    all_detections.append(object_info)
                    
                    # Draw bounding box
                    if nav_label == 'vehicle':
                        color = (0, 0, 255)
                    elif nav_label == 'person':
                        color = (0, 255, 255)
                    elif nav_label == 'bicycle':
                        color = (255, 0, 0)
                    else:
                        color = (0, 255, 0)
                    
                    cv2.rectangle(frame, (x1, y1), (x2, y2), color, 2)
                    label_text = f"{nav_label.upper()} {distance_category}"
                    (tw, th), _ = cv2.getTextSize(label_text, cv2.FONT_HERSHEY_SIMPLEX, 0.6, 2)
                    cv2.rectangle(frame, (x1, y1-th-10), (x1+tw+10, y1), color, -1)
                    cv2.putText(frame, label_text, (x1+5, y1-5),
                              cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 2)
        
        # Detect text
        current_time = time.time()
        if (current_time - self.last_announcement) > 1.5:
            text_info = self.detect_text_easyocr(frame)
            
            new_texts = []
            for text_data in text_info:
                text_hash = hash(text_data['text'][:20])
                if text_hash not in self.detected_items:
                    new_texts.append(text_data)
                    self.detected_items.add(text_hash)
            
            text_info = new_texts
            all_detections.extend(text_info)
            
            # Draw text bounding boxes
            for text_data in text_info:
                bbox = text_data['bbox']
                text = text_data['text']
                is_important = text_data['is_important']
                
                color = (255, 0, 255) if is_important else (255, 255, 0)
                thickness = 3 if is_important else 2
                
                pts = np.array(bbox, np.int32)
                pts = pts.reshape((-1, 1, 2))
                cv2.polylines(frame, [pts], True, color, thickness)
                
                label_text = f"🚩 {text}" if is_important else f"TEXT: {text}"
                x_coords = [point[0] for point in bbox]
                y_coords = [point[1] for point in bbox]
                text_x = int(min(x_coords))
                text_y = int(min(y_coords)) - 10
                
                if text_y < 20:
                    text_y = int(max(y_coords)) + 25
                
                (tw, th), _ = cv2.getTextSize(label_text, cv2.FONT_HERSHEY_SIMPLEX, 0.6, 2)
                cv2.rectangle(frame, (text_x, text_y-th-5), (text_x+tw+10, text_y+5), color, -1)
                cv2.putText(frame, label_text, (text_x+5, text_y),
                           cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 2)
        
        # Generate navigation message
        message = None
        if (current_time - self.last_announcement) > self.announcement_cooldown:
            seg_guidance = self.generate_segmentation_guidance(seg_analysis)
            object_message = self.generate_comprehensive_announcement(all_detections)
            
            if seg_guidance and "obstructed" in seg_guidance.lower():
                message = f"{seg_guidance}. {object_message}"
            elif seg_guidance and object_message == "Path clear":
                message = seg_guidance
            else:
                message = object_message
            
            if message and message != "Path clear":
                threading.Thread(target=self.speak_gtts, args=(message,)).start()
                self.last_announcement = current_time
        
        # Status overlay
        overlay = frame.copy()
        cv2.rectangle(overlay, (5, 5), (500, 35), (0, 0, 0), -1)
        cv2.addWeighted(overlay, 0.6, frame, 0.4, 0, frame)
        
        status_text = f"Objects: {len(objects_info)} | Texts: {len(text_info)}"
        cv2.putText(frame, status_text, (15, 28),
                   cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)
        
        # Draw center danger zone
        center_objects = [obj for obj in objects_info if obj['position'] == 'center' and obj['distance'] < 3]
        if center_objects:
            cv2.rectangle(frame, (self.frame_width//3, self.frame_height-100),
                         (2*self.frame_width//3, self.frame_height-10), (0, 0, 255), 3)
            cv2.putText(frame, "OBSTACLE IN PATH", (self.frame_width//3 + 20, self.frame_height-50),
                       cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2)
        
        return frame, message, len(objects_info), len(text_info)

    def process_video(self, video_path, output_path='output_navigation.mp4'):
        """Process uploaded video"""
        cap = cv2.VideoCapture(video_path)
        
        fps = int(cap.get(cv2.CAP_PROP_FPS))
        width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
        height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
        total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
        
        fourcc = cv2.VideoWriter_fourcc(*'mp4v')
        out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
        
        print(f"Processing video: {total_frames} frames at {fps} FPS")
        
        self.audio_timestamps = []
        self.audio_files = []
        self.detected_items = set()
        self.video_start_time = time.time()
        frame_count = 0
        
        try:
            while cap.isOpened():
                ret, frame = cap.read()
                if not ret:
                    break
                
                processed_frame, message, obj_count, text_count = self.process_frame(frame)
                out.write(processed_frame)
                frame_count += 1
                
                if frame_count % 30 == 0:
                    progress = (frame_count / total_frames) * 100
                    print(f"Progress: {progress:.1f}%")
        
        finally:
            cap.release()
            out.release()
            print(f"βœ… Video processing complete!")
        
        if self.audio_timestamps:
            final_output = 'final_with_audio.mp4'
            return self.merge_audio_into_video(output_path, final_output)
        else:
            return output_path

    def merge_audio_into_video(self, video_path, output_path='final_with_audio.mp4'):
        """Merge audio into video"""
        print("🎡 Merging audio into video...")
        
        if not self.audio_timestamps:
            return video_path
        
        try:
            video = VideoFileClip(video_path)
            video_duration = video.duration
            
            audio_clips = []
            for audio_info in self.audio_timestamps:
                if os.path.exists(audio_info['filename']):
                    try:
                        audio_clip = AudioFileClip(audio_info['filename'])
                        audio_clip = audio_clip.set_start(audio_info['timestamp'])
                        audio_clips.append(audio_clip)
                    except Exception as e:
                        print(f"⚠️ Failed to load {audio_info['filename']}: {e}")
            
            if not audio_clips:
                return video_path
            
            final_audio = CompositeAudioClip(audio_clips)
            final_audio = final_audio.set_duration(video_duration)
            final_video = video.set_audio(final_audio)
            
            final_video.write_videofile(
                output_path,
                codec='libx264',
                audio_codec='aac',
                fps=video.fps,
                verbose=False,
                logger=None
            )
            
            video.close()
            final_video.close()
            final_audio.close()
            for clip in audio_clips:
                clip.close()
            
            print(f"βœ… Video with audio saved!")
            return output_path
        
        except Exception as e:
            print(f"❌ Error merging audio: {e}")
            return video_path


# Initialize the system
nav_system = AudioNavigationSystem()


def process_video_gradio(video_file):
    """Gradio interface function"""
    try:
        if video_file is None:
            return None, "Please upload a video file"
        
        # Create temporary file
        with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as tmp_input:
            tmp_input.write(video_file)
            input_path = tmp_input.name
        
        # Check video duration
        cap = cv2.VideoCapture(input_path)
        fps = cap.get(cv2.CAP_PROP_FPS)
        frame_count = cap.get(cv2.CAP_PROP_FRAME_COUNT)
        duration = frame_count / fps if fps > 0 else 0
        cap.release()
        
        if duration > 15:
            return None, f"⚠️ Video is {duration:.1f} seconds long. Please upload a video shorter than 15 seconds."
        
        # Process video
        output_path = nav_system.process_video(input_path)
        
        # Generate transcript
        transcript_text = "Audio Guidance Transcript:\n\n"
        for item in nav_system.audio_timestamps:
            transcript_text += f"[{item['timestamp_str']}] {item['text']}\n\n"
        
        return output_path, transcript_text
    
    except Exception as e:
        return None, f"Error processing video: {str(e)}"


# Create Gradio interface
with gr.Blocks(title="Blind Assistance AI", theme=gr.themes.Soft()) as demo:
    gr.Markdown("""
    # 🦯 Blind Assistance AI - Video Navigation System
    
    Upload a video to receive audio navigation guidance with object detection, text recognition, and scene analysis.
    
    ⚠️ **Important:** Please upload videos **shorter than 15 seconds** for optimal processing.
    """)
    
    with gr.Row():
        with gr.Column():
            video_input = gr.Video(label="Upload Video (Max 15 seconds)")
            process_btn = gr.Button("Process Video", variant="primary", size="lg")
        
        with gr.Column():
            video_output = gr.Video(label="Processed Video with Audio Guidance")
            transcript_output = gr.Textbox(label="Audio Transcript", lines=10)
    
    gr.Markdown("""
    ### Features:
    - 🎯 **Object Detection**: Identifies people, vehicles, and obstacles
    - πŸ“ **Text Detection & OCR**: Reads signs, labels, and important text  
    - πŸ—ΊοΈ **Scene Analysis**: Understands environment and context
    - πŸ”Š **Voice Guidance**: Real-time audio navigation instructions
    """)
    
    process_btn.click(
        fn=process_video_gradio,
        inputs=[video_input],
        outputs=[video_output, transcript_output]
    )

# Launch the app
if __name__ == "__main__":
    demo.launch()