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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Advanced Video Analyzer for GAIA Agent - Phase 5
Comprehensive video analysis tool for YouTube videos with object detection and temporal tracking.

Features:
- YouTube video downloading and processing
- Advanced object detection using YOLO models
- Bird and animal species identification
- Temporal object tracking across frames
- Simultaneous object counting
- Integration with AGNO framework
"""

import os
import logging
import cv2
import numpy as np
from typing import Dict, Any, List, Optional, Tuple
import json
import tempfile
import shutil
from pathlib import Path
from datetime import datetime
import yt_dlp

# Import detection engines
try:
    from .object_detection_engine import ObjectDetectionEngine
    from .video_content_analyzer import create_video_content_analyzer
except ImportError:
    try:
        from object_detection_engine import ObjectDetectionEngine
        from video_content_analyzer import create_video_content_analyzer
    except ImportError:
        ObjectDetectionEngine = None
        create_video_content_analyzer = None

# Configure logging
logger = logging.getLogger(__name__)

class AdvancedVideoAnalyzer:
    """Advanced video analyzer for comprehensive video content analysis."""
    
    def __init__(self):
        """Initialize the advanced video analyzer."""
        self.available = True
        self.temp_dir = tempfile.mkdtemp()
        
        # Initialize detection engine
        self.detection_engine = None
        if ObjectDetectionEngine:
            try:
                self.detection_engine = ObjectDetectionEngine()
                if not self.detection_engine.available:
                    logger.warning("⚠️ Object detection engine not available")
            except Exception as e:
                logger.warning(f"⚠️ Failed to initialize object detection engine: {e}")
        
        # Initialize content analyzer
        self.content_analyzer = None
        if create_video_content_analyzer:
            try:
                self.content_analyzer = create_video_content_analyzer()
                if not self.content_analyzer.available:
                    logger.warning("⚠️ Video content analyzer not available")
            except Exception as e:
                logger.warning(f"⚠️ Failed to initialize video content analyzer: {e}")
        
        # Analysis parameters
        self.frame_sampling_rate = 1  # Analyze every frame by default
        self.max_frames = 1000  # Maximum frames to analyze
        self.confidence_threshold = 0.3
        self.nms_threshold = 0.4
        
        logger.info(f"πŸ“Ή Advanced Video Analyzer initialized - Available: {self.available}")
    
    def analyze_video(self, video_url: str, question: str = None, 
                     max_duration: int = 300) -> Dict[str, Any]:
        """
        Analyze a video comprehensively for object detection and counting.
        
        Args:
            video_url: URL of the video (YouTube supported)
            question: Optional question to guide analysis
            max_duration: Maximum video duration to process (seconds)
            
        Returns:
            Comprehensive video analysis results
        """
        try:
            logger.info(f"πŸ“Ή Starting video analysis for: {video_url}")
            
            # Download video
            video_path = self._download_video(video_url, max_duration)
            if not video_path:
                return {
                    'success': False,
                    'error': 'Failed to download video'
                }
            
            # Extract video metadata
            metadata = self._extract_video_metadata(video_path)
            
            # Perform frame-by-frame object detection
            detection_results = self._analyze_video_frames(video_path, question)
            
            # Perform content analysis
            content_analysis = None
            if self.content_analyzer:
                content_analysis = self.content_analyzer.analyze_video_content(
                    video_path, detection_results.get('frame_detections', []), question
                )
            
            # Generate comprehensive analysis report
            analysis_report = self._create_analysis_report(
                video_url, metadata, detection_results, content_analysis, question
            )
            
            # Cleanup
            self._cleanup_temp_files(video_path)
            
            return analysis_report
            
        except Exception as e:
            logger.error(f"❌ Video analysis failed: {e}")
            return {
                'success': False,
                'error': f'Video analysis failed: {str(e)}'
            }
    
    def _download_video(self, video_url: str, max_duration: int = 300) -> Optional[str]:
        """Download video from URL using yt-dlp."""
        try:
            output_path = os.path.join(self.temp_dir, 'video.%(ext)s')
            
            ydl_opts = {
                'format': 'best[height<=720][ext=mp4]/best[ext=mp4]/best',
                'outtmpl': output_path,
                'quiet': True,
                'no_warnings': True,
                'extract_flat': False,
                'writethumbnail': False,
                'writeinfojson': False,
                'match_filter': lambda info_dict: None if info_dict.get('duration', 0) <= max_duration else "Video too long"
            }
            
            with yt_dlp.YoutubeDL(ydl_opts) as ydl:
                # Extract info first to check duration
                info = ydl.extract_info(video_url, download=False)
                duration = info.get('duration', 0)
                
                if duration > max_duration:
                    logger.warning(f"⚠️ Video duration ({duration}s) exceeds maximum ({max_duration}s)")
                    return None
                
                # Download the video
                ydl.download([video_url])
            
            # Find the downloaded file
            for file in os.listdir(self.temp_dir):
                if file.startswith('video.') and file.endswith(('.mp4', '.webm', '.mkv')):
                    video_path = os.path.join(self.temp_dir, file)
                    logger.info(f"βœ… Video downloaded: {video_path}")
                    return video_path
            
            logger.error("❌ Downloaded video file not found")
            return None
            
        except Exception as e:
            logger.error(f"❌ Video download failed: {e}")
            return None
    
    def _extract_video_metadata(self, video_path: str) -> Dict[str, Any]:
        """Extract video metadata using OpenCV."""
        try:
            cap = cv2.VideoCapture(video_path)
            if not cap.isOpened():
                raise Exception("Failed to open video file")
            
            fps = cap.get(cv2.CAP_PROP_FPS)
            frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
            width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
            height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
            duration = frame_count / fps if fps > 0 else 0
            
            cap.release()
            
            metadata = {
                'duration_seconds': duration,
                'fps': fps,
                'frame_count': frame_count,
                'resolution': {'width': width, 'height': height},
                'file_size': os.path.getsize(video_path),
                'analysis_timestamp': datetime.now().isoformat()
            }
            
            logger.info(f"πŸ“Š Video metadata: {duration:.1f}s, {width}x{height}, {fps:.1f} FPS")
            return metadata
            
        except Exception as e:
            logger.error(f"❌ Failed to extract video metadata: {e}")
            return {}
    
    def _analyze_video_frames(self, video_path: str, question: str = None) -> Dict[str, Any]:
        """Analyze video frames for object detection and tracking."""
        try:
            if not self.detection_engine or not self.detection_engine.available:
                logger.warning("⚠️ Object detection engine not available")
                return {'frame_detections': [], 'summary': {}}
            
            cap = cv2.VideoCapture(video_path)
            if not cap.isOpened():
                raise Exception("Failed to open video file")
            
            frame_detections = []
            frame_count = 0
            total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
            fps = cap.get(cv2.CAP_PROP_FPS)
            
            # Determine frame sampling rate based on video length
            if total_frames > self.max_frames:
                self.frame_sampling_rate = max(1, total_frames // self.max_frames)
                logger.info(f"πŸ“Š Sampling every {self.frame_sampling_rate} frames")
            
            # Track objects across frames
            object_tracker = {}
            next_object_id = 0
            
            while cap.isOpened() and frame_count < total_frames:
                ret, frame = cap.read()
                if not ret:
                    break
                
                # Sample frames based on sampling rate
                if frame_count % self.frame_sampling_rate == 0:
                    # Detect objects in frame
                    detections = self.detection_engine.detect_objects(
                        frame, 
                        confidence_threshold=self.confidence_threshold,
                        nms_threshold=self.nms_threshold
                    )
                    
                    # Add temporal information
                    timestamp = frame_count / fps
                    for detection in detections:
                        detection['frame_number'] = frame_count
                        detection['timestamp'] = timestamp
                    
                    frame_detections.append(detections)
                    
                    # Progress logging
                    if len(frame_detections) % 50 == 0:
                        progress = (frame_count / total_frames) * 100
                        logger.info(f"πŸ“ˆ Analysis progress: {progress:.1f}% ({len(frame_detections)} frames analyzed)")
                
                frame_count += 1
                
                # Break if we've analyzed enough frames
                if len(frame_detections) >= self.max_frames:
                    break
            
            cap.release()
            
            # Generate detection summary
            summary = self._generate_detection_summary(frame_detections, question)
            
            logger.info(f"βœ… Frame analysis complete: {len(frame_detections)} frames analyzed")
            return {
                'frame_detections': frame_detections,
                'summary': summary,
                'analysis_params': {
                    'frame_sampling_rate': self.frame_sampling_rate,
                    'confidence_threshold': self.confidence_threshold,
                    'nms_threshold': self.nms_threshold,
                    'frames_analyzed': len(frame_detections)
                }
            }
            
        except Exception as e:
            logger.error(f"❌ Frame analysis failed: {e}")
            return {'frame_detections': [], 'summary': {}}
    
    def _generate_detection_summary(self, frame_detections: List[List[Dict[str, Any]]], 
                                  question: str = None) -> Dict[str, Any]:
        """Generate summary of detection results."""
        try:
            summary = {
                'total_frames_analyzed': len(frame_detections),
                'total_detections': 0,
                'species_counts': {},
                'max_simultaneous_objects': 0,
                'max_simultaneous_birds': 0,
                'max_simultaneous_animals': 0,
                'temporal_patterns': [],
                'answer_analysis': {}
            }
            
            # Analyze each frame
            simultaneous_counts = []
            bird_counts = []
            animal_counts = []
            
            for frame_dets in frame_detections:
                summary['total_detections'] += len(frame_dets)
                
                # Count objects by type
                frame_birds = 0
                frame_animals = 0
                frame_objects = len(frame_dets)
                
                for detection in frame_dets:
                    species_type = detection.get('species_type', 'unknown')
                    class_name = detection.get('class', 'unknown')
                    
                    # Update species counts
                    if species_type not in summary['species_counts']:
                        summary['species_counts'][species_type] = 0
                    summary['species_counts'][species_type] += 1
                    
                    # Count birds and animals
                    if species_type == 'bird':
                        frame_birds += 1
                    elif species_type == 'animal':
                        frame_animals += 1
                
                simultaneous_counts.append(frame_objects)
                bird_counts.append(frame_birds)
                animal_counts.append(frame_animals)
            
            # Calculate maximums
            if simultaneous_counts:
                summary['max_simultaneous_objects'] = max(simultaneous_counts)
            if bird_counts:
                summary['max_simultaneous_birds'] = max(bird_counts)
            if animal_counts:
                summary['max_simultaneous_animals'] = max(animal_counts)
            
            # Analyze question-specific patterns
            if question:
                summary['answer_analysis'] = self._analyze_question_specific_patterns(
                    question, frame_detections, bird_counts, animal_counts
                )
            
            # Generate temporal patterns
            summary['temporal_patterns'] = {
                'avg_objects_per_frame': np.mean(simultaneous_counts) if simultaneous_counts else 0,
                'avg_birds_per_frame': np.mean(bird_counts) if bird_counts else 0,
                'avg_animals_per_frame': np.mean(animal_counts) if animal_counts else 0,
                'object_variance': np.var(simultaneous_counts) if simultaneous_counts else 0
            }
            
            return summary
            
        except Exception as e:
            logger.error(f"❌ Detection summary generation failed: {e}")
            return {}
    
    def _analyze_question_specific_patterns(self, question: str, 
                                          frame_detections: List[List[Dict[str, Any]]],
                                          bird_counts: List[int],
                                          animal_counts: List[int]) -> Dict[str, Any]:
        """Analyze patterns specific to the question asked."""
        try:
            analysis = {
                'question_type': 'unknown',
                'target_answer': None,
                'confidence': 0.0,
                'reasoning': []
            }
            
            question_lower = question.lower()
            
            # Detect question type and provide specific analysis
            if 'bird' in question_lower and ('highest' in question_lower or 'maximum' in question_lower):
                analysis['question_type'] = 'max_birds_simultaneous'
                analysis['target_answer'] = max(bird_counts) if bird_counts else 0
                analysis['confidence'] = 0.9 if bird_counts else 0.1
                analysis['reasoning'].append(f"Maximum simultaneous birds detected: {analysis['target_answer']}")
                
                # Find frames with maximum birds
                max_bird_count = analysis['target_answer']
                max_frames = [i for i, count in enumerate(bird_counts) if count == max_bird_count]
                analysis['reasoning'].append(f"Maximum occurred in {len(max_frames)} frame(s)")
                
            elif 'animal' in question_lower and ('highest' in question_lower or 'maximum' in question_lower):
                analysis['question_type'] = 'max_animals_simultaneous'
                analysis['target_answer'] = max(animal_counts) if animal_counts else 0
                analysis['confidence'] = 0.9 if animal_counts else 0.1
                analysis['reasoning'].append(f"Maximum simultaneous animals detected: {analysis['target_answer']}")
                
            elif 'species' in question_lower and ('highest' in question_lower or 'maximum' in question_lower):
                analysis['question_type'] = 'max_species_simultaneous'
                # For species counting, we need to count unique species per frame
                max_species = 0
                for frame_dets in frame_detections:
                    unique_species = set()
                    for det in frame_dets:
                        species_type = det.get('species_type', 'unknown')
                        if species_type in ['bird', 'animal']:
                            class_name = det.get('class', 'unknown')
                            unique_species.add(class_name)
                    max_species = max(max_species, len(unique_species))
                
                analysis['target_answer'] = max_species
                analysis['confidence'] = 0.8 if max_species > 0 else 0.1
                analysis['reasoning'].append(f"Maximum simultaneous species detected: {analysis['target_answer']}")
            
            return analysis
            
        except Exception as e:
            logger.error(f"❌ Question-specific analysis failed: {e}")
            return {'question_type': 'unknown', 'target_answer': None, 'confidence': 0.0}
    
    def _create_analysis_report(self, video_url: str, metadata: Dict[str, Any],
                              detection_results: Dict[str, Any],
                              content_analysis: Dict[str, Any] = None,
                              question: str = None) -> Dict[str, Any]:
        """Create comprehensive analysis report."""
        try:
            report = {
                'success': True,
                'video_url': video_url,
                'question': question,
                'analysis_timestamp': datetime.now().isoformat(),
                'metadata': metadata,
                'detection_results': detection_results,
                'content_analysis': content_analysis,
                'final_answer': None,
                'confidence': 0.0,
                'reasoning': []
            }
            
            # Extract final answer from detection summary
            summary = detection_results.get('summary', {})
            answer_analysis = summary.get('answer_analysis', {})
            
            if answer_analysis.get('target_answer') is not None:
                report['final_answer'] = answer_analysis['target_answer']
                report['confidence'] = answer_analysis.get('confidence', 0.0)
                report['reasoning'] = answer_analysis.get('reasoning', [])
            else:
                # Fallback to general analysis
                if question and 'bird' in question.lower():
                    report['final_answer'] = summary.get('max_simultaneous_birds', 0)
                    report['confidence'] = 0.7
                    report['reasoning'] = [f"Maximum simultaneous birds detected: {report['final_answer']}"]
                elif question and 'animal' in question.lower():
                    report['final_answer'] = summary.get('max_simultaneous_animals', 0)
                    report['confidence'] = 0.7
                    report['reasoning'] = [f"Maximum simultaneous animals detected: {report['final_answer']}"]
                else:
                    report['final_answer'] = summary.get('max_simultaneous_objects', 0)
                    report['confidence'] = 0.5
                    report['reasoning'] = [f"Maximum simultaneous objects detected: {report['final_answer']}"]
            
            # Add analysis insights
            insights = []
            if summary.get('total_frames_analyzed', 0) > 0:
                insights.append(f"Analyzed {summary['total_frames_analyzed']} frames")
            if summary.get('total_detections', 0) > 0:
                insights.append(f"Total detections: {summary['total_detections']}")
            if summary.get('species_counts'):
                species_info = ", ".join([f"{k}: {v}" for k, v in summary['species_counts'].items()])
                insights.append(f"Species distribution: {species_info}")
            
            report['insights'] = insights
            
            logger.info("πŸ“Š Analysis report generated successfully")
            return report
            
        except Exception as e:
            logger.error(f"❌ Failed to create analysis report: {e}")
            return {
                'success': False,
                'error': f'Failed to create analysis report: {str(e)}'
            }
    
    def _cleanup_temp_files(self, video_path: str = None):
        """Clean up temporary files."""
        try:
            if video_path and os.path.exists(video_path):
                os.remove(video_path)
            
            # Clean up temp directory if it exists and is empty
            if os.path.exists(self.temp_dir):
                try:
                    os.rmdir(self.temp_dir)
                except OSError:
                    # Directory not empty, clean up individual files
                    shutil.rmtree(self.temp_dir, ignore_errors=True)
            
        except Exception as e:
            logger.warning(f"⚠️ Cleanup failed: {e}")
    
    def get_capabilities(self) -> Dict[str, Any]:
        """Get video analyzer capabilities."""
        return {
            'available': self.available,
            'detection_engine_available': self.detection_engine is not None and self.detection_engine.available,
            'content_analyzer_available': self.content_analyzer is not None and self.content_analyzer.available,
            'supported_formats': ['YouTube URLs', 'MP4', 'WebM', 'MKV'],
            'max_duration': 300,
            'max_frames': self.max_frames,
            'features': [
                'YouTube video downloading',
                'Object detection and classification',
                'Bird and animal species identification',
                'Temporal object tracking',
                'Simultaneous object counting',
                'Content analysis and summarization',
                'Question-specific analysis'
            ]
        }


# AGNO Framework Integration Functions
def get_advanced_video_analysis_tools() -> List[AdvancedVideoAnalyzer]:
    """Get advanced video analysis tools for AGNO framework integration."""
    try:
        analyzer = AdvancedVideoAnalyzer()
        if analyzer.available:
            return [analyzer]
        else:
            logger.warning("⚠️ Advanced video analyzer not available")
            return []
    except Exception as e:
        logger.error(f"❌ Failed to create advanced video analysis tools: {e}")
        return []


if __name__ == "__main__":
    # Test the advanced video analyzer
    analyzer = AdvancedVideoAnalyzer()
    print(f"Video analyzer available: {analyzer.available}")
    print(f"Capabilities: {json.dumps(analyzer.get_capabilities(), indent=2)}")
    
    # Test with a sample YouTube video (if available)
    test_url = "https://www.youtube.com/watch?v=L1vXCYZAYYM"
    test_question = "What is the highest number of bird species to be on camera simultaneously?"
    
    print(f"\nTesting with: {test_url}")
    print(f"Question: {test_question}")
    
    # Note: Actual testing would require running the analyzer
    # result = analyzer.analyze_video(test_url, test_question)
    # print(f"Result: {json.dumps(result, indent=2)}")