File size: 15,078 Bytes
8fb6272
 
479ff3a
8fb6272
 
 
 
c09ca6f
739fe61
 
c09ca6f
 
8fb6272
 
 
 
 
 
 
 
 
4add9f7
8fb6272
739fe61
397303b
37eeee3
8fb6272
 
 
 
 
 
 
397303b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
739fe61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8fb6272
4add9f7
 
 
397303b
4add9f7
397303b
4add9f7
 
c09ca6f
8fb6272
397303b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4add9f7
739fe61
 
c09ca6f
739fe61
37eeee3
739fe61
 
 
 
37eeee3
739fe61
c09ca6f
 
 
 
8fb6272
 
 
ea56678
8fb6272
 
 
 
 
 
 
397303b
 
 
 
 
 
 
 
 
 
 
 
 
 
8fb6272
397303b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8fb6272
397303b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8fb6272
397303b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8fb6272
397303b
 
 
 
f84a89d
8fb6272
397303b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8fb6272
397303b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8fb6272
 
 
 
 
 
 
 
 
479ff3a
8fb6272
479ff3a
 
 
 
 
 
8fb6272
479ff3a
 
 
 
 
 
 
 
 
 
 
397303b
479ff3a
 
 
 
 
 
 
 
 
 
 
397303b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
479ff3a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import os
from typing import Tuple, Any, Dict
from pathlib import Path
from src.application.use_cases.process_video import ProcessVideoUseCase, ProcessVideoRequest
from src.infrastructure.services.weapon_detector import WeaponDetectorService
from src.infrastructure.services.notification_services import NotificationServiceFactory
import logging
from huggingface_hub import hf_hub_download, HfApi
import tempfile

logger = logging.getLogger(__name__)

