Abs6187 commited on
Commit
4c95ba8
·
verified ·
1 Parent(s): bbe5030

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +64 -14
  2. isl_processor.py +27 -2
app.py CHANGED
@@ -75,33 +75,81 @@ try:
75
  st.warning(f"HuggingFace Hub import issue: {hf_error}")
76
  hf_hub_download = None
77
 
78
- # Try custom modules with fallback
 
 
 
 
 
 
79
  try:
80
  from pose_models import create_bodypose_model, create_handpose_model
 
 
 
 
 
 
 
 
81
  from expression_mapping import expression_mapping
82
- from isl_processor import ISLTranslationModel
83
- import pose_utils as utils
84
- except Exception as custom_error:
85
- st.warning(f"Custom modules import issue: {custom_error}")
86
  # Fallback expression mapping
87
  expression_mapping = {
88
  'hello': 0, 'thank_you': 1, 'please': 2, 'sorry': 3, 'help': 4,
89
  'good': 5, 'bad': 6, 'yes': 7, 'no': 8, 'water': 9,
90
  'food': 10, 'home': 11, 'work': 12, 'school': 13, 'family': 14
91
  }
92
- utils = None # Set utils to None if import fails
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93
 
94
  st.success("✅ Core dependencies loaded successfully!")
95
  except ImportError as e:
96
  st.error(f"❌ Critical import error: {e}")
97
  st.error("Running in fallback mode with limited functionality.")
98
 
99
- # Import pose_utils separately to ensure it's always available
100
- try:
101
- import pose_utils as utils
102
- except ImportError as utils_error:
103
- st.error(f" Failed to import pose_utils: {utils_error}")
104
- utils = None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
105
 
106
  # System information will be shown in About section
107
 
@@ -251,7 +299,9 @@ def load_translation_model():
251
  model.add(BatchNormalization())
252
  model.add(keras.layers.Activation('elu'))
253
  model.add(Dropout(0.2))
254
- model.add(Dense(len(list(expression_mapping.keys())), activation='softmax'))
 
 
255
 
256
  # Try to load weights
257
  model.load_weights(model_file)
@@ -836,7 +886,7 @@ elif app_mode == 'Test Video Translation':
836
  # Get top predictions
837
  top_prediction_idx = np.argmax(prediction_output)
838
  top_3_indices = prediction_output.argsort()[-3:][::-1]
839
- top_3_signs = [expression_mapping[i] for i in top_3_indices]
840
  top_3_probabilities = prediction_output[top_3_indices]
841
 
842
  # Current frame predictions
 
75
  st.warning(f"HuggingFace Hub import issue: {hf_error}")
76
  hf_hub_download = None
77
 
78
+ # Try custom modules with fallback - import each module separately for better error handling
79
+ pose_models = None
80
+ expression_mapping = None
81
+ isl_processor = None
82
+ utils = None
83
+
84
+ # Try importing pose_models
85
  try:
86
  from pose_models import create_bodypose_model, create_handpose_model
87
+ pose_models = True
88
+ st.success("✅ Pose models imported successfully")
89
+ except Exception as pose_error:
90
+ st.warning(f"Pose models import issue: {pose_error}")
91
+ pose_models = None
92
+
93
+ # Try importing expression_mapping
94
+ try:
95
  from expression_mapping import expression_mapping
96
+ st.success("✅ Expression mapping imported successfully")
97
+ except Exception as expr_error:
98
+ st.warning(f"Expression mapping import issue: {expr_error}")
 
99
  # Fallback expression mapping
100
  expression_mapping = {
101
  'hello': 0, 'thank_you': 1, 'please': 2, 'sorry': 3, 'help': 4,
102
  'good': 5, 'bad': 6, 'yes': 7, 'no': 8, 'water': 9,
103
  'food': 10, 'home': 11, 'work': 12, 'school': 13, 'family': 14
104
  }
105
+
106
+ # Try importing ISL processor
107
+ try:
108
+ from isl_processor import ISLTranslationModel
109
+ isl_processor = True
110
+ st.success("✅ ISL processor imported successfully")
111
+ except Exception as isl_error:
112
+ st.warning(f"ISL processor import issue: {isl_error}")
113
+ isl_processor = None
114
+
115
+ # Try importing pose_utils
116
+ try:
117
+ import pose_utils as utils
118
+ st.success("✅ Pose utils imported successfully")
119
+ except Exception as utils_error:
120
+ st.warning(f"Pose utils import issue: {utils_error}")
121
+ utils = None
122
 
