Spaces:
Running
Running
Commit
·
4c778c9
1
Parent(s):
ef29226
fix: [Lunes 25/03/2024 15:45] Corregido error de redimensionamiento de imágenes - Se agregaron validaciones para el tamaño de rostros detectados, manejo de coordenadas inválidas y redimensionamiento seguro de miniaturas. Se implementó un tamaño mínimo de 64x64 píxeles para las imágenes de rostros y se mejoró el manejo de errores en la visualización de miniaturas.
Browse files- streamlit_app.py +91 -50
streamlit_app.py
CHANGED
|
@@ -1430,7 +1430,24 @@ def main():
|
|
| 1430 |
if embeddings_all_models:
|
| 1431 |
# Guardar la imagen del rostro para referencia
|
| 1432 |
x1, y1, x2, y2, _ = bboxes[0]
|
| 1433 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1434 |
|
| 1435 |
# Guardar en la base de datos
|
| 1436 |
if add_to_existing and person_name in st.session_state.face_database:
|
|
@@ -1505,55 +1522,72 @@ def main():
|
|
| 1505 |
if embeddings_all_models:
|
| 1506 |
# Extraer la región del rostro para guardarla
|
| 1507 |
x1, y1, x2, y2, _ = bboxes[0]
|
| 1508 |
-
|
|
|
|
|
|
|
| 1509 |
|
| 1510 |
-
|
| 1511 |
-
|
| 1512 |
-
#
|
| 1513 |
-
if
|
| 1514 |
-
|
| 1515 |
-
|
| 1516 |
-
|
| 1517 |
-
|
| 1518 |
-
|
| 1519 |
-
|
| 1520 |
-
|
| 1521 |
-
|
| 1522 |
-
|
| 1523 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1524 |
|
| 1525 |
-
|
| 1526 |
-
|
| 1527 |
-
st.session_state.face_database[person_name]['embeddings'][model_idx] = embedding['embedding']
|
| 1528 |
-
else:
|
| 1529 |
-
# Añadir nuevo modelo
|
| 1530 |
-
st.session_state.face_database[person_name]['models'].append(model_name)
|
| 1531 |
-
st.session_state.face_database[person_name]['embeddings'].append(embedding['embedding'])
|
| 1532 |
-
|
| 1533 |
-
# Actualizar imagen de referencia
|
| 1534 |
-
st.session_state.face_database[person_name]['face_image'] = face_crop
|
| 1535 |
|
| 1536 |
# Incrementar contador
|
| 1537 |
st.session_state.face_database[person_name]['count'] += 1
|
| 1538 |
-
|
| 1539 |
-
|
| 1540 |
-
|
| 1541 |
-
|
| 1542 |
-
|
| 1543 |
-
|
| 1544 |
-
|
| 1545 |
-
|
| 1546 |
-
|
| 1547 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1548 |
|
| 1549 |
-
st.
|
| 1550 |
-
'embeddings': embeddings,
|
| 1551 |
-
'models': models,
|
| 1552 |
-
'count': 1,
|
| 1553 |
-
'face_image': face_crop
|
| 1554 |
-
}
|
| 1555 |
-
|
| 1556 |
-
st.success(f"Face registered successfully for {person_name}!")
|
| 1557 |
|
| 1558 |
# Guardar la base de datos actualizada
|
| 1559 |
if DATABASE_UTILS_AVAILABLE:
|
|
@@ -1633,12 +1667,19 @@ def main():
|
|
| 1633 |
|
| 1634 |
# Mostrar miniatura si está disponible
|
| 1635 |
with col_thumb:
|
| 1636 |
-
if row["Face"] is not None:
|
| 1637 |
-
|
| 1638 |
-
|
| 1639 |
-
|
| 1640 |
-
|
| 1641 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1642 |
else:
|
| 1643 |
st.write("No image")
|
| 1644 |
|
|
|
|
| 1430 |
if embeddings_all_models:
|
| 1431 |
# Guardar la imagen del rostro para referencia
|
| 1432 |
x1, y1, x2, y2, _ = bboxes[0]
|
| 1433 |
+
# Validar coordenadas
|
| 1434 |
+
x1, y1 = max(0, x1), max(0, y1)
|
| 1435 |
+
x2, y2 = min(image.shape[1], x2), min(image.shape[0], y2)
|
| 1436 |
+
|
| 1437 |
+
if x2 > x1 and y2 > y1:
|
| 1438 |
+
face_crop = image[y1:y2, x1:x2].copy()
|
| 1439 |
+
# Asegurar un tamaño mínimo para el rostro
|
| 1440 |
+
if face_crop.size > 0:
|
| 1441 |
+
min_size = 64
|
| 1442 |
+
face_h, face_w = face_crop.shape[:2]
|
| 1443 |
+
if face_h < min_size or face_w < min_size:
|
| 1444 |
+
scale = max(min_size/face_h, min_size/face_w)
|
| 1445 |
+
face_crop = cv2.resize(face_crop,
|
| 1446 |
+
(max(min_size, int(face_w * scale)),
|
| 1447 |
+
max(min_size, int(face_h * scale))))
|
| 1448 |
+
else:
|
| 1449 |
+
st.error("Invalid face region detected. Please try again with a clearer image.")
|
| 1450 |
+
return
|
| 1451 |
|
| 1452 |
# Guardar en la base de datos
|
| 1453 |
if add_to_existing and person_name in st.session_state.face_database:
|
|
|
|
| 1522 |
if embeddings_all_models:
|
| 1523 |
# Extraer la región del rostro para guardarla
|
| 1524 |
x1, y1, x2, y2, _ = bboxes[0]
|
| 1525 |
+
# Validar coordenadas
|
| 1526 |
+
x1, y1 = max(0, x1), max(0, y1)
|
| 1527 |
+
x2, y2 = min(image.shape[1], x2), min(image.shape[0], y2)
|
| 1528 |
|
| 1529 |
+
if x2 > x1 and y2 > y1:
|
| 1530 |
+
face_crop = image[y1:y2, x1:x2].copy()
|
| 1531 |
+
# Asegurar un tamaño mínimo para el rostro
|
| 1532 |
+
if face_crop.size > 0:
|
| 1533 |
+
min_size = 64
|
| 1534 |
+
face_h, face_w = face_crop.shape[:2]
|
| 1535 |
+
if face_h < min_size or face_w < min_size:
|
| 1536 |
+
scale = max(min_size/face_h, min_size/face_w)
|
| 1537 |
+
face_crop = cv2.resize(face_crop,
|
| 1538 |
+
(max(min_size, int(face_w * scale)),
|
| 1539 |
+
max(min_size, int(face_h * scale))))
|
| 1540 |
+
else:
|
| 1541 |
+
st.error("Invalid face region detected. Please try again with a clearer image.")
|
| 1542 |
+
return
|
| 1543 |
+
|
| 1544 |
+
# Guardar en la base de datos
|
| 1545 |
+
if add_to_existing and person_name in st.session_state.face_database:
|
| 1546 |
+
# Añadir a persona existente
|
| 1547 |
+
if 'embeddings' in st.session_state.face_database[person_name]:
|
| 1548 |
+
# Formato nuevo con múltiples embeddings
|
| 1549 |
+
for embedding in embeddings_all_models:
|
| 1550 |
+
model_name = embedding['model']
|
| 1551 |
+
model_idx = -1
|
| 1552 |
+
|
| 1553 |
+
# Buscar si ya existe un embedding de este modelo
|
| 1554 |
+
for i, model in enumerate(st.session_state.face_database[person_name]['models']):
|
| 1555 |
+
if model == model_name:
|
| 1556 |
+
model_idx = i
|
| 1557 |
+
break
|
| 1558 |
+
|
| 1559 |
+
if model_idx >= 0:
|
| 1560 |
+
# Actualizar embedding existente
|
| 1561 |
+
st.session_state.face_database[person_name]['embeddings'][model_idx] = embedding['embedding']
|
| 1562 |
+
else:
|
| 1563 |
+
# Añadir nuevo modelo
|
| 1564 |
+
st.session_state.face_database[person_name]['models'].append(model_name)
|
| 1565 |
+
st.session_state.face_database[person_name]['embeddings'].append(embedding['embedding'])
|
| 1566 |
|
| 1567 |
+
# Actualizar imagen de referencia
|
| 1568 |
+
st.session_state.face_database[person_name]['face_image'] = face_crop
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1569 |
|
| 1570 |
# Incrementar contador
|
| 1571 |
st.session_state.face_database[person_name]['count'] += 1
|
| 1572 |
+
else:
|
| 1573 |
+
# Crear nueva entrada en la base de datos
|
| 1574 |
+
st.sidebar.write(f"Creating new entry for {person_name}")
|
| 1575 |
+
|
| 1576 |
+
models = []
|
| 1577 |
+
embeddings = []
|
| 1578 |
+
|
| 1579 |
+
for embedding in embeddings_all_models:
|
| 1580 |
+
models.append(embedding['model'])
|
| 1581 |
+
embeddings.append(embedding['embedding'])
|
| 1582 |
+
|
| 1583 |
+
st.session_state.face_database[person_name] = {
|
| 1584 |
+
'embeddings': embeddings,
|
| 1585 |
+
'models': models,
|
| 1586 |
+
'count': 1,
|
| 1587 |
+
'face_image': face_crop
|
| 1588 |
+
}
|
| 1589 |
|
| 1590 |
+
st.success(f"Face registered successfully for {person_name}!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1591 |
|
| 1592 |
# Guardar la base de datos actualizada
|
| 1593 |
if DATABASE_UTILS_AVAILABLE:
|
|
|
|
| 1667 |
|
| 1668 |
# Mostrar miniatura si está disponible
|
| 1669 |
with col_thumb:
|
| 1670 |
+
if row["Face"] is not None and row["Face"].size > 0:
|
| 1671 |
+
try:
|
| 1672 |
+
# Redimensionar para crear miniatura
|
| 1673 |
+
face_img = row["Face"]
|
| 1674 |
+
h, w = face_img.shape[:2]
|
| 1675 |
+
if h > 0 and w > 0:
|
| 1676 |
+
thumbnail = cv2.resize(face_img, (max(1, w//4), max(1, h//4)))
|
| 1677 |
+
st.image(cv2.cvtColor(thumbnail, cv2.COLOR_BGR2RGB), width=50)
|
| 1678 |
+
else:
|
| 1679 |
+
st.write("Invalid image")
|
| 1680 |
+
except Exception as e:
|
| 1681 |
+
st.write("Error displaying image")
|
| 1682 |
+
st.error(f"Error: {str(e)}")
|
| 1683 |
else:
|
| 1684 |
st.write("No image")
|
| 1685 |
|