Spaces:
Runtime error
Runtime error
Upload 5 files
Browse files
camera_age_prediction.py
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import numpy as np
|
| 3 |
+
from tensorflow.keras.models import load_model
|
| 4 |
+
from tensorflow.keras.applications.mobilenet_v2 import preprocess_input
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
# Definir rutas
|
| 8 |
+
code_path = r'D:\Machine Learning\Machine Learning\Code'
|
| 9 |
+
models_path = os.path.join(code_path, 'models')
|
| 10 |
+
haarcascade_path = os.path.join(code_path, 'haarcascades', 'haarcascade_frontalface_default.xml')
|
| 11 |
+
|
| 12 |
+
# Verificar si el archivo Haar Cascade existe
|
| 13 |
+
if not os.path.exists(haarcascade_path):
|
| 14 |
+
print(f'No se encontr贸 el archivo Haar Cascade en {haarcascade_path}. Aseg煤rate de descargarlo y guardarlo correctamente.')
|
| 15 |
+
exit()
|
| 16 |
+
|
| 17 |
+
# Cargar el clasificador Haar Cascade para detecci贸n de rostros
|
| 18 |
+
face_cascade = cv2.CascadeClassifier(haarcascade_path)
|
| 19 |
+
|
| 20 |
+
# Cargar el modelo entrenado
|
| 21 |
+
modelo_guardado = os.path.join(models_path, 'modelo_estimacion_edad_mejor.h5')
|
| 22 |
+
if not os.path.exists(modelo_guardado):
|
| 23 |
+
print(f'No se encontr贸 el modelo en {modelo_guardado}. Por favor, entrena el modelo primero.')
|
| 24 |
+
exit()
|
| 25 |
+
|
| 26 |
+
modelo = load_model(modelo_guardado)
|
| 27 |
+
print('Modelo cargado exitosamente.')
|
| 28 |
+
|
| 29 |
+
# Configurar la captura de video (0 es la webcam predeterminada)
|
| 30 |
+
cap = cv2.VideoCapture(0)
|
| 31 |
+
|
| 32 |
+
if not cap.isOpened():
|
| 33 |
+
print("Error: No se puede acceder a la c谩mara.")
|
| 34 |
+
exit()
|
| 35 |
+
|
| 36 |
+
print("Iniciando la captura de video. Presiona 'q' para salir.")
|
| 37 |
+
|
| 38 |
+
while True:
|
| 39 |
+
# Capturar frame por frame
|
| 40 |
+
ret, frame = cap.read()
|
| 41 |
+
if not ret:
|
| 42 |
+
print("Error: No se pudo leer el frame de la c谩mara.")
|
| 43 |
+
break
|
| 44 |
+
|
| 45 |
+
# Convertir el frame a escala de grises para la detecci贸n de rostros
|
| 46 |
+
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
| 47 |
+
|
| 48 |
+
# Detectar rostros en el frame
|
| 49 |
+
rostros = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
|
| 50 |
+
|
| 51 |
+
for (x, y, w, h) in rostros:
|
| 52 |
+
# Extraer la regi贸n del rostro
|
| 53 |
+
rostro = frame[y:y+h, x:x+w]
|
| 54 |
+
|
| 55 |
+
# Preprocesar la imagen del rostro
|
| 56 |
+
rostro_rgb = cv2.cvtColor(rostro, cv2.COLOR_BGR2RGB)
|
| 57 |
+
rostro_resized = cv2.resize(rostro_rgb, (128, 128))
|
| 58 |
+
rostro_preprocessed = preprocess_input(rostro_resized)
|
| 59 |
+
rostro_expanded = np.expand_dims(rostro_preprocessed, axis=0)
|
| 60 |
+
|
| 61 |
+
# Realizar la predicci贸n de edad
|
| 62 |
+
edad_predicha = modelo.predict(rostro_expanded)[0][0]
|
| 63 |
+
edad_predicha = round(edad_predicha, 2)
|
| 64 |
+
|
| 65 |
+
# Dibujar un rect谩ngulo alrededor del rostro
|
| 66 |
+
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
|
| 67 |
+
|
| 68 |
+
# Preparar el texto de la edad predicha
|
| 69 |
+
texto = f'Edad: {edad_predicha} a帽os'
|
| 70 |
+
|
| 71 |
+
# Elegir la posici贸n del texto (justo encima del rect谩ngulo del rostro)
|
| 72 |
+
posicion_texto = (x, y-10 if y-10 > 10 else y+10)
|
| 73 |
+
|
| 74 |
+
# Escribir el texto en la imagen con color azul (BGR: 255, 0, 0)
|
| 75 |
+
cv2.putText(frame, texto, posicion_texto, cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 0, 0), 2)
|
| 76 |
+
|
| 77 |
+
# Mostrar el frame resultante
|
| 78 |
+
cv2.imshow('Estimaci贸n de Edad - Presiona "q" para salir', frame)
|
| 79 |
+
|
| 80 |
+
# Esperar por la tecla 'q' para salir
|
| 81 |
+
if cv2.waitKey(1) & 0xFF == ord('q'):
|
| 82 |
+
print("Programa finalizado por el usuario.")
|
| 83 |
+
break
|
| 84 |
+
|
| 85 |
+
# Liberar la captura y cerrar las ventanas
|
| 86 |
+
cap.release()
|
| 87 |
+
cv2.destroyAllWindows()
|
haarcascades/haarcascade_frontalface_default.xml
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
models/modelo_estimacion_edad_final.h5
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:59f0467f67acec65daa1f950fc78a282b306eba16d004e34758ad3be9e9a8253
|
| 3 |
+
size 11504968
|
models/modelo_estimacion_edad_mejor.h5
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:5afb39c8ba2ab3b945db40b65d63ef4e9de629d5b93f8f684645511e3f545281
|
| 3 |
+
size 11504968
|
requirements.txt
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
tensorflow==2.12.0
|
| 2 |
+
keras==2.12.0
|
| 3 |
+
numpy==1.24.3
|
| 4 |
+
pandas==2.1.1
|
| 5 |
+
matplotlib==3.7.1
|
| 6 |
+
scikit-learn==1.2.2
|
| 7 |
+
opencv-python==4.7.0.72
|
| 8 |
+
Pillow==9.5.0
|