123
  st.success("✅ Core dependencies loaded successfully!")
124
  except ImportError as e:
125
  st.error(f"❌ Critical import error: {e}")
126
  st.error("Running in fallback mode with limited functionality.")
127
 
128
+ # Ensure we have utils available globally after the main import block
129
+ if utils is None:
130
+ try:
131
+ import pose_utils as utils
132
+ st.info("ℹ️ Pose utils loaded on secondary attempt")
133
+ except ImportError as utils_error:
134
+ st.error(f"❌ Failed to import pose_utils: {utils_error}")
135
+ utils = None
136
+
137
+ # Ensure expression_mapping is available and create index-to-label mapping
138
+ if expression_mapping is None:
139
+ st.warning("⚠️ Using fallback expression mapping")
140
+ expression_mapping = {
141
+ 0: 'hello', 1: 'thank_you', 2: 'please', 3: 'sorry', 4: 'help',
142
+ 5: 'good', 6: 'bad', 7: 'yes', 8: 'no', 9: 'water',
143
+ 10: 'food', 11: 'home', 12: 'work', 13: 'school', 14: 'family'
144
+ }
145
+
146
+ # Create index-to-label mapping function for safe access
147
+ def get_sign_label(index):
148
+ """Safely get sign label from prediction index"""
149
+ if isinstance(expression_mapping, dict):
150
+ return expression_mapping.get(int(index), f'unknown_sign_{index}')
151
+ else:
152
+ return f'sign_{index}'
153
 
154
  # System information will be shown in About section
155
 
 
299
  model.add(BatchNormalization())
300
  model.add(keras.layers.Activation('elu'))
301
  model.add(Dropout(0.2))
302
+ # Determine number of classes - use 167 for the full dataset or fallback size
303
+ num_classes = len(list(expression_mapping.keys())) if expression_mapping else 167
304
+ model.add(Dense(num_classes, activation='softmax'))
305
 
306
  # Try to load weights
307
  model.load_weights(model_file)
 
886
  # Get top predictions
887
  top_prediction_idx = np.argmax(prediction_output)
888
  top_3_indices = prediction_output.argsort()[-3:][::-1]
889
+ top_3_signs = [get_sign_label(i) for i in top_3_indices]
890
  top_3_probabilities = prediction_output[top_3_indices]
891
 
892
  # Current frame predictions
isl_processor.py CHANGED
@@ -12,16 +12,41 @@ Institution: Shri Ram Group of Institutions
12
  """
13
 
14
  import keras
15
- from keras.layers import TorchModuleWrapper
16
  import numpy as np
17
  import cv2
18
  import torch
19
- from scipy.ndimage.filters import gaussian_filter
 
 
 
20
  import math
21
  import os
22
  from skimage.measure import label
23
  import pose_utils as utils
24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
26
  class ISLPoseEstimator(keras.Model):
27
  """
 
12
  """
13
 
14
  import keras
 
15
  import numpy as np
16
  import cv2
17
  import torch
18
+ try:
19
+ from scipy.ndimage.filters import gaussian_filter
20
+ except ImportError:
21
+ from scipy.ndimage import gaussian_filter
22
  import math
23
  import os
24
  from skimage.measure import label
25
  import pose_utils as utils
26
 
27
+ # Simple TorchModuleWrapper replacement for compatibility
28
+ class TorchModuleWrapper:
29
+ """
30
+ Simple wrapper to make PyTorch models compatible with Keras-style usage
31
+ """
32
+ def __init__(self, torch_model):
33
+ self.torch_model = torch_model
34
+ self.trainable = False
35
+
36
+ def __call__(self, x):
37
+ """Forward pass through the PyTorch model"""
38
+ return self.torch_model(x)
39
+
40
+ def eval(self):
41
+ """Set model to evaluation mode"""
42
+ if hasattr(self.torch_model, 'eval'):
43
+ self.torch_model.eval()
44
+
45
+ def train(self, mode=True):
46
+ """Set model to train mode"""
47
+ if hasattr(self.torch_model, 'train'):
48
+ self.torch_model.train(mode)
49
+
50
 
51
  class ISLPoseEstimator(keras.Model):
52
  """