Niharmahesh commited on
Commit
87c8b2d
·
verified ·
1 Parent(s): cd5769d

Create model.py

Browse files
Files changed (1) hide show
  1. model.py +42 -0
model.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import joblib
2
+ import pandas as pd
3
+ import numpy as np
4
+ from landmarks import normalize_landmarks, calculate_angles
5
+
6
+ @st.cache_resource
7
+ def load_model():
8
+ """Load the pre-trained Random Forest model."""
9
+ try:
10
+ return joblib.load('best_random_forest_model.pkl')
11
+ except Exception as e:
12
+ st.error(f"Error loading model: {e}")
13
+ return None
14
+
15
+ def process_and_predict(image, model):
16
+ """
17
+ Process the uploaded image to extract hand landmarks and predict the ASL sign.
18
+
19
+ Parameters:
20
+ image (numpy.ndarray): The uploaded image.
21
+ model (sklearn.base.BaseEstimator): The pre-trained model.
22
+
23
+ Returns:
24
+ tuple: A tuple containing predicted probabilities and landmarks.
25
+ """
26
+ mp_hands = mp.solutions.hands
27
+ with mp_hands.Hands(static_image_mode=True, max_num_hands=1, min_detection_confidence=0.5) as hands:
28
+ image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
29
+ results = hands.process(image_rgb)
30
+
31
+ if results.multi_hand_landmarks:
32
+ landmarks = np.array([[lm.x, lm.y] for lm in results.multi_hand_landmarks[0].landmark])
33
+ landmarks_normalized = normalize_landmarks(landmarks)
34
+ angles = calculate_angles(landmarks_normalized)
35
+
36
+ angle_columns = [f'angle_{i}' for i in range(len(angles))]
37
+ angles_df = pd.DataFrame([angles], columns=angle_columns)
38
+
39
+ probabilities = model.predict_proba(angles_df)[0]
40
+ return probabilities, landmarks
41
+
42
+ return None, None