Spaces:
Build error
Build error
Commit
·
1b820c5
1
Parent(s):
33bb295
Implementar modo de cámara simple compatible con Hugging Face Spaces
Browse files- streamlit_app.py +74 -0
streamlit_app.py
CHANGED
|
@@ -2389,6 +2389,80 @@ def main():
|
|
| 2389 |
# Add a note about privacy
|
| 2390 |
st.markdown("---")
|
| 2391 |
st.markdown("**Privacy Note**: Video is processed in your browser and on the server. No video data is stored permanently.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2392 |
|
| 2393 |
# Si se ejecuta este archivo directamente, llamar a la función main
|
| 2394 |
if __name__ == "__main__":
|
|
|
|
| 2389 |
# Add a note about privacy
|
| 2390 |
st.markdown("---")
|
| 2391 |
st.markdown("**Privacy Note**: Video is processed in your browser and on the server. No video data is stored permanently.")
|
| 2392 |
+
|
| 2393 |
+
# Añadir opción de cámara alternativa para entornos donde WebRTC no funciona bien
|
| 2394 |
+
st.markdown("---")
|
| 2395 |
+
st.markdown("### Modo de cámara alternativo")
|
| 2396 |
+
|
| 2397 |
+
col1, col2 = st.columns(2)
|
| 2398 |
+
if col1.button("Usar cámara simple"):
|
| 2399 |
+
st.session_state.simple_camera = True
|
| 2400 |
+
st.session_state.demo_running = False
|
| 2401 |
+
st.session_state.upload_mode = False
|
| 2402 |
+
|
| 2403 |
+
if col2.button("Detener cámara simple"):
|
| 2404 |
+
st.session_state.simple_camera = False
|
| 2405 |
+
|
| 2406 |
+
if st.session_state.get('simple_camera', False):
|
| 2407 |
+
# Contenedor para la cámara
|
| 2408 |
+
camera_container = st.container()
|
| 2409 |
+
|
| 2410 |
+
# Configurar métricas
|
| 2411 |
+
faces_metric.metric("Faces detected", 0)
|
| 2412 |
+
fps_metric.metric("FPS", "0.0")
|
| 2413 |
+
time_metric.metric("Status", "Running")
|
| 2414 |
+
|
| 2415 |
+
# Cámara simple que toma una imagen a la vez
|
| 2416 |
+
with camera_container:
|
| 2417 |
+
st.info("Cámara simple activada. Cada imagen se procesa individualmente.")
|
| 2418 |
+
|
| 2419 |
+
# Usar imagen de la cámara
|
| 2420 |
+
captured_image = st.camera_input("Tomar foto para reconocimiento", key="camera_simple")
|
| 2421 |
+
|
| 2422 |
+
# Procesar la imagen si está disponible
|
| 2423 |
+
if captured_image is not None:
|
| 2424 |
+
# Leer imagen
|
| 2425 |
+
image_bytes = captured_image.getvalue()
|
| 2426 |
+
image = cv2.imdecode(np.frombuffer(image_bytes, np.uint8), cv2.IMREAD_COLOR)
|
| 2427 |
+
|
| 2428 |
+
# Detectar rostros
|
| 2429 |
+
bboxes = detect_face_dnn(face_net, image, confidence_threshold)
|
| 2430 |
+
|
| 2431 |
+
# Actualizar métricas
|
| 2432 |
+
faces_metric.metric("Faces detected", len(bboxes))
|
| 2433 |
+
fps_metric.metric("FPS", "N/A")
|
| 2434 |
+
|
| 2435 |
+
# Dibujar resultados
|
| 2436 |
+
result_img = image.copy()
|
| 2437 |
+
for i, bbox in enumerate(bboxes):
|
| 2438 |
+
x1, y1, x2, y2, _ = bbox
|
| 2439 |
+
cv2.rectangle(result_img, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
| 2440 |
+
cv2.putText(result_img, f"Face {i+1}", (x1, y1-10),
|
| 2441 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)
|
| 2442 |
+
|
| 2443 |
+
# Mostrar resultado
|
| 2444 |
+
st.image(result_img, channels="BGR", caption="Rostros detectados", use_column_width=True)
|
| 2445 |
+
|
| 2446 |
+
if len(bboxes) > 0:
|
| 2447 |
+
st.success(f"Se detectaron {len(bboxes)} rostros")
|
| 2448 |
+
else:
|
| 2449 |
+
st.warning("No se detectaron rostros. Intente con una iluminación mejor o una posición diferente.")
|
| 2450 |
+
|
| 2451 |
+
# Opción alternativa en caso de problemas con WebRTC (mantenemos esta opción también)
|
| 2452 |
+
st.markdown("---")
|
| 2453 |
+
st.markdown("### Otras opciones")
|
| 2454 |
+
col1, col2 = st.columns(2)
|
| 2455 |
+
demo_mode = col1.button("Usar modo de demostración")
|
| 2456 |
+
upload_mode = col2.button("Subir imagen para reconocimiento")
|
| 2457 |
+
|
| 2458 |
+
if demo_mode:
|
| 2459 |
+
st.session_state.demo_running = True
|
| 2460 |
+
st.session_state.upload_mode = False
|
| 2461 |
+
st.session_state.simple_camera = False
|
| 2462 |
+
elif upload_mode:
|
| 2463 |
+
st.session_state.upload_mode = True
|
| 2464 |
+
st.session_state.demo_running = False
|
| 2465 |
+
st.session_state.simple_camera = False
|
| 2466 |
|
| 2467 |
# Si se ejecuta este archivo directamente, llamar a la función main
|
| 2468 |
if __name__ == "__main__":
|