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
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
"""
Core Analysis Engine

Main orchestration engine for DeepVision Prompt Builder.
Manages image/video processing, plugin execution, and result generation.
"""

from datetime import datetime
from pathlib import Path
from typing import Dict, List, Any, Optional, Union
from loguru import logger

from core.config import config
from core.image_processor import ImageProcessor
from core.video_processor import VideoProcessor
from core.result_manager import ResultManager
from core.exceptions import DeepVisionError


class AnalysisEngine:
    """
    Main analysis engine for processing images and videos.
    
    Orchestrates the complete analysis pipeline:
    1. File validation and preprocessing
    2. Plugin execution
    3. Result aggregation
    4. JSON output generation
    """
    
    def __init__(self):
        """Initialize AnalysisEngine."""
        self.image_processor = ImageProcessor()
        self.video_processor = VideoProcessor()
        self.result_manager = ResultManager()
        self.plugins: Dict[str, Any] = {}
        self.plugin_order: List[str] = []
        
        logger.info(f"AnalysisEngine initialized - {config.APP_NAME} v{config.APP_VERSION}")
    
    def register_plugin(self, plugin_name: str, plugin_instance: Any) -> None:
        """
        Register a plugin for analysis.
        
        Args:
            plugin_name: Unique name for the plugin
            plugin_instance: Instance of the plugin class
        """
        if plugin_name in self.plugins:
            logger.warning(f"Plugin '{plugin_name}' already registered, replacing")
        
        self.plugins[plugin_name] = plugin_instance
        
        # Maintain execution order
        if plugin_name not in self.plugin_order:
            self.plugin_order.append(plugin_name)
        
        logger.info(f"Registered plugin: {plugin_name}")
    
    def unregister_plugin(self, plugin_name: str) -> None:
        """
        Unregister a plugin.
        
        Args:
            plugin_name: Name of plugin to remove
        """
        if plugin_name in self.plugins:
            del self.plugins[plugin_name]
            
            if plugin_name in self.plugin_order:
                self.plugin_order.remove(plugin_name)
            
            logger.info(f"Unregistered plugin: {plugin_name}")
    
    def get_registered_plugins(self) -> List[str]:
        """
        Get list of registered plugins.
        
        Returns:
            List of plugin names
        """
        return list(self.plugins.keys())
    
    def analyze_image(
        self,
        image_path: Union[str, Path],
        plugins: Optional[List[str]] = None,
        **kwargs
    ) -> Dict[str, Any]:
        """
        Analyze a single image.
        
        Args:
            image_path: Path to image file
            plugins: List of plugin names to use (None for all)
            **kwargs: Additional arguments for processing
            
        Returns:
            Analysis results dictionary
        """
        start_time = datetime.now()
        image_path = Path(image_path)
        
        logger.info(f"Starting image analysis: {image_path.name}")
        
        try:
            # Clear previous results
            self.result_manager.clear()
            
            # Process image
            image = self.image_processor.process(
                image_path,
                resize=kwargs.get("resize", True),
                normalize=kwargs.get("normalize", False)
            )
            
            # Get image info
            image_info = self.image_processor.get_image_info(image_path)
            
            # Set file metadata
            self.result_manager.set_file_info(
                filename=image_info["filename"],
                file_type="image",
                file_size=image_info["file_size"],
                width=image_info["width"],
                height=image_info["height"],
                format=image_info["format"],
                hash=image_info["hash"],
            )
            
            # Execute plugins
            plugins_used = self._execute_plugins(
                image,
                image_path,
                plugins,
                media_type="image"
            )
            
            # Set processing metadata
            end_time = datetime.now()
            self.result_manager.set_processing_info(
                start_time=start_time,
                end_time=end_time,
                plugins_used=plugins_used
            )
            
            # Get final results
            results = self.result_manager.to_dict(
                include_metadata=config.INCLUDE_METADATA
            )
            
            logger.info(f"Image analysis completed: {image_path.name} "
                       f"({len(plugins_used)} plugins)")
            
            return results
            
        except Exception as e:
            logger.error(f"Image analysis failed: {e}")
            raise DeepVisionError(
                f"Analysis failed for {image_path.name}: {str(e)}",
                {"path": str(image_path), "error": str(e)}
            )
    
    def analyze_video(
        self,
        video_path: Union[str, Path],
        plugins: Optional[List[str]] = None,
        extract_method: str = "keyframes",
        num_frames: int = 5,
        **kwargs
    ) -> Dict[str, Any]:
        """
        Analyze a video by extracting and analyzing frames.
        
        Args:
            video_path: Path to video file
            plugins: List of plugin names to use
            extract_method: Frame extraction method ("fps" or "keyframes")
            num_frames: Number of frames to extract
            **kwargs: Additional arguments
            
        Returns:
            Analysis results dictionary
        """
        start_time = datetime.now()
        video_path = Path(video_path)
        
        logger.info(f"Starting video analysis: {video_path.name}")
        
        try:
            # Clear previous results
            self.result_manager.clear()
            
            # Get video info
            video_info = self.video_processor.get_video_info(video_path)
            
            # Set file metadata
            self.result_manager.set_file_info(
                filename=video_info["filename"],
                file_type="video",
                file_size=video_info["file_size"],
                width=video_info["width"],
                height=video_info["height"],
                fps=video_info["fps"],
                duration=video_info["duration"],
                frame_count=video_info["frame_count"],
            )
            
            # Extract frames
            if extract_method == "keyframes":
                frame_paths = self.video_processor.extract_key_frames(
                    video_path,
                    num_frames=num_frames
                )
            else:
                frame_paths = self.video_processor.extract_frames(
                    video_path,
                    max_frames=num_frames,
                    **kwargs
                )
            
            logger.info(f"Extracted {len(frame_paths)} frames from video")
            
            # Analyze each frame
            frame_results = []
            for idx, frame_path in enumerate(frame_paths):
                logger.info(f"Analyzing frame {idx + 1}/{len(frame_paths)}")
                
                # Process frame
                image = self.image_processor.process(frame_path, resize=True)
                
                # Execute plugins on frame
                plugins_used = self._execute_plugins(
                    image,
                    frame_path,
                    plugins,
                    media_type="video_frame"
                )
                
                # Get frame results
                frame_result = {
                    "frame_index": idx,
                    "frame_path": str(frame_path.name),
                    "results": dict(self.result_manager.results)
                }
                frame_results.append(frame_result)
                
                # Clear for next frame
                self.result_manager.results.clear()
            
            # Aggregate frame results
            aggregated = self._aggregate_video_results(frame_results)
            
            # Set aggregated results
            self.result_manager.results = aggregated
            
            # Set processing metadata
            end_time = datetime.now()
            self.result_manager.set_processing_info(
                start_time=start_time,
                end_time=end_time,
                plugins_used=plugins_used
            )
            
            # Add video-specific metadata
            self.result_manager.add_metadata({
                "frames_analyzed": len(frame_paths),
                "extraction_method": extract_method,
            })
            
            # Get final results
            results = self.result_manager.to_dict(
                include_metadata=config.INCLUDE_METADATA
            )
            
            logger.info(f"Video analysis completed: {video_path.name} "
                       f"({len(frame_paths)} frames, {len(plugins_used)} plugins)")
            
            return results
            
        except Exception as e:
            logger.error(f"Video analysis failed: {e}")
            raise DeepVisionError(
                f"Analysis failed for {video_path.name}: {str(e)}",
                {"path": str(video_path), "error": str(e)}
            )
    
    def _execute_plugins(
        self,
        media,
        media_path: Path,
        plugin_names: Optional[List[str]] = None,
        media_type: str = "image"
    ) -> List[str]:
        """
        Execute registered plugins on media.
        
        Args:
            media: Processed media (image or frame)
            media_path: Path to media file
            plugin_names: List of plugins to execute (None for all)
            media_type: Type of media being processed
            
        Returns:
            List of executed plugin names
        """
        # Determine which plugins to execute
        if plugin_names is None:
            plugins_to_run = self.plugin_order
        else:
            plugins_to_run = [
                p for p in self.plugin_order if p in plugin_names
            ]
        
        executed = []
        
        for plugin_name in plugins_to_run:
            if plugin_name not in self.plugins:
                logger.warning(f"Plugin '{plugin_name}' not found, skipping")
                continue
            
            try:
                logger.debug(f"Executing plugin: {plugin_name}")
                
                plugin = self.plugins[plugin_name]
                
                # Execute plugin
                result = plugin.analyze(media, media_path)
                
                # Add result
                self.result_manager.add_result(plugin_name, result)
                
                executed.append(plugin_name)
                
                logger.debug(f"Plugin '{plugin_name}' completed successfully")
                
            except Exception as e:
                logger.error(f"Plugin '{plugin_name}' failed: {e}")
                
                # Add error to results
                self.result_manager.add_result(
                    plugin_name,
                    {
                        "error": str(e),
                        "status": "failed"
                    }
                )
        
        return executed
    
    def _aggregate_video_results(
        self,
        frame_results: List[Dict[str, Any]]
    ) -> Dict[str, Any]:
        """
        Aggregate results from multiple video frames.
        
        Args:
            frame_results: List of results from each frame
            
        Returns:
            Aggregated results dictionary
        """
        aggregated = {
            "frames": frame_results,
            "summary": {}
        }
        
        # For each plugin, aggregate results across frames
        if not frame_results:
            return aggregated
        
        # Get plugin names from first frame
        first_frame = frame_results[0]["results"]
        
        for plugin_name in first_frame.keys():
            plugin_summary = self._aggregate_plugin_results(
                plugin_name,
                [f["results"].get(plugin_name, {}) for f in frame_results]
            )
            aggregated["summary"][plugin_name] = plugin_summary
        
        return aggregated
    
    def _aggregate_plugin_results(
        self,
        plugin_name: str,
        results: List[Dict[str, Any]]
    ) -> Dict[str, Any]:
        """
        Aggregate results for a specific plugin across frames.
        
        Args:
            plugin_name: Name of the plugin
            results: List of results from each frame
            
        Returns:
            Aggregated result for the plugin
        """
        # Default aggregation: collect all unique values
        aggregated = {
            "frames_processed": len(results),
        }
        
        # Plugin-specific aggregation logic
        if plugin_name == "object_detector":
            all_objects = []
            for result in results:
                all_objects.extend(result.get("objects", []))
            
            # Count object occurrences
            object_counts = {}
            for obj in all_objects:
                name = obj["name"]
                object_counts[name] = object_counts.get(name, 0) + 1
            
            aggregated["total_objects"] = len(all_objects)
            aggregated["unique_objects"] = len(object_counts)
            aggregated["object_frequency"] = object_counts
        
        elif plugin_name == "caption_generator":
            captions = [r.get("caption", "") for r in results if r.get("caption")]
            aggregated["captions"] = captions
            aggregated["caption_count"] = len(captions)
        
        elif plugin_name == "color_analyzer":
            all_colors = []
            for result in results:
                all_colors.extend(result.get("dominant_colors", []))
            
            # Get most frequent colors
            color_counts = {}
            for color in all_colors:
                name = color["name"]
                color_counts[name] = color_counts.get(name, 0) + 1
            
            aggregated["color_frequency"] = color_counts
        
        return aggregated
    
    def analyze(
        self,
        file_path: Union[str, Path],
        **kwargs
    ) -> Dict[str, Any]:
        """
        Automatically detect file type and analyze.
        
        Args:
            file_path: Path to image or video file
            **kwargs: Additional arguments
            
        Returns:
            Analysis results
        """
        file_path = Path(file_path)
        
        # Detect file type
        ext = file_path.suffix.lower()
        
        if ext in config.ALLOWED_IMAGE_FORMATS:
            return self.analyze_image(file_path, **kwargs)
        elif ext in config.ALLOWED_VIDEO_FORMATS:
            return self.analyze_video(file_path, **kwargs)
        else:
            raise ValueError(f"Unsupported file format: {ext}")
    
    def __repr__(self) -> str:
        """Object representation."""
        return (f"AnalysisEngine(plugins={len(self.plugins)}, "
                f"registered={self.get_registered_plugins()})")