class GradioInterface:
    """Interface Gradio usando Clean Architecture."""
    
    def __init__(self):
        self.detector = WeaponDetectorService()
        self.notification_factory = NotificationServiceFactory()
        self.default_fps = 2 if self.detector.device_type == "GPU" else 1
        self.default_resolution = "640" if self.detector.device_type == "GPU" else "480"
        self.is_huggingface = os.getenv('SPACE_ID') is not None
        
        if self.is_huggingface:
            self._setup_huggingface_environment()
        
        self.use_case = ProcessVideoUseCase(
            detector=self.detector,
            notification_factory=self.notification_factory,
            default_fps=self.default_fps,
            default_resolution=int(self.default_resolution)
        )
    
    def _setup_huggingface_environment(self):
        """Configura o ambiente Hugging Face."""
        self.dataset_id = "marcuscanhaco/weapon-test"
        self.cache_dir = os.path.join(tempfile.gettempdir(), 'weapon_detection_videos')
        os.makedirs(self.cache_dir, exist_ok=True)
        
        self.hf_token = os.getenv('HF_TOKEN')
        self.api = HfApi(token=self.hf_token)
        
        try:
            files = self.api.list_repo_files(self.dataset_id, repo_type="dataset")
            self.sample_videos = [
                {
                    'path': f,
                    'name': Path(f).stem.replace('_', ' ').title(),
                    'ground_truth': '🚨 Vídeo de Teste'
                }
                for f in files if f.lower().endswith(('.mp4', '.avi', '.mov', '.mkv'))
            ]
            logger.info(f"Encontrados {len(self.sample_videos)} vídeos no dataset")
        except Exception as e:
            logger.error(f"Erro ao listar arquivos do dataset: {str(e)}")
            self.sample_videos = []
    
    def _download_video(self, video_path: str) -> str:
        """Baixa um vídeo do dataset e retorna o caminho local."""
        try:
            local_path = hf_hub_download(
                repo_id=self.dataset_id,
                filename=video_path,
                repo_type="dataset",
                local_dir=self.cache_dir,
                token=self.hf_token,
                local_dir_use_symlinks=False
            )
            logger.info(f"Vídeo baixado com sucesso: {local_path}")
            return local_path
        except Exception as e:
            logger.error(f"Erro ao baixar vídeo {video_path}: {str(e)}")
            return ""
    
    def list_sample_videos(self) -> list:
        """Lista os vídeos de exemplo do dataset ou da pasta local."""
        try:
            if self.is_huggingface:
                return self._list_huggingface_videos()
            else:
                return self._list_local_videos()
            
        except Exception as e:
            logger.error(f"Erro ao listar vídeos: {str(e)}")
            return []
    
    def _list_huggingface_videos(self) -> list:
        """Lista vídeos do ambiente Hugging Face."""
        logger.info("Ambiente Hugging Face detectado")
        videos = []
        for video in self.sample_videos:
            local_path = self._download_video(video['path'])
            if local_path:
                videos.append({
                    'path': local_path,
                    'name': video['name'],
                    'ground_truth': video['ground_truth']
                })
        return videos
    
    def _list_local_videos(self) -> list:
        """Lista vídeos do ambiente local."""
        logger.info("Ambiente local detectado, usando pasta videos")
        video_extensions = ['.mp4', '.avi', '.mov', '.mkv']
        videos = []
        base_dir = Path("videos")
        if not base_dir.exists():
            os.makedirs(base_dir)
            logger.info(f"Diretório videos criado: {base_dir}")
        
        for ext in video_extensions:
            for video_path in base_dir.glob(f'*{ext}'):
                videos.append({
                    'path': str(video_path),
                    'name': video_path.name,
                    'ground_truth': '📼 Vídeo de Teste'
                })
        
        return videos

    def load_sample_video(self, video_path: str) -> str:
        """Carrega um vídeo de exemplo."""
        try:
            if not video_path:
                return ""
            
            if os.path.exists(video_path):
                logger.info(f"Carregando vídeo: {video_path}")
                return video_path
                
            logger.warning(f"Vídeo não encontrado: {video_path}")
            return ""
        except Exception as e:
            logger.error(f"Erro ao carregar vídeo: {str(e)}")
            return ""
    
    def create_interface(self) -> gr.Blocks:
        """Cria a interface Gradio."""
        title = "FIAP VisionGuard - Risk Detection - Hackatoon 1IADT"
        sample_videos = self.list_sample_videos()
        
        with gr.Blocks(
            title=title,
            theme=gr.themes.Ocean(),
            css="footer {display: none !important}"
        ) as demo:
            self._create_header(title)
            self._create_processing_config()
            self._create_notification_config()
            self._create_video_interface()
            self._create_sample_videos(sample_videos)
        
        return demo
    
    def _create_header(self, title: str):
        """Cria o cabeçalho da interface."""
        gr.Markdown(f"""# 🎯 {title} 🔪🔫
        
        Faça upload de um vídeo para detectar objetos perigosos.
        Opcionalmente, configure notificações para receber alertas em caso de detecções.

        **Importante para melhor performance:**
        - Vídeos de até 60 segundos
        - FPS entre 1-2 para análise com maior performance
        - FPS maior que 2 para análise com maior precisão
        """)
    
    def _create_processing_config(self):
        """Cria a seção de configuração de processamento."""
        with gr.Group():
            gr.Markdown("""### Configuração de Processamento""")
            with gr.Row():
                self.threshold = gr.Slider(
                    minimum=0.1,
                    maximum=1.0,
                    value=0.5,
                    step=0.1,
                    label="Limiar de Detecção",
                )
                self.fps = gr.Slider(
                    minimum=1,
                    maximum=5,
                    value=self.default_fps,
                    step=1,
                    label="Frames por Segundo",
                )
                self.resolution = gr.Radio(
                    choices=["480", "640", "768"],
                    value=self.default_resolution,
                    label="Resolução de Processamento",
                )
    
    def _create_notification_config(self):
        """Cria a seção de configuração de notificações."""
        with gr.Group():
            gr.Markdown("""### Configuração de Notificações de Detecção (Opcional)""")
            with gr.Row():
                self.notification_type = gr.Radio(
                    choices=self.notification_factory.get_available_services(),
                    value="email",
                    label="Tipo de Notificação",
                    interactive=True,
                )
                self.notification_target = gr.Textbox(
                    label="Destino da Notificação (E-mail)",
                    placeholder="[email protected]",
                )
    
    def _create_video_interface(self):
        """Cria a interface de vídeo."""
        with gr.Row():
            with gr.Column(scale=2):
                self.input_video = gr.Video(
                    label="Vídeo de Entrada",
                    format="mp4",
                    interactive=True,
                    height=400
                )
                
                self.submit_btn = gr.Button(
                    "Detectar",
                    variant="primary",
                    scale=2
                )
            
            with gr.Column(scale=1):
                self.status = gr.Textbox(
                    label="Status da Detecção",
                    lines=4,
                    show_copy_button=True
                )
                with gr.Accordion("Detalhes Técnicos", open=False):
                    self.json_output = gr.JSON(
                        label="Detalhes Técnicos",
                    )
                
                with gr.Accordion("Informações Adicionais", open=False):
                    gr.Markdown("""
                    ### Sobre o Detector
                    Este sistema utiliza um modelo de IA avançado para detectar objetos perigosos em vídeos.
                    
                    ### Tipos de Objetos Detectados
                    - Armas de fogo (pistolas, rifles, etc.)
                    - Armas brancas (facas, canivetes, etc.)
                    - Objetos perigosos (bastões, objetos pontiagudos, etc.)
                    
                    ### Recomendações
                    - Use vídeos com boa iluminação
                    - Evite vídeos muito longos
                    - Mantenha os objetos visíveis e em foco
                    """)
        
        self.submit_btn.click(
            fn=lambda *args: self._process_video(*args),
            inputs=[
                self.input_video,
                self.threshold,
                self.fps,
                self.resolution,
                self.notification_type,
                self.notification_target
            ],
            outputs=[self.status, self.json_output]
        )
    
    def _create_sample_videos(self, sample_videos: list):
        """Cria a seção de vídeos de exemplo."""
        if sample_videos:
            gr.Markdown("### Vídeos de Exemplo")
            examples = [
                [video['path']] for video in sample_videos
            ]
            gr.Examples(
                examples=examples,
                inputs=self.input_video,
                outputs=self.input_video,
                fn=self.load_sample_video,
                label="Clique em um vídeo para carregá-lo"
            )
    
    def _process_video(
        self,
        video_path: str,
        threshold: float = 0.5,
        fps: int = None,
        resolution: str = None,
        notification_type: str = None,
        notification_target: str = None
    ) -> Tuple[str, Dict[str, Any]]:
        """Processa o vídeo usando o caso de uso."""
        try:
            if not video_path:
                return "Erro: Nenhum vídeo fornecido", {}
                
            fps = fps or self.default_fps
            resolution = resolution or self.default_resolution
            
            request = ProcessVideoRequest(
                video_path=video_path,
                threshold=threshold,
                fps=fps,
                resolution=int(resolution),
                notification_type=notification_type,
                notification_target=notification_target
            )
            
            response = self.use_case.execute(request)
            status_msg = self._format_status_message(response.detection_result)
            technical_data = self._format_technical_data(response, fps, resolution)
            
            return status_msg, technical_data
            
        except Exception as e:
            logger.error(f"Erro ao processar vídeo: {str(e)}")
            return "Erro ao processar o vídeo. Por favor, tente novamente.", {
                "error": str(e),
                "device_type": "unknown",
                "total_detections": 0,
                "frames_analyzed": 0
            }
    
    def _format_technical_data(
        self,
        response: Any,
        fps: int,
        resolution: str
    ) -> Dict[str, Any]:
        """Formata os dados técnicos do processamento."""
        technical_data = {
            "device_info": {
                "type": response.detection_result.device_type,
                "memory": response.memory_info,
                "details": response.device_info
            },
            "processing_stats": {
                "total_detections": len(response.detection_result.detections),
                "frames_analyzed": response.detection_result.frames_analyzed,
                "total_time": round(response.detection_result.total_time, 2),
                "frame_extraction_time": round(response.detection_result.frame_extraction_time, 2),
                "analysis_time": round(response.detection_result.analysis_time, 2),
                "fps": fps,
                "resolution": resolution
            },
            "detections": [],
            "cache_stats": response.cache_stats if hasattr(response, 'cache_stats') else {}
        }
        
        for det in response.detection_result.detections[:10]:
            technical_data["detections"].append({
                "label": det.label,
                "confidence": round(det.confidence * 100 if det.confidence <= 1.0 else det.confidence, 2),
                "frame": det.frame,
                "timestamp": f"{int(det.timestamp // 60):02d}:{int(det.timestamp % 60):02d}",
                "box": det.box if hasattr(det, "box") else None
            })
        
        return technical_data
            
    def _format_status_message(self, result) -> str:
        """Formata a mensagem de status do processamento."""
        try:
            status = "⚠️ RISCO DETECTADO" if result.detections else "✅ SEGURO"
            
            message = f"""Status: {status}
Processado em: {result.device_type}
Total de detecções: {len(result.detections)}
Frames analisados: {result.frames_analyzed}
Tempo total: {result.total_time:.2f}s"""

            if result.detections:
                message += "\n\nDetecções encontradas:"
                for i, det in enumerate(result.detections[:5], 1):
                    confidence_pct = det.confidence * 100 if det.confidence <= 1.0 else det.confidence
                    message += f"\n{i}. {det.label} (Confiança: {confidence_pct:.1f}%, Frame: {det.frame})"
                if len(result.detections) > 5:
                    message += f"\n... e mais {len(result.detections) - 5} detecção(ões)"
            
            return message
            
        except Exception as e:
            logger.error(f"Erro ao formatar mensagem de status: {str(e)}")
            return "Erro ao processar o vídeo. Por favor, tente novamente."