Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from datetime import datetime | |
| import os | |
| def procesar_imagen(imagen): | |
| """Procesa la imagen capturada o subida""" | |
| if imagen is None: | |
| return None, "❌ No se ha capturado ninguna imagen" | |
| # Guardar imagen con timestamp | |
| timestamp = datetime.now().strftime('%Y%m%d_%H%M%S') | |
| filename = f"captura_{timestamp}.jpg" | |
| # Gradio devuelve la imagen como PIL Image o path | |
| if hasattr(imagen, 'save'): | |
| imagen.save(filename) | |
| else: | |
| # Si es un path, copiarlo | |
| import shutil | |
| shutil.copy(imagen, filename) | |
| mensaje = f""" | |
| ✅ **Imagen procesada correctamente** | |
| 📁 Guardada como: `{filename}` | |
| 🕒 Fecha: {datetime.now().strftime('%d/%m/%Y %H:%M:%S')} | |
| ℹ️ Puedes descargarla o seguir capturando más fotos. | |
| """ | |
| return imagen, mensaje | |
| def limpiar(): | |
| """Limpia la interfaz""" | |
| return None, "" | |
| # ============= INTERFAZ GRADIO ============= | |
| with gr.Blocks(title="Captura de Fotos con Webcam") as demo: | |
| gr.Markdown(""" | |
| # 📸 CAPTURA DE FOTOS | |
| ### Usa tu webcam o sube una imagen desde tu dispositivo | |
| """) | |
| with gr.Row(): | |
| with gr.Column(scale=1): | |
| gr.Markdown("### 📷 Capturar o Subir Foto") | |
| # Componente de imagen con webcam - PARÁMETROS CORREGIDOS | |
| imagen_input = gr.Image( | |
| label="Webcam / Subir imagen", | |
| sources=["webcam", "upload"], | |
| type="pil" | |
| ) | |
| with gr.Row(): | |
| btn_procesar = gr.Button("✅ Procesar Imagen", variant="primary", size="lg") | |
| btn_limpiar = gr.Button("🗑️ Limpiar", variant="secondary") | |
| with gr.Column(scale=1): | |
| gr.Markdown("### 🖼️ Resultado") | |
| imagen_output = gr.Image( | |
| label="Imagen procesada", | |
| type="pil" | |
| ) | |
| resultado_texto = gr.Markdown( | |
| value="*Captura o sube una imagen para comenzar...*" | |
| ) | |
| gr.Markdown("---") | |
| gr.Markdown(""" | |
| ### 💡 Instrucciones: | |
| 1. **Opción A:** Click en el icono de cámara 📷 para usar tu webcam | |
| 2. **Opción B:** Click en "Upload" para subir una foto desde tu dispositivo | |
| 3. Captura la foto o selecciona el archivo | |
| 4. Click en "Procesar Imagen" | |
| """) | |
| # Conectar eventos | |
| btn_procesar.click( | |
| fn=procesar_imagen, | |
| inputs=[imagen_input], | |
| outputs=[imagen_output, resultado_texto] | |
| ) | |
| btn_limpiar.click( | |
| fn=limpiar, | |
| inputs=[], | |
| outputs=[imagen_input, resultado_texto] | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() |