jarondon82/ComputerVisionProject
1
1"""2Módulo para parchear DeepFace y RetinaFace para compatibilidad con diferentes versiones de TensorFlow3"""4import os5import sys6import importlib7import warnings8 9def patch_retina_face():10 """Parchea RetinaFace para funcionar con Keras independiente o integrado en TensorFlow"""11 try:12 # Verificar si podemos importar retina_face13 import retina_face14 15 # Verificar la versión de TensorFlow16 import tensorflow as tf17 tf_version = tf.__version__18 19 # Para TF 2.15.x, usamos keras estándar20 if tf_version.startswith('2.15'):21 try:22 import keras23 print(f"Using standard Keras {keras.__version__} with TensorFlow {tf_version}")24 25 # Monkeypatch para RetinaFace si es necesario26 try:27 # Intentar importar el módulo que podría usar keras28 from retina_face.commons import postprocess29 if not hasattr(postprocess, '_keras_patched'):30 # Verificar si está usando tf.keras31 if hasattr(postprocess, 'keras') and postprocess.keras.__name__ == 'tensorflow.keras':32 print("Patching RetinaFace to use standard keras instead of tf.keras")33 postprocess.keras = keras34 postprocess._keras_patched = True35 except ImportError:36 pass37 except ImportError:38 print("Standard Keras not found, using tf.keras")39 40 # Para TF 2.19.x, necesitamos tf-keras41 elif tf_version.startswith('2.19'):42 try:43 import tf_keras44 print(f"Using tf-keras with TensorFlow {tf_version}")45 46 # Monkeypatch para RetinaFace si es necesario47 try:48 from retina_face.commons import postprocess49 if not hasattr(postprocess, '_keras_patched'):50 if hasattr(postprocess, 'keras'):51 print("Patching RetinaFace to use tf-keras")52 postprocess.keras = tf_keras53 postprocess._keras_patched = True54 except ImportError:55 pass56 except ImportError:57 print("Warning: tf-keras not installed. RetinaFace may not work properly.")58 59 except ImportError as e:60 print(f"Warning: Could not patch RetinaFace: {e}")61 62def patch_deepface():63 """Parchea DeepFace para funcionar con diferentes versiones de TensorFlow"""64 try:65 import deepface66 import tensorflow as tf67 tf_version = tf.__version__68 69 if tf_version.startswith('2.19'):70 try:71 import tf_keras72 # Intentar parchear los módulos relevantes de DeepFace73 deepface_modules = [74 'deepface.commons.functions',75 'deepface.detectors.RetinaFaceWrapper',76 'deepface.detectors.FaceDetector'77 ]78 79 for module_name in deepface_modules:80 try:81 module = importlib.import_module(module_name)82 if hasattr(module, 'keras') and module.keras.__name__ == 'tensorflow.keras':83 module.keras = tf_keras84 print(f"Patched {module_name} to use tf-keras")85 except (ImportError, AttributeError):86 pass87 except ImportError:88 print("Warning: tf-keras not installed. DeepFace may not work properly with TF 2.19")89 except ImportError as e:90 print(f"Warning: Could not patch DeepFace: {e}")91 92def apply_patches():93 """Aplica todos los parches necesarios"""94 warnings.filterwarnings('ignore') # Reducir mensajes de advertencia95 patch_retina_face()96 patch_deepface()97 print("Patches applied successfully")98 99if __name__ == "__main__":100 apply_patches() 