Spaces:
Running
Running
Commit
·
072709c
1
Parent(s):
4c778c9
fix: [Lunes 25/03/2024 15:50] Mejorado manejo de imágenes - Implementadas validaciones robustas para el procesamiento y visualización de imágenes faciales, incluyendo conversión de formatos y manejo seguro de redimensionamiento
Browse files- streamlit_app.py +67 -27
streamlit_app.py
CHANGED
|
@@ -1620,25 +1620,41 @@ def main():
|
|
| 1620 |
|
| 1621 |
# Preparar datos para la tabla
|
| 1622 |
for name, info in st.session_state.face_database.items():
|
| 1623 |
-
|
| 1624 |
-
|
| 1625 |
-
|
| 1626 |
-
|
| 1627 |
-
|
| 1628 |
-
|
| 1629 |
-
|
| 1630 |
-
|
| 1631 |
-
|
| 1632 |
-
|
| 1633 |
-
|
| 1634 |
-
|
| 1635 |
-
|
| 1636 |
-
|
| 1637 |
-
|
| 1638 |
-
|
| 1639 |
-
|
| 1640 |
-
|
| 1641 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1642 |
|
| 1643 |
# Debug de los datos procesados
|
| 1644 |
st.sidebar.write(f"Processed {len(data)} entries for display")
|
|
@@ -1667,22 +1683,46 @@ def main():
|
|
| 1667 |
|
| 1668 |
# Mostrar miniatura si está disponible
|
| 1669 |
with col_thumb:
|
| 1670 |
-
if row["Face"] is not None
|
| 1671 |
try:
|
| 1672 |
-
#
|
| 1673 |
face_img = row["Face"]
|
| 1674 |
-
|
| 1675 |
-
|
| 1676 |
-
|
| 1677 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 1686 |
with col1:
|
| 1687 |
st.write(row["Name"])
|
| 1688 |
with col2:
|
|
|
|
| 1620 |
|
| 1621 |
# Preparar datos para la tabla
|
| 1622 |
for name, info in st.session_state.face_database.items():
|
| 1623 |
+
try:
|
| 1624 |
+
# Determinar el número de embeddings
|
| 1625 |
+
if 'embeddings' in info:
|
| 1626 |
+
num_embeddings = len(info['embeddings'])
|
| 1627 |
+
models = ', '.join(info['models'])
|
| 1628 |
+
else:
|
| 1629 |
+
num_embeddings = 1
|
| 1630 |
+
models = 'VGG-Face' # Modelo por defecto para formato antiguo
|
| 1631 |
+
|
| 1632 |
+
# Determinar el número de imágenes
|
| 1633 |
+
num_images = info.get('count', 1)
|
| 1634 |
+
|
| 1635 |
+
# Validar y preparar la imagen facial
|
| 1636 |
+
face_image = info.get('face_image', None)
|
| 1637 |
+
if face_image is not None:
|
| 1638 |
+
if isinstance(face_image, np.ndarray) and face_image.size > 0:
|
| 1639 |
+
# Asegurar que la imagen sea válida y tenga el formato correcto
|
| 1640 |
+
if len(face_image.shape) == 2: # Si es grayscale
|
| 1641 |
+
face_image = cv2.cvtColor(face_image, cv2.COLOR_GRAY2BGR)
|
| 1642 |
+
elif len(face_image.shape) == 3 and face_image.shape[2] == 4: # Si tiene canal alpha
|
| 1643 |
+
face_image = cv2.cvtColor(face_image, cv2.COLOR_BGRA2BGR)
|
| 1644 |
+
else:
|
| 1645 |
+
face_image = None
|
| 1646 |
+
|
| 1647 |
+
# Añadir a los datos
|
| 1648 |
+
data.append({
|
| 1649 |
+
"Name": name,
|
| 1650 |
+
"Images": num_images,
|
| 1651 |
+
"Embeddings": num_embeddings,
|
| 1652 |
+
"Models": models,
|
| 1653 |
+
"Face": face_image
|
| 1654 |
+
})
|
| 1655 |
+
except Exception as e:
|
| 1656 |
+
st.error(f"Error processing entry for {name}: {str(e)}")
|
| 1657 |
+
continue
|
| 1658 |
|
| 1659 |
# Debug de los datos procesados
|
| 1660 |
st.sidebar.write(f"Processed {len(data)} entries for display")
|
|
|
|
| 1683 |
|
| 1684 |
# Mostrar miniatura si está disponible
|
| 1685 |
with col_thumb:
|
| 1686 |
+
if row["Face"] is not None:
|
| 1687 |
try:
|
| 1688 |
+
# Validar la imagen antes de redimensionar
|
| 1689 |
face_img = row["Face"]
|
| 1690 |
+
if isinstance(face_img, np.ndarray) and face_img.size > 0 and len(face_img.shape) >= 2:
|
| 1691 |
+
h, w = face_img.shape[:2]
|
| 1692 |
+
if h > 0 and w > 0:
|
| 1693 |
+
# Calcular nuevo tamaño manteniendo el aspect ratio
|
| 1694 |
+
target_width = 50
|
| 1695 |
+
aspect_ratio = float(w) / float(h)
|
| 1696 |
+
target_height = int(target_width / aspect_ratio)
|
| 1697 |
+
|
| 1698 |
+
# Asegurar dimensiones mínimas
|
| 1699 |
+
target_width = max(1, target_width)
|
| 1700 |
+
target_height = max(1, target_height)
|
| 1701 |
+
|
| 1702 |
+
# Redimensionar usando INTER_AREA para mejor calidad en reducción
|
| 1703 |
+
thumbnail = cv2.resize(face_img,
|
| 1704 |
+
(target_width, target_height),
|
| 1705 |
+
interpolation=cv2.INTER_AREA)
|
| 1706 |
+
|
| 1707 |
+
# Convertir a RGB si es necesario
|
| 1708 |
+
if len(thumbnail.shape) == 2: # Si es grayscale
|
| 1709 |
+
thumbnail = cv2.cvtColor(thumbnail, cv2.COLOR_GRAY2RGB)
|
| 1710 |
+
elif thumbnail.shape[2] == 4: # Si tiene canal alpha
|
| 1711 |
+
thumbnail = cv2.cvtColor(thumbnail, cv2.COLOR_BGRA2RGB)
|
| 1712 |
+
else:
|
| 1713 |
+
thumbnail = cv2.cvtColor(thumbnail, cv2.COLOR_BGR2RGB)
|
| 1714 |
+
|
| 1715 |
+
st.image(thumbnail, width=50)
|
| 1716 |
+
else:
|
| 1717 |
+
st.write("Invalid dimensions")
|
| 1718 |
else:
|
| 1719 |
+
st.write("Invalid image format")
|
| 1720 |
except Exception as e:
|
| 1721 |
st.write("Error displaying image")
|
| 1722 |
st.error(f"Error: {str(e)}")
|
| 1723 |
else:
|
| 1724 |
st.write("No image")
|
| 1725 |
+
|
| 1726 |
with col1:
|
| 1727 |
st.write(row["Name"])
|
| 1728 |
with col2:
|