import joblib import pandas as pd import numpy as np from landmarks import normalize_landmarks, calculate_angles import streamlit as st @st.cache_resource def load_model(): """Load the pre-trained Random Forest model.""" try: return joblib.load('best_random_forest_model.pkl') except Exception as e: st.error(f"Error loading model: {e}") return None def process_and_predict(image, model): """ Process the uploaded image to extract hand landmarks and predict the ASL sign. Parameters: image (numpy.ndarray): The uploaded image. model (sklearn.base.BaseEstimator): The pre-trained model. Returns: tuple: A tuple containing predicted probabilities and landmarks. """ mp_hands = mp.solutions.hands with mp_hands.Hands(static_image_mode=True, max_num_hands=1, min_detection_confidence=0.5) as hands: image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) results = hands.process(image_rgb) if results.multi_hand_landmarks: landmarks = np.array([[lm.x, lm.y] for lm in results.multi_hand_landmarks[0].landmark]) landmarks_normalized = normalize_landmarks(landmarks) angles = calculate_angles(landmarks_normalized) angle_columns = [f'angle_{i}' for i in range(len(angles))] angles_df = pd.DataFrame([angles], columns=angle_columns) probabilities = model.predict_proba(angles_df)[0] return probabilities, landmarks return None, None