Spaces:
Runtime error
Runtime error
File size: 9,537 Bytes
8fb6272 |
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 |
import gradio as gr
import os
from typing import Tuple, Any
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
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.use_case = ProcessVideoUseCase(
detector=self.detector,
notification_factory=self.notification_factory,
default_fps=self.default_fps,
default_resolution=int(self.default_resolution)
)
def list_sample_videos(self) -> list:
"""Lista os vídeos de exemplo na pasta videos."""
video_dir = Path("videos")
if not video_dir.exists():
os.makedirs(video_dir)
return []
video_extensions = ['.mp4', '.avi', '.mov', '.mkv']
videos = []
# Procurar em subdiretórios específicos
for status_dir in ['seguro', 'risco_detectado']:
dir_path = video_dir / status_dir
if dir_path.exists():
for ext in video_extensions:
for video_path in dir_path.glob(f'*{ext}'):
videos.append({
'path': str(video_path),
'name': video_path.name,
'ground_truth': '✅ SEGURO (Ground Truth)' if status_dir == 'seguro' else '⚠️ RISCO DETECTADO (Ground Truth)'
})
return videos
def load_sample_video(self, video_path: str) -> str:
"""Carrega um vídeo de exemplo."""
try:
return video_path
except Exception as e:
return None
def create_interface(self) -> gr.Blocks:
"""Cria a interface Gradio."""
title = "Detector de Riscos em Vídeos"
sample_videos = self.list_sample_videos()
with gr.Blocks(
title=title,
theme=gr.themes.Ocean(),
css="footer {display: none !important}"
) as demo:
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
""")
with gr.Group():
gr.Markdown("""### Configuração de Processamento""")
with gr.Row():
threshold = gr.Slider(
minimum=0.1,
maximum=1.0,
value=0.5,
step=0.1,
label="Limiar de Detecção",
)
fps = gr.Slider(
minimum=1,
maximum=5,
value=self.default_fps,
step=1,
label="Frames por Segundo",
)
resolution = gr.Radio(
choices=["480", "640", "768"],
value=self.default_resolution,
label="Resolução de Processamento",
)
with gr.Group():
gr.Markdown("""### Configuração de Notificações de Detecção (Opcional)""")
with gr.Row():
notification_type = gr.Radio(
choices=self.notification_factory.get_available_services(),
value="email",
label="Tipo de Notificação",
interactive=True,
)
notification_target = gr.Textbox(
label="Destino da Notificação (E-mail)",
placeholder="[email protected]",
)
with gr.Row():
with gr.Column(scale=2):
input_video = gr.Video(
label="Vídeo de Entrada",
format="mp4",
interactive=True,
height=400
)
submit_btn = gr.Button(
"Analisar Vídeo",
variant="primary",
scale=2
)
with gr.Column(scale=1):
status = gr.Textbox(
label="Status da Detecção",
lines=4,
show_copy_button=True
)
with gr.Accordion("Detalhes Técnicos", open=False):
json_output = gr.JSON(
label="Detalhes Técnicos",
)
# Informações adicionais
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
""")
# Vídeos de exemplo
if sample_videos:
with gr.Group():
gr.Markdown("### Vídeos de Exemplo")
with gr.Row():
with gr.Column(scale=3):
gr.Markdown("#### Vídeo")
with gr.Column(scale=2):
gr.Markdown("#### Status Real")
with gr.Column(scale=1):
gr.Markdown("#### Ação")
for video in sample_videos:
with gr.Row():
with gr.Column(scale=3):
gr.Video(
value=video['path'],
format="mp4",
height=150,
interactive=False,
show_label=False
)
with gr.Column(scale=2, min_width=200):
gr.Markdown(video['ground_truth'])
with gr.Column(scale=1, min_width=100):
gr.Button(
"📥 Carregar",
size="sm"
).click(
fn=self.load_sample_video,
inputs=[gr.State(video['path'])],
outputs=[input_video]
)
# Configurar callback do botão
submit_btn.click(
fn=lambda *args: self._process_video(*args),
inputs=[
input_video,
threshold,
fps,
resolution,
notification_type,
notification_target
],
outputs=[status, json_output]
)
return demo
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, Any]:
"""Processa o vídeo usando o caso de uso."""
if not video_path:
return "Erro: Nenhum vídeo fornecido", {}
# Usar valores padrão se não especificados
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)
return (
response.status_message,
response.detection_result.__dict__
) |