Niharmahesh commited on
Commit
3c0e7f6
·
verified ·
1 Parent(s): 6d9f9fc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -27
app.py CHANGED
@@ -3,35 +3,26 @@ import cv2
3
  import numpy as np
4
  import mediapipe as mp
5
  import pickle
6
- import os
7
-
8
- # Define the models directory
9
- models_dir = 'models'
10
-
11
- # Load models
12
- def load_model(file_path):
13
- try:
14
- with open(file_path, 'rb') as file:
15
- return pickle.load(file)
16
- except FileNotFoundError:
17
- st.error(f"Error: Model file not found at {file_path}")
18
- except Exception as e:
19
- st.error(f"Error loading model from {file_path}: {e}")
20
- return None
21
 
22
  models = {
23
- 'Random Forest': load_model(os.path.join(models_dir, 'random_forest_model.pkl')),
24
- 'SVM': load_model(os.path.join(models_dir, 'svm_model.pkl')),
25
- 'Hard Voting': load_model(os.path.join(models_dir, 'hard_voting_classifier.pkl')),
26
- 'Soft Voting': load_model(os.path.join(models_dir, 'soft_voting_classifier.pkl'))
27
  }
28
 
29
- # Load label encoder
30
- label_encoder = load_model(os.path.join(models_dir, 'label_encoder.pkl'))
31
- if label_encoder is None:
32
- st.error("Failed to load label encoder. Exiting.")
33
- st.stop()
34
-
35
  # Initialize MediaPipe Hands
36
  mp_hands = mp.solutions.hands
37
  hands = mp_hands.Hands(static_image_mode=True, max_num_hands=1, min_detection_confidence=0.5)
@@ -67,8 +58,6 @@ def predict_alphabet(image):
67
  predictions = {}
68
 
69
  for model_name, model in models.items():
70
- if model is None:
71
- continue
72
  prediction, probability = get_model_predictions(data_aux, model)
73
  predictions[model_name] = (prediction, probability)
74
 
 
3
  import numpy as np
4
  import mediapipe as mp
5
  import pickle
6
+
7
+ # Load models directly
8
+ with open('random_forest_model.pkl', 'rb') as file:
9
+ random_forest_model = pickle.load(file)
10
+ with open('svm_model.pkl', 'rb') as file:
11
+ svm_model = pickle.load(file)
12
+ with open('hard_voting_classifier.pkl', 'rb') as file:
13
+ hard_voting_model = pickle.load(file)
14
+ with open('soft_voting_classifier.pkl', 'rb') as file:
15
+ soft_voting_model = pickle.load(file)
16
+ with open('label_encoder.pkl', 'rb') as file:
17
+ label_encoder = pickle.load(file)
 
 
 
18
 
19
  models = {
20
+ 'Random Forest': random_forest_model,
21
+ 'SVM': svm_model,
22
+ 'Hard Voting': hard_voting_model,
23
+ 'Soft Voting': soft_voting_model
24
  }
25
 
 
 
 
 
 
 
26
  # Initialize MediaPipe Hands
27
  mp_hands = mp.solutions.hands
28
  hands = mp_hands.Hands(static_image_mode=True, max_num_hands=1, min_detection_confidence=0.5)
 
58
  predictions = {}
59
 
60
  for model_name, model in models.items():
 
 
61
  prediction, probability = get_model_predictions(data_aux, model)
62
  predictions[model_name] = (prediction, probability)
63